[DependencyInjection] made a speed optimization

This commit is contained in:
Fabien Potencier 2010-01-21 21:28:21 +01:00
parent ee3e298c5d
commit c9d0a73fe3
4 changed files with 63 additions and 12 deletions

View File

@ -174,6 +174,18 @@ class Builder extends Container
$this->aliases[$alias] = $id; $this->aliases[$alias] = $id;
} }
/**
* Returns true if an alias exists under the given identifier.
*
* @param string $id The service identifier
*
* @return Boolean true if the alias exists, false otherwise
*/
public function hasAlias($id)
{
return array_key_exists($id, $this->aliases);
}
/** /**
* Gets all defined aliases. * Gets all defined aliases.
* *
@ -184,6 +196,25 @@ class Builder extends Container
return $this->aliases; return $this->aliases;
} }
/**
* Gets an alias.
*
* @param string $id The service identifier
*
* @return string The aliased service identifier
*
* @throws \InvalidArgumentException if the alias does not exist
*/
public function getAlias($id)
{
if (!$this->hasAlias($id))
{
throw new \InvalidArgumentException(sprintf('The service alias "%s" does not exist.', $id));
}
return $this->aliases[$id];
}
/** /**
* Registers a service definition. * Registers a service definition.
* *

View File

@ -442,7 +442,7 @@ EOF;
protected function getServiceCall($id, Reference $reference = null) protected function getServiceCall($id, Reference $reference = null)
{ {
if ('service_container' == $id) if ('service_container' === $id)
{ {
return '$this'; return '$this';
} }
@ -453,7 +453,14 @@ EOF;
} }
else else
{ {
return sprintf('$this->getService(\'%s\')', $id); if ($this->container->hasDefinition($id) || $this->container->hasAlias($id))
{
return sprintf('$this->get%sService()', Container::camelize($id));
}
else
{
return sprintf('$this->getService(\'%s\')', $id);
}
} }
} }
} }

View File

@ -38,7 +38,7 @@ class ProjectServiceContainer extends Container
{ {
require_once '%path%/foo.php'; require_once '%path%/foo.php';
$instance = call_user_func(array('FooClass', 'getInstance'), 'foo', $this->getService('foo.baz'), array($this->getParameter('foo') => 'foo is '.$this->getParameter('foo'), 'bar' => $this->getParameter('foo')), true, $this); $instance = call_user_func(array('FooClass', 'getInstance'), 'foo', $this->getFoo_BazService(), array($this->getParameter('foo') => 'foo is '.$this->getParameter('foo'), 'bar' => $this->getParameter('foo')), true, $this);
$instance->setBar('bar'); $instance->setBar('bar');
$instance->initialize(); $instance->initialize();
sc_configure($instance); sc_configure($instance);
@ -58,8 +58,8 @@ class ProjectServiceContainer extends Container
{ {
if (isset($this->shared['bar'])) return $this->shared['bar']; if (isset($this->shared['bar'])) return $this->shared['bar'];
$instance = new FooClass('foo', $this->getService('foo.baz'), $this->getParameter('foo_bar')); $instance = new FooClass('foo', $this->getFoo_BazService(), $this->getParameter('foo_bar'));
$this->getService('foo.baz')->configure($instance); $this->getFoo_BazService()->configure($instance);
return $this->shared['bar'] = $instance; return $this->shared['bar'] = $instance;
} }
@ -113,7 +113,7 @@ class ProjectServiceContainer extends Container
if (isset($this->shared['method_call1'])) return $this->shared['method_call1']; if (isset($this->shared['method_call1'])) return $this->shared['method_call1'];
$instance = new FooClass(); $instance = new FooClass();
$instance->setBar($this->getService('foo')); $instance->setBar($this->getFooService());
$instance->setBar($this->getService('foo', Container::NULL_ON_INVALID_REFERENCE)); $instance->setBar($this->getService('foo', Container::NULL_ON_INVALID_REFERENCE));
if ($this->hasService('foo')) if ($this->hasService('foo'))
{ {
@ -134,7 +134,7 @@ class ProjectServiceContainer extends Container
*/ */
protected function getAliasForFooService() protected function getAliasForFooService()
{ {
return $this->getService('foo'); return $this->getFooService();
} }
/** /**

View File

@ -17,7 +17,7 @@ use Symfony\Components\DependencyInjection\Reference;
$fixturesPath = __DIR__.'/../../../../fixtures/Symfony/Components/DependencyInjection/'; $fixturesPath = __DIR__.'/../../../../fixtures/Symfony/Components/DependencyInjection/';
$t = new LimeTest(55); $t = new LimeTest(59);
// ->setDefinitions() ->addDefinitions() ->getDefinitions() ->setDefinition() ->getDefinition() ->hasDefinition() // ->setDefinitions() ->addDefinitions() ->getDefinitions() ->setDefinition() ->getDefinition() ->hasDefinition()
$t->diag('->setDefinitions() ->addDefinitions() ->getDefinitions() ->setDefinition() ->getDefinition() ->hasDefinition()'); $t->diag('->setDefinitions() ->addDefinitions() ->getDefinitions() ->setDefinition() ->getDefinition() ->hasDefinition()');
@ -89,7 +89,7 @@ try
@$builder->getService('baz'); @$builder->getService('baz');
$t->fail('->getService() throws a LogicException if the service has a circular reference to itself'); $t->fail('->getService() throws a LogicException if the service has a circular reference to itself');
} }
catch (LogicException $e) catch (\LogicException $e)
{ {
$t->pass('->getService() throws a LogicException if the service has a circular reference to itself'); $t->pass('->getService() throws a LogicException if the service has a circular reference to itself');
} }
@ -105,14 +105,27 @@ $builder->bar = $bar = new stdClass();
$builder->register('bar', 'stdClass'); $builder->register('bar', 'stdClass');
$t->is($builder->getServiceIds(), array('foo', 'bar', 'service_container'), '->getServiceIds() returns all defined service ids'); $t->is($builder->getServiceIds(), array('foo', 'bar', 'service_container'), '->getServiceIds() returns all defined service ids');
// ->setAlias() // ->setAlias() ->getAlias() ->hasAlias()
$t->diag('->setAlias()'); $t->diag('->setAlias() ->getAlias() ->hasAlias()');
$builder = new Builder(); $builder = new Builder();
$builder->register('foo', 'stdClass'); $builder->register('foo', 'stdClass');
$builder->setAlias('bar', 'foo'); $builder->setAlias('bar', 'foo');
$t->ok($builder->hasAlias('bar'), '->hasAlias() returns true if the alias exists');
$t->ok(!$builder->hasAlias('foobar'), '->hasAlias() returns false if the alias does not exist');
$t->is($builder->getAlias('bar'), 'foo', '->getAlias() returns the aliased service');
$t->ok($builder->hasService('bar'), '->setAlias() defines a new service'); $t->ok($builder->hasService('bar'), '->setAlias() defines a new service');
$t->ok($builder->getService('bar') === $builder->getService('foo'), '->setAlias() creates a service that is an alias to another one'); $t->ok($builder->getService('bar') === $builder->getService('foo'), '->setAlias() creates a service that is an alias to another one');
try
{
$builder->getAlias('foobar');
$t->fail('->getAlias() throws an InvalidArgumentException if the alias does not exist');
}
catch (\InvalidArgumentException $e)
{
$t->pass('->getAlias() throws an InvalidArgumentException if the alias does not exist');
}
// ->getAliases() // ->getAliases()
$t->diag('->getAliases()'); $t->diag('->getAliases()');
$builder = new Builder(); $builder = new Builder();
@ -186,7 +199,7 @@ try
$builder->getService('foo4'); $builder->getService('foo4');
$t->fail('->createService() throws an InvalidArgumentException if the configure callable is not a valid callable'); $t->fail('->createService() throws an InvalidArgumentException if the configure callable is not a valid callable');
} }
catch (InvalidArgumentException $e) catch (\InvalidArgumentException $e)
{ {
$t->pass('->createService() throws an InvalidArgumentException if the configure callable is not a valid callable'); $t->pass('->createService() throws an InvalidArgumentException if the configure callable is not a valid callable');
} }