@@ -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,13 +218,17 @@ public function dump(array $options = [])
215
218
}
216
219
}
217
220
221
+ $ proxyClasses = $ this ->inlineFactories ? $ this ->generateProxyClasses () : null ;
222
+
218
223
$ code =
219
224
$ this ->startClass ($ options ['class ' ], $ baseClass , $ baseClassWithNamespace ).
220
225
$ this ->addServices ($ services ).
221
226
$ this ->addDeprecatedAliases ().
222
227
$ this ->addDefaultParametersMethod ()
223
228
;
224
229
230
+ $ proxyClasses = $ proxyClasses ?? $ this ->generateProxyClasses ();
231
+
225
232
if ($ this ->addGetService ) {
226
233
$ code = preg_replace (
227
234
"/( \r? \n\r? \n public function __construct.+? \\{ \r? \n)/s " ,
@@ -258,13 +265,24 @@ public function dump(array $options = [])
258
265
$ files ['removed-ids.php ' ] = $ c .= "]; \n" ;
259
266
}
260
267
261
- foreach ($ this ->generateServiceFiles ($ services ) as $ file => $ c ) {
262
- $ files [$ file ] = $ fileStart .$ c ;
268
+ if (!$ this ->inlineFactories ) {
269
+ foreach ($ this ->generateServiceFiles ($ services ) as $ file => $ c ) {
270
+ $ files [$ file ] = $ fileStart .$ c ;
271
+ }
272
+ foreach ($ proxyClasses as $ file => $ c ) {
273
+ $ files [$ file ] = "<?php \n" .$ c ;
274
+ }
263
275
}
264
- foreach ($ this ->generateProxyClasses () as $ file => $ c ) {
265
- $ files [$ file ] = "<?php \n" .$ c ;
276
+
277
+ $ code .= $ this ->endClass ();
278
+
279
+ if ($ this ->inlineFactories ) {
280
+ foreach ($ proxyClasses as $ c ) {
281
+ $ code .= $ c ;
282
+ }
266
283
}
267
- $ files [$ options ['class ' ].'.php ' ] = $ code .$ this ->endClass ();
284
+
285
+ $ files [$ options ['class ' ].'.php ' ] = $ code ;
268
286
$ hash = ucfirst (strtr (ContainerBuilder::hash ($ files ), '._ ' , 'xx ' ));
269
287
$ code = [];
270
288
@@ -303,7 +321,7 @@ public function dump(array $options = [])
303
321
EOF ;
304
322
} else {
305
323
$ code .= $ this ->endClass ();
306
- foreach ($ this -> generateProxyClasses () as $ c ) {
324
+ foreach ($ proxyClasses as $ c ) {
307
325
$ code .= $ c ;
308
326
}
309
327
}
@@ -422,8 +440,9 @@ private function collectLineage($class, array &$lineage)
422
440
$ lineage [$ class ] = substr ($ exportedFile , 1 , -1 );
423
441
}
424
442
425
- private function generateProxyClasses ()
443
+ private function generateProxyClasses (): array
426
444
{
445
+ $ proxyClasses = [];
427
446
$ alreadyGenerated = [];
428
447
$ definitions = $ this ->container ->getDefinitions ();
429
448
$ strip = '' === $ this ->docStar && method_exists ('Symfony\Component\HttpKernel\Kernel ' , 'stripComments ' );
@@ -442,19 +461,39 @@ private function generateProxyClasses()
442
461
if ("\n" === $ proxyCode = "\n" .$ proxyDumper ->getProxyCode ($ definition )) {
443
462
continue ;
444
463
}
464
+
465
+ if ($ this ->inlineRequires ) {
466
+ $ lineage = [];
467
+ $ this ->collectLineage ($ class , $ lineage );
468
+
469
+ $ code = '' ;
470
+ foreach (array_diff_key (array_flip ($ lineage ), $ this ->inlinedRequires ) as $ file => $ class ) {
471
+ if ($ this ->inlineFactories ) {
472
+ $ this ->inlinedRequires [$ file ] = true ;
473
+ }
474
+ $ file = preg_replace ('#^ \\$this->targetDirs\[(\d++)\]# ' , sprintf ('\dirname(__DIR__, %d + $1) ' , $ this ->asFiles ), $ file );
475
+ $ code .= sprintf ("include_once %s; \n" , $ file );
476
+ }
477
+
478
+ $ proxyCode = $ code .$ proxyCode ;
479
+ }
480
+
445
481
if ($ strip ) {
446
482
$ proxyCode = "<?php \n" .$ proxyCode ;
447
483
$ proxyCode = substr (Kernel::stripComments ($ proxyCode ), 5 );
448
484
}
449
- yield sprintf ('%s.php ' , explode (' ' , $ proxyCode , 3 )[1 ]) => $ proxyCode ;
485
+
486
+ $ proxyClasses [sprintf ('%s.php ' , explode (' ' , $ proxyCode , 3 )[1 ])] = $ proxyCode ;
450
487
}
488
+
489
+ return $ proxyClasses ;
451
490
}
452
491
453
492
private function addServiceInclude (string $ cId , Definition $ definition ): string
454
493
{
455
494
$ code = '' ;
456
495
457
- if ($ this ->inlineRequires && !$ this ->isHotPath ($ definition )) {
496
+ if ($ this ->inlineRequires && ( !$ this ->isHotPath ($ definition) || $ this -> getProxyDumper ()-> isProxyCandidate ( $ definition ) )) {
458
497
$ lineage = [];
459
498
foreach ($ this ->inlinedDefinitions as $ def ) {
460
499
if (!$ def ->isDeprecated () && \is_string ($ class = \is_array ($ factory = $ def ->getFactory ()) && \is_string ($ factory [0 ]) ? $ factory [0 ] : $ def ->getClass ())) {
@@ -685,7 +724,7 @@ private function addService(string $id, Definition $definition): array
685
724
$ lazyInitialization = '' ;
686
725
}
687
726
688
- $ asFile = $ this ->asFiles && !$ this ->isHotPath ($ definition );
727
+ $ asFile = $ this ->asFiles && !$ this ->inlineFactories && ! $ this -> isHotPath ($ definition );
689
728
$ methodName = $ this ->generateMethodName ($ id );
690
729
if ($ asFile ) {
691
730
$ file = $ methodName .'.php ' ;
@@ -711,17 +750,16 @@ protected function {$methodName}($lazyInitialization)
711
750
$ this ->serviceCalls = [];
712
751
$ this ->inlinedDefinitions = $ this ->getDefinitionsFromArguments ([$ definition ], null , $ this ->serviceCalls );
713
752
714
- $ code .= $ this ->addServiceInclude ($ id , $ definition );
753
+ if ($ definition ->isDeprecated ()) {
754
+ $ code .= sprintf (" @trigger_error(%s, E_USER_DEPRECATED); \n\n" , $ this ->export ($ definition ->getDeprecationMessage ($ id )));
755
+ }
715
756
716
757
if ($ this ->getProxyDumper ()->isProxyCandidate ($ definition )) {
717
758
$ factoryCode = $ asFile ? ($ definition ->isShared () ? "\$this->load('%s.php', false) " : '$this->factories[%2$s](false) ' ) : '$this->%s(false) ' ;
718
759
$ code .= $ this ->getProxyDumper ()->getProxyFactoryCode ($ definition , $ id , sprintf ($ factoryCode , $ methodName , $ this ->doExport ($ id )));
719
760
}
720
761
721
- if ($ definition ->isDeprecated ()) {
722
- $ code .= sprintf (" @trigger_error(%s, E_USER_DEPRECATED); \n\n" , $ this ->export ($ definition ->getDeprecationMessage ($ id )));
723
- }
724
-
762
+ $ code .= $ this ->addServiceInclude ($ id , $ definition );
725
763
$ code .= $ this ->addInlineService ($ id , $ definition );
726
764
727
765
if ($ asFile ) {
@@ -1024,7 +1062,7 @@ public function __construct()
1024
1062
1025
1063
$ code .= $ this ->addSyntheticIds ();
1026
1064
$ code .= $ this ->addMethodMap ();
1027
- $ code .= $ this ->asFiles ? $ this ->addFileMap () : '' ;
1065
+ $ code .= $ this ->asFiles && ! $ this -> inlineFactories ? $ this ->addFileMap () : '' ;
1028
1066
$ code .= $ this ->addAliases ();
1029
1067
$ code .= $ this ->addInlineRequires ();
1030
1068
$ code .= <<<EOF
@@ -1043,7 +1081,7 @@ public function isCompiled()
1043
1081
EOF ;
1044
1082
$ code .= $ this ->addRemovedIds ();
1045
1083
1046
- if ($ this ->asFiles ) {
1084
+ if ($ this ->asFiles && ! $ this -> inlineFactories ) {
1047
1085
$ code .= <<<EOF
1048
1086
1049
1087
protected function load( \$file, \$lazyLoad = true)
@@ -1059,10 +1097,10 @@ protected function load(\$file, \$lazyLoad = true)
1059
1097
if (!$ proxyDumper ->isProxyCandidate ($ definition )) {
1060
1098
continue ;
1061
1099
}
1062
- if ($ this ->asFiles ) {
1100
+ if ($ this ->asFiles && ! $ this -> inlineFactories ) {
1063
1101
$ proxyLoader = '$this->load("{$class}.php") ' ;
1064
- } elseif ($ this ->namespace ) {
1065
- $ proxyLoader = 'class_alias(" ' . $ this -> namespace . ' \\\\{ $class} ", $class, false) ' ;
1102
+ } elseif ($ this ->namespace || $ this -> inlineFactories ) {
1103
+ $ proxyLoader = 'class_alias(__NAMESPACE__." \\\\$class", $class, false) ' ;
1066
1104
} else {
1067
1105
$ proxyLoader = '' ;
1068
1106
}
@@ -1140,7 +1178,7 @@ private function addMethodMap(): string
1140
1178
$ definitions = $ this ->container ->getDefinitions ();
1141
1179
ksort ($ definitions );
1142
1180
foreach ($ definitions as $ id => $ definition ) {
1143
- if (!$ definition ->isSynthetic () && $ definition ->isPublic () && (!$ this ->asFiles || $ this ->isHotPath ($ definition ))) {
1181
+ if (!$ definition ->isSynthetic () && $ definition ->isPublic () && (!$ this ->asFiles || $ this ->inlineFactories || $ this -> isHotPath ($ definition ))) {
1144
1182
$ code .= ' ' .$ this ->doExport ($ id ).' => ' .$ this ->doExport ($ this ->generateMethodName ($ id )).", \n" ;
1145
1183
}
1146
1184
}
@@ -1237,6 +1275,11 @@ private function addInlineRequires(): string
1237
1275
1238
1276
foreach ($ this ->container ->findTaggedServiceIds ($ this ->hotPathTag ) as $ id => $ tags ) {
1239
1277
$ definition = $ this ->container ->getDefinition ($ id );
1278
+
1279
+ if ($ this ->getProxyDumper ()->isProxyCandidate ($ definition )) {
1280
+ continue ;
1281
+ }
1282
+
1240
1283
$ inlinedDefinitions = $ this ->getDefinitionsFromArguments ([$ definition ]);
1241
1284
1242
1285
foreach ($ inlinedDefinitions as $ def ) {
@@ -1578,7 +1621,7 @@ private function dumpValue($value, bool $interpolate = true): string
1578
1621
continue ;
1579
1622
}
1580
1623
$ definition = $ this ->container ->findDefinition ($ id = (string ) $ v );
1581
- $ load = !($ definition ->hasErrors () && $ e = $ definition ->getErrors ()) ? $ this ->asFiles && !$ this ->isHotPath ($ definition ) : reset ($ e );
1624
+ $ load = !($ definition ->hasErrors () && $ e = $ definition ->getErrors ()) ? $ this ->asFiles && !$ this ->inlineFactories && ! $ this -> isHotPath ($ definition ) : reset ($ e );
1582
1625
$ serviceMap .= sprintf ("\n %s => [%s, %s, %s, %s], " ,
1583
1626
$ this ->export ($ k ),
1584
1627
$ this ->export ($ definition ->isShared () ? ($ definition ->isPublic () ? 'services ' : 'privates ' ) : false ),
@@ -1716,7 +1759,7 @@ private function getServiceCall(string $id, Reference $reference = null): string
1716
1759
$ code = sprintf ('$this->%s[%s] = %s ' , $ definition ->isPublic () ? 'services ' : 'privates ' , $ this ->doExport ($ id ), $ code );
1717
1760
}
1718
1761
$ code = "( $ code) " ;
1719
- } elseif ($ this ->asFiles && !$ this ->isHotPath ($ definition )) {
1762
+ } elseif ($ this ->asFiles && !$ this ->inlineFactories && ! $ this -> isHotPath ($ definition )) {
1720
1763
$ code = sprintf ("\$this->load('%s.php') " , $ this ->generateMethodName ($ id ));
1721
1764
if (!$ definition ->isShared ()) {
1722
1765
$ factory = sprintf ('$this->factories%s[%s] ' , $ definition ->isPublic () ? '' : "['service_container'] " , $ this ->doExport ($ id ));
0 commit comments