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

报告相同问题?

悬赏问题

  • ¥50 导入文件到网吧的电脑并且在重启之后不会被恢复
  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号
  • ¥50 安装pyaudiokits失败
  • ¥15 计组这些题应该咋做呀
  • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?
  • ¥15 让node服务器有自动加载文件的功能