ZF2 + doctrine without composer - php

I have a working ZF2 (skeleton) application and want to integrate Doctrine.
I have downloaded the 2 modules (DoctrineModule and DoctrineORMModule) from github as I'm unable to use composer (so please don't answer with; "get composer").
After hours of trying to find the problem I keep getting the following error:
Fatal error: Class 'Doctrine\Common\Annotations\AnnotationRegistry' not found in doctrine/DoctrineModule/src/DoctrineModule/Module.php on line 54.
I have spend hours of searching and trying to debug but I can't find a way to fix it. Please help me.

DoctrineORMModule does not explicitly support autoloading without composer (since it's a mess).
As of current (0.7.*) version of DoctrineORMModule, the required packages are following:
doctrine/common (2.3.0)
doctrine/dbal (2.3.2)
symfony/console (v2.1.7)
doctrine/orm (2.3.2)
doctrine/doctrine-module (0.7.*)
doctrine/doctrine-orm-module (0.7.0)
What you can do is defining all the autoloading namespaces in init_autoloader.php in your skeleton application (explaining the mess). The code to replace is the part about the autoloader factory:
Zend\Loader\AutoloaderFactory::factory(array(
'Zend\Loader\StandardAutoloader' => array(
'autoregister_zf' => true,
'namespaces' => array(
'Doctrine\Common' => __DIR__ . '/vendor/doctrine/common',
'Doctrine\DBAL' => __DIR__ . '/vendor/doctrine/dbal',
'Symfony\Console' => __DIR__ . '/vendor/symfony/console',
'DoctrineModule' => __DIR__ . '/vendor/doctrine/doctrine-module',
'DoctrineORMModule' => __DIR__ . '/vendor/doctrine/doctrine-orm-module',
),
),
));
You will have to configure the various git submodules on your own
As of version 0.8.* of the modules, the dependencies will increase quite a bit because of how doctrine/common was splitted recently, so consider finding a solution to start using composer or you will just be delaying huge problems in future.
This solution should work for now.

Try to edit the following method in the Module.php of the Application module:
public function getAutoloaderConfig()
{
return array(
'Zend\Loader\StandardAutoloader' => array(
'namespaces' => array(
__NAMESPACE__ => __DIR__ . '/src/' . __NAMESPACE__,
'Doctrine' => //Path where your doctrine dist resides
),
),
);
}
There is no need to define autoloading configuration for DoctrineModule and DoctrineORMModule because this are ZF2 modules and are providing autoloading configuration already.

Form annotations require Doctrine\Common, which contains an annotation parsering engine. The simplest way to install Doctrine\Common is if you are using Composer; simply update your composer.json and add the following line to the require section:
"doctrine/common": ">=2.1",
Then run php composer.phar update to install the dependency.
If you’re not using Composer, visit the Doctrine project website for more details on installation.

Related

CakePHP: upgrade from 3.6.x to 4.0 "Missing Template Exception"

I followed the upgrade guide (after I upgraded from 3.6 to 3.8) but I get this error:
Error: [Cake\View\Exception\MissingTemplateException] Template file "Error\error500.ctp" is missing
....
If you want to customize this error message, create src\Template\Error\fatal_error.ctp
After the upgrade procedure my templates moved to app_name\templates directory and renamed to *.php instead of *.ctp . I updated the app.php and app.default.php with the new paths:
'App' => [
'namespace' => 'App',
'encoding' => env('APP_ENCODING', 'UTF-8'),
'defaultLocale' => env('APP_DEFAULT_LOCALE', 'en_US'),
'defaultTimezone' => env('APP_DEFAULT_TIMEZONE', 'UTC'),
'base' => false,
'dir' => 'src',
'webroot' => 'webroot',
'wwwRoot' => WWW_ROOT,
//'baseUrl' => env('SCRIPT_NAME'),
'fullBaseUrl' => false,
'imageBaseUrl' => 'img/',
'cssBaseUrl' => 'css/',
'jsBaseUrl' => 'js/',
'paths' => [
'plugins' => [ROOT . DS . 'plugins' . DS],
'templates' => [ROOT . DS . 'templates' . DS],
'locales' => [ROOT . DS . 'Locale' . DS],
],
],
But still the application is looking for the template files with .ctp extension and under src\Template\...
What am I missing?
From my experience:
Upgrade CakePHP 3.x to 4.x
composer update
run phpstan from --level 0 to 2 and fix your code or test in scrutinizer
read 4.0 Migration Guide : https://book.cakephp.org/4/en/appendices/4-0-migration-guide.html
read 4.0 Upgrade Guide : https://book.cakephp.org/4/en/appendices/4-0-upgrade-guide.html
Steps:
Install the upgrade tool
Rename locale files
Rename template files
Once you've renamed your template and locale files, make sure you update App.paths.locales and App.paths.templates paths to be correct.
From your app composer file remove all cakephp/* (cakephp3) packages, also phpstan, code standards, phpunit,.. run composer update
Applying Rector Refactorings
composer require --update-with-dependencies "phpunit/phpunit:^8.0"
composer require --update-with-dependencies "cakephp/cakephp:4.0.*"
Install fresh cakephp4 inside your app folder. Example: mkdir cakephp4 && cd cakeph4; and run composer create-project --prefer-dist cakephp/app:4.* .;
compare your old cakephp files with files from cakephp4 folder, update all and copy missing files
delete cakephp4 and upgrade folders
don't forget at top of you php files to add <?php declare(strict_types=1);
composer cs-check then composer cs-fix

Composer with Koseven (Kohana)

I'm developing an application where I started using Kohana and now Koseven, and I need to use an api that was available in the composer, I followed the steps to download the files I created a folder to sell inside the application, and put in the bootstrap.php code to call the autoload of the composer.
But after doing this when trying to use a class of this api error of "class not found" occurs.
I do not know what else to do, can you help me?
In order to use composer with Kohana or Koseven, you need to call composer's autoloader from within bootstrap.php.
After Kohana::modules($modules); and before Route::set, insert the following code:
/**
* Autoload composer libraries
*
*/
require APPPATH . 'vendor/autoload.php';
This assumes your composer install command is run the from the root of your app, and it uses the default vendor directory.
Probably you must add this to your composer.json. (Check inc composer doc.) I don't know, because in modules directory I have second instance.
"extra": {
"installer-paths": {
"modules/{$name}/": ["type:kohana-module"]
}
},
And enable module composer as first:
Kohana::modules(array(
'composer' => MODPATH.'composer', //
'auth' => MODPATH.'auth', // Basic authentication
'cache' => MODPATH.'cache', // Caching with multiple backends
It works for me ko3

PHP Autoloader doesn't work on Ubuntu production server

I have to environments i'm working developing my API (PHP based):
Local development: Mac OS Yosemite - running PHP 5.5.20
Production server: Ubuntu server - running PHP 5.5.9
My code uses composer for auto loading as followed:
{
"require": {
"facebook/php-sdk": "#stable",
"everyman/neo4jphp": "dev-master",
"predis/predis": "1.0.1",
"aws/aws-sdk-php": "2.*"
},
"autoload": {
"psr-0": {
"PicoCore\\": "",
"PicoCore\\Authentication\\" : "PicoCore/authentication",
"PicoCore\\Aws\\" : "PicoCore/aws",
"PicoCore\\Cache\\" : "PicoCore/cache",
"PicoCore\\Database\\" : "PicoCore/database",
"PicoCore\\Database\\Managers\\" : "PicoCore/database/managers",
"PicoCore\\Facebook\\" : "PicoCore/facebook",
"PicoCore\\Objects\\" : "PicoCore/objects",
"PicoCore\\Rest\\" : "PicoCore/rest",
"PicoCore\\Configuration\\" : "PicoCore/configuration",
"PicoCore\\Configuration\\Api\\" : "PicoCore/configuration/api",
"PicoCore\\Configuration\\PictureReceiver\\" : "PicoCore/configuration/pictureReceiver",
"PicoCore\\Configuration\\PictureUploader\\" : "PicoCore/configuration/pictureUploader",
"PicoCore\\Scripts\\" : "PicoCore/scripts",
"PicoCore\\Times" : "PicoCore/times"
}
}
}
This is my loading function:
// autoload_namespaces.php #generated by Composer
$vendorDir = dirname(dirname(__FILE__));
$baseDir = dirname($vendorDir);
return array(
'PicoCore\\Times' => array($baseDir . '/PicoCore/times'),
'PicoCore\\Scripts\\' => array($baseDir . '/PicoCore/scripts'),
'PicoCore\\Rest\\' => array($baseDir . '/PicoCore/rest'),
'PicoCore\\Objects\\' => array($baseDir . '/PicoCore/objects'),
'PicoCore\\Facebook\\' => array($baseDir . '/PicoCore/facebook'),
'PicoCore\\Database\\Managers\\' => array($baseDir . '/PicoCore/database/managers'),
'PicoCore\\Database\\' => array($baseDir . '/PicoCore/database'),
'PicoCore\\Configuration\\PictureUploader\\' => array($baseDir . '/PicoCore/configuration/pictureUploader'),
'PicoCore\\Configuration\\PictureReceiver\\' => array($baseDir . '/PicoCore/configuration/pictureReceiver'),
'PicoCore\\Configuration\\Api\\' => array($baseDir . '/PicoCore/configuration/api'),
'PicoCore\\Configuration\\' => array($baseDir . '/PicoCore/configuration'),
'PicoCore\\Cache\\' => array($baseDir . '/PicoCore/cache'),
'PicoCore\\Aws\\' => array($baseDir . '/PicoCore/aws'),
'PicoCore\\Authentication\\' => array($baseDir . '/PicoCore/authentication'),
'PicoCore\\' => array($baseDir . '/'),
'Guzzle\\Tests' => array($vendorDir . '/guzzle/guzzle/tests'),
'Guzzle' => array($vendorDir . '/guzzle/guzzle/src'),
'Everyman\\Neo4j' => array($vendorDir . '/everyman/neo4jphp/lib'),
'Aws' => array($vendorDir . '/aws/aws-sdk-php/src'),
);
When i'm running my code on my local development - it works perfectly, so I pushed it to my remote repository and pull it from the production server.
When i'm trying to run my code in my Production server I receive a Class not found error indicating that my autoloading didn't run properly.
Any ideas what could be the reason ?
UPDATE:
When i'm trying to load the external libs (like AWS) it does work, so the problem is defiantly something with how my folders are being loaded.
UPDATE:
This is the class i'm trying to load from index.php:
<?php
//require Pico Core autoload
require 'core/vendor/autoload.php';
//require Pico Api autoload
require 'vendor/autoload.php';
use PicoCore\Rest\ApiInitializer;
use PicoCore\Configuration\Error;
use PicoApi\Managers\ApiManager;
try {
//initialize the API
ApiInitializer::initialize();
//initialize a new Api with the request
$api = new ApiManager($_REQUEST['request']);
echo utf8_encode($api->processApi());
} catch (Exception $e) {
echo json_encode(Array(Error::jsonErrorField() => $e->getMessage()));
}
This is the error I receive Class 'PicoCore\Rest\ApiInitializer' not found
The structure is:
api
|
core
|
PicoCore
|
folders..
rest
|
ApiInitializer.php
folders...
vender
PicoApi (folder)
vendor (foldeR)
index.php
It's your wrong autoloading definition.
You want to use the class
PicoCore\Rest\ApiInitializer
And you have these files:
api/index.php
api/vendor/... (with your required external dependencies)
api/composer.json (with the autoloading mentioned in your question)
api/core/PicoCore/rest/ApiInitializer.php
And you have this autoloading configured:
"autoload": {
"psr-0": {
"PicoCore\Rest\" : "PicoCore/rest"
}
}
This is a mismatch. When you use the class PicoCore\Rest\ApiInitializer, composer will try to locate an entry with a prefix of that class. It will sucessfully detect that classes with prefix PicoCode\Rest can be found via PSR-0 rules in the directory (relative to the position of composer.json) PicoCore/rest.
First strange thing: There is an additional directory named "core" here, and you seem to include TWO autoloaders. Don't do that, Composer works best with only one autoloader.
Second thing: If I ignore that "core" directory for a bit, the PSR-0 rules state that the classname will be converted to a path - and then be searched in the path given for the prefix.
PicoCore\Rest\ApiInitializer as a path is PicoCore/Rest/ApiInitializer.php (note the uppercase "R" in "Rest"), and the path this is to be found is PicoCore/rest/PicoCore/Rest/ApiInitializer.php.
You don't have this file. And thats why Composer cannot find and load it. And this is even without counting this "core" directory level, it will also not be found if you used two composer.json files, one in api and one in api/core, and the one in api/core was used to find the class.
Suggestions:
Use PSR-4 for every class that is inside a namespace.
Don't lowercase namespace or classname parts for the filesystem.
Shorten your autoloading definition. If you'd rename all those lowercase directories below PicoCore into the proper uppercase variants that are being used in your namespace, you will only need one line of autoloading definition, defining the PicoCore prefix. The rest is done by PSR-4 (or PSR-0).
MacOS is using a case insensitive file system by default, and it is considered a bad thing to switch it to case sensitive: https://apple.stackexchange.com/questions/71357/how-to-check-if-my-hd-is-case-sensitive-or-not That's why it is working on Mac, but not Linux.

How to call GetId3 classes within a namespaced application

I am sorry if this has once been asked but I lack understanding on how this should work.
I am building a File Server to manage uploading and downloading of music files and have found getID3 library that I can use to get information about the files being uploaded.
I have installed the library via composer. Below is my entry on composer.json in my root director
"james-heinrich/getid3": "dev-master"
Vendor folder was successfully updated with the library.
The library has not been added onto autoload_namespaces.php file in vendor/composer (see below):
$vendorDir = dirname(dirname(__FILE__));
$baseDir = dirname($vendorDir);
return array(
'Symfony\\Component\\Filesystem\\' => array($vendorDir . '/symfony/filesystem'),
'Psr\\Log\\' => array($vendorDir . '/psr/log'),
'GetId3_' => array($vendorDir . '/phansys/getid3'),
);
Also under autoload_psr4.php file in vendor/composer the namespace for the library has not been registered either. I am not sure if this would be the case. Below is the content of the file:
$vendorDir = dirname(dirname(__FILE__));
$baseDir = dirname($vendorDir);
return array(
'Monolog\\' => array($vendorDir . '/monolog/monolog/src/Monolog'),
'FileServer\\' => array($baseDir . '/src'),
);
Question(s)
How can I access the class libraries from getID3? Is there any special operation that I need to perform to make these classes available to me?
NB: The demos folder of the library does not use namespaces but rather "require" to include class files and that in my understanding forfeits the point of composer and namespaces. Resorting to Phansys Symdony library didnt help at all as I was not even sure if the library can be used on standalone projects.
Thanking you in advance.
Command composer dump-autoload is the answer thanks to #Frank.

PHP Fatal error: Class 'Doctrine\Common\Persistence\Mapping\Driver\FileDriver' not found

I follow the instructions at: http://symfony.com/doc/current/bundles/DoctrineMongoDBBundle/index.html step by step and have a Mongo server running however when I try to do :
php app/console generate:bundle --namespace=Acme/StoreBundle
I get
Class 'Doctrine\Common\Persistence\Mapping\Driver\FileDriver' not found in /Users/username/Sites/myapp/vendor/doctrine-mongodb-odm/lib/Doctrine/ODM/MongoDB/Mapping/Driver/XmlDriver.php on line 37
I am thinking something could be wrong with my parameters.ini file which still mentions MYSQL however the link above did not mention anything about that:
[parameters]
database_driver="pdo_mysql"
database_host="localhost"
database_port=""
database_name="somedb"
database_user="root"
database_password="mypassword"
mailer_transport="smtp"
mailer_host="localhost"
mailer_user=""
mailer_password=""
locale="en"
secret="093faacf47bcdcdcdcdcdc9d152fc8b"
what am I doing wrong?
UPDATE:
I have Doctrine Common downloaded and here is my registerNameSpaces function:
$loader->registerNamespaces(array(
'Symfony' => array(__DIR__.'/../vendor/symfony/src', __DIR__.'/../vendor/bundles'),
'Sensio' => __DIR__.'/../vendor/bundles',
'JMS' => __DIR__.'/../vendor/bundles',
'Doctrine\\Common' => __DIR__.'/../vendor/doctrine-common/lib',
'Doctrine\\DBAL' => __DIR__.'/../vendor/doctrine-dbal/lib',
'Doctrine\\ODM\\MongoDB' => __DIR__.'/../vendor/doctrine-mongodb-odm/lib',
'Doctrine\\MongoDB' => __DIR__.'/../vendor/doctrine-mongodb/lib',
'Doctrine' => __DIR__.'/../vendor/doctrine/lib',
'Monolog' => __DIR__.'/../vendor/monolog/src',
'Assetic' => __DIR__.'/../vendor/assetic/src',
'Metadata' => __DIR__.'/../vendor/metadata/src',
'FOS' => __DIR__.'/../vendor/bundles',
'FOS\\Rest' => __DIR__.'/../vendor/fos',
'JMS' => __DIR__.'/../vendor/bundles',
));
my deps file:
[symfony]
git=http://github.com/symfony/symfony.git
version=origin/2.0
[twig]
git=http://github.com/fabpot/Twig.git
version=v1.8.2
[monolog]
git=http://github.com/Seldaek/monolog.git
version=1.0.2
[doctrine-common]
git=http://github.com/doctrine/common.git
version=2.1.4
[doctrine-dbal]
git=http://github.com/doctrine/dbal.git
version=2.1.7
[doctrine]
git=http://github.com/doctrine/doctrine2.git
version=2.1.7
[swiftmailer]
git=http://github.com/swiftmailer/swiftmailer.git
version=v4.2.0
[assetic]
git=http://github.com/kriswallsmith/assetic.git
version=v1.0.3
[twig-extensions]
git=http://github.com/fabpot/Twig-extensions.git
[metadata]
git=http://github.com/schmittjoh/metadata.git
version=1.0.0
[SensioFrameworkExtraBundle]
git=http://github.com/sensio/SensioFrameworkExtraBundle.git
target=/bundles/Sensio/Bundle/FrameworkExtraBundle
version=origin/2.0
[JMSSecurityExtraBundle]
git=http://github.com/schmittjoh/JMSSecurityExtraBundle.git
target=/bundles/JMS/SecurityExtraBundle
version=origin/1.0.x
[SensioDistributionBundle]
git=http://github.com/sensio/SensioDistributionBundle.git
target=/bundles/Sensio/Bundle/DistributionBundle
version=origin/2.0
[SensioGeneratorBundle]
git=http://github.com/sensio/SensioGeneratorBundle.git
target=/bundles/Sensio/Bundle/GeneratorBundle
version=origin/2.0
[AsseticBundle]
git=http://github.com/symfony/AsseticBundle.git
target=/bundles/Symfony/Bundle/AsseticBundle
version=origin/2.0
[FOSUserBundle]
git=git://github.com/FriendsOfSymfony/FOSUserBundle.git
target=bundles/FOS/UserBundle
version=1.2.0
[FOSRest]
git=git://github.com/FriendsOfSymfony/FOSRest.git
target=fos/FOS/Rest
[FOSRestBundle]
git=git://github.com/FriendsOfSymfony/FOSRestBundle.git
target=bundles/FOS/RestBundle
[JMSSerializerBundle]
git=git://github.com/schmittjoh/JMSSerializerBundle.git
target=bundles/JMS/SerializerBundle
[doctrine-mongodb]
git=http://github.com/doctrine/mongodb.git
[doctrine-mongodb-odm]
git=http://github.com/doctrine/mongodb-odm.git
[DoctrineMongoDBBundle]
git=http://github.com/doctrine/DoctrineMongoDBBundle.git
target=/bundles/Symfony/Bundle/DoctrineMongoDBBundle
version=origin/2.0
I also do not see a Driver folder under mapping. I have done a php bin/vendors install --reinstall bu even that doesnt help.
You have to update the three: DBAL, Common and ORM. The three have been released at the same time. So you should use version 2.2 for the 3.
Here is the blog post http://www.doctrine-project.org/blog/doctrine-2-2-final.html
Hope this helps, it worked for me.
Doctrine MongoDB ODM was recently refactored to utilize Doctrine Common's mapping API, which exists in version 2.2+ (see PR's #350 and #370). In turn, both the 2.0 and master branches of the bundle were updated (PR #124).
Based on your deps file, you're using Doctrine Common 2.1.x, which doesn't contain the required classes. You can either bump Common to 2.2, which should be compatible with Symfony 2.0, or lock ODM and the bundle to an earlier commit hash before those PR's were merged. Checking the composer.json files in each repository would be a good way to keep track of these dependencies, even if you aren't using Composer. Hopefully, we'll have tags on the Mongo ODM repositories soon, which should alleviate this headache in the future.
You either don't have Doctrine Common installed, or you don't have it registered in your autoloader. Install it (link: https://github.com/doctrine/common) or redownload the sf2 standard edition.

Categories