1111
1212namespace Symfony \Component \DependencyInjection \Dumper ;
1313
14+ use Composer \Autoload \ClassLoader ;
15+ use Symfony \Component \Debug \DebugClassLoader as LegacyDebugClassLoader ;
1416use Symfony \Component \DependencyInjection \Argument \ArgumentInterface ;
1517use Symfony \Component \DependencyInjection \Argument \IteratorArgument ;
1618use Symfony \Component \DependencyInjection \Argument \ServiceClosureArgument ;
3638use Symfony \Component \DependencyInjection \ServiceLocator as BaseServiceLocator ;
3739use Symfony \Component \DependencyInjection \TypedReference ;
3840use Symfony \Component \DependencyInjection \Variable ;
41+ use Symfony \Component \ErrorHandler \DebugClassLoader ;
3942use Symfony \Component \ExpressionLanguage \Expression ;
4043use 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
307313use Symfony\Component\DependencyInjection\Dumper\Preloader;
308314
309- require dirname(__DIR__, 3).'/vendor/autoload.php' ;
315+ require $ autoloadFile ;
310316require __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):
10761079class $ class extends $ baseClass
10771080{
10781081 private \$parameters = [];
1079- private \$targetDirs = [];
10801082
10811083 public function __construct()
10821084 {
10831085
10841086EOF ;
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}
0 commit comments