11
11
12
12
namespace Symfony \Component \DependencyInjection \Dumper ;
13
13
14
+ use Composer \Autoload \ClassLoader ;
15
+ use Symfony \Component \Debug \DebugClassLoader as LegacyDebugClassLoader ;
14
16
use Symfony \Component \DependencyInjection \Argument \ArgumentInterface ;
15
17
use Symfony \Component \DependencyInjection \Argument \IteratorArgument ;
16
18
use Symfony \Component \DependencyInjection \Argument \ServiceClosureArgument ;
36
38
use Symfony \Component \DependencyInjection \ServiceLocator as BaseServiceLocator ;
37
39
use Symfony \Component \DependencyInjection \TypedReference ;
38
40
use Symfony \Component \DependencyInjection \Variable ;
41
+ use Symfony \Component \ErrorHandler \DebugClassLoader ;
39
42
use Symfony \Component \ExpressionLanguage \Expression ;
40
43
use Symfony \Component \HttpKernel \Kernel ;
41
44
@@ -296,8 +299,11 @@ public function dump(array $options = [])
296
299
$ namespaceLine = $ this ->namespace ? "\nnamespace {$ this ->namespace }; \n" : '' ;
297
300
$ time = $ options ['build_time ' ];
298
301
$ 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 );
299
306
300
- if ($ preload ) {
301
307
$ code [$ options ['class ' ].'.preload.php ' ] = <<<EOF
302
308
<?php
303
309
@@ -306,7 +312,7 @@ public function dump(array $options = [])
306
312
307
313
use Symfony\Component\DependencyInjection\Dumper\Preloader;
308
314
309
- require dirname(__DIR__, 3).'/vendor/autoload.php' ;
315
+ require $ autoloadFile ;
310
316
require __DIR__.'/Container {$ hash }/ {$ options ['class ' ]}.php';
311
317
312
318
\$classes = [];
@@ -511,7 +517,6 @@ private function generateProxyClasses(): array
511
517
if ($ this ->inlineFactories ) {
512
518
$ this ->inlinedRequires [$ file ] = true ;
513
519
}
514
- $ file = preg_replace ('#^ \\$this->targetDirs\[(\d++)\]# ' , sprintf ('\dirname(__DIR__, %d + $1) ' , $ this ->asFiles ), $ file );
515
520
$ code .= sprintf ("include_once %s; \n" , $ file );
516
521
}
517
522
@@ -553,7 +558,6 @@ private function addServiceInclude(string $cId, Definition $definition): string
553
558
}
554
559
555
560
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 );
557
561
$ code .= sprintf (" include_once %s; \n" , $ file );
558
562
}
559
563
}
@@ -562,7 +566,6 @@ private function addServiceInclude(string $cId, Definition $definition): string
562
566
if ($ file = $ def ->getFile ()) {
563
567
$ file = $ this ->dumpValue ($ file );
564
568
$ file = '( ' === $ file [0 ] ? substr ($ file , 1 , -1 ) : $ file ;
565
- $ file = preg_replace ('#^ \\$this->targetDirs\[(\d++)\]# ' , sprintf ('\dirname(__DIR__, %d + $1) ' , $ this ->asFiles ), $ file );
566
569
$ code .= sprintf (" include_once %s; \n" , $ file );
567
570
}
568
571
}
@@ -1076,27 +1079,21 @@ private function startClass(string $class, string $baseClass, ?array &$preload):
1076
1079
class $ class extends $ baseClass
1077
1080
{
1078
1081
private \$parameters = [];
1079
- private \$targetDirs = [];
1080
1082
1081
1083
public function __construct()
1082
1084
{
1083
1085
1084
1086
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
- }
1095
1087
if ($ this ->asFiles ) {
1096
1088
$ code = str_replace ('$parameters ' , "\$buildParameters; \n private \$containerDir; \n private \$parameters " , $ code );
1097
1089
$ code = str_replace ('__construct() ' , '__construct(array $buildParameters = [], $containerDir = __DIR__) ' , $ code );
1098
1090
$ code .= " \$this->buildParameters = \$buildParameters; \n" ;
1099
1091
$ 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
+ }
1100
1097
}
1101
1098
1102
1099
if (Container::class !== $ this ->baseClass ) {
@@ -1350,12 +1347,11 @@ private function addInlineRequires(?array &$preload): string
1350
1347
foreach ($ lineage as $ file ) {
1351
1348
if (!isset ($ this ->inlinedRequires [$ file ])) {
1352
1349
$ this ->inlinedRequires [$ file ] = true ;
1353
- $ file = preg_replace ('#^ \\$this->targetDirs\[(\d++)\]# ' , sprintf ('\dirname(__DIR__, %d + $1) ' , $ this ->asFiles ), $ file );
1354
1350
$ code .= sprintf ("\n include_once %s; " , $ file );
1355
1351
}
1356
1352
}
1357
1353
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 ) : '' ;
1359
1355
}
1360
1356
1361
1357
private function addDefaultParametersMethod (): string
@@ -1374,7 +1370,7 @@ private function addDefaultParametersMethod(): string
1374
1370
$ export = $ this ->exportParameters ([$ value ]);
1375
1371
$ export = explode ('0 => ' , substr (rtrim ($ export , " ] \n" ), 2 , -1 ), 2 );
1376
1372
1377
- if (preg_match ("/ \\\$this->(?:getEnv\('(?:\w++:)*+\w++'\)|targetDirs\[\d++\] )/ " , $ export [1 ])) {
1373
+ if (preg_match ("/ \\\$this->(?:getEnv\('(?:\w++:)*+\w++'\)|targetDir\.'' )/ " , $ export [1 ])) {
1378
1374
$ dynamicPhp [$ key ] = sprintf ('%scase %s: $value = %s; break; ' , $ export [0 ], $ this ->export ($ key ), $ export [1 ]);
1379
1375
} else {
1380
1376
$ php [] = sprintf ('%s%s => %s, ' , $ export [0 ], $ this ->export ($ key ), $ export [1 ]);
@@ -1776,7 +1772,7 @@ private function dumpParameter(string $name): string
1776
1772
return $ dumpedValue ;
1777
1773
}
1778
1774
1779
- if (!preg_match ("/ \\\$this->(?:getEnv\('(?:\w++:)*+\w++'\)|targetDirs\[\d++\] )/ " , $ dumpedValue )) {
1775
+ if (!preg_match ("/ \\\$this->(?:getEnv\('(?:\w++:)*+\w++'\)|targetDir\.'' )/ " , $ dumpedValue )) {
1780
1776
return sprintf ('$this->parameters[%s] ' , $ this ->doExport ($ name ));
1781
1777
}
1782
1778
}
@@ -1977,8 +1973,10 @@ private function export($value)
1977
1973
$ dirname = $ this ->asFiles ? '$this->containerDir ' : '__DIR__ ' ;
1978
1974
$ offset = 1 + $ this ->targetDirMaxMatches - \count ($ matches );
1979
1975
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
1982
1980
}
1983
1981
1984
1982
if ($ prefix || $ suffix ) {
@@ -2027,4 +2025,37 @@ private function doExport($value, bool $resolveEnv = false)
2027
2025
2028
2026
return $ export ;
2029
2027
}
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
+ }
2030
2061
}
0 commit comments