Skip to content

Instantly share code, notes, and snippets.

@chalasr
Last active July 4, 2016 22:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chalasr/6a0707ca3d9f78647e2c66a6db657e13 to your computer and use it in GitHub Desktop.
Save chalasr/6a0707ca3d9f78647e2c66a6db657e13 to your computer and use it in GitHub Desktop.
JWTProvider override
  • First, create your custom service class, it could looks like:
namespace AppBundle\Security;

use FooThirdPartyBundle\OriginalService;

class CustomService extends OriginlService
{
    protected function customMethodOverride()
    {
    }  
}
  • Then, create the compiler pass to override the default service
namespace AppBundle\DependencyInjection;

use AppBundle\Security\CustomService;
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;

class ThirdPartyCompilerPass implements CompilerPassInterface
{
    public function process(ContainerBuilder $container)
    {
        $definition = $container
            ->getDefinition('third_party.original_service')
            ->setClass(CustomService::class);
    }
}
  • Last, register the compiler pass in your bundle class:
namespace AppBundle;

use AppBundle\DependencyInjection\ThirdPartyCompilerPass;
use Symfony\Component\HttpKernel\Bundle\Bundle;
use Symfony\Component\DependencyInjection\ContainerBuilder;

class AppBundle extends Bundle
{   
    public function build(ContainerBuilder $container)
    {
        parent::build($container);

        $container->addCompilerPass(new ThirdPartyCompilerPass());
    }
}

Voilà!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment