@@ -66,6 +66,7 @@ class PhpDumper extends Dumper
66
66
private $ docStar ;
67
67
private $ serviceIdToMethodNameMap ;
68
68
private $ usedMethodNames ;
69
+ private $ class ;
69
70
70
71
/**
71
72
* @var \Symfony\Component\DependencyInjection\LazyProxy\PhpDumper\DumperInterface
@@ -121,6 +122,7 @@ public function dump(array $options = array())
121
122
'debug ' => true ,
122
123
), $ options );
123
124
125
+ $ this ->class = $ options ['class ' ];
124
126
$ this ->initializeMethodNamesMap ($ options ['base_class ' ]);
125
127
126
128
$ this ->docStar = $ options ['debug ' ] ? '* ' : '' ;
@@ -156,9 +158,10 @@ public function dump(array $options = array())
156
158
$ this ->addReset ().
157
159
$ this ->addCompile ().
158
160
$ this ->addIsCompiled ().
159
- $ this ->addServices ().
161
+ $ this ->addPublicServices ().
160
162
$ this ->addDefaultParametersMethod ().
161
163
$ this ->endClass ().
164
+ $ this ->addPrivateServices ().
162
165
$ this ->addProxyClasses ()
163
166
;
164
167
$ this ->targetDirRegex = null ;
@@ -402,7 +405,7 @@ private function addServiceInstance($id, Definition $definition, $isSimpleInstan
402
405
$ instantiation = '' ;
403
406
404
407
if (!$ isProxyCandidate && $ definition ->isShared ()) {
405
- $ instantiation = sprintf ('$this ->%s[ \'%s \'] = %s ' , $ this ->container ->getDefinition ($ id )->isPublic () ? 'services ' : 'privates ' , $ id , $ isSimpleInstance ? '' : '$instance ' );
408
+ $ instantiation = sprintf ('$container ->%s[ \'%s \'] = %s ' , $ this ->container ->getDefinition ($ id )->isPublic () ? 'services ' : 'privates ' , $ id , $ isSimpleInstance ? '' : '$instance ' );
406
409
} elseif (!$ isSimpleInstance ) {
407
410
$ instantiation = '$instance ' ;
408
411
}
@@ -658,15 +661,21 @@ private function addService($id, Definition $definition)
658
661
$ autowired = $ definition ->isAutowired () ? ' autowired ' : '' ;
659
662
660
663
if ($ definition ->isLazy ()) {
661
- $ lazyInitialization = '$lazyLoad = true ' ;
662
- } else {
664
+ $ lazyInitialization = 'bool $lazyLoad = true ' ;
665
+ if (!$ definition ->isPublic ()) {
666
+ $ lazyInitialization = 'parent $container, ' .$ lazyInitialization ;
667
+ }
668
+ } elseif ($ definition ->isPublic ()) {
663
669
$ lazyInitialization = '' ;
670
+ } else {
671
+ $ lazyInitialization = 'parent $container ' ;
664
672
}
665
673
666
674
// with proxies, for 5.3.3 compatibility, the getter must be public to be accessible to the initializer
667
675
$ isProxyCandidate = $ this ->getProxyDumper ()->isProxyCandidate ($ definition );
668
- $ visibility = $ isProxyCandidate ? 'public ' : ($ definition ->isPublic () ? 'protected ' : 'private ' );
669
- $ methodName = $ this ->generateMethodName ($ id );
676
+ $ visibility = $ isProxyCandidate ? 'public ' : ($ definition ->isPublic () ? 'protected ' : 'protected static ' );
677
+ $ generatedMethodName = $ this ->generateMethodName ($ id );
678
+ $ methodName = $ definition ->isPublic () ? $ generatedMethodName : 'create ' ;
670
679
$ code = <<<EOF
671
680
672
681
/* {$ this ->docStar }
@@ -679,7 +688,11 @@ private function addService($id, Definition $definition)
679
688
680
689
EOF ;
681
690
682
- $ code .= $ isProxyCandidate ? $ this ->getProxyDumper ()->getProxyFactoryCode ($ definition , $ id , $ methodName ) : '' ;
691
+ if ($ definition ->isPublic ()) {
692
+ $ code .= " \$container = \$this; \n\n" ;
693
+ }
694
+
695
+ $ code .= $ isProxyCandidate ? $ this ->getProxyDumper ()->getProxyFactoryCode ($ definition , $ id , $ definition ->isPublic () ? $ methodName : $ this ->class .'_ ' .substr ($ generatedMethodName , 3 )) : '' ;
683
696
684
697
if ($ definition ->isDeprecated ()) {
685
698
$ code .= sprintf (" @trigger_error(%s, E_USER_DEPRECATED); \n\n" , $ this ->export ($ definition ->getDeprecationMessage ($ id )));
@@ -700,31 +713,52 @@ private function addService($id, Definition $definition)
700
713
$ this ->addServiceReturn ($ id , $ isSimpleInstance )
701
714
;
702
715
716
+ if (!$ definition ->isPublic ()) {
717
+ $ code = sprintf ("\nclass %s_%s extends %1 \$s \n{%s} \n" , $ this ->class , substr ($ generatedMethodName , 3 ), $ code );
718
+ }
719
+
703
720
$ this ->definitionVariables = null ;
704
721
$ this ->referenceVariables = null ;
705
722
706
723
return $ code ;
707
724
}
708
725
709
726
/**
710
- * Adds multiple services.
727
+ * Adds multiple public services.
711
728
*
712
729
* @return string
713
730
*/
714
- private function addServices ()
731
+ private function addPublicServices ()
715
732
{
716
- $ publicServices = $ privateServices = '' ;
733
+ $ publicServices = '' ;
717
734
$ definitions = $ this ->container ->getDefinitions ();
718
735
ksort ($ definitions );
719
736
foreach ($ definitions as $ id => $ definition ) {
720
737
if ($ definition ->isPublic ()) {
721
738
$ publicServices .= $ this ->addService ($ id , $ definition );
722
- } elseif (!$ this ->isTrivialInstance ($ definition )) {
739
+ }
740
+ }
741
+
742
+ return $ publicServices ;
743
+ }
744
+
745
+ /**
746
+ * Adds multiple private services.
747
+ *
748
+ * @return string
749
+ */
750
+ private function addPrivateServices ()
751
+ {
752
+ $ privateServices = '' ;
753
+ $ definitions = $ this ->container ->getDefinitions ();
754
+ ksort ($ definitions );
755
+ foreach ($ definitions as $ id => $ definition ) {
756
+ if (!$ definition ->isPublic () && !$ this ->isTrivialInstance ($ definition )) {
723
757
$ privateServices .= $ this ->addService ($ id , $ definition );
724
758
}
725
759
}
726
760
727
- return $ publicServices . $ privateServices ;
761
+ return $ privateServices ;
728
762
}
729
763
730
764
private function addNewInstance (Definition $ definition , $ return , $ instantiation , $ id )
@@ -809,9 +843,9 @@ private function startClass($class, $baseClass, $namespace)
809
843
*/
810
844
class $ class extends $ baseClass
811
845
{
812
- private \$parameters;
813
- private \$targetDirs = array();
814
- private \$privates = array();
846
+ protected \$parameters;
847
+ protected \$targetDirs = array();
848
+ protected \$privates = array();
815
849
816
850
EOF ;
817
851
}
@@ -979,7 +1013,7 @@ private function addDefaultParametersMethod()
979
1013
$ export = $ this ->exportParameters (array ($ value ));
980
1014
$ export = explode ('0 => ' , substr (rtrim ($ export , " ) \n" ), 7 , -1 ), 2 );
981
1015
982
- if (preg_match ("/ \\\$this ->(?:getEnv\('\w++'\)|targetDirs\[\d++\])/ " , $ export [1 ])) {
1016
+ if (preg_match ("/ \\\$container ->(?:getEnv\('\w++'\)|targetDirs\[\d++\])/ " , $ export [1 ])) {
983
1017
$ dynamicPhp [$ key ] = sprintf ('%scase %s: $value = %s; break; ' , $ export [0 ], $ this ->export ($ key ), $ export [1 ]);
984
1018
} else {
985
1019
$ php [] = sprintf ('%s%s => %s, ' , $ export [0 ], $ this ->export ($ key ), $ export [1 ]);
@@ -1358,19 +1392,19 @@ private function dumpValue($value, $interpolate = true)
1358
1392
1359
1393
$ code = sprintf ('return %s; ' , $ code );
1360
1394
1361
- return sprintf ("function ()%s { \n %s \n } " , $ returnedType , $ code );
1395
+ return sprintf ("function () use ( \$ container) %s { \n %s \n } " , $ returnedType , $ code );
1362
1396
}
1363
1397
1364
1398
if ($ value instanceof IteratorArgument) {
1365
1399
$ operands = array (0 );
1366
1400
$ code = array ();
1367
- $ code [] = 'new RewindableGenerator(function () { ' ;
1401
+ $ code [] = 'new RewindableGenerator(function () use ($container) { ' ;
1368
1402
1369
1403
if (!$ values = $ value ->getValues ()) {
1370
1404
$ code [] = ' return new \EmptyIterator(); ' ;
1371
1405
} else {
1372
1406
$ countCode = array ();
1373
- $ countCode [] = 'function () { ' ;
1407
+ $ countCode [] = 'function () use ($container) { ' ;
1374
1408
1375
1409
foreach ($ values as $ k => $ v ) {
1376
1410
($ c = $ this ->getServiceConditionals ($ v )) ? $ operands [] = "(int) ( $ c) " : ++$ operands [0 ];
@@ -1455,7 +1489,7 @@ private function dumpValue($value, $interpolate = true)
1455
1489
1456
1490
return $ this ->getServiceCall ((string ) $ value , $ value );
1457
1491
} elseif ($ value instanceof Expression) {
1458
- return $ this ->getExpressionLanguage ()->compile ((string ) $ value , array ('this ' => 'container ' ));
1492
+ return $ this ->getExpressionLanguage ()->compile ((string ) $ value , array ('container ' => 'container ' ));
1459
1493
} elseif ($ value instanceof Parameter) {
1460
1494
return $ this ->dumpParameter ($ value );
1461
1495
} elseif (true === $ interpolate && is_string ($ value )) {
@@ -1521,12 +1555,12 @@ private function dumpParameter($name)
1521
1555
return $ dumpedValue ;
1522
1556
}
1523
1557
1524
- if (!preg_match ("/ \\\$this ->(?:getEnv\('\w++'\)|targetDirs\[\d++\])/ " , $ dumpedValue )) {
1525
- return sprintf ("\$this ->parameters['%s'] " , $ name );
1558
+ if (!preg_match ("/ \\\$container ->(?:getEnv\('\w++'\)|targetDirs\[\d++\])/ " , $ dumpedValue )) {
1559
+ return sprintf ("\$container ->parameters['%s'] " , $ name );
1526
1560
}
1527
1561
}
1528
1562
1529
- return sprintf ("\$this ->getParameter('%s') " , $ name );
1563
+ return sprintf ("\$container ->getParameter('%s') " , $ name );
1530
1564
}
1531
1565
1532
1566
/**
@@ -1544,27 +1578,30 @@ private function getServiceCall($id, Reference $reference = null)
1544
1578
}
1545
1579
1546
1580
if ('service_container ' === $ id ) {
1547
- return '$this ' ;
1581
+ return '$container ' ;
1548
1582
}
1549
1583
1550
1584
if ($ this ->container ->hasDefinition ($ id )) {
1551
1585
$ definition = $ this ->container ->getDefinition ($ id );
1552
1586
1553
- if ($ definition ->isPublic () || ! $ this -> isTrivialInstance ( $ definition ) ) {
1554
- $ code = sprintf ('$this ->%s() ' , $ this ->generateMethodName ($ id ));
1555
- } else {
1587
+ if ($ definition ->isPublic ()) {
1588
+ $ code = sprintf ('$container ->%s() ' , $ this ->generateMethodName ($ id ));
1589
+ } elseif ( $ this -> isTrivialInstance ( $ definition )) {
1556
1590
$ code = substr ($ this ->addNewInstance($ definition , '' , '' , $ id ), 8 , -2 );
1557
1591
if ($ definition ->isShared ()) {
1558
- $ code = sprintf ('($this->privates [ \'%s \'] = %s) ' , $ id , $ code );
1592
+ $ code = sprintf ('($container->%s [ \'%s \'] = %s) ' , $ definition -> isPublic () ? ' services ' : ' privates ' , $ id , $ code );
1559
1593
}
1594
+ } else {
1595
+ $ code = sprintf ('%s_%s::create($container) ' , $ this ->class , substr ($ this ->generateMethodName ($ id ), 3 ));
1560
1596
}
1597
+
1561
1598
if ($ definition ->isShared ()) {
1562
- $ code = sprintf ('($this ->%s[ \'%s \'] ?? %s) ' , $ definition ->isPublic () ? 'services ' : 'privates ' , $ id , $ code );
1599
+ $ code = sprintf ('($container ->%s[ \'%s \'] ?? %s) ' , $ definition ->isPublic () ? 'services ' : 'privates ' , $ id , $ code );
1563
1600
}
1564
1601
} elseif (null !== $ reference && ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE !== $ reference ->getInvalidBehavior ()) {
1565
- $ code = sprintf ('$this ->get( \'%s \', ContainerInterface::NULL_ON_INVALID_REFERENCE) ' , $ id );
1602
+ $ code = sprintf ('$container ->get( \'%s \', ContainerInterface::NULL_ON_INVALID_REFERENCE) ' , $ id );
1566
1603
} else {
1567
- $ code = sprintf ('$this ->get( \'%s \') ' , $ id );
1604
+ $ code = sprintf ('$container ->get( \'%s \') ' , $ id );
1568
1605
}
1569
1606
1570
1607
return $ code ;
@@ -1670,7 +1707,7 @@ private function getExpressionLanguage()
1670
1707
return $ this ->getServiceCall ($ id );
1671
1708
}
1672
1709
1673
- return sprintf ('$this ->get(%s) ' , $ arg );
1710
+ return sprintf ('$container ->get(%s) ' , $ arg );
1674
1711
});
1675
1712
1676
1713
if ($ this ->container ->isTrackingResources ()) {
@@ -1703,7 +1740,7 @@ private function export($value)
1703
1740
$ dirname = '__DIR__ ' ;
1704
1741
1705
1742
if (0 < $ offset = 1 + $ this ->targetDirMaxMatches - count ($ matches )) {
1706
- $ dirname = sprintf ('$this ->targetDirs[%d] ' , $ offset );
1743
+ $ dirname = sprintf ('$container ->targetDirs[%d] ' , $ offset );
1707
1744
}
1708
1745
1709
1746
if ($ prefix || $ suffix ) {
@@ -1720,7 +1757,11 @@ private function doExport($value)
1720
1757
{
1721
1758
$ export = var_export ($ value , true );
1722
1759
1723
- if ("' " === $ export [0 ] && $ export !== $ resolvedExport = $ this ->container ->resolveEnvPlaceholders ($ export , "'. \$this->getEnv('%s').' " )) {
1760
+ if (false !== strpos ($ export , "\n\nclass " )) {
1761
+ $ export = str_replace ("\n\nclass " , "\n'.' \nclass " , $ export );
1762
+ }
1763
+
1764
+ if ("' " === $ export [0 ] && $ export !== $ resolvedExport = $ this ->container ->resolveEnvPlaceholders ($ export , "'. \$container->getEnv('%s').' " )) {
1724
1765
$ export = $ resolvedExport ;
1725
1766
if ("' " === $ export [1 ]) {
1726
1767
$ export = substr ($ export , 3 );
0 commit comments