[英]How to extend specific file of a third-party bundle in Symfony application?
So I have this website that uses ezPublish (which itself is based on Symfony 2.8). 因此,我有一个使用ezPublish的网站(其本身基于Symfony 2.8)。
To make it simple, I have this one file BinaryContent.php
which contains the class BinaryContent
located at : 为简单BinaryContent.php
,我有一个文件BinaryContent.php
,其中包含位于以下位置的类BinaryContent
:
/vendor/ezsystems/ezpublish-kernel/eZ/Publish/Core/REST/Server/Controller
where /
is the root of the project (which contains app/
, src/
, web/
folders and so on). /vendor/ezsystems/ezpublish-kernel/eZ/Publish/Core/REST/Server/Controller
,其中/
是项目的根目录(包含app/
, src/
, web/
文件夹等)。
I intend to alter BinaryContent.php
to implement my own specific logic in a given method. 我打算更改BinaryContent.php
以在给定方法中实现我自己的特定逻辑。 I choose, in that way, to override this file by extending the bundle. 我选择通过扩展捆绑包覆盖此文件。
Therefore: 因此:
Overriding controllers in this way only works if the bundle refers to the controller using the standard FOSUserBundle:Registration:register syntax in routes and templates . 仅当捆绑包使用标准FOSUserBundle:Registration:register路由和模板中的语法引用控制器时,以这种方式覆盖控制器才有效 。 This is the best practice. 这是最佳做法。
I already know it doesn't. 我已经知道不是。
services.yml
of this bundle that this class is registered as a service: 我已经将此类注册为services.yml
的该捆绑包的services.yml中: ezpublish_rest.controller.binary_content: class: "%ezpublish_rest.controller.binary_content.class%" parent: ezpublish_rest.controller.base arguments: - "@ezpublish.fieldType.ezimage.variation_service"
along with 随着
parameters: ezpublish_rest.controller.binary_content.class: eZ\Publish\Core\REST\Server\Controller\BinaryContent
I therefore tried to register the same service with my own class to no avail. 因此,我尝试在我自己的课程中注册相同的服务,但无济于事。 When visiting the application, my own BinaryContent.php
file is not executed (I intentionnaly left a parse error to make it clear). 访问该应用程序时,不会执行我自己的BinaryContent.php
文件(我故意留下了一个解析错误以使其清晰可见)。
What should I do? 我该怎么办?
As per iainn's comment , here is how I try to override the service. 根据iainn的评论 ,这是我尝试覆盖服务的方式。
In my services.yml
file: 在我的services.yml
文件中:
ezpublish_rest.controller.binary_content: class: Smile\CoreBundle\Core\REST\Server\Controller\BinaryContent parent: ezpublish_rest.controller.base arguments: - "@ezpublish.fieldType.ezimage.variation_service"
My custom application bundle is called after the EzPublishCoreBundle
in AppKernel.php
. 在EzPublishCoreBundle
中的AppKernel.php
之后,将调用我的自定义应用程序捆绑包。
Here's the definition of the BinaryContent
class: 这是BinaryContent
类的定义:
<?php
namespace Smile\CoreBundle\Core\REST\Server\Controller;
use eZ\Publish\Core\REST\Server\Controller\BinaryContent as BaseController;
class BinaryContent extends BaseController {}
Create a compiler pass which modifies the class of the service definition. 创建一个修改服务定义类的编译器通道。
final class ReplaceBundleServicePass implements CompilerPassInterface
{
public function process(ContainerBuilder $container)
{
$serviceDefinition = $container->getDefinition('bundle_service');
$serviceDefinition->setClass(MyOwnClass::class);
}
}
Adding compiler passes and when it runs can be found in the official documentation: https://symfony.com/doc/current/components/dependency_injection/compilation.html#components-di-compiler-pass 可以在官方文档中找到添加编译器通行证及其运行时间: https : //symfony.com/doc/current/components/dependency_injection/compilation.html#components-di-compiler-pass
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.