@@ -60,6 +60,7 @@ class PhpDumper extends Dumper
6060 private $ docStar ;
6161 private $ serviceIdToMethodNameMap ;
6262 private $ usedMethodNames ;
63+ private $ envPlaceholders ;
6364
6465 /**
6566 * @var \Symfony\Component\DependencyInjection\LazyProxy\PhpDumper\DumperInterface
@@ -74,6 +75,7 @@ public function __construct(ContainerBuilder $container)
7475 parent ::__construct ($ container );
7576
7677 $ this ->inlinedDefinitions = new \SplObjectStorage ();
78+ $ this ->envPlaceholders = $ container ->getEnvPlaceholders ();
7779 }
7880
7981 /**
@@ -384,7 +386,7 @@ private function addServiceInstance($id, $definition)
384386
385387 $ class = $ this ->dumpValue ($ class );
386388
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 )) {
388390 throw new InvalidArgumentException (sprintf ('"%s" is not a valid class name for the "%s" service. ' , $ class , $ id ));
389391 }
390392
@@ -539,7 +541,7 @@ private function addServiceConfigurator($id, $definition, $variableName = 'insta
539541
540542 $ class = $ this ->dumpValue ($ callable [0 ]);
541543 // 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 , ' $ ' ) ) {
543545 return sprintf (" %s::%s( \$%s); \n" , $ this ->dumpLiteralClass ($ class ), $ callable [1 ], $ variableName );
544546 }
545547
@@ -572,6 +574,7 @@ private function addService($id, $definition)
572574 if ($ definition ->isSynthetic ()) {
573575 $ return [] = '@throws RuntimeException always since this service is expected to be injected dynamically ' ;
574576 } elseif ($ class = $ definition ->getClass ()) {
577+ $ class = $ this ->reverseEnvPlaceholders ($ class );
575578 $ return [] = sprintf ('@return %s A %s instance ' , 0 === strpos ($ class , '% ' ) ? 'object ' : '\\' .ltrim ($ class , '\\' ), ltrim ($ class , '\\' ));
576579 } elseif ($ definition ->getFactory ()) {
577580 $ factory = $ definition ->getFactory ();
@@ -595,6 +598,7 @@ private function addService($id, $definition)
595598 }
596599
597600 $ return = str_replace ("\n * \n" , "\n * \n" , implode ("\n * " , $ return ));
601+ $ return = $ this ->reverseEnvPlaceholders ($ return );
598602
599603 $ doc = '' ;
600604 if ($ definition ->isShared ()) {
@@ -654,7 +658,7 @@ private function addService($id, $definition)
654658 $ 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 );
655659 } else {
656660 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 )));
658662 }
659663
660664 $ code .=
@@ -720,7 +724,7 @@ private function addNewInstance(Definition $definition, $return, $instantiation,
720724
721725 $ class = $ this ->dumpValue ($ callable [0 ]);
722726 // 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 , ' $ ' ) ) {
724728 if ("'' " === $ class ) {
725729 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 ));
726730 }
@@ -735,7 +739,7 @@ private function addNewInstance(Definition $definition, $return, $instantiation,
735739 return sprintf (" $ return {$ instantiation }call_user_func(array(%s, '%s')%s); \n" , $ this ->dumpValue ($ callable [0 ]), $ callable [1 ], $ arguments ? ', ' .implode (', ' , $ arguments ) : '' );
736740 }
737741
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 ) : '' );
739743 }
740744
741745 if (false !== strpos ($ class , '$ ' )) {
@@ -904,7 +908,7 @@ private function addMethodMap()
904908 $ code = " \$this->methodMap = array( \n" ;
905909 ksort ($ definitions );
906910 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" ;
908912 }
909913
910914 return $ code ." ); \n" ;
@@ -925,7 +929,7 @@ private function addPrivateServices()
925929 ksort ($ definitions );
926930 foreach ($ definitions as $ id => $ definition ) {
927931 if (!$ definition ->isPublic ()) {
928- $ code .= ' ' .var_export ($ id, true )." => true, \n" ;
932+ $ code .= ' ' .$ this -> export ($ id )." => true, \n" ;
929933 }
930934 }
931935
@@ -962,7 +966,7 @@ private function addAliases()
962966 while (isset ($ aliases [$ id ])) {
963967 $ id = (string ) $ aliases [$ id ];
964968 }
965- $ code .= ' ' .var_export ($ alias, true ).' => ' .var_export ($ id, true ).", \n" ;
969+ $ code .= ' ' .$ this -> export ($ alias ).' => ' .$ this -> export ($ id ).", \n" ;
966970 }
967971
968972 return $ code ." ); \n" ;
@@ -1081,7 +1085,7 @@ private function exportParameters($parameters, $path = '', $indent = 12)
10811085 $ value = $ this ->export ($ value );
10821086 }
10831087
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 );
10851089 }
10861090
10871091 return sprintf ("array( \n%s \n%s) " , implode ("\n" , $ php ), str_repeat (' ' , $ indent - 4 ));
@@ -1283,7 +1287,7 @@ private function dumpValue($value, $interpolate = true)
12831287 $ factory = $ value ->getFactory ();
12841288
12851289 if (is_string ($ factory )) {
1286- return sprintf ('\\ %s(%s) ' , $ factory , implode (', ' , $ arguments ));
1290+ return sprintf ('%s(%s) ' , $ this -> dumpLiteralClass ( $ this -> dumpValue ( $ factory)) , implode (', ' , $ arguments ));
12871291 }
12881292
12891293 if (is_array ($ factory )) {
@@ -1358,7 +1362,7 @@ private function dumpValue($value, $interpolate = true)
13581362 private function dumpLiteralClass ($ class )
13591363 {
13601364 if (false !== strpos ($ class , '$ ' )) {
1361- throw new RuntimeException ( ' Cannot dump definitions which have a variable class name. ' );
1365+ return sprintf ( ' ${($_ = %s) && false ?: "_"} ' , $ class );
13621366 }
13631367 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 )) {
13641368 throw new RuntimeException (sprintf ('Cannot dump definition because of invalid class name (%s) ' , $ class ?: 'n/a ' ));
@@ -1529,9 +1533,9 @@ private function exportTargetDirs()
15291533 private function export ($ value )
15301534 {
15311535 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 ])).'. ' : '' ;
15331537 $ 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 )) : '' ;
15351539 $ dirname = '__DIR__ ' ;
15361540
15371541 if (0 < $ offset = 1 + $ this ->targetDirMaxMatches - count ($ matches )) {
@@ -1545,6 +1549,34 @@ private function export($value)
15451549 return $ dirname ;
15461550 }
15471551
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 ;
15491581 }
15501582}
0 commit comments