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

Skip to content

Commit b6ade35

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

File tree

4 files changed

+155
-6
lines changed

4 files changed

+155
-6
lines changed

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

Lines changed: 46 additions & 3 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 $targetDirRegex;
55+
private $targetDirMaxMatches;
5456

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

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

105126
if ($this->container->isFrozen()) {
@@ -979,7 +1000,7 @@ private function exportParameters($parameters, $path = '', $indent = 12)
9791000
} elseif ($value instanceof Reference) {
9801001
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));
9811002
} else {
982-
$value = var_export($value, true);
1003+
$value = $this->export($value);
9831004
}
9841005

9851006
$php[] = sprintf('%s%s => %s,', str_repeat(' ', $indent), var_export($key, true), $value);
@@ -1214,14 +1235,14 @@ private function dumpValue($value, $interpolate = true)
12141235
return "'.".$that->dumpParameter(strtolower($match[2])).".'";
12151236
};
12161237

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

12191240
return $code;
12201241
}
12211242
} elseif (is_object($value) || is_resource($value)) {
12221243
throw new RuntimeException('Unable to dump a service container if a parameter is an object or a resource.');
12231244
} else {
1224-
return var_export($value, true);
1245+
return $this->export($value);
12251246
}
12261247
}
12271248

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

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

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,18 @@ public function testDumpOptimizationString()
8080
$this->assertStringEqualsFile(self::$fixturesPath.'/p F438 hp/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
*/
@@ -101,13 +113,13 @@ public function testAddService()
101113
// without compilation
102114
$container = include self::$fixturesPath.'/containers/container9.php';
103115
$dumper = new PhpDumper($container);
104-
$this->assertEquals(str_replace('%path%', str_replace('\\','\\\\',self::$fixturesPath.DIRECTORY_SEPARATOR.'includes'.DIRECTORY_SEPARATOR), file_get_contents(self::$fixturesPath.'/php/services9.php')), $dumper->dump(), '->dump() dumps services');
116+
$this->assertEquals(str_replace('%path%', str_replace('\\', '\\\\', self::$fixturesPath.DIRECTORY_SEPARATOR.'includes'.DIRECTORY_SEPARATOR), file_get_contents(self::$fixturesPath.'/php/services9.php')), $dumper->dump(), '->dump() dumps services');
105117

106118
// with compilation
107119
$container = include self::$fixturesPath.'/containers/container9.php';
108120
$container->compile();
109121
$dumper = new PhpDumper($container);
110-
$this->assertEquals(str_replace('%path%', str_replace('\\','\\\\',self::$fixturesPath.DIRECTORY_SEPARATOR.'includes'.DIRECTORY_SEPARATOR), file_get_contents(self::$fixturesPath.'/php/services9_compiled.php')), $dumper->dump(), '->dump() dumps services');
122+
$this->assertEquals(str_replace('%path%', str_replace('\\', '\\\\', self::$fixturesPath.DIRECTORY_SEPARATOR.'includes'.DIRECTORY_SEPARATOR), file_get_contents(self::$fixturesPath.'/php/services9_compiled.php')), $dumper->dump(), '->dump() dumps services');
111123

112124
$dumper = new PhpDumper($container = new ContainerBuilder());
113125
$container->register('foo', 'FooClass')->addArgument(new \stdClass());
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' => (string) $cache));
714714
if (!$this->debug) {
715715
$content = static::stripComments($content);
716716
}

0 commit comments

Comments
 (0)
0