@@ -72,6 +72,7 @@ class PhpDumper extends Dumper
72
72
private $ namespace ;
73
73
private $ asFiles ;
74
74
private $ hotPathTag ;
75
+ private $ inlineFactories ;
75
76
private $ inlineRequires ;
76
77
private $ inlinedRequires = [];
77
78
private $ circularReferences = [];
@@ -134,6 +135,7 @@ public function dump(array $options = [])
134
135
'as_files ' => false ,
135
136
'debug ' => true ,
136
137
'hot_path_tag ' => 'container.hot_path ' ,
138
+ 'inline_factories_parameter ' => 'container.dumper.inline_factories ' ,
137
139
'inline_class_loader_parameter ' => 'container.dumper.inline_class_loader ' ,
138
140
'service_locator_tag ' => 'container.service_locator ' ,
139
141
'build_time ' => time (),
@@ -143,6 +145,7 @@ public function dump(array $options = [])
143
145
$ this ->namespace = $ options ['namespace ' ];
144
146
$ this ->asFiles = $ options ['as_files ' ];
145
147
$ this ->hotPathTag = $ options ['hot_path_tag ' ];
148
+ $ this ->inlineFactories = $ this ->asFiles && $ options ['inline_factories_parameter ' ] && $ this ->container ->hasParameter ($ options ['inline_factories_parameter ' ]) && $ this ->container ->getParameter ($ options ['inline_factories_parameter ' ]);
146
149
$ this ->inlineRequires = $ options ['inline_class_loader_parameter ' ] && $ this ->container ->hasParameter ($ options ['inline_class_loader_parameter ' ]) && $ this ->container ->getParameter ($ options ['inline_class_loader_parameter ' ]);
147
150
$ this ->serviceLocatorTag = $ options ['service_locator_tag ' ];
148
151
@@ -215,6 +218,8 @@ public function dump(array $options = [])
215
218
}
216
219
}
217
220
221
+ $ proxyClasses = $ this ->generateProxyClasses ();
222
+
218
223
$ code =
219
224
$ this ->startClass ($ options ['class ' ], $ baseClass , $ baseClassWithNamespace ).
220
225
$ this ->addServices ($ services ).
@@ -258,13 +263,24 @@ public function dump(array $options = [])
258
263
$ files ['removed-ids.php ' ] = $ c .= "]; \n" ;
259
264
}
260
265
261
- foreach ($ this ->generateServiceFiles ($ services ) as $ file => $ c ) {
262
- $ files [$ file ] = $ fileStart .$ c ;
266
+ if (!$ this ->inlineFactories ) {
267
+ foreach ($ this ->generateServiceFiles ($ services ) as $ file => $ c ) {
268
+ $ files [$ file ] = $ fileStart .$ c ;
269
+ }
270
+ foreach ($ proxyClasses as $ file => $ c ) {
271
+ $ files [$ file ] = "<?php \n" .$ c ;
272
+ }
263
273
}
264
- foreach ($ this ->generateProxyClasses () as $ file => $ c ) {
265
- $ files [$ file ] = "<?php \n" .$ c ;
274
+
275
+ $ code .= $ this ->endClass ();
276
+
277
+ if ($ this ->inlineFactories ) {
278
+ foreach ($ proxyClasses as $ c ) {
279
+ $ code .= $ c ;
280
+ }
266
281
}
267
- $ files [$ options ['class ' ].'.php ' ] = $ code .$ this ->endClass ();
282
+
283
+ $ files [$ options ['class ' ].'.php ' ] = $ code ;
268
284
$ hash = ucfirst (strtr (ContainerBuilder::hash ($ files ), '._ ' , 'xx ' ));
269
285
$ code = [];
270
286
@@ -303,7 +319,7 @@ public function dump(array $options = [])
303
319
EOF ;
304
320
} else {
305
321
$ code .= $ this ->endClass ();
306
- foreach ($ this -> generateProxyClasses () as $ c ) {
322
+ foreach ($ proxyClasses as $ c ) {
307
323
$ code .= $ c ;
308
324
}
309
325
}
@@ -422,8 +438,9 @@ private function collectLineage($class, array &$lineage)
422
438
$ lineage [$ class ] = substr ($ exportedFile , 1 , -1 );
423
439
}
424
440
425
- private function generateProxyClasses ()
441
+ private function generateProxyClasses (): array
426
442
{
443
+ $ proxyClasses = [];
427
444
$ alreadyGenerated = [];
428
445
$ definitions = $ this ->container ->getDefinitions ();
429
446
$ strip = '' === $ this ->docStar && method_exists ('Symfony\Component\HttpKernel\Kernel ' , 'stripComments ' );
@@ -442,19 +459,37 @@ private function generateProxyClasses()
442
459
if ("\n" === $ proxyCode = "\n" .$ proxyDumper ->getProxyCode ($ definition )) {
443
460
continue ;
444
461
}
462
+
463
+ if ($ this ->inlineRequires ) {
464
+ $ lineage = [];
465
+ $ this ->collectLineage ($ class , $ lineage );
466
+
467
+ $ code = '' ;
468
+ foreach (array_diff_key (array_flip ($ lineage ), $ this ->inlinedRequires ) as $ file => $ class ) {
469
+ $ this ->inlinedRequires [$ file ] = true ;
470
+ $ file = preg_replace ('#^ \\$this->targetDirs\[(\d++)\]# ' , sprintf ('\dirname(__DIR__, %d + $1) ' , $ this ->asFiles ), $ file );
471
+ $ code .= sprintf ("include_once %s; \n" , $ file );
472
+ }
473
+
474
+ $ proxyCode = $ code .$ proxyCode ;
475
+ }
476
+
445
477
if ($ strip ) {
446
478
$ proxyCode = "<?php \n" .$ proxyCode ;
447
479
$ proxyCode = substr (Kernel::stripComments ($ proxyCode ), 5 );
448
480
}
449
- yield sprintf ('%s.php ' , explode (' ' , $ proxyCode , 3 )[1 ]) => $ proxyCode ;
481
+
482
+ $ proxyClasses [sprintf ('%s.php ' , explode (' ' , $ proxyCode , 3 )[1 ])] = $ proxyCode ;
450
483
}
484
+
485
+ return $ proxyClasses ;
451
486
}
452
487
453
488
private function addServiceInclude (string $ cId , Definition $ definition ): string
454
489
{
455
490
$ code = '' ;
456
491
457
- if ($ this ->inlineRequires && !$ this ->isHotPath ($ definition )) {
492
+ if ($ this ->inlineRequires && ( !$ this ->isHotPath ($ definition) || $ this -> getProxyDumper ()-> isProxyCandidate ( $ definition ) )) {
458
493
$ lineage = [];
459
494
foreach ($ this ->inlinedDefinitions as $ def ) {
460
495
if (!$ def ->isDeprecated () && \is_string ($ class = \is_array ($ factory = $ def ->getFactory ()) && \is_string ($ factory [0 ]) ? $ factory [0 ] : $ def ->getClass ())) {
@@ -685,7 +720,7 @@ private function addService(string $id, Definition $definition): array
685
720
$ lazyInitialization = '' ;
686
721
}
687
722
688
- $ asFile = $ this ->asFiles && !$ this ->isHotPath ($ definition );
723
+ $ asFile = $ this ->asFiles && !$ this ->inlineFactories && ! $ this -> isHotPath ($ definition );
689
724
$ methodName = $ this ->generateMethodName ($ id );
690
725
if ($ asFile ) {
691
726
$ file = $ methodName .'.php ' ;
@@ -711,17 +746,16 @@ protected function {$methodName}($lazyInitialization)
711
746
$ this ->serviceCalls = [];
712
747
$ this ->inlinedDefinitions = $ this ->getDefinitionsFromArguments ([$ definition ], null , $ this ->serviceCalls );
713
748
714
- $ code .= $ this ->addServiceInclude ($ id , $ definition );
749
+ if ($ definition ->isDeprecated ()) {
750
+ $ code .= sprintf (" @trigger_error(%s, E_USER_DEPRECATED); \n\n" , $ this ->export ($ definition ->getDeprecationMessage ($ id )));
751
+ }
715
752
716
753
if ($ this ->getProxyDumper ()->isProxyCandidate ($ definition )) {
717
754
$ factoryCode = $ asFile ? ($ definition ->isShared () ? "\$this->load('%s.php', false) " : '$this->factories[%2$s](false) ' ) : '$this->%s(false) ' ;
718
755
$ code .= $ this ->getProxyDumper ()->getProxyFactoryCode ($ definition , $ id , sprintf ($ factoryCode , $ methodName , $ this ->doExport ($ id )));
719
756
}
720
757
721
- if ($ definition ->isDeprecated ()) {
722
- $ code .= sprintf (" @trigger_error(%s, E_USER_DEPRECATED); \n\n" , $ this ->export ($ definition ->getDeprecationMessage ($ id )));
723
- }
724
-
758
+ $ code .= $ this ->addServiceInclude ($ id , $ definition );
725
759
$ code .= $ this ->addInlineService ($ id , $ definition );
726
760
727
761
if ($ asFile ) {
@@ -1024,7 +1058,7 @@ public function __construct()
1024
1058
1025
1059
$ code .= $ this ->addSyntheticIds ();
1026
1060
$ code .= $ this ->addMethodMap ();
1027
- $ code .= $ this ->asFiles ? $ this ->addFileMap () : '' ;
1061
+ $ code .= $ this ->asFiles && ! $ this -> inlineFactories ? $ this ->addFileMap () : '' ;
1028
1062
$ code .= $ this ->addAliases ();
1029
1063
$ code .= $ this ->addInlineRequires ();
1030
1064
$ code .= <<<EOF
@@ -1043,7 +1077,7 @@ public function isCompiled()
1043
1077
EOF ;
1044
1078
$ code .= $ this ->addRemovedIds ();
1045
1079
1046
- if ($ this ->asFiles ) {
1080
+ if ($ this ->asFiles && ! $ this -> inlineFactories ) {
1047
1081
$ code .= <<<EOF
1048
1082
1049
1083
protected function load( \$file, \$lazyLoad = true)
@@ -1059,10 +1093,10 @@ protected function load(\$file, \$lazyLoad = true)
1059
1093
if (!$ proxyDumper ->isProxyCandidate ($ definition )) {
1060
1094
continue ;
1061
1095
}
1062
- if ($ this ->asFiles ) {
1096
+ if ($ this ->asFiles && ! $ this -> inlineFactories ) {
1063
1097
$ proxyLoader = '$this->load("{$class}.php") ' ;
1064
- } elseif ($ this ->namespace ) {
1065
- $ proxyLoader = 'class_alias(" ' . $ this -> namespace . ' \\\\{ $class} ", $class, false) ' ;
1098
+ } elseif ($ this ->namespace || $ this -> inlineFactories ) {
1099
+ $ proxyLoader = 'class_alias(__NAMESPACE__." \\\\$class", $class, false) ' ;
1066
1100
} else {
1067
1101
$ proxyLoader = '' ;
1068
1102
}
@@ -1140,7 +1174,7 @@ private function addMethodMap(): string
1140
1174
$ definitions = $ this ->container ->getDefinitions ();
1141
1175
ksort ($ definitions );
1142
1176
foreach ($ definitions as $ id => $ definition ) {
1143
- if (!$ definition ->isSynthetic () && $ definition ->isPublic () && (!$ this ->asFiles || $ this ->isHotPath ($ definition ))) {
1177
+ if (!$ definition ->isSynthetic () && $ definition ->isPublic () && (!$ this ->asFiles || $ this ->inlineFactories || $ this -> isHotPath ($ definition ))) {
1144
1178
$ code .= ' ' .$ this ->doExport ($ id ).' => ' .$ this ->doExport ($ this ->generateMethodName ($ id )).", \n" ;
1145
1179
}
1146
1180
}
@@ -1237,6 +1271,11 @@ private function addInlineRequires(): string
1237
1271
1238
1272
foreach ($ this ->container ->findTaggedServiceIds ($ this ->hotPathTag ) as $ id => $ tags ) {
1239
1273
$ definition = $ this ->container ->getDefinition ($ id );
1274
+
1275
+ if ($ this ->getProxyDumper ()->isProxyCandidate ($ definition )) {
1276
+ continue ;
1277
+ }
1278
+
1240
1279
$ inlinedDefinitions = $ this ->getDefinitionsFromArguments ([$ definition ]);
1241
1280
1242
1281
foreach ($ inlinedDefinitions as $ def ) {
@@ -1578,7 +1617,7 @@ private function dumpValue($value, bool $interpolate = true): string
1578
1617
continue ;
1579
1618
}
1580
1619
$ definition = $ this ->container ->findDefinition ($ id = (string ) $ v );
1581
- $ load = !($ definition ->hasErrors () && $ e = $ definition ->getErrors ()) ? $ this ->asFiles && !$ this ->isHotPath ($ definition ) : reset ($ e );
1620
+ $ load = !($ definition ->hasErrors () && $ e = $ definition ->getErrors ()) ? $ this ->asFiles && !$ this ->inlineFactories && ! $ this -> isHotPath ($ definition ) : reset ($ e );
1582
1621
$ serviceMap .= sprintf ("\n %s => [%s, %s, %s, %s], " ,
1583
1622
$ this ->export ($ k ),
1584
1623
$ this ->export ($ definition ->isShared () ? ($ definition ->isPublic () ? 'services ' : 'privates ' ) : false ),
@@ -1716,7 +1755,7 @@ private function getServiceCall(string $id, Reference $reference = null): string
1716
1755
$ code = sprintf ('$this->%s[%s] = %s ' , $ definition ->isPublic () ? 'services ' : 'privates ' , $ this ->doExport ($ id ), $ code );
1717
1756
}
1718
1757
$ code = "( $ code) " ;
1719
- } elseif ($ this ->asFiles && !$ this ->isHotPath ($ definition )) {
1758
+ } elseif ($ this ->asFiles && !$ this ->inlineFactories && ! $ this -> isHotPath ($ definition )) {
1720
1759
$ code = sprintf ("\$this->load('%s.php') " , $ this ->generateMethodName ($ id ));
1721
1760
if (!$ definition ->isShared ()) {
1722
1761
$ factory = sprintf ('$this->factories%s[%s] ' , $ definition ->isPublic () ? '' : "['service_container'] " , $ this ->doExport ($ id ));
0 commit comments