I am trying to override the core Symfony components with my own. The method I am using is:
"autoload": {
"psr-0": {
"": "src/",
"Symfony": "src/vendor/symfony/src/"
}
}
However when I run composer update and install it never seems to take this into account. Is this not the correct method to force the autoloader to look into another directory first before the Symfony core?
Any pointers appreciated!
EDIT: Paths to files
src/vendor/symfony/src/Symfony/Bridge/Twig/Extension/TranslationExtension.php
src/vendor/symfony/src/Symfony/Component/Translation/Translator.php
src/vendor/symfony/src/Symfony/Component/Translation/Loader/YamlFileLoader.php
<?php
// autoload_namespaces.php generated by Composer
$vendorDir = dirname(__DIR__);
$baseDir = dirname($vendorDir);
return array(
'Twig_Extensions_' => $vendorDir . '/twig/extensions/lib/',
'Twig_' => $vendorDir . '/twig/twig/lib/',
'Symfony\\Bundle\\SwiftmailerBundle' => $vendorDir . '/symfony/swiftmailer-bundle/',
'Symfony\\Bundle\\MonologBundle' => $vendorDir . '/symfony/monolog-bundle/',
'Symfony\\Bundle\\AsseticBundle' => $vendorDir . '/symfony/assetic-bundle/',
'Symfony' => array($vendorDir . '/symfony/symfony/src/', '/src/vendor/symfony/src/'),
'SessionHandlerInterface' => $vendorDir . '/symfony/symfony/src/Symfony/Component/HttpFoundation/Resources/stubs',
'Sensio\\Bundle\\GeneratorBundle' => $vendorDir . '/sensio/generator-bundle/',
'Sensio\\Bundle\\FrameworkExtraBundle' => $vendorDir . '/sensio/framework-extra-bundle/',
'Sensio\\Bundle\\DistributionBundle' => $vendorDir . '/sensio/distribution-bundle/',
'Monolog' => $vendorDir . '/monolog/monolog/src/',
'Metadata\\' => $vendorDir . '/jms/metadata/src/',
'JMS\\SecurityExtraBundle' => $vendorDir . '/jms/security-extra-bundle/',
'JMS\\DiExtraBundle' => $vendorDir . '/jms/di-extra-bundle/',
'JMS\\AopBundle' => $vendorDir . '/jms/aop-bundle/',
'Doctrine\\ORM' => $vendorDir . '/doctrine/orm/lib/',
'Doctrine\\DBAL' => $vendorDir . '/doctrine/dbal/lib/',
'Doctrine\\Common' => $vendorDir . '/doctrine/common/lib/',
'Doctrine\\Bundle\\DoctrineBundle' => $vendorDir . '/doctrine/doctrine-bundle/',
'CG\\' => $vendorDir . '/jms/cg/src/',
'Assetic' => $vendorDir . '/kriswallsmith/assetic/src/',
'' => $baseDir . '/src/',
);
If I manually edit the autoload line to:
'Symfony' => array('/Users/macbook/Sites/Opia/Gaia/src/vendor/symfony/src/', $vendorDir . '/symfony/symfony/src/'),
Everything works as it is meant to. So now it is basically finding the solution to why composer does not compose properly.
EDIT 2:
Doing some more searching it appears the above method is not a good implementation:
https://groups.google.com/forum/#!msg/composer-dev/HprRV_wJZg4/M_zQ8O7lB6IJ
Instead I need to extend the given classes and tell Symfony to use those classes instead which should be possible through the parameters:
parameters:
security.authentication.listener.anonymous.class: Acme\DemoBundle
\Security\Http\Firewall\AcmeAnonymousAuthenticationListener
However I am not sure what parameters I need to set to load the extended classes in place of the core.
I finally found the answer to this.
Firstly if you need to find out which classes to override go to:
symfony/src/Symfony/Bundle/FrameworkBundle/Resources/%Your file to override%.xml
There you will find a list of possible Symfony classes that can be extended/overridden.
Then in your config.yml add (I chose to put mine in an extend namespace):
parameters:
twig.extension.trans.class: Extend\Symfony\Bridge\Twig\Extension\TranslationExtension
translation.loader.yml.class: Extend\Symfony\Component\Translation\Loader\YamlFileLoader
translator.class: Extend\Symfony\Bundle\FrameworkBundle\Translation\Translator
Finally in my composer.json:
"autoload": {
"psr-0": {
"": "src/",
"Extend\\Symfony": "/path/to/your/symfony/classes"
}
},
Related
Recently I discovered that Zend Framework 1 (ZF1) can be installed using composer so I did a: composer require zendframework/zendframework1. That change my composer.json by adding the following:
{
...
"autoload": {
"psr-0": { "": ["application/models", "application/controllers", "application/forms"]},
"psr-4": { "": ["src/", "library/"]}
},
"require": {
...
"zendframework/zendframework1": "^1.12"
}
}
After run the command composer update and composer dump-autoload I look into the composer generated files and I can see the following:
/vendor/composer/autoload_files.php
===================================
// autoload_files.php #generated by Composer
$vendorDir = dirname(dirname(__FILE__));
$baseDir = dirname($vendorDir);
return array(
'92c8763cd6170fce6fcfe7e26b4e8c10' => $vendorDir . '/symfony/phpunit-bridge/bootstrap.php',
'0e6d7bf4a5811bfa5cf40c5ccd6fae6a' => $vendorDir . '/symfony/polyfill-mbstring/bootstrap.php',
'c964ee0ededf28c96ebd9db5099ef910' => $vendorDir . '/guzzlehttp/promises/src/functions_include.php',
'a0edc8309cc5e1d60e3047b5df6b7052' => $vendorDir . '/guzzlehttp/psr7/src/functions_include.php',
'2c102faa651ef8ea5874edb585946bce' => $vendorDir . '/swiftmailer/swiftmailer/lib/swift_required.php',
'b067bc7112e384b61c701452d53a14a8' => $vendorDir . '/mtdowling/jmespath.php/src/JmesPath.php',
'37a3dc5111fe8f707ab4c132ef1dbc62' => $vendorDir . '/guzzlehttp/guzzle/src/functions_include.php',
'8a9dc1de0ca7e01f3e08231539562f61' => $vendorDir . '/aws/aws-sdk-php/src/functions.php',
'5255c38a0faeba867671b61dfda6d864' => $vendorDir . '/paragonie/random_compat/lib/random.php',
'023d27dca8066ef29e6739335ea73bad' => $vendorDir . '/symfony/polyfill-php70/bootstrap.php',
'bd9634f2d41831496de0d3dfe4c94881' => $vendorDir . '/symfony/polyfill-php56/bootstrap.php',
'6a47392539ca2329373e0d33e1dba053' => $vendorDir . '/symfony/polyfill-intl-icu/bootstrap.php',
'32dcc8afd4335739640db7d200c1971d' => $vendorDir . '/symfony/polyfill-apcu/bootstrap.php',
'3a37ebac017bc098e9a86b35401e7a68' => $vendorDir . '/mongodb/mongodb/src/functions.php',
);
/vendor/composer/autoload_namespaces.php
===================================
$vendorDir = dirname(dirname(__FILE__));
$baseDir = dirname($vendorDir);
return array(
'Zend_' => array($vendorDir . '/zendframework/zendframework1/library'),
'Twig_' => array($vendorDir . '/twig/twig/lib'),
'SensioLabs\\Security' => array($vendorDir . '/sensiolabs/security-checker'),
'Doctrine\\ORM\\' => array($vendorDir . '/doctrine/orm/lib'),
'Doctrine\\DBAL\\' => array($vendorDir . '/doctrine/dbal/lib'),
'Doctrine\\Common\\Lexer\\' => array($vendorDir . '/doctrine/lexer/lib'),
'Doctrine\\Common\\Inflector\\' => array($vendorDir . '/doctrine/inflector/lib'),
'Doctrine\\Common\\Collections\\' => array($vendorDir . '/doctrine/collections/lib'),
'' => array($baseDir . '/application/models', $baseDir . '/application/controllers', $baseDir . '/application/forms'),
);
/vendor/composer/include_paths.php
===================================
$vendorDir = dirname(dirname(__FILE__));
$baseDir = dirname($vendorDir);
return array(
$vendorDir . '/zendframework/zendframework1/library',
);
I am trying to use then the class from the application/bootstrap.php file as for example:
use Zend_Config_Ini;
$config = new Zend_Config_Ini('./../config/config.ini', 'general');
But PHP is complaining with the following error:
PHP Fatal error: Uncaught Error: Class 'Zend_Config_Ini' not found in
/var/www/html/application/bootstrap.php:23
Why? What I am missing here?
That are composer basics, you need to include the autoloader file to be able to load those classes.
require __DIR__ . '/vendor/autoload.php';
I have already seen that there is an autoload_namespace.php file in Symfony but I cant seem to get it to work.
I want to autoload the: Symfony\Component\HttpFoundation\Session\Session namespace.
Here is the autoload_namespace.php so far
<?php
// autoload_namespaces.php #generated by Composer
$vendorDir = dirname(dirname(__FILE__));
$baseDir = dirname($vendorDir);
return array(
'Twig_' => array($vendorDir . '/twig/twig/lib'),
'SensioLabs\\Security' => array($vendorDir . '/sensiolabs/security-checker'),
'Psr\\Log\\' => array($vendorDir . '/psr/log'),
'Doctrine\\ORM\\' => array($vendorDir . '/doctrine/orm/lib'),
'Doctrine\\DBAL\\' => array($vendorDir . '/doctrine/dbal/lib'),
'Doctrine\\Common\\Lexer\\' => array($vendorDir . '/doctrine/lexer/lib'),
'Doctrine\\Common\\Inflector\\' => array($vendorDir . '/doctrine/inflector/lib'),
'Doctrine\\Common\\Collections\\' => array($vendorDir . '/doctrine/collections/lib'),
'Doctrine\\Common\\Annotations\\' => array($vendorDir . '/doctrine/annotations/lib'),
//this is the session namespace i want to auto include
'Symfony\\Component\\HttpFoundation\\Session\\Session\\' => array($vendorDir . '/symfony/symfony/src/Symfony/Component/HttpFoundation/Session/Session.php')
);
Thanks
So basically I just installed Phalcon 2.0 on my ubuntu machine, with apache2, mysql and all the stuff installed. I went straight into the documentation of Phalcon to get me started. After following the exact steps from their tutorial, I got this error:
Fatal error: Class 'Users' not found in /var/www/html/phalcon/tutorial/app/controllers/SignupController.php on line 14
I think it may be related to namespacing of classes and stuff like that, but I wasn't able to figure it out so far.
Looks like you didn't register your folders properly. I add a resume of my code to give you an idea.
File index.php
use Phalcon\Mvc\Micro;
use Phalcon\Events\Manager as EventsManager;
define('APP_DIR', dirname(__DIR__) .'/');
try {
$config = require APP_DIR .'config/config.php';
require APP_DIR .'config/loader.php';
...
} catch (Exception $e) {}
File config.php
return new Phalcon\Config([
'application' => [
'environment' => 'development',
'controllers' => APP_DIR .'controllers/',
'library' => APP_DIR .'library/',
'models' => APP_DIR .'models/',
'plugins' => APP_DIR .'plugins/',
'routes' => APP_DIR .'routes/',
'logs' => APP_DIR .'logs/',
'base_uri' => '/',
'debug' => false
]
]);
File loader.php
<?php
$loader = new \Phalcon\Loader;
// Register directories
$loader->registerDirs([
$config->application->controllers,
$config->application->library,
$config->application->models,
$config->application->plugins,
])->register();
// Composer autoloader
require_once APP_DIR .'vendor/autoload.php';
This should resolve any class specified in config folders.
I'm working with composer and I have the following classmap:
return array(
'Wit\\Cache\\CacheAdapterInterface' => $baseDir . '/src/Wit/Cache/CacheAdapterInterface.php',
'Wit\\Cache\\CacheManager' => $baseDir . '/src/Wit/Cache/CacheManager.php',
'Wit\\Cache\\FileAdapter' => $baseDir . '/src/Wit/Cache/FileAdapter.php',
'Wit\\Cache\\RedisAdapter' => $baseDir . '/src/Wit/Cache/RedisAdapter.php',
'Wit\\Request\\Request' => $baseDir . '/src/Wit/Request/Request.php',
'Wit\\Zenpoint\\ZenpointUser' => $baseDir . '/src/Wit/Zenpoint/ZenpointUser.php',
'Wit\\Zenpoint\\ZenpointConfig' => $baseDir . '/src/Wit/Zenpoint/ZenpointConfig.php',
'Wit\\Zenpoint\\ZenpointCarrello' => $baseDir . '/src/Wit/Zenpoint/ZenpointCarrello.php',
'Wit\\Zenpoint\\ZenpointAcquisitions' => $baseDir . '/src/Wit/Zenpoint/ZenpointAcquisitions.php',
'Wit\\Zenpoint\\ZenpointAcquisition' => $baseDir . '/src/Wit/Zenpoint/ZenpointAcquisition.php',
'Wit\\Cart\\ZipCodeChecker' => $baseDir . '/src/Wit/Cart/ZipCodeChecker.php',
'Wit\\Cart\\ZipCodeLoader' => $baseDir . '/src/Wit/Cart/ZipCodeLoader.php',
'Wit\\Complimentary\\OmaggioOrdine' => $baseDir . '/src/Wit/Complimentary/OmaggioOrdine.php',
'Wit\\Complimentary\\PacchettoOmaggio' => $baseDir . '/src/Wit/Complimentary/PacchettoOmaggio.php',
'Wit\\Complimentary\\ProdottiAScelta' => $baseDir . '/src/Wit/Complimentary/ProdottiAScelta.php',
'Wit\\Complimentary\\OmaggioLoader' => $baseDir . '/src/Wit/Complimentary/OmaggioLoader.php',
'Wit\\Complimentary\\PacchettoLoader' => $baseDir . '/src/Wit/Complimentary/PacchettoLoader.php',
'Wit\\Complimentary\\ProdottiASceltaLoader' => $baseDir . '/src/Wit/Complimentary/ProdottiASceltaLoader.php',
'Wit\\Complimentary\\ProdottiLoader' => $baseDir . '/src/Wit/Complimentary/ProdottiLoader.php',
);
I then have the following in a file: use WIT\Complimentary\OmaggioLoader;
The problem is that PHP says it cannot find the class:
Fatal error: Class 'WIT\ComplimentaryProducts\OmaggioLoader' not found.
The first thing I thought was that the path was wrong, but it doesn't seem so. So I tried to see what was happening in the Composer ClassLoader.php file and dumped the following:
var_dump($class); //string(31) "WIT\Complimentary\OmaggioLoader"
var_dump(array_keys($this->classMap)); //[15]=> string(31) "Wit\Complimentary\OmaggioLoader"
var_dump(array_key_exists($class, $this->classMap)); //bool(false)
var_dump($this->classMap[$class]); //NULL
var_dump($this->classMap['Wit\\Complimentary\\OmaggioLoader']);//string(69) "/path/to/Wit/Complimentary/OmaggioLoader.php"
So, why PHP cannot find the array element if the key is correct? Why if I insert it manually, will PHP finds it?
Class names in PHP are case insensitive, array keys are not. They have different beginning.
I'm having I confusion in understanding the namespace from the Yii documentation as their are not enough example to understand, as I'm new to Yii framework please give some easy and detailed examples so that I can understand it's purpose.
You may try read this documentation http://yiiframework.ru/doc/guide/en/basics.namespace
In general Yii namespaces - it is aliases for folders.
I.E.
'aliases' => array(
'frontend' => dirname(__FILE__) . '/../..' . '/frontend',
'common' => dirname(__FILE__) . '/../..' . '/common',
'backend' => dirname(__FILE__) . '/../..' . '/backend',
'vendor' => dirname(__FILE__) . '/../..' . '/common/lib/vendor'
),
...
'import' => array(
'common.extensions.components.*',
'common.components.*',
'common.helpers.*',
'common.models.*',
'application.controllers.*',
// In result "common" = dirname(__FILE__) . '/../..' . '/common'