Symfony 2.1 Extending Core Classes

不问归期 提交于 2019-12-08 00:13:50

问题


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.


回答1:


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"
             }
},


来源:https://stackoverflow.com/questions/12749524/symfony-2-1-extending-core-classes

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!