From c3f00ed3f0e394782e6275fe518e1bee42792261 Mon Sep 17 00:00:00 2001 From: liuggio Date: Tue, 14 Jan 2014 15:50:04 +0100 Subject: [PATCH] adding standard contexts as alias --- .../Container/SymfonyContainerBuilder.php | 2 +- lib/PUGX/Godfather/Godfather.php | 5 +-- .../DependencyInjection/Configuration.php | 2 +- .../GodfatherExtension.php | 31 ++++++++++++++++--- .../Resources/config/services.xml | 3 ++ .../GodfatherExtensionTest.php | 4 +-- 6 files changed, 37 insertions(+), 10 deletions(-) diff --git a/lib/PUGX/Godfather/Container/SymfonyContainerBuilder.php b/lib/PUGX/Godfather/Container/SymfonyContainerBuilder.php index 61346e0..2ac7144 100644 --- a/lib/PUGX/Godfather/Container/SymfonyContainerBuilder.php +++ b/lib/PUGX/Godfather/Container/SymfonyContainerBuilder.php @@ -78,7 +78,7 @@ public function has($id) public function get($id) { if ($this->hasAlias($id)) { - $id = $this->aliasDefinitions[strtolower($id)]; + $id = (string) $this->aliasDefinitions[strtolower($id)]; } return $this->container->get($id); diff --git a/lib/PUGX/Godfather/Godfather.php b/lib/PUGX/Godfather/Godfather.php index b734653..f90991c 100644 --- a/lib/PUGX/Godfather/Godfather.php +++ b/lib/PUGX/Godfather/Godfather.php @@ -48,9 +48,10 @@ public function getStrategy($contextName, $object) // prefix.context_name $contextServiceId = $this->converter->getServiceNamespace($this->servicePrefix, $contextName); // get the correct strategy service by the object - $strategy = $this->container->get($contextServiceId)->getStrategyName($object); + $strategy = $this->container->get($contextServiceId); + $strategy = $strategy->getStrategyName($object); // get service namespace - $strategyAlias =$this->converter->getServiceNamespace($this->servicePrefix, array($contextName, $strategy)); + $strategyAlias = $this->converter->getServiceNamespace($this->servicePrefix, array($contextName, $strategy)); if (!$this->container->has($strategyAlias)) { $strategyAlias = $this->container->get($contextServiceId)->getFallbackStrategy(); } diff --git a/sf2-bundle/PUGX/GodfatherBundle/DependencyInjection/Configuration.php b/sf2-bundle/PUGX/GodfatherBundle/DependencyInjection/Configuration.php index 50c3679..8cacd34 100755 --- a/sf2-bundle/PUGX/GodfatherBundle/DependencyInjection/Configuration.php +++ b/sf2-bundle/PUGX/GodfatherBundle/DependencyInjection/Configuration.php @@ -39,7 +39,7 @@ private function addContextSection(ArrayNodeDefinition $rootNode) ->prototype('array') ->children() ->scalarNode('fallback')->defaultNull()->end() - ->scalarNode('class')->defaultValue('PUGX\Godfather\Context\Context')->end() + ->scalarNode('class')->defaultNull()->end() ->end() ->end() ->end() diff --git a/sf2-bundle/PUGX/GodfatherBundle/DependencyInjection/GodfatherExtension.php b/sf2-bundle/PUGX/GodfatherBundle/DependencyInjection/GodfatherExtension.php index eb70399..d86fa8f 100644 --- a/sf2-bundle/PUGX/GodfatherBundle/DependencyInjection/GodfatherExtension.php +++ b/sf2-bundle/PUGX/GodfatherBundle/DependencyInjection/GodfatherExtension.php @@ -66,12 +66,17 @@ protected function addInstance($name, array $parameters, ContainerBuilder $conta protected function addContext(ContainerBuilder $container, $prefix, $name, array $context) { $fallback = null; - if (isset($context['fallback'])) { + $serviceName = $prefix.'.'.$name; + $default = 'godfather.context'; + if (isset($context['fallback']) || isset($context['class'])) { $fallback = $context['fallback']; + $context = new Definition($context['class'], array($fallback)); + $container->setDefinition($serviceName, $context); + + return ; } - $context = new Definition($context['class'], array($fallback)); - $container->setDefinition($prefix.'.'.$name, $context); + $container->setAlias($serviceName, $default); } /** @@ -104,7 +109,25 @@ protected function getOrCreateDefinition(ContainerBuilder $container, $name = 'd */ protected function createGodFatherDefinition(ContainerBuilder $container, $prefix) { - $container = new Reference('godfather_service_container'); + $container_definition = new Reference('service_container'); + $service_container = new Definition('%godfather.container.class%', array($container_definition)); + $container->setDefinition(sprintf('godfather_service_container.%s',$prefix), $service_container); + + return new Definition('%godfather.class%', array($service_container, $prefix)); + } + + + /** + * Definition Factory + * + * @param ContainerBuilder $container + * @param string $prefix + * + * @return Definition + */ + protected function createServiceContainerDefinition(ContainerBuilder $container, $prefix) + { + $container = new Definition('%godfather.class%'); return new Definition('%godfather.class%', array($container, $prefix)); } diff --git a/sf2-bundle/PUGX/GodfatherBundle/Resources/config/services.xml b/sf2-bundle/PUGX/GodfatherBundle/Resources/config/services.xml index ddb2d25..131aaf4 100644 --- a/sf2-bundle/PUGX/GodfatherBundle/Resources/config/services.xml +++ b/sf2-bundle/PUGX/GodfatherBundle/Resources/config/services.xml @@ -6,6 +6,7 @@ PUGX\Godfather\Godfather + PUGX\Godfather\Context\Context PUGX\Godfather\Container\SymfonyContainerBuilder @@ -14,6 +15,8 @@ godfather + + diff --git a/sf2-bundle/PUGX/GodfatherBundle/Tests/DependencyInjection/GodfatherExtensionTest.php b/sf2-bundle/PUGX/GodfatherBundle/Tests/DependencyInjection/GodfatherExtensionTest.php index a72bb44..a506db0 100755 --- a/sf2-bundle/PUGX/GodfatherBundle/Tests/DependencyInjection/GodfatherExtensionTest.php +++ b/sf2-bundle/PUGX/GodfatherBundle/Tests/DependencyInjection/GodfatherExtensionTest.php @@ -23,7 +23,7 @@ public function testMinimalWithMultipleContexts() $loader->load(array($this->getMinimalMultipleInstances()), $container); $this->assertTrue($container->hasDefinition('godfather'), 'The godfather is loaded'); - $this->assertTrue($container->hasDefinition('godfather.manager'), 'The godfather.manager is loaded'); + $this->assertTrue($container->hasAlias('godfather.manager'), 'The godfather.manager is loaded'); } public function testContextCreated() @@ -34,7 +34,7 @@ public function testContextCreated() $this->assertTrue($container->hasDefinition('godfather'), 'The godfather is loaded'); $this->assertTrue($container->hasDefinition('godfather.instanceA'), 'The godfather is loaded'); - $this->assertTrue($container->hasDefinition('godfather.instanceA.manager'), 'The godfather is loaded'); + $this->assertTrue($container->hasAlias('godfather.instanceA.manager'), 'The godfather is loaded'); } public function testFallbackService()