Skip to content

Commit

Permalink
fix compatibility issue with latest version for FOSRestBundle and Doc…
Browse files Browse the repository at this point in the history
…trine dependencies
  • Loading branch information
mpoiriert committed Jun 5, 2019
1 parent d9df4c5 commit 4725e8d
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand All @@ -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]);
}
}
40 changes: 40 additions & 0 deletions DependencyInjection/FOSRestBundleCompilerPass.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php namespace Draw\DrawBundle\DependencyInjection;

use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Definition;
use Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException;
use Symfony\Component\DependencyInjection\Reference;

class FOSRestBundleCompilerPass implements CompilerPassInterface
{

/**
* You can modify the container here before it is dumped to PHP code.
*
* @param ContainerBuilder $container
*
* @api
*/
public function process(ContainerBuilder $container)
{
try {
$exceptionSubscriberDefinition = $container->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'));
}
}
}
2 changes: 2 additions & 0 deletions DrawDrawBundle.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -14,6 +15,7 @@ public function build(ContainerBuilder $container)
{
$container->addCompilerPass(new CompilerPass());
$container->addCompilerPass(new DoctrineServiceRepositoryCompilerPass());
$container->addCompilerPass(new FOSRestBundleCompilerPass());
}

public function getContainerExtension()
Expand Down
2 changes: 1 addition & 1 deletion Resources/config/api_exception_subscriber.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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']]
Expand Down
4 changes: 4 additions & 0 deletions Serializer/SelfLinkEventListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down

0 comments on commit 4725e8d

Please sign in to comment.