@@ -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
@@ -216,13 +219,17 @@ public function dump(array $options = [])
216
219
}
217
220
}
218
221
222
+ $ proxyClasses = $ this ->inlineFactories ? $ this ->generateProxyClasses () : null ;
223
+
219
224
$ code =
220
225
$ this ->startClass ($ options ['class ' ], $ baseClass , $ baseClassWithNamespace ).
221
226
$ this ->addServices ($ services ).
222
227
$ this ->addDeprecatedAliases ().
223
228
$ this ->addDefaultParametersMethod ()
224
229
;
225
230
231
+ $ proxyClasses = $ proxyClasses ?? $ this ->generateProxyClasses ();
232
+
226
233
if ($ this ->addGetService ) {
227
234
$ code = preg_replace (
228
235
"/( \r? \n\r? \n public function __construct.+? \\{ \r? \n)/s " ,
@@ -259,13 +266,24 @@ public function dump(array $options = [])
259
266
$ files ['removed-ids.php ' ] = $ c .= "]; \n" ;
260
267
}
261
268
262
- foreach ($ this ->generateServiceFiles ($ services ) as $ file => $ c ) {
263
- $ files [$ file ] = $ fileStart .$ c ;
269
+ if (!$ this ->inlineFactories ) {
270
+ foreach ($ this ->generateServiceFiles ($ services ) as $ file => $ c ) {
271
+ $ files [$ file ] = $ fileStart .$ c ;
272
+ }
273
+ foreach ($ proxyClasses as $ file => $ c ) {
274
+ $ files [$ file ] = "<?php \n" .$ c ;
275
+ }
264
276
}
265
- foreach ($ this ->generateProxyClasses () as $ file => $ c ) {
266
- $ files [$ file ] = "<?php \n" .$ c ;
277
+
278
+ $ code .= $ this ->endClass ();
279
+
280
+ if ($ this ->inlineFactories ) {
281
+ foreach ($ proxyClasses as $ c ) {
282
+ $ code .= $ c ;
283
+ }
267
284
}
268
- $ files [$ options ['class ' ].'.php ' ] = $ code .$ this ->endClass ();
285
+
286
+ $ files [$ options ['class ' ].'.php ' ] = $ code ;
269
287
$ hash = ucfirst (strtr (ContainerBuilder::hash ($ files ), '._ ' , 'xx ' ));
270
288
$ code = [];
271
289
@@ -304,7 +322,7 @@ public function dump(array $options = [])
304
322
EOF ;
305
323
} else {
306
324
$ code .= $ this ->endClass ();
307
- foreach ($ this -> generateProxyClasses () as $ c ) {
325
+ foreach ($ proxyClasses as $ c ) {
308
326
$ code .= $ c ;
309
327
}
310
328
}
@@ -431,8 +449,9 @@ private function collectLineage($class, array &$lineage)
431
449
$ lineage [$ class ] = substr ($ exportedFile , 1 , -1 );
432
450
}
433
451
434
- private function generateProxyClasses ()
452
+ private function generateProxyClasses (): array
435
453
{
454
+ $ proxyClasses = [];
436
455
$ alreadyGenerated = [];
437
456
$ definitions = $ this ->container ->getDefinitions ();
438
457
$ strip = '' === $ this ->docStar && method_exists ('Symfony\Component\HttpKernel\Kernel ' , 'stripComments ' );
@@ -451,19 +470,39 @@ private function generateProxyClasses()
451
470
if ("\n" === $ proxyCode = "\n" .$ proxyDumper ->getProxyCode ($ definition )) {
452
471
continue ;
453
472
}
473
+
474
+ if ($ this ->inlineRequires ) {
475
+ $ lineage = [];
476
+ $ this ->collectLineage ($ class , $ lineage );
477
+
478
+ $ code = '' ;
479
+ foreach (array_diff_key (array_flip ($ lineage ), $ this ->inlinedRequires ) as $ file => $ class ) {
480
+ if ($ this ->inlineFactories ) {
481
+ $ this ->inlinedRequires [$ file ] = true ;
482
+ }
483
+ $ file = preg_replace ('#^ \\$this->targetDirs\[(\d++)\]# ' , sprintf ('\dirname(__DIR__, %d + $1) ' , $ this ->asFiles ), $ file );
484
+ $ code .= sprintf ("include_once %s; \n" , $ file );
485
+ }
486
+
487
+ $ proxyCode = $ code .$ proxyCode ;
488
+ }
489
+
454
490
if ($ strip ) {
455
491
$ proxyCode = "<?php \n" .$ proxyCode ;
456
492
$ proxyCode = substr (Kernel::stripComments ($ proxyCode ), 5 );
457
493
}
458
- yield sprintf ('%s.php ' , explode (' ' , $ proxyCode , 3 )[1 ]) => $ proxyCode ;
494
+
495
+ $ proxyClasses [sprintf ('%s.php ' , explode (' ' , $ proxyCode , 3 )[1 ])] = $ proxyCode ;
459
496
}
497
+
498
+ return $ proxyClasses ;
460
499
}
461
500
462
501
private function addServiceInclude (string $ cId , Definition $ definition ): string
463
502
{
464
503
$ code = '' ;
465
504
466
- if ($ this ->inlineRequires && !$ this ->isHotPath ($ definition )) {
505
+ if ($ this ->inlineRequires && ( !$ this ->isHotPath ($ definition) || $ this -> getProxyDumper ()-> isProxyCandidate ( $ definition ) )) {
467
506
$ lineage = [];
468
507
foreach ($ this ->inlinedDefinitions as $ def ) {
469
508
if (!$ def ->isDeprecated () && \is_string ($ class = \is_array ($ factory = $ def ->getFactory ()) && \is_string ($ factory [0 ]) ? $ factory [0 ] : $ def ->getClass ())) {
@@ -693,7 +732,7 @@ private function addService(string $id, Definition $definition): array
693
732
$ lazyInitialization = '' ;
694
733
}
695
734
696
- $ asFile = $ this ->asFiles && !$ this ->isHotPath ($ definition );
735
+ $ asFile = $ this ->asFiles && !$ this ->inlineFactories && ! $ this -> isHotPath ($ definition );
697
736
$ methodName = $ this ->generateMethodName ($ id );
698
737
if ($ asFile ) {
699
738
$ file = $ methodName .'.php ' ;
@@ -719,17 +758,16 @@ protected function {$methodName}($lazyInitialization)
719
758
$ this ->serviceCalls = [];
720
759
$ this ->inlinedDefinitions = $ this ->getDefinitionsFromArguments ([$ definition ], null , $ this ->serviceCalls );
721
760
722
- $ code .= $ this ->addServiceInclude ($ id , $ definition );
761
+ if ($ definition ->isDeprecated ()) {
762
+ $ code .= sprintf (" @trigger_error(%s, E_USER_DEPRECATED); \n\n" , $ this ->export ($ definition ->getDeprecationMessage ($ id )));
763
+ }
723
764
724
765
if ($ this ->getProxyDumper ()->isProxyCandidate ($ definition )) {
725
766
$ factoryCode = $ asFile ? ($ definition ->isShared () ? "\$this->load('%s.php', false) " : '$this->factories[%2$s](false) ' ) : '$this->%s(false) ' ;
726
767
$ code .= $ this ->getProxyDumper ()->getProxyFactoryCode ($ definition , $ id , sprintf ($ factoryCode , $ methodName , $ this ->doExport ($ id )));
727
768
}
728
769
729
- if ($ definition ->isDeprecated ()) {
730
- $ code .= sprintf (" @trigger_error(%s, E_USER_DEPRECATED); \n\n" , $ this ->export ($ definition ->getDeprecationMessage ($ id )));
731
- }
732
-
770
+ $ code .= $ this ->addServiceInclude ($ id , $ definition );
733
771
$ code .= $ this ->addInlineService ($ id , $ definition );
734
772
735
773
if ($ asFile ) {
@@ -1044,7 +1082,7 @@ public function __construct()
1044
1082
1045
1083
$ code .= $ this ->addSyntheticIds ();
1046
1084
$ code .= $ this ->addMethodMap ();
1047
- $ code .= $ this ->asFiles ? $ this ->addFileMap () : '' ;
1085
+ $ code .= $ this ->asFiles && ! $ this -> inlineFactories ? $ this ->addFileMap () : '' ;
1048
1086
$ code .= $ this ->addAliases ();
1049
1087
$ code .= $ this ->addInlineRequires ();
1050
1088
$ code .= <<<EOF
@@ -1063,7 +1101,7 @@ public function isCompiled()
1063
1101
EOF ;
1064
1102
$ code .= $ this ->addRemovedIds ();
1065
1103
1066
- if ($ this ->asFiles ) {
1104
+ if ($ this ->asFiles && ! $ this -> inlineFactories ) {
1067
1105
$ code .= <<<EOF
1068
1106
1069
1107
protected function load( \$file, \$lazyLoad = true)
@@ -1079,10 +1117,10 @@ protected function load(\$file, \$lazyLoad = true)
1079
1117
if (!$ proxyDumper ->isProxyCandidate ($ definition )) {
1080
1118
continue ;
1081
1119
}
1082
- if ($ this ->asFiles ) {
1120
+ if ($ this ->asFiles && ! $ this -> inlineFactories ) {
1083
1121
$ proxyLoader = '$this->load("{$class}.php") ' ;
1084
- } elseif ($ this ->namespace ) {
1085
- $ proxyLoader = 'class_alias(" ' . $ this -> namespace . ' \\\\{ $class} ", $class, false) ' ;
1122
+ } elseif ($ this ->namespace || $ this -> inlineFactories ) {
1123
+ $ proxyLoader = 'class_alias(__NAMESPACE__." \\\\$class", $class, false) ' ;
1086
1124
} else {
1087
1125
$ proxyLoader = '' ;
1088
1126
}
@@ -1160,7 +1198,7 @@ private function addMethodMap(): string
1160
1198
$ definitions = $ this ->container ->getDefinitions ();
1161
1199
ksort ($ definitions );
1162
1200
foreach ($ definitions as $ id => $ definition ) {
1163
- if (!$ definition ->isSynthetic () && $ definition ->isPublic () && (!$ this ->asFiles || $ this ->isHotPath ($ definition ))) {
1201
+ if (!$ definition ->isSynthetic () && $ definition ->isPublic () && (!$ this ->asFiles || $ this ->inlineFactories || $ this -> isHotPath ($ definition ))) {
1164
1202
$ code .= ' ' .$ this ->doExport ($ id ).' => ' .$ this ->doExport ($ this ->generateMethodName ($ id )).", \n" ;
1165
1203
}
1166
1204
}
@@ -1257,6 +1295,11 @@ private function addInlineRequires(): string
1257
1295
1258
1296
foreach ($ this ->container ->findTaggedServiceIds ($ this ->hotPathTag ) as $ id => $ tags ) {
1259
1297
$ definition = $ this ->container ->getDefinition ($ id );
1298
+
1299
+ if ($ this ->getProxyDumper ()->isProxyCandidate ($ definition )) {
1300
+ continue ;
1301
+ }
<
10000
code> 1302
+
1260
1303
$ inlinedDefinitions = $ this ->getDefinitionsFromArguments ([$ definition ]);
1261
1304
1262
1305
foreach ($ inlinedDefinitions as $ def ) {
@@ -1604,7 +1647,7 @@ private function dumpValue($value, bool $interpolate = true): string
1604
1647
continue ;
1605
1648
}
1606
1649
$ definition = $ this ->container ->findDefinition ($ id = (string ) $ v );
1607
- $ load = !($ definition ->hasErrors () && $ e = $ definition ->getErrors ()) ? $ this ->asFiles && !$ this ->isHotPath ($ definition ) : reset ($ e );
1650
+ $ load = !($ definition ->hasErrors () && $ e = $ definition ->getErrors ()) ? $ this ->asFiles && !$ this ->inlineFactories && ! $ this -> isHotPath ($ definition ) : reset ($ e );
1608
1651
$ serviceMap .= sprintf ("\n %s => [%s, %s, %s, %s], " ,
1609
1652
$ this ->export ($ k ),
1610
1653
$ this ->export ($ definition ->isShared () ? ($ definition ->isPublic () ? 'services ' : 'privates ' ) : false ),
@@ -1747,7 +1790,7 @@ private function getServiceCall(string $id, Reference $reference = null): string
1747
1790
$ code = sprintf ('$this->%s[%s] = %s ' , $ definition ->isPublic () ? 'services ' : 'privates ' , $ this ->doExport ($ id ), $ code );
1748
1791
}
1749
1792
$ code = "( $ code) " ;
1750
- } elseif ($ this ->asFiles && !$ this ->isHotPath ($ definition )) {
1793
+ } elseif ($ this ->asFiles && !$ this ->inlineFactories && ! $ this -> isHotPath ($ definition )) {
1751
1794
$ code = sprintf ("\$this->load('%s.php') " , $ this ->generateMethodName ($ id ));
1752
1795
if (!$ definition ->isShared ()) {
1753
1796
$ factory = sprintf ('$this->factories%s[%s] ' , $ definition ->isPublic () ? '' : "['service_container'] " , $ this ->doExport ($ id ));
0 commit comments