Skip to content

Commit

Permalink
bug #26662 [DI] Fix hardcoded cache dir for warmups (nicolas-grekas)
Browse files Browse the repository at this point in the history
This PR was merged into the 3.4 branch.

Discussion
----------

[DI] Fix hardcoded cache dir for warmups

| Q             | A
| ------------- | ---
| Branch?       | 3.4
| Bug fix?      | yes
| New feature?  | no
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | #26506, #26219
| License       | MIT
| Doc PR        | -

Commits
-------

599dbca [DI] Fix hardcoded cache dir for warmups
  • Loading branch information
fabpot committed Mar 28, 2018
2 parents fad6c9c + 599dbca commit 02e59d4
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 48 deletions.
23 changes: 12 additions & 11 deletions src/Symfony/Component/DependencyInjection/Dumper/PhpDumper.php
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,7 @@ public function dump(array $options = array())
'container.build_hash' => '$hash',
'container.build_id' => '$id',
'container.build_time' => $time,
));
), __DIR__.\\DIRECTORY_SEPARATOR.'Container{$hash}');
EOF;
} else {
Expand Down Expand Up @@ -789,7 +789,7 @@ protected function {$methodName}($lazyInitialization)
}

if ($this->getProxyDumper()->isProxyCandidate($definition)) {
$factoryCode = $asFile ? "\$this->load(__DIR__.'/%s.php', false)" : '$this->%s(false)';
$factoryCode = $asFile ? "\$this->load('%s.php', false)" : '$this->%s(false)';
$code .= $this->getProxyDumper()->getProxyFactoryCode($definition, $id, sprintf($factoryCode, $methodName));
}

Expand Down Expand Up @@ -963,7 +963,7 @@ public function __construct()
EOF;
if (null !== $this->targetDirRegex) {
$dir = $this->asFiles ? '$this->targetDirs[0] = \\dirname(__DIR__)' : '__DIR__';
$dir = $this->asFiles ? '$this->targetDirs[0] = \\dirname($containerDir)' : '__DIR__';
$code .= <<<EOF
\$dir = {$dir};
for (\$i = 1; \$i <= {$this->targetDirMaxMatches}; ++\$i) {
Expand All @@ -973,9 +973,10 @@ public function __construct()
EOF;
}
if ($this->asFiles) {
$code = str_replace('$parameters', "\$buildParameters;\n private \$parameters", $code);
$code = str_replace('__construct()', '__construct(array $buildParameters = array())', $code);
$code = str_replace('$parameters', "\$buildParameters;\n private \$containerDir;\n private \$parameters", $code);
$code = str_replace('__construct()', '__construct(array $buildParameters = array(), $containerDir = __DIR__)', $code);
$code .= " \$this->buildParameters = \$buildParameters;\n";
$code .= " \$this->containerDir = \$containerDir;\n";
}

if ($this->container->isCompiled()) {
Expand Down Expand Up @@ -1042,7 +1043,7 @@ public function isFrozen()
protected function load(\$file, \$lazyLoad = true)
{
return require \$file;
return require \$this->containerDir.\\DIRECTORY_SEPARATOR.\$file;
}
EOF;
Expand All @@ -1054,7 +1055,7 @@ protected function load(\$file, \$lazyLoad = true)
continue;
}
if ($this->asFiles) {
$proxyLoader = '$this->load(__DIR__."/{$class}.php")';
$proxyLoader = '$this->load("{$class}.php")';
} elseif ($this->namespace) {
$proxyLoader = 'class_alias("'.$this->namespace.'\\\\{$class}", $class, false)';
} else {
Expand Down Expand Up @@ -1126,7 +1127,7 @@ private function addRemovedIds()
return '';
}
if ($this->asFiles) {
$code = "require __DIR__.'/removed-ids.php'";
$code = "require \$this->containerDir.\\DIRECTORY_SEPARATOR.'removed-ids.php'";
} else {
$code = '';
$ids = array_keys($ids);
Expand Down Expand Up @@ -1179,7 +1180,7 @@ private function addFileMap()
ksort($definitions);
foreach ($definitions as $id => $definition) {
if (!$definition->isSynthetic() && $definition->isShared() && !$this->isHotPath($definition)) {
$code .= sprintf(" %s => __DIR__.'/%s.php',\n", $this->doExport($id), $this->generateMethodName($id));
$code .= sprintf(" %s => '%s.php',\n", $this->doExport($id), $this->generateMethodName($id));
}
}

Expand Down Expand Up @@ -1901,7 +1902,7 @@ private function getServiceCall($id, Reference $reference = null)
$code = sprintf('$this->services[\'%s\'] = %s', $id, $code);
}
} elseif ($this->asFiles && $definition->isShared() && !$this->isHotPath($definition)) {
$code = sprintf("\$this->load(__DIR__.'/%s.php')", $this->generateMethodName($id));
$code = sprintf("\$this->load('%s.php')", $this->generateMethodName($id));
} else {
$code = sprintf('$this->%s()', $this->generateMethodName($id));
}
Expand Down Expand Up @@ -2043,7 +2044,7 @@ private function export($value)
$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), true) : '';
$dirname = '__DIR__';
$dirname = $this->asFiles ? '$this->containerDir' : '__DIR__';
$offset = 1 + $this->targetDirMaxMatches - count($matches);

if ($this->asFiles || 0 < $offset) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ use Symfony\Component\DependencyInjection\Argument\RewindableGenerator;

$this->services['baz'] = $instance = new \Baz();

$instance->setFoo(${($_ = isset($this->services['foo_with_inline']) ? $this->services['foo_with_inline'] : $this->load(__DIR__.'/getFooWithInlineService.php')) && false ?: '_'});
$instance->setFoo(${($_ = isset($this->services['foo_with_inline']) ? $this->services['foo_with_inline'] : $this->load('getFooWithInlineService.php')) && false ?: '_'});

return $instance;

Expand All @@ -38,7 +38,7 @@ use Symfony\Component\DependencyInjection\Argument\RewindableGenerator;
$this->services['configured_service'] = $instance = new \stdClass();

$a = new \ConfClass();
$a->setFoo(${($_ = isset($this->services['baz']) ? $this->services['baz'] : $this->load(__DIR__.'/getBazService.php')) && false ?: '_'});
$a->setFoo(${($_ = isset($this->services['baz']) ? $this->services['baz'] : $this->load('getBazService.php')) && false ?: '_'});

$a->configureStdClass($instance);

Expand Down Expand Up @@ -93,7 +93,7 @@ use Symfony\Component\DependencyInjection\Argument\RewindableGenerator;
// This file has been auto-generated by the Symfony Dependency Injection Component for internal use.
// Returns the public 'factory_service' shared service.

return $this->services['factory_service'] = ${($_ = isset($this->services['foo.baz']) ? $this->services['foo.baz'] : $this->load(__DIR__.'/getFoo_BazService.php')) && false ?: '_'}->getInstance();
return $this->services['factory_service'] = ${($_ = isset($this->services['foo.baz']) ? $this->services['foo.baz'] : $this->load('getFoo_BazService.php')) && false ?: '_'}->getInstance();

[Container%s/getFactoryServiceSimpleService.php] => <?php

Expand All @@ -102,7 +102,7 @@ use Symfony\Component\DependencyInjection\Argument\RewindableGenerator;
// This file has been auto-generated by the Symfony Dependency Injection Component for internal use.
// Returns the public 'factory_service_simple' shared service.

return $this->services['factory_service_simple'] = ${($_ = isset($this->services['factory_simple']) ? $this->services['factory_simple'] : $this->load(__DIR__.'/getFactorySimpleService.php')) && false ?: '_'}->getInstance();
return $this->services['factory_service_simple'] = ${($_ = isset($this->services['factory_simple']) ? $this->services['factory_simple'] : $this->load('getFactorySimpleService.php')) && false ?: '_'}->getInstance();

[Container%s/getFactorySimpleService.php] => <?php

Expand All @@ -122,7 +122,7 @@ use Symfony\Component\DependencyInjection\Argument\RewindableGenerator;
// This file has been auto-generated by the Symfony Dependency Injection Component for internal use.
// Returns the public 'foo' shared service.

$a = ${($_ = isset($this->services['foo.baz']) ? $this->services['foo.baz'] : $this->load(__DIR__.'/getFoo_BazService.php')) && false ?: '_'};
$a = ${($_ = isset($this->services['foo.baz']) ? $this->services['foo.baz'] : $this->load('getFoo_BazService.php')) && false ?: '_'};

$this->services['foo'] = $instance = \Bar\FooClass::getInstance('foo', $a, array('bar' => 'foo is bar', 'foobar' => 'bar'), true, $this);

Expand Down Expand Up @@ -160,7 +160,7 @@ $this->services['foo_with_inline'] = $instance = new \Foo();
$a = new \Bar();

$a->pub = 'pub';
$a->setBaz(${($_ = isset($this->services['baz']) ? $this->services['baz'] : $this->load(__DIR__.'/getBazService.php')) && false ?: '_'});
$a->setBaz(${($_ = isset($this->services['baz']) ? $this->services['baz'] : $this->load('getBazService.php')) && false ?: '_'});

$instance->setBar($a);

Expand All @@ -174,7 +174,7 @@ use Symfony\Component\DependencyInjection\Argument\RewindableGenerator;
// Returns the public 'lazy_context' shared service.

return $this->services['lazy_context'] = new \LazyContext(new RewindableGenerator(function () {
yield 'k1' => ${($_ = isset($this->services['foo.baz']) ? $this->services['foo.baz'] : $this->load(__DIR__.'/getFoo_BazService.php')) && false ?: '_'};
yield 'k1' => ${($_ = isset($this->services['foo.baz']) ? $this->services['foo.baz'] : $this->load('getFoo_BazService.php')) && false ?: '_'};
yield 'k2' => $this;
}, 2), new RewindableGenerator(function () {
return new \EmptyIterator();
Expand All @@ -188,7 +188,7 @@ use Symfony\Component\DependencyInjection\Argument\RewindableGenerator;
// Returns the public 'lazy_context_ignore_invalid_ref' shared service.

return $this->services['lazy_context_ignore_invalid_ref'] = new \LazyContext(new RewindableGenerator(function () {
yield 0 => ${($_ = isset($this->services['foo.baz']) ? $this->services['foo.baz'] : $this->load(__DIR__.'/getFoo_BazService.php')) && false ?: '_'};
yield 0 => ${($_ = isset($this->services['foo.baz']) ? $this->services['foo.baz'] : $this->load('getFoo_BazService.php')) && false ?: '_'};
}, 1), new RewindableGenerator(function () {
return new \EmptyIterator();
}, 0));
Expand All @@ -204,9 +204,9 @@ include_once ($this->targetDirs[0].'/Fixtures/includes/foo.php');

$this->services['method_call1'] = $instance = new \Bar\FooClass();

$instance->setBar(${($_ = isset($this->services['foo']) ? $this->services['foo'] : $this->load(__DIR__.'/getFooService.php')) && false ?: '_'});
$instance->setBar(${($_ = isset($this->services['foo']) ? $this->services['foo'] : $this->load('getFooService.php')) && false ?: '_'});
$instance->setBar(NULL);
$instance->setBar((${($_ = isset($this->services['foo']) ? $this->services['foo'] : $this->load(__DIR__.'/getFooService.php')) && false ?: '_'}->foo() . (($this->hasParameter("foo")) ? ($this->getParameter("foo")) : ("default"))));
$instance->setBar((${($_ = isset($this->services['foo']) ? $this->services['foo'] : $this->load('getFooService.php')) && false ?: '_'}->foo() . (($this->hasParameter("foo")) ? ($this->getParameter("foo")) : ("default"))));

return $instance;

Expand Down Expand Up @@ -243,7 +243,7 @@ use Symfony\Component\DependencyInjection\Argument\RewindableGenerator;
// Returns the public 'tagged_iterator' shared service.

return $this->services['tagged_iterator'] = new \Bar(new RewindableGenerator(function () {
yield 0 => ${($_ = isset($this->services['foo']) ? $this->services['foo'] : $this->load(__DIR__.'/getFooService.php')) && false ?: '_'};
yield 0 => ${($_ = isset($this->services['foo']) ? $this->services['foo'] : $this->load('getFooService.php')) && false ?: '_'};
yield 1 => ${($_ = isset($this->services['tagged_iterator_foo']) ? $this->services['tagged_iterator_foo'] : $this->services['tagged_iterator_foo'] = new \Bar()) && false ?: '_'};
}, 2));

Expand Down Expand Up @@ -277,16 +277,18 @@ use Symfony\Component\DependencyInjection\ParameterBag\FrozenParameterBag;
class ProjectServiceContainer extends Container
{
private $buildParameters;
private $containerDir;
private $parameters;
private $targetDirs = array();

public function __construct(array $buildParameters = array())
public function __construct(array $buildParameters = array(), $containerDir = __DIR__)
{
$dir = $this->targetDirs[0] = \dirname(__DIR__);
$dir = $this->targetDirs[0] = \dirname($containerDir);
for ($i = 1; $i <= 5; ++$i) {
$this->targetDirs[$i] = $dir = \dirname($dir);
}
$this->buildParameters = $buildParameters;
$this->containerDir = $containerDir;
$this->parameters = $this->getDefaultParameters();

$this->services = array();
Expand All @@ -298,25 +300,25 @@ class ProjectServiceContainer extends Container
'foo_bar' => 'getFooBarService',
);
$this->fileMap = array(
'baz' => __DIR__.'/getBazService.php',
'configured_service' => __DIR__.'/getConfiguredServiceService.php',
'configured_service_simple' => __DIR__.'/getConfiguredServiceSimpleService.php',
'decorator_service' => __DIR__.'/getDecoratorServiceService.php',
'decorator_service_with_name' => __DIR__.'/getDecoratorServiceWithNameService.php',
'deprecated_service' => __DIR__.'/getDeprecatedServiceService.php',
'factory_service' => __DIR__.'/getFactoryServiceService.php',
'factory_service_simple' => __DIR__.'/getFactoryServiceSimpleService.php',
'factory_simple' => __DIR__.'/getFactorySimpleService.php',
'foo' => __DIR__.'/getFooService.php',
'foo.baz' => __DIR__.'/getFoo_BazService.php',
'foo_with_inline' => __DIR__.'/getFooWithInlineService.php',
'lazy_context' => __DIR__.'/getLazyContextService.php',
'lazy_context_ignore_invalid_ref' => __DIR__.'/getLazyContextIgnoreInvalidRefService.php',
'method_call1' => __DIR__.'/getMethodCall1Service.php',
'new_factory_service' => __DIR__.'/getNewFactoryServiceService.php',
'service_from_static_method' => __DIR__.'/getServiceFromStaticMethodService.php',
'tagged_iterator' => __DIR__.'/getTaggedIteratorService.php',
'tagged_iterator_foo' => __DIR__.'/getTaggedIteratorFooService.php',
'baz' => 'getBazService.php',
'configured_service' => 'getConfiguredServiceService.php',
'configured_service_simple' => 'getConfiguredServiceSimpleService.php',
'decorator_service' => 'getDecoratorServiceService.php',
'decorator_service_with_name' => 'getDecoratorServiceWithNameService.php',
'deprecated_service' => 'getDeprecatedServiceService.php',
'factory_service' => 'getFactoryServiceService.php',
'factory_service_simple' => 'getFactoryServiceSimpleService.php',
'factory_simple' => 'getFactorySimpleService.php',
'foo' => 'getFooService.php',
'foo.baz' => 'getFoo_BazService.php',
'foo_with_inline' => 'getFooWithInlineService.php',
'lazy_context' => 'getLazyContextService.php',
'lazy_context_ignore_invalid_ref' => 'getLazyContextIgnoreInvalidRefService.php',
'method_call1' => 'getMethodCall1Service.php',
'new_factory_service' => 'getNewFactoryServiceService.php',
'service_from_static_method' => 'getServiceFromStaticMethodService.php',
'tagged_iterator' => 'getTaggedIteratorService.php',
'tagged_iterator_foo' => 'getTaggedIteratorFooService.php',
);
$this->privates = array(
'factory_simple' => true,
Expand All @@ -331,7 +333,7 @@ class ProjectServiceContainer extends Container

public function getRemovedIds()
{
return require __DIR__.'/removed-ids.php';
return require $this->containerDir.\DIRECTORY_SEPARATOR.'removed-ids.php';
}

public function compile()
Expand All @@ -353,7 +355,7 @@ class ProjectServiceContainer extends Container

protected function load($file, $lazyLoad = true)
{
return require $file;
return require $this->containerDir.\DIRECTORY_SEPARATOR.$file;
}

/**
Expand All @@ -363,7 +365,7 @@ class ProjectServiceContainer extends Container
*/
protected function getBarService()
{
$a = ${($_ = isset($this->services['foo.baz']) ? $this->services['foo.baz'] : $this->load(__DIR__.'/getFoo_BazService.php')) && false ?: '_'};
$a = ${($_ = isset($this->services['foo.baz']) ? $this->services['foo.baz'] : $this->load('getFoo_BazService.php')) && false ?: '_'};

$this->services['bar'] = $instance = new \Bar\FooClass('foo', $a, $this->getParameter('foo_bar'));

Expand All @@ -379,7 +381,7 @@ class ProjectServiceContainer extends Container
*/
protected function getFooBarService()
{
return new \Bar\FooClass(${($_ = isset($this->services['deprecated_service']) ? $this->services['deprecated_service'] : $this->load(__DIR__.'/getDeprecatedServiceService.php')) && false ?: '_'});
return new \Bar\FooClass(${($_ = isset($this->services['deprecated_service']) ? $this->services['deprecated_service'] : $this->load('getDeprecatedServiceService.php')) && false ?: '_'});
}

public function getParameter($name)
Expand Down Expand Up @@ -502,6 +504,6 @@ return new \Container%s\ProjectServiceContainer(array(
'container.build_hash' => '%s',
'container.build_id' => '%s',
'container.build_time' => %d,
));
), __DIR__.\DIRECTORY_SEPARATOR.'Container%s');

)

0 comments on commit 02e59d4

Please sign in to comment.