diff --git a/src/Symfony/Components/DependencyInjection/Builder.php b/src/Symfony/Components/DependencyInjection/Builder.php index f2165aea89..2154503c56 100644 --- a/src/Symfony/Components/DependencyInjection/Builder.php +++ b/src/Symfony/Components/DependencyInjection/Builder.php @@ -174,6 +174,18 @@ class Builder extends Container $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. * @@ -184,6 +196,25 @@ class Builder extends Container 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. * diff --git a/src/Symfony/Components/DependencyInjection/Dumper/PhpDumper.php b/src/Symfony/Components/DependencyInjection/Dumper/PhpDumper.php index b3f98b6b49..109dbc288f 100644 --- a/src/Symfony/Components/DependencyInjection/Dumper/PhpDumper.php +++ b/src/Symfony/Components/DependencyInjection/Dumper/PhpDumper.php @@ -442,7 +442,7 @@ EOF; protected function getServiceCall($id, Reference $reference = null) { - if ('service_container' == $id) + if ('service_container' === $id) { return '$this'; } @@ -453,7 +453,14 @@ EOF; } 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); + } } } } diff --git a/tests/fixtures/Symfony/Components/DependencyInjection/php/services9.php b/tests/fixtures/Symfony/Components/DependencyInjection/php/services9.php index b9a42245ce..13ce78a480 100644 --- a/tests/fixtures/Symfony/Components/DependencyInjection/php/services9.php +++ b/tests/fixtures/Symfony/Components/DependencyInjection/php/services9.php @@ -38,7 +38,7 @@ class ProjectServiceContainer extends Container { 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->initialize(); sc_configure($instance); @@ -58,8 +58,8 @@ class ProjectServiceContainer extends Container { if (isset($this->shared['bar'])) return $this->shared['bar']; - $instance = new FooClass('foo', $this->getService('foo.baz'), $this->getParameter('foo_bar')); - $this->getService('foo.baz')->configure($instance); + $instance = new FooClass('foo', $this->getFoo_BazService(), $this->getParameter('foo_bar')); + $this->getFoo_BazService()->configure($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']; $instance = new FooClass(); - $instance->setBar($this->getService('foo')); + $instance->setBar($this->getFooService()); $instance->setBar($this->getService('foo', Container::NULL_ON_INVALID_REFERENCE)); if ($this->hasService('foo')) { @@ -134,7 +134,7 @@ class ProjectServiceContainer extends Container */ protected function getAliasForFooService() { - return $this->getService('foo'); + return $this->getFooService(); } /** diff --git a/tests/unit/Symfony/Components/DependencyInjection/BuilderTest.php b/tests/unit/Symfony/Components/DependencyInjection/BuilderTest.php index c23462613d..46f7eedce6 100644 --- a/tests/unit/Symfony/Components/DependencyInjection/BuilderTest.php +++ b/tests/unit/Symfony/Components/DependencyInjection/BuilderTest.php @@ -17,7 +17,7 @@ use Symfony\Components\DependencyInjection\Reference; $fixturesPath = __DIR__.'/../../../../fixtures/Symfony/Components/DependencyInjection/'; -$t = new LimeTest(55); +$t = new LimeTest(59); // ->setDefinitions() ->addDefinitions() ->getDefinitions() ->setDefinition() ->getDefinition() ->hasDefinition() $t->diag('->setDefinitions() ->addDefinitions() ->getDefinitions() ->setDefinition() ->getDefinition() ->hasDefinition()'); @@ -89,7 +89,7 @@ try @$builder->getService('baz'); $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'); } @@ -105,14 +105,27 @@ $builder->bar = $bar = new stdClass(); $builder->register('bar', 'stdClass'); $t->is($builder->getServiceIds(), array('foo', 'bar', 'service_container'), '->getServiceIds() returns all defined service ids'); -// ->setAlias() -$t->diag('->setAlias()'); +// ->setAlias() ->getAlias() ->hasAlias() +$t->diag('->setAlias() ->getAlias() ->hasAlias()'); $builder = new Builder(); $builder->register('foo', 'stdClass'); $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->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() $t->diag('->getAliases()'); $builder = new Builder(); @@ -186,7 +199,7 @@ try $builder->getService('foo4'); $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'); }