douxidao3524 2012-10-05 15:33
浏览 45
已采纳

Symfony 2.1扩展核心类

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条回答 默认 最新

  • douxuan0698 2012-10-06 15:39
    关注

    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"
                 }
    },
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改
  • ¥20 wireshark抓不到vlan
  • ¥20 关于#stm32#的问题:需要指导自动酸碱滴定仪的原理图程序代码及仿真
  • ¥20 设计一款异域新娘的视频相亲软件需要哪些技术支持
  • ¥15 stata安慰剂检验作图但是真实值不出现在图上
  • ¥15 c程序不知道为什么得不到结果
  • ¥15 键盘指令混乱情况下的启动盘系统重装
  • ¥40 复杂的限制性的商函数处理