Skip to content

[DependencyInjection] Perf php dumper #12855

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 3 commits into from
Dec 11, 2014
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
33 changes: 26 additions & 7 deletions src/Symfony/Component/DependencyInjection/Dumper/PhpDumper.php
Original file line number Diff line number Diff line change
Expand Up @@ -104,17 +104,19 @@ public function dump(array $options = array())
), $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.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why at least 2 root dirs ?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because we need a distinctive enough string: zero would mean matching a single DIRECTORY_SEPARATOR, which is not enough - and one looked too low also to me. Two should work for everyone - and be distinctive enough


$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);
}

Expand Down Expand Up @@ -764,6 +766,9 @@ private function startClass($class, $baseClass)
*/
class $class extends $baseClass
{
private \$parameters;
private \$targetDirs = array();

EOF;
}

Expand All @@ -774,6 +779,7 @@ class $class extends $baseClass
*/
private function addConstructor()
{
$targetDirs = $this->exportTargetDirs();
$arguments = $this->container->getParameterBag()->all() ? 'new ParameterBag($this->getDefaultParameters())' : null;

$code = <<<EOF
Expand All @@ -782,7 +788,7 @@ private function addConstructor()
* Constructor.
*/
public function __construct()
{
{{$targetDirs}
parent::__construct($arguments);

EOF;
Expand Down Expand Up @@ -811,13 +817,15 @@ public function __construct()
*/
private function addFrozenConstructor()
{
$targetDirs = $this->exportTargetDirs();

$code = <<<EOF

/**
* Constructor.
*/
public function __construct()
{
{{$targetDirs}
EOF;

if ($this->container->getParameterBag()->all()) {
Expand Down Expand Up @@ -1351,6 +1359,17 @@ private function getNextVariableName()
}
}

private function exportTargetDirs()
{
return null === $this->targetDirRegex ? '' : <<<EOF

\$dir = __DIR__;
for (\$i = 1; \$i <= {$this->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)) {
Expand All @@ -1359,8 +1378,8 @@ private function export($value)
$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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,13 +85,14 @@ public function testDumpRelativeDir()
$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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@
*/
class Container extends AbstractContainer
{
private $parameters;
private $targetDirs = array();

/**
* Constructor.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@
*/
class ProjectServiceContainer extends Container
{
private $parameters;
private $targetDirs = array();

/**
* Constructor.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@
*/
class ProjectServiceContainer extends Container
{
private $parameters;
private $targetDirs = array();

/**
* Constructor.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@
*/
class ProjectServiceContainer extends Container
{
private $parameters;
private $targetDirs = array();

/**
* Constructor.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,18 @@
*/
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 =
Expand Down Expand Up @@ -48,7 +55,7 @@ public function __construct()
*/
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].'/')));
}

/**
Expand Down Expand Up @@ -102,9 +109,10 @@ public function getParameterBag()
protected function getDefaultParameters()
{
return array(
'foo' => ('wiz'.dirname(__DIR__)),
'foo' => ('wiz'.$this->targetDirs[1]),
'bar' => __DIR__,
'baz' => (__DIR__.'/PhpDumperTest.php'),
'buz' => $this->targetDirs[2],
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@
*/
class ProjectServiceContainer extends Container
{
private $parameters;
private $targetDirs = array();

/**
* Constructor.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@
*/
class ProjectServiceContainer extends Container
{
private $parameters;
private $targetDirs = array();

/**
* Constructor.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@
*/
class ProjectServiceContainer extends Container
{
private $parameters;
private $targetDirs = array();

/**
* Constructor.
*/
Expand Down