8000 minor #22186 [DI] Throw on "configured-keys <> getSubscribedServices(… · symfony/symfony@329b181 · GitHub
[go: up one dir, main page]

Skip to content

Commit 329b181

Browse files
committed
minor #22186 [DI] Throw on "configured-keys <> getSubscribedServices()" mismatch (nicolas-grekas)
This PR was merged into the 3.3-dev branch. Discussion ---------- [DI] Throw on "configured-keys <> getSubscrib 8000 edServices()" mismatch | Q | A | ------------- | --- | Branch? | master | Bug fix? | no | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | - | License | MIT | Doc PR | - As reported on Slack, this creates DX issues, and provides no practical benefit. Let's throw instead of logging. Commits ------- 4da8884 [DI] Throw on "configured-keys <> getSubscribedServices()" mismatch
2 parents 05d15cf + 4da8884 commit 329b181

File tree

4 files changed

+23
-3
lines changed

4 files changed

+23
-3
lines changed

src/Symfony/Component/DependencyInjection/Compiler/RegisterServiceSubscribersPass.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,8 @@ protected function processValue($value, $isRoot = false)
9292
}
9393

9494
if ($serviceMap = array_keys($serviceMap)) {
95-
$this->container->log($this, sprintf('Service keys "%s" do not exist in the map returned by %s::getSubscribedServices() for service "%s".', implode('", "', $serviceMap), $class, $this->currentId));
95+
$message = sprintf(1 < count($serviceMap) ? 'keys "%s" do' : 'key "%s" does', str_replace('%', '%%', implode('", "', $serviceMap)));
96+
throw new InvalidArgumentException(sprintf('Service %s not exist in the map returned by %s::getSubscribedServices() for service "%s".', $message, $class, $this->currentId));
9697
}
9798

9899
$serviceLocator = $this->serviceLocator;

src/Symfony/Component/DependencyInjection/Tests/Compiler/RegisterServiceSubscribersPassTest.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,4 +115,23 @@ public function testWithAttributes()
115115

116116
$this->assertEquals($expected, $locator->getArgument(0));
117117
}
118+
119+
/**
120+
* @expectedException \Symfony\Component\DependencyInjection\Exception\InvalidArgumentException
121+
* @expectedExceptionMessage Service key "test" does not exist in the map returned by TestServiceSubscriber::getSubscribedServices() for service "foo_service".
122+
*/
123+
public function testExtraServiceSubscriber()
124+
{
125+
$container = new ContainerBuilder();
126+
$container->register('foo_service', 'TestServiceSubscriber')
127+
->setAutowired(true)
128+
->addArgument(new Reference('container'))
129+
->addTag('container.service_subscriber', array(
130+
'key' => 'test',
131+
'id' => 'TestServiceSubscriber',
132+
))
133+
;
134+
$container->register('TestServiceSubscriber', 'TestServiceSubscriber');
135+
$container->compile();
136+
}
118137
}

src/Symfony/Component/DependencyInjection/Tests/Dumper/PhpDumperTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -561,7 +561,7 @@ public function testServiceSubscriber()
561561
->setAutowired(true)
562562
->addArgument(new Reference('container'))
563563
->addTag('container.service_subscriber', array(
564-
'key' => 'test',
564+
'key' => 'bar',
565565
'id' => 'TestServiceSubscriber',
566566
))
567567
;

src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services_subscriber.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ protected function getFooServiceService()
102102
}, 'stdClass' => function () {
103103
$f = function (\stdClass $v = null) { return $v; }; return $f(${($_ = isset($this->services['autowired.stdClass']) ? $this->services['autowired.stdClass'] : $this->getAutowired_StdClassService()) && false ?: '_'});
104104
}, 'bar' => function () {
105-
$f = function (\stdClass $v) { return $v; }; return $f(${($_ = isset($this->services['autowired.stdClass']) ? $this->services['autowired.stdClass'] : $this->getAutowired_StdClassService()) && false ?: '_'});
105+
$f = function (\stdClass $v) { return $v; }; return $f(${($_ = isset($this->services['TestServiceSubscriber']) ? $this->services['TestServiceSubscriber'] : $this->get('TestServiceSubscriber')) && false ?: '_'});
106106
}, 'baz' => function () {
107107
$f = function (\stdClass $v = null) { return $v; }; return $f(${($_ = isset($this->services['autowired.stdClass']) ? $this->services 4144 ['autowired.stdClass'] : $this->getAutowired_StdClassService()) && false ?: '_'});
108108
})));

0 commit comments

Comments
 (0)
0