Symfony 2.1扩展核心类 [英] Symfony 2.1 Extending Core Classes

查看:60
本文介绍了Symfony 2.1扩展核心类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图用自己的核心重写Symfony的核心组件。我使用的方法是:

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

但是,当我运行composer update并安装它时,似乎从来没有考虑到这一点。

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?

任何指针都赞赏!

编辑:文件路径

src / vendor / symfony / src / Symfony / Bridge / Twig / Extension / TranslationExtension.php

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

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.

编辑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

相反,我需要扩展给定的类,并告诉Symfony使用这些类,而应该通过参数来实现:

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.

推荐答案

我终于找到了答案。

首先,如果您需要找出要重写的类:

Firstly if you need to find out which classes to override go to:

symfony/src/Symfony/Bundle/FrameworkBundle/Resources/%Your file to override%.xml

在那里您会找到可以扩展的可能Symfony类的列表/

There you will find a list of possible Symfony classes that can be extended/overridden.

然后在您的config.yml中添加(我选择将我的文件放置在扩展名称空间中):

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

最后在我的composer.json中:

Finally in my composer.json:

"autoload": {
    "psr-0": { 
                "": "src/",
                "Extend\\Symfony": "/path/to/your/symfony/classes"
             }
},

这篇关于Symfony 2.1扩展核心类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆