Skip to content

Commit

Permalink
adding standard contexts as alias
Browse files Browse the repository at this point in the history
  • Loading branch information
liuggio committed Jan 14, 2014
1 parent 2d88bb7 commit c3f00ed
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 10 deletions.
2 changes: 1 addition & 1 deletion lib/PUGX/Godfather/Container/SymfonyContainerBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
5 changes: 3 additions & 2 deletions lib/PUGX/Godfather/Godfather.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

/**
Expand Down Expand Up @@ -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));
}
Expand Down
3 changes: 3 additions & 0 deletions sf2-bundle/PUGX/GodfatherBundle/Resources/config/services.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

<parameters>
<parameter key="godfather.class">PUGX\Godfather\Godfather</parameter>
<parameter key="godfather.context.class">PUGX\Godfather\Context\Context</parameter>
<parameter key="godfather.container.class">PUGX\Godfather\Container\SymfonyContainerBuilder</parameter>
</parameters>

Expand All @@ -14,6 +15,8 @@
<argument type="service" id="godfather_service_container" />
<argument>godfather</argument>
</service>
<service id="godfather.context" class="%godfather.context.class%">
</service>
<service id="godfather_service_container" class="%godfather.container.class%" public="false">
<argument type="service" id="service_container" />
</service>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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()
Expand Down

0 comments on commit c3f00ed

Please sign in to comment.