Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

[DI] Dont resolve envs in service ids #25146

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 24, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -77,16 +77,20 @@ public function process(ContainerBuilder $container)
}
}

$resolvedId = $container->resolveEnvPlaceholders($id, null, $usedEnvs);
if (null !== $usedEnvs) {
throw new EnvParameterException(array($resolvedId), null, 'A service name ("%s") cannot contain dynamic values.');
if ($definition->isPublic()) {
$resolvedId = $container->resolveEnvPlaceholders($id, null, $usedEnvs);
if (null !== $usedEnvs) {
throw new EnvParameterException(array($resolvedId), null, 'A service name ("%s") cannot contain dynamic values.');
}
}
}

foreach ($container->getAliases() as $id => $alias) {
$resolvedId = $container->resolveEnvPlaceholders($id, null, $usedEnvs);
if (null !== $usedEnvs) {
throw new EnvParameterException(array($resolvedId), null, 'An alias name ("%s") cannot contain dynamic values.');
if ($alias->isPublic()) {
$resolvedId = $container->resolveEnvPlaceholders($id, null, $usedEnvs);
if (null !== $usedEnvs) {
throw new EnvParameterException(array($resolvedId), null, 'An alias name ("%s") cannot contain dynamic values.');
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ protected function processValue($value, $isRoot = false)

$value = parent::processValue($value, $isRoot);

if ($value && is_array($value)) {
if ($value && is_array($value) && !$isRoot) {
$value = array_combine($this->container->resolveEnvPlaceholders(array_keys($value), true), $value);
}

Expand Down
33 changes: 17 additions & 16 deletions 33 src/Symfony/Component/DependencyInjection/Dumper/PhpDumper.php
Original file line number Diff line number Diff line change
Expand Up @@ -680,6 +680,7 @@ private function addServices()
private function addNewInstance(Definition $definition, $return, $instantiation, $id)
{
$class = $this->dumpValue($definition->getClass());
$return = ' '.$return.$instantiation;

$arguments = array();
foreach ($definition->getArguments() as $value) {
Expand All @@ -695,7 +696,7 @@ private function addNewInstance(Definition $definition, $return, $instantiation,

if ($callable[0] instanceof Reference
|| ($callable[0] instanceof Definition && $this->definitionVariables->contains($callable[0]))) {
return sprintf(" $return{$instantiation}%s->%s(%s);\n", $this->dumpValue($callable[0]), $callable[1], $arguments ? implode(', ', $arguments) : '');
return $return.sprintf("%s->%s(%s);\n", $this->dumpValue($callable[0]), $callable[1], $arguments ? implode(', ', $arguments) : '');
}

$class = $this->dumpValue($callable[0]);
Expand All @@ -705,24 +706,24 @@ private function addNewInstance(Definition $definition, $return, $instantiation,
throw new RuntimeException(sprintf('Cannot dump definition: The "%s" service is defined to be created by a factory but is missing the service reference, did you forget to define the factory service id or class?', $id));
}

return sprintf(" $return{$instantiation}%s::%s(%s);\n", $this->dumpLiteralClass($class), $callable[1], $arguments ? implode(', ', $arguments) : '');
return $return.sprintf("%s::%s(%s);\n", $this->dumpLiteralClass($class), $callable[1], $arguments ? implode(', ', $arguments) : '');
}

if (0 === strpos($class, 'new ')) {
return sprintf(" $return{$instantiation}(%s)->%s(%s);\n", $this->dumpValue($callable[0]), $callable[1], $arguments ? implode(', ', $arguments) : '');
return $return.sprintf("(%s)->%s(%s);\n", $this->dumpValue($callable[0]), $callable[1], $arguments ? implode(', ', $arguments) : '');
}

return sprintf(" $return{$instantiation}call_user_func(array(%s, '%s')%s);\n", $this->dumpValue($callable[0]), $callable[1], $arguments ? ', '.implode(', ', $arguments) : '');
return $return.sprintf("call_user_func(array(%s, '%s')%s);\n", $this->dumpValue($callable[0]), $callable[1], $arguments ? ', '.implode(', ', $arguments) : '');
}

return sprintf(" $return{$instantiation}%s(%s);\n", $this->dumpLiteralClass($this->dumpValue($callable)), $arguments ? implode(', ', $arguments) : '');
return $return.sprintf("%s(%s);\n", $this->dumpLiteralClass($this->dumpValue($callable)), $arguments ? implode(', ', $arguments) : '');
}

if (false !== strpos($class, '$')) {
return sprintf(" \$class = %s;\n\n $return{$instantiation}new \$class(%s);\n", $class, implode(', ', $arguments));
return sprintf(" \$class = %s;\n\n%snew \$class(%s);\n", $class, $return, implode(', ', $arguments));
}

return sprintf(" $return{$instantiation}new %s(%s);\n", $this->dumpLiteralClass($class), implode(', ', $arguments));
return $return.sprintf("new %s(%s);\n", $this->dumpLiteralClass($class), implode(', ', $arguments));
}

/**
Expand Down Expand Up @@ -890,7 +891,7 @@ private function addNormalizedIds()
ksort($normalizedIds);
foreach ($normalizedIds as $id => $normalizedId) {
if ($this->container->has($normalizedId)) {
$code .= ' '.$this->export($id).' => '.$this->export($normalizedId).",\n";
$code .= ' '.$this->doExport($id).' => '.$this->doExport($normalizedId).",\n";
}
}

Expand All @@ -912,7 +913,7 @@ private function addMethodMap()
$code = " \$this->methodMap = array(\n";
ksort($definitions);
foreach ($definitions as $id => $definition) {
$code .= ' '.$this->export($id).' => '.$this->export($this->generateMethodName($id)).",\n";
$code .= ' '.$this->doExport($id).' => '.$this->doExport($this->generateMethodName($id)).",\n";
}

return $code." );\n";
Expand All @@ -933,7 +934,7 @@ private function addPrivateServices()
ksort($definitions);
foreach ($definitions as $id => $definition) {
if (!$definition->isPublic()) {
$code .= ' '.$this->export($id)." => true,\n";
$code .= ' '.$this->doExport($id)." => true,\n";
}
}

Expand Down Expand Up @@ -966,7 +967,7 @@ private function addAliases()
while (isset($aliases[$id])) {
$id = (string) $aliases[$id];
}
$code .= ' '.$this->export($alias).' => '.$this->export($id).",\n";
$code .= ' '.$this->doExport($alias).' => '.$this->doExport($id).",\n";
}

return $code." );\n";
Expand Down Expand Up @@ -1688,9 +1689,9 @@ private function exportTargetDirs()
private function export($value)
{
if (null !== $this->targetDirRegex && is_string($value) && preg_match($this->targetDirRegex, $value, $matches, PREG_OFFSET_CAPTURE)) {
$prefix = $matches[0][1] ? $this->doExport(substr($value, 0, $matches[0][1])).'.' : '';
$prefix = $matches[0][1] ? $this->doExport(substr($value, 0, $matches[0][1]), true).'.' : '';
$suffix = $matches[0][1] + strlen($matches[0][0]);
$suffix = isset($value[$suffix]) ? '.'.$this->doExport(substr($value, $suffix)) : '';
$suffix = isset($value[$suffix]) ? '.'.$this->doExport(substr($value, $suffix), true) : '';
$dirname = '__DIR__';

if (0 < $offset = 1 + $this->targetDirMaxMatches - count($matches)) {
Expand All @@ -1704,10 +1705,10 @@ private function export($value)
return $dirname;
}

return $this->doExport($value);
return $this->doExport($value, true);
}

private function doExport($value)
private function doExport($value, $resolveEnv = false)
{
if (is_string($value) && false !== strpos($value, "\n")) {
$cleanParts = explode("\n", $value);
Expand All @@ -1717,7 +1718,7 @@ private function doExport($value)
$export = var_export($value, true);
}

if ("'" === $export[0] && $export !== $resolvedExport = $this->container->resolveEnvPlaceholders($export, "'.\$this->getEnv('%s').'")) {
if ($resolveEnv && "'" === $export[0] && $export !== $resolvedExport = $this->container->resolveEnvPlaceholders($export, "'.\$this->getEnv('%s').'")) {
$export = $resolvedExport;
if ("'" === $export[1]) {
$export = substr($export, 3);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
namespace Symfony\Component\DependencyInjection\Tests\Compiler;

use PHPUnit\Framework\TestCase;
use Symfony\Component\DependencyInjection\Alias;
use Symfony\Component\DependencyInjection\Compiler\CheckDefinitionValidityPass;
use Symfony\Component\DependencyInjection\ContainerBuilder;

Expand Down Expand Up @@ -79,27 +80,39 @@ public function testInvalidTags()
/**
* @expectedException \Symfony\Component\DependencyInjection\Exception\EnvParameterException
*/
public function testDynamicServiceName()
public function testDynamicPublicServiceName()
{
$container = new ContainerBuilder();
$env = $container->getParameterBag()->get('env(BAR)');
$container->register("foo.$env", 'class');
$container->register("foo.$env", 'class')->setPublic(true);

$this->process($container);
}

/**
* @expectedException \Symfony\Component\DependencyInjection\Exception\EnvParameterException
*/
public function testDynamicAliasName()
public function testDynamicPublicAliasName()
{
$container = new ContainerBuilder();
$env = $container->getParameterBag()->get('env(BAR)');
$container->setAlias("foo.$env", 'class');
$container->setAlias("foo.$env", new Alias('class', true));

$this->process($container);
}

public function testDynamicPrivateName()
{
$container = new ContainerBuilder();
$env = $container->getParameterBag()->get('env(BAR)');
$container->register("foo.$env", 'class')->setPublic(false);
$container->setAlias("bar.$env", new Alias('class', false));

$this->process($container);

$this->addToAssertionCount(1);
}

protected function process(ContainerBuilder $container)
{
$pass = new CheckDefinitionValidityPass();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -665,6 +665,22 @@ public function testCompileWithResolveMissingEnv()
$container->compile(true);
}

public function testEnvInId()
{
$container = include __DIR__.'/Fixtures/containers/container_env_in_id.php';
$container->compile(true);

$expected = array(
'service_container',
'foo',
'bar',
'bar_%env(BAR)%',
);
$this->assertSame($expected, array_keys($container->getDefinitions()));

$this->assertSame(array('baz_bar'), array_keys($container->getDefinition('foo')->getArgument(1)));
}

/**
* @expectedException \LogicException
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -327,6 +327,15 @@ public function testDumpAutowireData()
$this->assertStringEqualsFile(self::$fixturesPath.'/php/services24.php', $dumper->dump());
}

public function testEnvInId()
{
$container = include self::$fixturesPath.'/containers/container_env_in_id.php';
$container->compile();
$dumper = new PhpDumper($container);

$this->assertStringEqualsFile(self::$fixturesPath.'/php/services_env_in_id.php', $dumper->dump());
}

public function testEnvParameter()
{
$container = new ContainerBuilder();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Definition;
use Symfony\Component\DependencyInjection\Dumper\PhpDumper;
use Symfony\Component\DependencyInjection\Reference;

$container = new ContainerBuilder();

$container->setParameter('env(BAR)', 'bar');

$container->register('foo', 'stdClass')->setPublic(true)
->addArgument(new Reference('bar_%env(BAR)%'))
->addArgument(array('baz_%env(BAR)%' => new Reference('baz_%env(BAR)%')));

$container->register('bar', 'stdClass')->setPublic(true)
->addArgument(new Reference('bar_%env(BAR)%'));

$container->register('bar_%env(BAR)%', 'stdClass')->setPublic(false);
$container->register('baz_%env(BAR)%', 'stdClass')->setPublic(false);

return $container;
Loading
Morty Proxy This is a proxified and sanitized view of the page, visit original site.