8000 [HttpKernel] make paths relative to __DIR__ in the generated container · symfony/symfony@e53b716 · GitHub
[go: up one dir, main page]

Skip to content

Commit e53b716

Browse files
[HttpKernel] make paths relative to __DIR__ in the generated container
1 parent 6945a2a commit e53b716

File tree

4 files changed

+156
-7
lines changed

4 files changed

+156
-7
lines changed

src/Symfony/Component/DependencyInjection/Dumper/PhpDumper.php

Lines changed: 49 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,8 @@ class PhpDumper extends Dumper
5151
private $referenceVariables;
5252
private $variableCount;
5353
private $reservedVariables = array('instance', 'class');
54+
private $targetDirRx;
55+
private $targetDirMaxMatches;
5456

5557
/**
5658
* @var \Symfony\Component\DependencyInjection\LazyProxy\PhpDumper\DumperInterface
@@ -95,11 +97,32 @@ public function setProxyDumper(ProxyDumper $proxyDumper)
9597
*/
9698
public function dump(array $options = array())
9799
{
100+
$this->targetDirRx = null;
101+
$this->targetDirMaxMatches = 0;
102+
98103
$options = array_merge(array(
99104
'class' => 'ProjectServiceContainer',
100105
'base_class' => 'Container',
101106
), $options);
102107

108+
if (isset($options['file'][0]) && '/' === $options['file'][0]) {
109+
if (is_dir($dir = dirname($options['file']))) {
110+
$dir = explode('/', realpath($dir));
111+
$i = count($dir);
112+
if (3 <= $i) {
113+
$rx = '';
114+
while (2 < --$i) {
115+
$rx = sprintf('(/%s%s)?', preg_quote($dir[$i], '#'), $rx);
116+
++$this->targetDirMaxMatches;
117+
}
118+
do {
119+
$rx = preg_quote('/'.$dir[$i], '#').$rx;
120+
} while (0 < --$i);
121+
$this->targetDirRx = '#'.preg_quote($dir[0], '#').$rx.'#';
122+
}
123+
}
124+
}
125+
103126
$code = $this->startClass($options['class'], $options['base_class']);
104127

105128
if ($this->container->isFrozen()) {
@@ -841,7 +864,7 @@ private function addMethodMap()
841864
$code = " \$this->methodMap = array(\n";
842865
ksort($definitions);
843866
foreach ($definitions as $id => $definition) {
844-
$code .= ' '.var_export($id, true).' => '.var_export('get'.$this->camelize($id).'Service', true).",\n";
867+
$code .= ' '.$this->export($id).' => '.$this->export('get'.$this->camelize($id).'Service').",\n";
845868
}
846869

847870
return $code." );\n";
@@ -869,7 +892,7 @@ private function addAliases()
869892
while (isset($aliases[$id])) {
870893
$id = (string) $aliases[$id];
871894
}
872-
$code .= ' '.var_export($alias, true).' => '.var_export($id, true).",\n";
895+
$code .= ' '.$this->export($alias).' => '.$this->export($id).",\n";
873896
}
874897

875898
return $code." );\n";
@@ -979,10 +1002,10 @@ private function exportParameters($parameters, $path = '', $indent = 12)
9791002
} elseif ($value instanceof Reference) {
9801003
throw new InvalidArgumentException(sprintf('You cannot dump a container with parameters that contain references to other services (reference to service "%s" found in "%s").', $value, $path.'/'.$key));
9811004
} else {
982-
$value = var_export($value, true);
1005+
$value = $this->export($value);
9831006
}
9841007

985-
$php[ A935 ] = sprintf('%s%s => %s,', str_repeat(' ', $indent), var_export($key, true), $value);
1008+
$php[] = sprintf('%s%s => %s,', str_repeat(' ', $indent), $this->export($key), $value);
9861009
}
9871010

9881011
return sprintf("array(\n%s\n%s)", implode("\n", $php), str_repeat(' ', $indent - 4));
@@ -1214,14 +1237,14 @@ private function dumpValue($value, $interpolate = true)
12141237
return "'.".$that->dumpParameter(strtolower($match[2])).".'";
12151238
};
12161239

1217-
$code = str_replace('%%', '%', preg_replace_callback('/(?<!%)(%)([^%]+)\1/', $replaceParameters, var_export($value, true)));
1240+
$code = str_replace('%%', '%', preg_replace_callback('/(?<!%)(%)([^%]+)\1/', $replaceParameters, $this->export($value)));
12181241

12191242
return $code;
12201243
}
12211244
} elseif (is_object($value) || is_resource($value)) {
12221245
throw new RuntimeException('Unable to dump a service container if a parameter is an object or a resource.');
12231246
} else {
1224-
return var_export($value, true);
1247+
return $this->export($value);
12251248
}
12261249
}
12271250

@@ -1323,4 +1346,24 @@ private function getNextVariableName()
13231346
return $name;
13241347
}
13251348
}
1349+
1350+
private function export($value)
1351+
{
1352+
if (null !== $this->targetDirRx && is_string($value) && preg_match($this->targetDirRx, $value, $matches, PREG_OFFSET_CAPTURE)) {
1353+
$prefix = $matches[0][1] ? var_export(substr($value, 0, $matches[0][1]), true).'.' : '';
1354+
$suffix = $matches[0][1] + strlen($matches[0][0]);
1355+
$suffix = isset($value[$suffix]) ? '.'.var_export(substr($value, $suffix), true) : '';
1356+
$dirname = '__DIR__';
1357+
for ($i = $this->targetDirMaxMatches - count($matches); 0 <= $i; --$i) {
1358+
$dirname = sprintf('dirname(%s)', $dirname);
1359+
}
1360+
if ($prefix || $suffix) {
1361+
return sprintf('(%s%s%s)', $prefix, $dirname, $suffix);
1362+
} else {
1363+
return $dirname;
1364+
}
1365+
}
1366+
1367+
return var_export($value, true);
1368+
}
13261369
}

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,18 @@ public function testDumpOptimizationString()
8080
$this->assertStringEqualsFile(self::$fixturesPath.'/php/services10.php', $dumper->dump(), '->dump() dumps an empty container as an empty PHP class');
8181
}
8282

83+
public function testDumpRelativeDir()
84+
{
85+
$container = new ContainerBuilder();
86+
$container->setParameter('foo', __FILE__);
87+
$container->setParameter('bar', dirname(__FILE__));
88+
$container->setParameter('baz', 'wiz'.dirname(dirname(__FILE__)));
89+
$container->compile();
90+
91+
$dumper = new PhpDumper($container);
92+
$this->assertStringEqualsFile(self::$fixturesPath.'/php/services12.php', $dumper->dump(array('file' => __FILE__)), '->dump() dumps __DIR__ relative strings');
93+
}
94+
8395
/**
8496
* @expectedException \InvalidArgumentException
8597
*/
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
<?php
2+
3+
use Symfony\Component\DependencyInjection\ContainerInterface;
4+
use Symfony\Component\DependencyInjection\Container;
5+
use Symfony\Component\DependencyInjection\Exception\InactiveScopeException;
6+
use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException;
7+
use Symfony\Component\DependencyInjection\Exception\LogicException;
8+
use Symfony\Component\DependencyInjection\Exception\RuntimeException;
9+
use Symfony\Component\DependencyInjection\ParameterBag\FrozenParameterBag;
10+
11+
/**
12+
* ProjectServiceContainer
13+
*
14+
* This class has been auto-generated
15+
* by the Symfony Dependency Injection Component.
16+
*/
17+
class ProjectServiceContainer extends Container
18+
{
19+
/**
20+
* Constructor.
21+
*/
22+
public function __construct()
23+
{
24+
$this->parameters = $this->getDefaultParameters();
25+
26+
$this->services =
27+
$this->scopedServices =
28+
$this->scopeStacks = array();
29+
30+
$this->set('service_container', $this);
31+
32+
$this->scopes = array();
33+
$this->scopeChildren = array();
34+
35+
$this->aliases = array();
36+
}
37+
38+
/**
39+
* {@inheritdoc}
40+
*/
41+
public function getParameter($name)
42+
{
43+
$name = strtolower($name);
44+
45+
if (!(isset($this->parameters[$name]) || array_key_exists($name, $this->parameters))) {
46+
throw new InvalidArgumentException(sprintf('The parameter "%s" must be defined.', $name));
47+
}
48+
49+
return $this->parameters[$name];
50+
}
51+
52+
/**
53+
* {@inheritdoc}
54+
*/
55+
public function hasParameter($name)
56+
{
57+
$name = strtolower($name);
58+
59+
return isset($this->parameters[$name]) || array_key_exists($name, $this->parameters);
60+
}
61+
62+
/**
63+
* {@inheritdoc}
64+
*/
65+
public function setParameter($name, $value)
66+
{
67+
throw new LogicException('Impossible to call set() on a frozen ParameterBag.');
68+
}
69+
70+
/**
71+
* {@inheritdoc}
72+
*/
73+
public function getParameterBag()
74+
{
75+
if (null === $this->parameterBag) {
76+
$this->parameterBag = new FrozenParameterBag($this->parameters);
77+
}
78+
79+
return $this->parameterBag;
80+
}
81+
/**
82+
* Gets the default parameters.
83+
*
84+
* @return array An array of the default parameters
85+
*/
86+
protected function getDefaultParameters()
87+
{
88+
return array(
89+
'foo' => (__DIR__.'/PhpDumperTest.php'),
90+
'bar' => __DIR__,
91+
'baz' => ('wiz'.dirname(__DIR__)),
92+
);
93+
}
94+
}

src/Symfony/Component/HttpKernel/Kernel.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -710,7 +710,7 @@ protected function dumpContainer(ConfigCache $cache, ContainerBuilder $container
710710
$dumper->setProxyDumper(new ProxyDumper());
711711
}
712712

713-
$content = $dumper->dump(array('class' => $class, 'base_class' => $baseClass));
713+
$content = $dumper->dump(array('class' => $class, 'base_class' => $baseClass, 'file' => $cache->__toString()));
714714
if (!$this->debug) {
715715
$content = static::stripComments($content);
716716
}

0 commit comments

Comments
 (0)
0