@@ -60,6 +60,7 @@ class PhpDumper extends Dumper
60
60
private $ docStar ;
61
61
private $ serviceIdToMethodNameMap ;
62
62
private $ usedMethodNames ;
63
+ private $ envPlaceholders ;
63
64
64
65
/**
65
66
* @var \Symfony\Component\DependencyInjection\LazyProxy\PhpDumper\DumperInterface
@@ -74,6 +75,7 @@ public function __construct(ContainerBuilder $container)
74
75
parent ::__construct ($ container );
75
76
76
77
$ this ->inlinedDefinitions = new \SplObjectStorage ();
78
+ $ this ->envPlaceholders = $ container ->getEnvPlaceholders ();
77
79
}
78
80
79
81
/**
@@ -384,7 +386,7 @@ private function addServiceInstance($id, $definition)
384
386
385
387
$ class = $ this ->dumpValue ($ class );
386
388
387
- if (0 === strpos ($ class , "' " ) && !preg_match ('/^ \'[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*( \\\{2}[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*)* \'$/ ' , $ class )) {
389
+ if (0 === strpos ($ class , "' " ) && false === strpos ( $ class , ' $ ' ) && !preg_match ('/^ \'[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*( \\\{2}[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*)* \'$/ ' , $ class )) {
388
390
throw new InvalidArgumentException (sprintf ('"%s" is not a valid class name for the "%s" service. ' , $ class , $ id ));
389
391
}
390
392
@@ -539,7 +541,7 @@ private function addServiceConfigurator($id, $definition, $variableName = 'insta
539
541
540
542
$ class = $ this ->dumpValue ($ callable [0 ]);
541
543
// If the class is a string we can optimize call_user_func away
542
- if (strpos ($ class , "' " ) === 0 ) {
544
+ if (0 === strpos ($ class , "' " ) && false === strpos ( $ class , ' $ ' ) ) {
543
545
return sprintf (" %s::%s( \$%s); \n" , $ this ->dumpLiteralClass ($ class ), $ callable [1 ], $ variableName );
544
546
}
545
547
@@ -572,6 +574,7 @@ private function addService($id, $definition)
572
574
if ($ definition ->isSynthetic ()) {
573
575
$ return [] = '@throws RuntimeException always since this service is expected to be injected dynamically ' ;
574
576
} elseif ($ class = $ definition ->getClass ()) {
577
+ $ class = $ this ->reverseEnvPlaceholders ($ class );
575
578
$ return [] = sprintf ('@return %s A %s instance ' , 0 === strpos ($ class , '% ' ) ? 'object ' : '\\' .ltrim ($ class , '\\' ), ltrim ($ class , '\\' ));
576
579
} elseif ($ definition ->getFactory ()) {
577
580
$ factory = $ definition ->getFactory ();
@@ -595,6 +598,7 @@ private function addService($id, $definition)
595
598
}
596
599
597
600
$ return = str_replace ("\n * \n" , "\n * \n" , implode ("\n * " , $ return ));
601
+ $ return = $ this ->reverseEnvPlaceholders ($ return );
598
602
599
603
$ doc = '' ;
600
604
if ($ definition ->isShared ()) {
@@ -654,7 +658,7 @@ private function addService($id, $definition)
654
658
$ code .= sprintf (" throw new RuntimeException('You have requested a synthetic service ( \"%s \"). The DIC does not know how to construct this service.'); \n } \n" , $ id );
655
659
} else {
656
660
if ($ definition ->isDeprecated ()) {
657
- $ code .= sprintf (" @trigger_error(%s, E_USER_DEPRECATED); \n\n" , var_export ($ definition ->getDeprecationMessage ($ id ), true ));
661
+ $ code .= sprintf (" @trigger_error(%s, E_USER_DEPRECATED); \n\n" , $ this -> export ($ definition ->getDeprecationMessage ($ id )));
658
662
}
659
663
660
664
$ code .=
@@ -720,7 +724,7 @@ private function addNewInstance(Definition $definition, $return, $instantiation,
720
724
721
725
$ class = $ this ->dumpValue ($ callable [0 ]);
722
726
// If the class is a string we can optimize call_user_func away
723
- if (strpos ($ class , "' " ) === 0 ) {
727
+ if (0 === strpos ($ class , "' " ) && false === strpos ( $ class , ' $ ' ) ) {
724
728
if ("'' " === $ class ) {
725
729
throw new RuntimeException (sprintf ('Cannot dump definition: The "%s" service is defined to be created by a factory but is missing the service reference, did you forget to define the factory service id or class? ' , $ id ));
726
730
}
@@ -735,7 +739,7 @@ private function addNewInstance(Definition $definition, $return, $instantiation,
735
739
return sprintf (" $ return {$ instantiation }call_user_func(array(%s, '%s')%s); \n" , $ this ->dumpValue ($ callable [0 ]), $ callable [1 ], $ arguments ? ', ' .implode (', ' , $ arguments ) : '' );
736
740
}
737
741
738
- return sprintf (" $ return {$ instantiation }\\ %s(%s); \n" , $ callable , $ arguments ? implode (', ' , $ arguments ) : '' );
742
+ return sprintf (" $ return {$ instantiation }%s(%s); \n" , $ this -> dumpLiteralClass ( $ this -> dumpValue ( $ callable)) , $ arguments ? implode (', ' , $ arguments ) : '' );
739
743
}
740
744
741
745
if (false !== strpos ($ class , '$ ' )) {
@@ -904,7 +908,7 @@ private function addMethodMap()
904
908
$ code = " \$this->methodMap = array( \n" ;
905
909
ksort ($ definitions );
906
910
foreach ($ definitions as $ id => $ definition ) {
907
- $ code .= ' ' .var_export ($ id, true ).' => ' .var_export ($ this ->generateMethodName ($ id ), true ).", \n" ;
911
+ $ code .= ' ' .$ this -> export ($ id ).' => ' .$ this -> export ($ this ->generateMethodName ($ id )).", \n" ;
908
912
}
909
913
910
914
return $ code ." ); \n" ;
@@ -925,7 +929,7 @@ private function addPrivateServices()
925
929
ksort ($ definitions );
926
930
foreach ($ definitions as $ id => $ definition ) {
927
931
if (!$ definition ->isPublic ()) {
928
- $ code .= ' ' .var_export ($ id, true )." => true, \n" ;
932
+ $ code .= ' ' .$ this -> export ($ id )." => true, \n" ;
929
933
}
930
934
}
931
935
@@ -962,7 +966,7 @@ private function addAliases()
962
966
while (isset ($ aliases [$ id ])) {
963
967
$ id = (string ) $ aliases [$ id ];
964
968
}
965
- $ code .= ' ' .var_export ($ alias, true ).' => ' .var_export ($ id, true ).", \n" ;
969
+ $ code .= ' ' .$ this -> export ($ alias ).' => ' .$ this -> export ($ id ).", \n" ;
966
970
}
967
971
968
972
return $ code ." ); \n" ;
@@ -1081,7 +1085,7 @@ private function exportParameters($parameters, $path = '', $indent = 12)
1081
1085
$ value = $ this ->export ($ value );
1082
1086
}
1083
1087
1084
- $ php [] = sprintf ('%s%s => %s, ' , str_repeat (' ' , $ indent ), var_export ($ key, true ), $ value );
1088
+ $ php [] = sprintf ('%s%s => %s, ' , str_repeat (' ' , $ indent ), $ this -> export ($ key ), $ value );
1085
1089
}
1086
1090
1087
1091
return sprintf ("array( \n%s \n%s) " , implode ("\n" , $ php ), str_repeat (' ' , $ indent - 4 ));
@@ -1283,7 +1287,7 @@ private function dumpValue($value, $interpolate = true)
1283
1287
$ factory = $ value ->getFactory ();
1284
1288
1285
1289
if (is_string ($ factory )) {
1286
- return sprintf ('\\ %s(%s) ' , $ factory , implode (', ' , $ arguments ));
1290
+ return sprintf ('%s(%s) ' , $ this -> dumpLiteralClass ( $ this -> dumpValue ( $ factory)) , implode (', ' , $ arguments ));
1287
1291
}
1288
1292
1289
1293
if (is_array ($ factory )) {
@@ -1358,7 +1362,7 @@ private function dumpValue($value, $interpolate = true)
1358
1362
private function dumpLiteralClass ($ class )
1359
1363
{
1360
1364
if (false !== strpos ($ class , '$ ' )) {
1361
- throw new RuntimeException ( ' Cannot dump definitions which have a variable class name. ' );
1365
+ return sprintf ( ' ${($_ = %s) && false ?: "_"} ' , $ class );
1362
1366
}
1363
1367
if (0 !== strpos ($ class , "' " ) || !preg_match ('/^ \'[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*( \\\{2}[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*)* \'$/ ' , $ class )) {
1364
1368
throw new RuntimeException (sprintf ('Cannot dump definition because of invalid class name (%s) ' , $ class ?: 'n/a ' ));
@@ -1529,9 +1533,9 @@ private function exportTargetDirs()
1529
1533
private function export ($ value )
1530
1534
{
1531
1535
if (null !== $ this ->targetDirRegex && is_string ($ value ) && preg_match ($ this ->targetDirRegex , $ value , $ matches , PREG_OFFSET_CAPTURE )) {
1532
- $ prefix = $ matches [0 ][1 ] ? var_export (substr ($ value , 0 , $ matches [0 ][1 ]), true ).'. ' : '' ;
1536
+ $ prefix = $ matches [0 ][1 ] ? $ this -> doExport (substr ($ value , 0 , $ matches [0 ][1 ])).'. ' : '' ;
1533
1537
$ suffix = $ matches [0 ][1 ] + strlen ($ matches [0 ][0 ]);
1534
- $ suffix = isset ($ value [$ suffix ]) ? '. ' .var_export (substr ($ value , $ suffix ), true ) : '' ;
1538
+ $ suffix = isset ($ value [$ suffix ]) ? '. ' .$ this -> doExport (substr ($ value , $ suffix )) : '' ;
1535
1539
$ dirname = '__DIR__ ' ;
1536
1540
1537
1541
if (0 < $ offset = 1 + $ this ->targetDirMaxMatches - count ($ matches )) {
@@ -1545,6 +1549,34 @@ private function export($value)
1545
1549
return $ dirname ;
1546
1550
}
1547
1551
1548
- return var_export ($ value , true );
1552
+ return $ this ->doExport ($ value );
1553
+ }
1554
+
1555
+ private function doExport ($ value )
1556
+ {
1557
+ $ export = var_export ($ value , true );
1558
+
1559
+ if ("' " === $ export [0 ] && $ this ->envPlaceholders && false !== strpos ($ export , 'env_ ' )) {
1560
+ foreach ($ this ->envPlaceholders as $ env => $ placeholder ) {
1561
+ $ export = str_replace ($ placeholder , "'. \$this->getEnv(' $ env').' " , $ export );
1562
+ }
1563
+ if ("' " === $ export [1 ]) {
1564
+ $ export = substr ($ export , 3 );
1565
+ }
1566
+ if (".'' " === substr ($ export , -3 )) {
1567
+ $ export = substr ($ export , 0 , -3 );
1568
+ }
1569
+ }
1570
+
1571
+ return $ export ;
1572
+ }
1573
+
1574
+ private function reverseEnvPlaceholders ($ string )
1575
+ {
1576
+ foreach ($ this ->envPlaceholders as $ env => $ placeholder ) {
1577
+ $ string = str_replace ($ placeholder , "%env( $ env)% " , $ string );
1578
+ }
1579
+
1580
+ return $ string ;
1549
1581
}
1550
1582
}
0 commit comments