From 5ccad45ea22cd4299e95cbe1666c5cab193550b0 Mon Sep 17 00:00:00 2001 From: Miha Vrhovnik Date: Sat, 22 Apr 2017 13:07:35 +0200 Subject: [PATCH] Add suppor for symfony's 3.3 service locator --- .../Compiler/ModelPass.php | 33 ++++++++++++++++--- .../lib/Model/ContainerModelLayerPooler.php | 23 ++++++++++--- sources/lib/Model/ContainerModelPooler.php | 23 ++++++++++--- 3 files changed, 64 insertions(+), 15 deletions(-) diff --git a/sources/lib/DependencyInjection/Compiler/ModelPass.php b/sources/lib/DependencyInjection/Compiler/ModelPass.php index e0ab0f9..6774976 100644 --- a/sources/lib/DependencyInjection/Compiler/ModelPass.php +++ b/sources/lib/DependencyInjection/Compiler/ModelPass.php @@ -30,6 +30,9 @@ private function addTagged(DI\ContainerBuilder $container, $tag, $defaultService /** @var DI\Definition[] $definitions */ $definitions = []; + $poolerToService = []; + $gteSf33 = version_compare(\Symfony\Component\HttpKernel\Kernel::VERSION, '3.3', '>='); + // find all service IDs with the appropriate tag $taggedServices = $container->findTaggedServiceIds($tag); @@ -49,28 +52,48 @@ private function addTagged(DI\ContainerBuilder $container, $tag, $defaultService if (!in_array($interface, class_implements($definitions[$serviceId]->getClass()), true)) { throw new \RuntimeException(sprintf('Your pooler should implement %s.', $interface)); } + + $poolerToService[$serviceId] = []; } else { throw new \RuntimeException(sprintf('There is no pooler service with id %s.', $serviceId)); } } - $definitions[$serviceId]->addMethodCall('addModelToServiceMapping', [$class, $id . '.pomm.inner']); + $innerId = $id . '.pomm.inner'; + $definitions[$serviceId]->addMethodCall('addModelToServiceMapping', [$class, $innerId]); + $poolerToService[$serviceId][$innerId] = new DI\Reference($innerId); $old = $container->getDefinition($id); $old->setPublic(true); $container->removeDefinition($id); - $container->addDefinitions([$id . '.pomm.inner' => $old]); + $container->addDefinitions([$innerId => $old]); $service = $container->register($id, $old->getClass()) ->setFactory([new DI\Reference($sessionId), $method]) ->addArgument($old->getClass()) ; - if (version_compare(\Symfony\Component\HttpKernel\Kernel::VERSION, '3.3', '<')) { - $service->addAutowiringType($old->getClass()); + if ($gteSf33) { + $service->setPublic($old->isPublic()); + //there is no need for inner service to be public anymore, because only service locator uses it. + $old->setPublic(false); + //add alias with full class to be compatible with sf 4.0 autowiring + if ($id !== $class) { + $container->setAlias($class, $id); + } + } else { + $service->setAutowiringTypes([$old->getClass()]); //set this one as a default for autowire } + } - $container->setAlias($class, $id); + if ($gteSf33) { + foreach ($poolerToService as $service => $references) { + $container->getDefinition($service) + ->removeMethodCall('setContaner') + ->addMethodCall('setContainer', [ + DI\Compiler\ServiceLocatorTagPass::register($container, $references) + ]); + } } } } diff --git a/sources/lib/Model/ContainerModelLayerPooler.php b/sources/lib/Model/ContainerModelLayerPooler.php index c33a530..00fb7c6 100644 --- a/sources/lib/Model/ContainerModelLayerPooler.php +++ b/sources/lib/Model/ContainerModelLayerPooler.php @@ -11,8 +11,8 @@ use PommProject\Foundation\Client\ClientPooler; use PommProject\ModelManager\ModelLayer\ModelLayerPooler; -use Symfony\Component\DependencyInjection\ContainerAwareInterface; -use Symfony\Component\DependencyInjection\ContainerAwareTrait; +use Symfony\Component\DependencyInjection\ContainerInterface; +use Psr\Container\ContainerInterface as PsrContainerInterface; /** * ModelLayerPooler @@ -25,11 +25,11 @@ * @license X11 {@link http://opensource.org/licenses/mit-license.php} * @see ClientPooler */ -class ContainerModelLayerPooler extends ModelLayerPooler implements ContainerAwareInterface, ServiceMapInterface +class ContainerModelLayerPooler extends ModelLayerPooler implements ServiceMapInterface { - use ContainerAwareTrait; - private $serviceMap = []; + /** @var PsrContainerInterface|ContainerInterface */ + private $container; /** * {@inheritdoc} @@ -50,4 +50,17 @@ protected function createClient($class) return parent::createClient($class); } + + /** + * @param PsrContainerInterface|ContainerInterface $container + */ + public function setContainer($container) + { + if (!($container instanceof PsrContainerInterface) && !($container instanceof ContainerInterface)) { + throw new \InvalidArgumentException( + '$contaner should be instance of Symfony\Component\DependencyInjection\ContainerInterface or Psr\Container\ContainerInterface' + ); + } + $this->container = $container; + } } diff --git a/sources/lib/Model/ContainerModelPooler.php b/sources/lib/Model/ContainerModelPooler.php index 60f110f..318d9c2 100644 --- a/sources/lib/Model/ContainerModelPooler.php +++ b/sources/lib/Model/ContainerModelPooler.php @@ -10,8 +10,8 @@ namespace PommProject\PommBundle\Model; use PommProject\ModelManager\Model\ModelPooler; -use Symfony\Component\DependencyInjection\ContainerAwareInterface; -use Symfony\Component\DependencyInjection\ContainerAwareTrait; +use Symfony\Component\DependencyInjection\ContainerInterface; +use Psr\Container\ContainerInterface as PsrContainerInterface; /** * PommExtension @@ -24,11 +24,11 @@ * @license X11 {@link http://opensource.org/licenses/mit-license.php} * @see Extension */ -class ContainerModelPooler extends ModelPooler implements ContainerAwareInterface, ServiceMapInterface +class ContainerModelPooler extends ModelPooler implements ServiceMapInterface { - use ContainerAwareTrait; - private $serviceMap = []; + /** @var PsrContainerInterface|ContainerInterface */ + private $container; /** * {@inheritdoc} @@ -49,4 +49,17 @@ protected function createClient($class) return parent::createClient($class); } + + /** + * @param PsrContainerInterface|ContainerInterface $container + */ + public function setContainer($container) + { + if (!($container instanceof PsrContainerInterface) && !($container instanceof ContainerInterface)) { + throw new \InvalidArgumentException( + '$contaner should be instance of Symfony\Component\DependencyInjection\ContainerInterface or Psr\Container\ContainerInterface' + ); + } + $this->container = $container; + } }