Skip to content

Commit

Permalink
Compiler::loadDefinitions: allow to get to existing service by class [C…
Browse files Browse the repository at this point in the history
…loses #145]
  • Loading branch information
matej21 authored and dg committed Mar 7, 2017
1 parent 5384822 commit f0c45ae
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 3 deletions.
6 changes: 4 additions & 2 deletions src/DI/Compiler.php
Expand Up @@ -284,14 +284,16 @@ public static function loadDefinitions(ContainerBuilder $builder, array $service
throw new ServiceCreationException("Circular reference detected for service '$name'.");
}
}
$depths[$name] = count($path);
$depths[$name] = count($path) + preg_match('#^@[\w\\\\]+\z#', $name);
}
array_multisort($depths, $services);

foreach ($services as $name => $def) {
if ((string) (int) $name === (string) $name) {
if (is_int($name)) {
$postfix = $def instanceof Statement && is_string($def->getEntity()) ? '.' . $def->getEntity() : (is_scalar($def) ? ".$def" : '');
$name = (count($builder->getDefinitions()) + 1) . preg_replace('#\W+#', '_', $postfix);
} elseif (preg_match('#^@[\w\\\\]+\z#', $name)) {
$name = $builder->getByType(substr($name, 1), TRUE);
} elseif ($namespace) {
$name = $namespace . '.' . $name;
}
Expand Down
15 changes: 14 additions & 1 deletion tests/DI/Compiler.services.byClass.phpt
Expand Up @@ -20,6 +20,15 @@ class Lorem

class Ipsum
{
public $value;


public function __construct($value)
{
$this->value = $value;
}


static function foo()
{
}
Expand All @@ -34,16 +43,20 @@ services:
class: Lorem(@\Ipsum)
two:
class: Ipsum
class: Ipsum(1)
setup:
- @\Ipsum::foo()
four: @\Lorem
@\Ipsum:
arguments: [2]
');


Assert::type(Lorem::class, $container->getService('one'));
Assert::type(Ipsum::class, $container->getService('two'));
Assert::same(2, $container->getService('two')->value);
Assert::type(Lorem::class, $container->getService('three'));
Assert::same($container->getService('one'), $container->getService('three'));
Assert::type(Lorem::class, $container->getService('four'));
Expand Down

0 comments on commit f0c45ae

Please sign in to comment.