How to extend specific file of a third-party bundle in Symfony application?

317 Views Asked by At

So I have this website that uses ezPublish (which itself is based on Symfony 2.8).

To make it simple, I have this one file BinaryContent.php which contains the class BinaryContent located at :

/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).

I intend to alter BinaryContent.php to implement my own specific logic in a given method. I choose, in that way, to override this file by extending the bundle.

Therefore:

  • I tried to extend the bundle as per the Symfony documentation but it cleary states that:

    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. This is the best practice.

I already know it doesn't.

  • I have located in services.yml of this bundle that this class is registered as a service:
    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).

What should I do?


As per iainn's comment, here is how I try to override the service.

In my services.yml file:

    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.

Here's the definition of the BinaryContent class:

<?php
namespace Smile\CoreBundle\Core\REST\Server\Controller;

use eZ\Publish\Core\REST\Server\Controller\BinaryContent as BaseController;

class BinaryContent extends BaseController {}
1

There are 1 best solutions below

0
Renato Mefi On

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