How can I override Core Symfony2 Classes?

后端 未结 2 706
长情又很酷
长情又很酷 2021-01-24 07:42

I want to override a core Symfony2 Class.

Specifically I want to override vendor/symfony/symfony/src/Symfony/Bundle/FrameworkBundle/Templating/TemplateReference.php, th

相关标签:
2条回答
  • You can easily override some classes defined as service (you can check on app/cache/dev/appDevDebugProejctContainer.xml), and override the service name on your parameter.ini.

    BUT if the class it not defined like this, you can create your own "patch" you apply after a composer update.

    Lot of people will say its dirty to edit core classes, but if you want good performance, its often the only solution to achieve it ;-), its simple, fast & easy.

    Other solutions are welcome ..

    0 讨论(0)
  • 2021-01-24 08:07

    Do not do this, unless you know exactly what you're doing, and you're 100% sure you won't break something ...

    If you're using the Symfony 2.0 ClassLoader component:

    $loader->registerNamespaces(array(
        'Symfony' => array(
            __DIR__.'/../src/vendor/symfony/src',
            __DIR__.'/../vendor/symfony/src',
            __DIR__.'/../vendor/bundles',
        ),
        ...
    ));
    

    To do this with composer, this should work, edit your composer.json file:

    "autoload": {
        "psr-0": {
             "": "src/",
             "Symfony": "src/vendor/symfony/src/"
        }
    },
    

    This tells the autoloader, that when it tries to load a class from the Symfony namespace, to first look in your src/vendor/symfony/src, then in vendor/symfony/src then in vendor/bundles ...

    And then duplicate the file you want to edit there:

    src/vendor/symfony/src/Symfony/Bundle/FrameworkBundle/Templating/TemplateReference.php
    
    0 讨论(0)
提交回复
热议问题