8000 Throw · symfony/symfony@59a400e · GitHub
[go: up one dir, main page]

Skip to content

Commit 59a400e

Browse files
committed
Throw
1 parent 4197002 commit 59a400e

File tree

4 files changed

+21
-22
lines changed

4 files changed

+21
-22
lines changed

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,11 @@ public function process(ContainerBuilder $container)
7777
}
7878
}
7979
}
80+
81+
$resolvedId = $container->resolveEnvPlaceholders($id, null, $usedEnvs);
82+
if (null !== $usedEnvs) {
83+
throw new RuntimeException(sprintf('A service name ("%s") cannot contain dynamic values.', $resolvedId));
84+
}
8085
}
8186
}
8287
}

src/Symfony/Component/DependencyInjection/Container.php

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,7 @@ public function set($id, $service)
219219
*/
220220
public function has($id)
221221
{
222-
for ($i = 2; ;) {
222+
for ($i = 2;;) {
223223
if (isset($this->privates[$id])) {
224224
@trigger_error(sprintf('The "%s" service is private, checking for its existence is deprecated since Symfony 3.2 and will fail in 4.0.', $id), E_USER_DEPRECATED);
225225
}
@@ -277,7 +277,7 @@ public function get($id, $invalidBehavior = self::EXCEPTION_ON_INVALID_REFERENCE
277277
// available services. Service IDs are case insensitive, however since
278278
// this method can be called thousands of times during a request, avoid
279279
// calling $this->normalizeId($id) unless necessary.
280-
for ($i = 2; ;) {
280+
for ($i = 2;;) {
281281
if (isset($this->privates[$id])) {
282282
@trigger_error(sprintf('The "%s" service is private, getting it from the container is deprecated since Symfony 3.2 and will fail in 4.0. You should either make the service public, or stop using the container directly and use dependency injection instead.', $id), E_USER_DEPRECATED);
283283
}
@@ -469,9 +469,7 @@ protected function getEnv($name)
469469
return $this->envCache[$name];
470470
}
471471
if (!$this->has($id = 'container.env_var_processors_locator')) {
472-
// We don't call set() to prevent an infinite loop:
473-
// set() calls getRemovedIds(), getRemovedIds() calls getEnv() in some cases
474-
$this->services[$id] = new ServiceLocator(array());
472+
$this->set($id, new ServiceLocator(array()));
475473
}
476474
if (!$this->getEnv) {
477475
$this->getEnv = new \ReflectionMethod($this, __FUNCTION__);

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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
use PHPUnit\Framework\TestCase;
1515
use Symfony\Component\DependencyInjection\Compiler\CheckDefinitionValidityPass;
16+
use Symfony\Component\DependencyInjection\Compiler\ResolveEnvPlaceholdersPass;
1617
use Symfony\Component\DependencyInjection\ContainerBuilder;
1718

1819
class CheckDefinitionValidityPassTest extends TestCase
@@ -76,6 +77,18 @@ public function testInvalidTags()
7677
$this->process($container);
7778
}
7879

80+
/**
81+
* @expectedException \Symfony\Component\DependencyInjection\Exception\RuntimeException
82+
*/
83+
public function testDynamicServiceName()
84+
{
85+
$container = new ContainerBuilder();
86+
$env = ($container->getParameterBag()->get('env(BAR)'));
87+
$container->register("foo.$env", 'class');
88+
89+
$this->process($container);
90+
}
91+
7992
protected function process(ContainerBuilder $container)
8093
{
8194
$pass = new CheckDefinitionValidityPass();

src/Symfony/Component/DependencyInjection/Tests/ContainerTest.php

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -538,23 +538,6 @@ public function testReplacingAPreDefinedServiceIsDeprecated()
538538

539539
$this->assertSame($bar, $c->get('bar'), '->set() replaces a pre-defined service');
540540
}
541-
542-
public function testNoInfiniteLoopInGetEnv()
543-
{
544-
$_ENV['bar'] = 'hello';
545-
$this->assertCount(1, (new RemovedIdsCallGetEnvCo 685C ntainer())->getRemovedIds());
546-
unset($_ENV['bar']);
547-
}
548-
}
549-
550-
class RemovedIdsCallGetEnvContainer extends Container
551-
{
552-
public function getRemovedIds()
553-
{
554-
return array(
555-
'foo.'.$this->getEnv('bar') => true,
556-
);
557-
}
558541
}
559542

560543
class ProjectServiceContainer extends Container

0 commit comments

Comments
 (0)
0