From 4725e8d5ed5a013fddeddef89c0e7abf6a83fb93 Mon Sep 17 00:00:00 2001 From: Martin Poirier-Theoret Date: Wed, 5 Jun 2019 12:04:31 -0400 Subject: [PATCH] fix compatibility issue with latest version for FOSRestBundle and Doctrine dependencies --- .../DoctrineServiceRepositoryCompilerPass.php | 4 +- .../FOSRestBundleCompilerPass.php | 40 +++++++++++++++++++ DrawDrawBundle.php | 2 + Resources/config/api_exception_subscriber.yml | 2 +- Serializer/SelfLinkEventListener.php | 4 ++ 5 files changed, 50 insertions(+), 2 deletions(-) create mode 100644 DependencyInjection/FOSRestBundleCompilerPass.php diff --git a/DependencyInjection/DoctrineServiceRepositoryCompilerPass.php b/DependencyInjection/DoctrineServiceRepositoryCompilerPass.php index 72dc7ae..00d3594 100644 --- a/DependencyInjection/DoctrineServiceRepositoryCompilerPass.php +++ b/DependencyInjection/DoctrineServiceRepositoryCompilerPass.php @@ -22,8 +22,10 @@ public function process(ContainerBuilder $container) { try { $factory = $container->findDefinition('draw.doctrine.repository.factory'); + $configurationDefinition = $container->findDefinition('doctrine.orm.configuration'); } catch (ServiceNotFoundException $e) { //The configuration draw.use_doctrine_repository_factory is probably set to false + //Or doctrine.orm.configuration is not available because of doctrine bundle not present return; } @@ -41,6 +43,6 @@ public function process(ContainerBuilder $container) } } $factory->replaceArgument(0, $repositories); - $container->findDefinition('doctrine.orm.configuration')->addMethodCall('setRepositoryFactory', [$factory]); + $configurationDefinition->addMethodCall('setRepositoryFactory', [$factory]); } } \ No newline at end of file diff --git a/DependencyInjection/FOSRestBundleCompilerPass.php b/DependencyInjection/FOSRestBundleCompilerPass.php new file mode 100644 index 0000000..e6ce0b4 --- /dev/null +++ b/DependencyInjection/FOSRestBundleCompilerPass.php @@ -0,0 +1,40 @@ +findDefinition('draw.exception_subscriber'); + } catch (ServiceNotFoundException $e) { + //The configuration draw.use_api_exception_subscriber is probably set to false + return; + } + + // This is to be compatible with old and new version for FOSRestBundle + if(!$container->hasParameter('fos_rest.exception.codes')) { + if($container->hasDefinition('fos_rest.exception.codes_map')) { + $exceptionCodes = $container->getDefinition('fos_rest.exception.codes_map')->getArgument(0); + $container->setParameter('fos_rest.exception.codes', $exceptionCodes); + } + } + + if($container->hasParameter('fos_rest.exception.codes')) { + $exceptionSubscriberDefinition->setArgument(2, $container->getParameter('fos_rest.exception.codes')); + } + } +} \ No newline at end of file diff --git a/DrawDrawBundle.php b/DrawDrawBundle.php index cab51ac..0880b2a 100644 --- a/DrawDrawBundle.php +++ b/DrawDrawBundle.php @@ -5,6 +5,7 @@ use Draw\DrawBundle\DependencyInjection\CompilerPass; use Draw\DrawBundle\DependencyInjection\DoctrineServiceRepositoryCompilerPass; use Draw\DrawBundle\DependencyInjection\DrawDrawExtension; +use Draw\DrawBundle\DependencyInjection\FOSRestBundleCompilerPass; use Symfony\Component\DependencyInjection\ContainerBuilder; use Symfony\Component\HttpKernel\Bundle\Bundle; @@ -14,6 +15,7 @@ public function build(ContainerBuilder $container) { $container->addCompilerPass(new CompilerPass()); $container->addCompilerPass(new DoctrineServiceRepositoryCompilerPass()); + $container->addCompilerPass(new FOSRestBundleCompilerPass()); } public function getContainerExtension() diff --git a/Resources/config/api_exception_subscriber.yml b/Resources/config/api_exception_subscriber.yml index d0c5832..cb4b499 100644 --- a/Resources/config/api_exception_subscriber.yml +++ b/Resources/config/api_exception_subscriber.yml @@ -7,7 +7,7 @@ services: arguments: - '@draw.error_handling.exception_message_formatter' - '%kernel.debug%' - - '%fos_rest.exception.codes%' + - [] - '%draw.exception_subscriber.violation_key%' calls: - [setLogger, ['@logger']] diff --git a/Serializer/SelfLinkEventListener.php b/Serializer/SelfLinkEventListener.php index a718670..d4fbc09 100644 --- a/Serializer/SelfLinkEventListener.php +++ b/Serializer/SelfLinkEventListener.php @@ -44,6 +44,10 @@ public function setAddClass($addClass) public function onPostSerialize(ObjectEvent $objectEvent) { + if(!class_exists(ClassUtils::class)) { + return; + } + $visitor = $objectEvent->getVisitor(); $object = $objectEvent->getObject(); $router = $this->container->get("router");