diff --git a/src/Symfony/Component/DependencyInjection/Dumper/PhpDumper.php b/src/Symfony/Component/DependencyInjection/Dumper/PhpDumper.php index 5e9208e5cf..f3e5fdb3cf 100644 --- a/src/Symfony/Component/DependencyInjection/Dumper/PhpDumper.php +++ b/src/Symfony/Component/DependencyInjection/Dumper/PhpDumper.php @@ -104,17 +104,19 @@ class PhpDumper extends Dumper ), $options); if (!empty($options['file']) && is_dir($dir = dirname($options['file']))) { - // Build a regexp where the first two root dirs are mandatory, + // Build a regexp where the first root dirs are mandatory, // but every other sub-dir is optional up to the full path in $dir + // Mandate at least 2 root dirs and not more that 5 optional dirs. $dir = explode(DIRECTORY_SEPARATOR, realpath($dir)); $i = count($dir); if (3 <= $i) { $regex = ''; - $this->targetDirMaxMatches = $i - 3; + $lastOptionalDir = $i > 8 ? $i - 5 : 3; + $this->targetDirMaxMatches = $i - $lastOptionalDir; - while (2 < --$i) { + while (--$i >= $lastOptionalDir) { $regex = sprintf('(%s%s)?', preg_quote(DIRECTORY_SEPARATOR.$dir[$i], '#'), $regex); } @@ -764,6 +766,9 @@ $bagClass */ class $class extends $baseClass { + private \$parameters; + private \$targetDirs = array(); + EOF; } @@ -774,6 +779,7 @@ EOF; */ private function addConstructor() { + $targetDirs = $this->exportTargetDirs(); $arguments = $this->container->getParameterBag()->all() ? 'new ParameterBag($this->getDefaultParameters())' : null; $code = <<exportTargetDirs(); + $code = <<container->getParameterBag()->all()) { @@ -1351,6 +1359,17 @@ EOF; } } + private function exportTargetDirs() + { + return null === $this->targetDirRegex ? '' : <<targetDirMaxMatches}; ++\$i) { + \$this->targetDirs[\$i] = \$dir = dirname(\$dir); + } +EOF; + } + private function export($value) { if (null !== $this->targetDirRegex && is_string($value) && preg_match($this->targetDirRegex, $value, $matches, PREG_OFFSET_CAPTURE)) { @@ -1359,8 +1378,8 @@ EOF; $suffix = isset($value[$suffix]) ? '.'.var_export(substr($value, $suffix), true) : ''; $dirname = '__DIR__'; - for ($i = $this->targetDirMaxMatches - count($matches); 0 <= $i; --$i) { - $dirname = sprintf('dirname(%s)', $dirname); + if (0 < $offset = 1 + $this->targetDirMaxMatches - count($matches)) { + $dirname = sprintf('$this->targetDirs[%d]', $offset); } if ($prefix || $suffix) { diff --git a/src/Symfony/Component/DependencyInjection/Tests/Dumper/PhpDumperTest.php b/src/Symfony/Component/DependencyInjection/Tests/Dumper/PhpDumperTest.php index cd3ad9b727..905e600e78 100644 --- a/src/Symfony/Component/DependencyInjection/Tests/Dumper/PhpDumperTest.php +++ b/src/Symfony/Component/DependencyInjection/Tests/Dumper/PhpDumperTest.php @@ -85,13 +85,14 @@ class PhpDumperTest extends \PHPUnit_Framework_TestCase $definition = new Definition(); $definition->setClass('stdClass'); $definition->addArgument('%foo%'); - $definition->addArgument(array('%foo%' => '%foo%')); + $definition->addArgument(array('%foo%' => '%buz%/')); $container = new ContainerBuilder(); $container->setDefinition('test', $definition); $container->setParameter('foo', 'wiz'.dirname(dirname(__FILE__))); $container->setParameter('bar', dirname(__FILE__)); $container->setParameter('baz', '%bar%/PhpDumperTest.php'); + $container->setParameter('buz', dirname(dirname(__DIR__))); $container->compile(); $dumper = new PhpDumper($container); diff --git a/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services1-1.php b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services1-1.php index 547941eb95..e83498f2c5 100644 --- a/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services1-1.php +++ b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services1-1.php @@ -16,6 +16,9 @@ use Symfony\Component\DependencyInjection\ParameterBag\ParameterBag; */ class Container extends AbstractContainer { + private $parameters; + private $targetDirs = array(); + /** * Constructor. */ diff --git a/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services1.php b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services1.php index 084e7891af..90b59ff4eb 100644 --- a/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services1.php +++ b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services1.php @@ -16,6 +16,9 @@ use Symfony\Component\DependencyInjection\ParameterBag\ParameterBag; */ class ProjectServiceContainer extends Container { + private $parameters; + private $targetDirs = array(); + /** * Constructor. */ diff --git a/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services10.php b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services10.php index 37927a6e70..43a56eec51 100644 --- a/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services10.php +++ b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services10.php @@ -16,6 +16,9 @@ use Symfony\Component\DependencyInjection\ParameterBag\FrozenParameterBag; */ class ProjectServiceContainer extends Container { + private $parameters; + private $targetDirs = array(); + /** * Constructor. */ diff --git a/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services11.php b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services11.php index 0dcd83845a..23bae47920 100644 --- a/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services11.php +++ b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services11.php @@ -16,6 +16,9 @@ use Symfony\Component\DependencyInjection\ParameterBag\FrozenParameterBag; */ class ProjectServiceContainer extends Container { + private $parameters; + private $targetDirs = array(); + /** * Constructor. */ diff --git a/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services12.php b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services12.php index 52d2702afd..3081abd6b0 100644 --- a/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services12.php +++ b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services12.php @@ -16,11 +16,18 @@ use Symfony\Component\DependencyInjection\ParameterBag\FrozenParameterBag; */ class ProjectServiceContainer extends Container { + private $parameters; + private $targetDirs = array(); + /** * Constructor. */ public function __construct() { + $dir = __DIR__; + for ($i = 1; $i <= 5; ++$i) { + $this->targetDirs[$i] = $dir = dirname($dir); + } $this->parameters = $this->getDefaultParameters(); $this->services = @@ -48,7 +55,7 @@ class ProjectServiceContainer extends Container */ protected function getTestService() { - return $this->services['test'] = new \stdClass(('wiz'.dirname(__DIR__)), array(('wiz'.dirname(__DIR__)) => ('wiz'.dirname(__DIR__)))); + return $this->services['test'] = new \stdClass(('wiz'.$this->targetDirs[1]), array(('wiz'.$this->targetDirs[1]) => ($this->targetDirs[2].'/'))); } /** @@ -102,9 +109,10 @@ class ProjectServiceContainer extends Container protected function getDefaultParameters() { return array( - 'foo' => ('wiz'.dirname(__DIR__)), + 'foo' => ('wiz'.$this->targetDirs[1]), 'bar' => __DIR__, 'baz' => (__DIR__.'/PhpDumperTest.php'), + 'buz' => $this->targetDirs[2], ); } } diff --git a/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services8.php b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services8.php index f922d2000c..1b86dfc3f8 100644 --- a/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services8.php +++ b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services8.php @@ -16,6 +16,9 @@ use Symfony\Component\DependencyInjection\ParameterBag\ParameterBag; */ class ProjectServiceContainer extends Container { + private $parameters; + private $targetDirs = array(); + /** * Constructor. */ diff --git a/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services9.php b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services9.php index a4b42d78ad..ecbd626abe 100644 --- a/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services9.php +++ b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services9.php @@ -16,6 +16,9 @@ use Symfony\Component\DependencyInjection\ParameterBag\ParameterBag; */ class ProjectServiceContainer extends Container { + private $parameters; + private $targetDirs = array(); + /** * Constructor. */ diff --git a/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services9_compiled.php b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services9_compiled.php index b881b5e300..2e7e10480b 100644 --- a/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services9_compiled.php +++ b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services9_compiled.php @@ -16,6 +16,9 @@ use Symfony\Component\DependencyInjection\ParameterBag\FrozenParameterBag; */ class ProjectServiceContainer extends Container { + private $parameters; + private $targetDirs = array(); + /** * Constructor. */