Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 2eae300

Browse filesBrowse files
[DI] use dirname() when possible
1 parent 1c3d409 commit 2eae300
Copy full SHA for 2eae300

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.
Dismiss banner

48 files changed

+92
-148
lines changed

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

Copy file name to clipboardExpand all lines: src/Symfony/Component/DependencyInjection/Dumper/PhpDumper.php
+53-22Lines changed: 53 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111

1212
namespace Symfony\Component\DependencyInjection\Dumper;
1313

14+
use Composer\Autoload\ClassLoader;
15+
use Symfony\Component\Debug\DebugClassLoader as LegacyDebugClassLoader;
1416
use Symfony\Component\DependencyInjection\Argument\ArgumentInterface;
1517
use Symfony\Component\DependencyInjection\Argument\IteratorArgument;
1618
use Symfony\Component\DependencyInjection\Argument\ServiceClosureArgument;
@@ -36,6 +38,7 @@
3638
use Symfony\Component\DependencyInjection\ServiceLocator as BaseServiceLocator;
3739
use Symfony\Component\DependencyInjection\TypedReference;
3840
use Symfony\Component\DependencyInjection\Variable;
41+
use Symfony\Component\ErrorHandler\DebugClassLoader;
3942
use Symfony\Component\ExpressionLanguage\Expression;
4043
use Symfony\Component\HttpKernel\Kernel;
4144

@@ -296,8 +299,11 @@ public function dump(array $options = [])
296299
$namespaceLine = $this->namespace ? "\nnamespace {$this->namespace};\n" : '';
297300
$time = $options['build_time'];
298301
$id = hash('crc32', $hash.$time);
302+
$this->asFiles = false;
303+
304+
if ($preload && null !== $autoloadFile = $this->getAutoloadFile()) {
305+
$autoloadFile = substr($this->export($autoloadFile), 2, -1);
299306

300-
if ($preload) {
301307
$code[$options['class'].'.preload.php'] = <<<EOF
302308
<?php
303309
@@ -306,7 +312,7 @@ public function dump(array $options = [])
306312
307313
use Symfony\Component\DependencyInjection\Dumper\Preloader;
308314
309-
require dirname(__DIR__, 3).'/vendor/autoload.php';
315+
require $autoloadFile;
310316
require __DIR__.'/Container{$hash}/{$options['class']}.php';
311317
312318
\$classes = [];
@@ -511,7 +517,6 @@ private function generateProxyClasses(): array
511517
if ($this->inlineFactories) {
512518
$this->inlinedRequires[$file] = true;
513519
}
514-
$file = preg_replace('#^\\$this->targetDirs\[(\d++)\]#', sprintf('\dirname(__DIR__, %d + $1)', $this->asFiles), $file);
515520
$code .= sprintf("include_once %s;\n", $file);
516521
}
517522

@@ -553,7 +558,6 @@ private function addServiceInclude(string $cId, Definition $definition): string
553558
}
554559

555560
foreach (array_diff_key(array_flip($lineage), $this->inlinedRequires) as $file => $class) {
556-
$file = preg_replace('#^\\$this->targetDirs\[(\d++)\]#', sprintf('\dirname(__DIR__, %d + $1)', $this->asFiles), $file);
557561
$code .= sprintf(" include_once %s;\n", $file);
558562
}
559563
}
@@ -562,7 +566,6 @@ private function addServiceInclude(string $cId, Definition $definition): string
562566
if ($file = $def->getFile()) {
563567
$file = $this->dumpValue($file);
564568
$file = '(' === $file[0] ? substr($file, 1, -1) : $file;
565-
$file = preg_replace('#^\\$this->targetDirs\[(\d++)\]#', sprintf('\dirname(__DIR__, %d + $1)', $this->asFiles), $file);
566569
$code .= sprintf(" include_once %s;\n", $file);
567570
}
568571
}
@@ -1076,27 +1079,21 @@ private function startClass(string $class, string $baseClass, ?array &$preload):
10761079
class $class extends $baseClass
10771080
{
10781081
private \$parameters = [];
1079-
private \$targetDirs = [];
10801082
10811083
public function __construct()
10821084
{
10831085
10841086
EOF;
1085-
if (null !== $this->targetDirRegex) {
1086-
$dir = $this->asFiles ? '$this->targetDirs[0] = \\dirname($containerDir)' : '__DIR__';
1087-
$code .= <<<EOF
1088-
\$dir = {$dir};
1089-
for (\$i = 1; \$i <= {$this->targetDirMaxMatches}; ++\$i) {
1090-
\$this->targetDirs[\$i] = \$dir = \\dirname(\$dir);
1091-
}
1092-
1093-
EOF;
1094-
}
10951087
if ($this->asFiles) {
10961088
$code = str_replace('$parameters', "\$buildParameters;\n private \$containerDir;\n private \$parameters", $code);
10971089
$code = str_replace('__construct()', '__construct(array $buildParameters = [], $containerDir = __DIR__)', $code);
10981090
$code .= " \$this->buildParameters = \$buildParameters;\n";
10991091
$code .= " \$this->containerDir = \$containerDir;\n";
1092+
1093+
if (null !== $this->targetDirRegex) {
1094+
$code = str_replace('$parameters', "\$targetDir;\n private \$parameters", $code);
1095+
$code .= ' $this->targetDir = \\dirname($containerDir);'."\n";
1096+
}
11001097
}
11011098

11021099
if (Container::class !== $this->baseClass) {
@@ -1350,12 +1347,11 @@ private function addInlineRequires(?array &$preload): string
13501347
foreach ($lineage as $file) {
13511348
if (!isset($this->inlinedRequires[$file])) {
13521349
$this->inlinedRequires[$file] = true;
1353-
$file = preg_replace('#^\\$this->targetDirs\[(\d++)\]#', sprintf('\dirname(__DIR__, %d + $1)', $this->asFiles), $file);
13541350
$code .= sprintf("\n include_once %s;", $file);
13551351
}
13561352
}
13571353

1358-
return $code ? sprintf("\n \$this->privates['service_container'] = static function () {%s\n };\n", $code) : '';
1354+
return $code ? sprintf("\n \$this->privates['service_container'] = function () {%s\n };\n", $code) : '';
13591355
}
13601356

13611357
private function addDefaultParametersMethod(): string
@@ -1374,7 +1370,7 @@ private function addDefaultParametersMethod(): string
13741370
$export = $this->exportParameters([$value]);
13751371
$export = explode('0 => ', substr(rtrim($export, " ]\n"), 2, -1), 2);
13761372

1377-
if (preg_match("/\\\$this->(?:getEnv\('(?:\w++:)*+\w++'\)|targetDirs\[\d++\])/", $export[1])) {
1373+
if (preg_match("/\\\$this->(?:getEnv\('(?:\w++:)*+\w++'\)|targetDir\.'')/", $export[1])) {
13781374
$dynamicPhp[$key] = sprintf('%scase %s: $value = %s; break;', $export[0], $this->export($key), $export[1]);
13791375
} else {
13801376
$php[] = sprintf('%s%s => %s,', $export[0], $this->export($key), $export[1]);
@@ -1776,7 +1772,7 @@ private function dumpParameter(string $name): string
17761772
return $dumpedValue;
17771773
}
17781774

1779-
if (!preg_match("/\\\$this->(?:getEnv\('(?:\w++:)*+\w++'\)|targetDirs\[\d++\])/", $dumpedValue)) {
1775+
if (!preg_match("/\\\$this->(?:getEnv\('(?:\w++:)*+\w++'\)|targetDir\.'')/", $dumpedValue)) {
17801776
return sprintf('$this->parameters[%s]', $this->doExport($name));
17811777
}
17821778
}
@@ -1977,8 +1973,10 @@ private function export($value)
19771973
$dirname = $this->asFiles ? '$this->containerDir' : '__DIR__';
19781974
$offset = 1 + $this->targetDirMaxMatches - \count($matches);
19791975

1980-
if ($this->asFiles || 0 < $offset) {
1981-
$dirname = sprintf('$this->targetDirs[%d]', $offset);
1976+
if (0 < $offset) {
1977+
$dirname = sprintf('\dirname(__DIR__, %d)', $offset + (int) $this->asFiles);
1978+
} elseif ($this->asFiles) {
1979+
$dirname = "\$this->targetDir.''"; // empty string concatenation on purpose
19821980
}
19831981

19841982
if ($prefix || $suffix) {
@@ -2027,4 +2025,37 @@ private function doExport($value, bool $resolveEnv = false)
20272025

20282026
return $export;
20292027
}
2028+
2029+
private function getAutoloadFile(): ?string
2030+
{
2031+
if (null === $this->targetDirRegex) {
2032+
return null;
2033+
}
2034+
2035+
foreach (spl_autoload_functions() as $autoloader) {
2036+
if (!\is_array($autoloader)) {
2037+
continue;
2038+
}
2039+
2040+
if ($autoloader[0] instanceof DebugClassLoader || $autoloader[0] instanceof LegacyDebugClassLoader) {
2041+
$autoloader = $autoloader[0]->getClassLoader();
2042+
}
2043+
2044+
if (!\is_array($autoloader) || !$autoloader[0] instanceof ClassLoader || !$autoloader[0]->findFile(__CLASS__)) {
2045+
continue;
2046+
}
2047+
2048+
foreach (get_declared_classes() as $class) {
2049+
if (0 === strpos($class, 'ComposerAutoloaderInit') && $class::getLoader() === $autoloader[0]) {
2050+
$file = (new \ReflectionClass($class))->getFileName();
2051+
2052+
if (preg_match($this->targetDirRegex.'A', $file)) {
2053+
return $file;
2054+
}
2055+
}
2056+
}
2057+
}
2058+
2059+
return null;
2060+
}
20302061
}

‎src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/container_alias_deprecation.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/container_alias_deprecation.php
-1Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
class Symfony_DI_PhpDumper_Test_Aliases_Deprecation extends Container
1919
{
2020
private $parameters = [];
21-
private $targetDirs = [];
2221

2322
public function __construct()
2423
{

‎src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/custom_container_class_constructor_without_arguments.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/custom_container_class_constructor_without_arguments.php
-1Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
class ProjectServiceContainer extends \Symfony\Component\DependencyInjection\Tests\Fixtures\Container\ConstructorWithoutArgumentsContainer
2121
{
2222
private $parameters = [];
23-
private $targetDirs = [];
2423

2524
public function __construct()
2625
{

‎src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/custom_container_class_with_mandatory_constructor_arguments.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/custom_container_class_with_mandatory_constructor_arguments.php
-1Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
class ProjectServiceContainer extends \Symfony\Component\DependencyInjection\Tests\Fixtures\Container\ConstructorWithMandatoryArgumentsContainer
2121
{
2222
private $parameters = [];
23-
private $targetDirs = [];
2423

2524
public function __construct()
2625
{

‎src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/custom_container_class_with_optional_constructor_arguments.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/custom_container_class_with_optional_constructor_arguments.php
-1Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
class ProjectServiceContainer extends \Symfony\Component\DependencyInjection\Tests\Fixtures\Container\ConstructorWithOptionalArgumentsContainer
2121
{
2222
private $parameters = [];
23-
private $targetDirs = [];
2423

2524
public function __construct()
2625
{

‎src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/custom_container_class_without_constructor.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/custom_container_class_without_constructor.php
-1Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
class ProjectServiceContainer extends \Symfony\Component\DependencyInjection\Tests\Fixtures\Container\NoConstructorContainer
2121
{
2222
private $parameters = [];
23-
private $targetDirs = [];
2423

2524
public function __construct()
2625
{

‎src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services1-1.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services1-1.php
-1Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
class Container extends \Symfony\Component\DependencyInjection\Dump\AbstractContainer
2121
{
2222
private $parameters = [];
23-
private $targetDirs = [];
2423

2524
public function __construct()
2625
{

‎src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services1.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services1.php
-1Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
class ProjectServiceContainer extends Container
1919
{
2020
private $parameters = [];
21-
private $targetDirs = [];
2221

2322
public function __construct()
2423
{

‎src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services10.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services10.php
-1Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
class ProjectServiceContainer extends Container
1919
{
2020
private $parameters = [];
21-
private $targetDirs = [];
2221

2322
public function __construct()
2423
{

‎src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services12.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services12.php
+5-18Lines changed: 5 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,9 @@
1818
class ProjectServiceContainer extends Container
1919
{
2020
private $parameters = [];
21-
private $targetDirs = [];
2221

2322
public function __construct()
2423
{
25-
$dir = __DIR__;
26-
for ($i = 1; $i <= 5; ++$i) {
27-
$this->targetDirs[$i] = $dir = \dirname($dir);
28-
}
2924
$this->parameters = $this->getDefaultParameters();
3025

3126
$this->services = $this->privates = [];
@@ -61,7 +56,7 @@ public function getRemovedIds(): array
6156
*/
6257
protected function getTestService()
6358
{
64-
return $this->services['test'] = new \stdClass(('wiz'.$this->targetDirs[1]), [('wiz'.$this->targetDirs[1]) => ($this->targetDirs[2].'/')]);
59+
return $this->services['test'] = new \stdClass(('wiz'.\dirname(__DIR__, 1)), [('wiz'.\dirname(__DIR__, 1)) => (\dirname(__DIR__, 2).'/')]);
6560
}
6661

6762
public function getParameter($name)
@@ -103,29 +98,21 @@ public function getParameterBag(): ParameterBagInterface
10398
return $this->parameterBag;
10499
}
105100

106-
private $loadedDynamicParameters = [
107-
'foo' => false,
108-
'buz' => false,
109-
];
101+
private $loadedDynamicParameters = [];
110102
private $dynamicParameters = [];
111103

112104
private function getDynamicParameter(string $name)
113105
{
114-
switch ($name) {
115-
case 'foo': $value = ('wiz'.$this->targetDirs[1]); break;
116-
case 'buz': $value = $this->targetDirs[2]; break;
117-
default: throw new InvalidArgumentException(sprintf('The dynamic parameter "%s" must be defined.', $name));
118-
}
119-
$this->loadedDynamicParameters[$name] = true;
120-
121-
return $this->dynamicParameters[$name] = $value;
106+
throw new InvalidArgumentException(sprintf('The dynamic parameter "%s" must be defined.', $name));
122107
}
123108

124109
protected function getDefaultParameters(): array
125110
{
126111
return [
112+
'foo' => ('wiz'.\dirname(__DIR__, 1)),
127113
'bar' => __DIR__,
128114
'baz' => (__DIR__.'/PhpDumperTest.php'),
115+
'buz' => \dirname(__DIR__, 2),
129116
];
130117
}
131118
}

‎src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services13.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services13.php
-1Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
class ProjectServiceContainer extends Container
1919
{
2020
private $parameters = [];
21-
private $targetDirs = [];
2221

2322
public function __construct()
2423
{

‎src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services19.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services19.php
-1Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
class ProjectServiceContainer extends Container
1919
{
2020
private $parameters = [];
21-
private $targetDirs = [];
2221

2322
public function __construct()
2423
{

‎src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services24.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services24.php
-1Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
class ProjectServiceContainer extends Container
1919
{
2020
private $parameters = [];
21-
private $targetDirs = [];
2221

2322
public function __construct()
2423
{

‎src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services26.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services26.php
+1-7Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,9 @@
1818
class Symfony_DI_PhpDumper_Test_EnvParameters extends Container
1919
{
2020
private $parameters = [];
21-
private $targetDirs = [];
2221

2322
public function __construct()
2423
{
25-
$dir = __DIR__;
26-
for ($i = 1; $i <= 5; ++$i) {
27-
$this->targetDirs[$i] = $dir = \dirname($dir);
28-
}
2924
$this->parameters = $this->getDefaultParameters();
3025

3126
$this->services = $this->privates = [];
@@ -119,7 +114,6 @@ public function getParameterBag(): ParameterBagInterface
119114
'baz' => false,
120115
'json' => false,
121116
'db_dsn' => false,
122-
'env(json_file)' => false,
123117
];
124118
private $dynamicParameters = [];
125119

@@ -130,7 +124,6 @@ private function getDynamicParameter(string $name)
130124
case 'baz': $value = $this->getEnv('int:Baz'); break;
131125
case 'json': $value = $this->getEnv('json:file:json_file'); break;
132126
case 'db_dsn': $value = $this->getEnv('resolve:DB'); break;
133-
case 'env(json_file)': $value = ($this->targetDirs[1].'/array.json'); break;
134127
default: throw new InvalidArgumentException(sprintf('The dynamic parameter "%s" must be defined.', $name));
135128
}
136129
$this->loadedDynamicParameters[$name] = true;
@@ -144,6 +137,7 @@ protected function getDefaultParameters(): array
144137
'project_dir' => '/foo/bar',
145138
'env(FOO)' => 'foo',
146139
'env(DB)' => 'sqlite://%project_dir%/var/data.db',
140+
'env(json_file)' => (\dirname(__DIR__, 1).'/array.json'),
147141
];
148142
}
149143
}

‎src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services33.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services33.php
-1Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
class ProjectServiceContainer extends Container
1919
{
2020
private $parameters = [];
21-
private $targetDirs = [];
2221

2322
public function __construct()
2423
{

‎src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services8.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services8.php
-1Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
class ProjectServiceContainer extends Container
1919
{
2020
private $parameters = [];
21-
private $targetDirs = [];
2221

2322
public function __construct()
2423
{

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.