@@ -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
/**
@@ -383,7 +385,7 @@ private function addServiceInstance($id, $definition)
383
385
384
386
$ class = $ this ->dumpValue ($ class );
385
387
386
- 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 )) {
388
+ 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 )) {
387
389
throw new InvalidArgumentException (sprintf ('"%s" is not a valid class name for the "%s" service. ' , $ class , $ id ));
388
390
}
389
391
@@ -538,7 +540,7 @@ private function addServiceConfigurator($id, $definition, $variableName = 'insta
538
540
539
541
$ class = $ this ->dumpValue ($ callable [0 ]);
540
542
// If the class is a string we can optimize call_user_func away
541
- if (strpos ($ class , "' " ) === 0 ) {
543
+ if (0 === strpos ($ class , "' " ) && false === strpos ( $ class , ' $ ' ) ) {
542
544
return sprintf (" %s::%s( \$%s); \n" , $ this ->dumpLiteralClass ($ class ), $ callable [1 ], $ variableName );
543
545
}
544
546
@@ -571,6 +573,7 @@ private function addService($id, $definition)
571
573
if ($ definition ->isSynthetic ()) {
572
574
$ return [] = '@throws RuntimeException always since this service is expected to be injected dynamically ' ;
573
575
} elseif ($ class = $ definition ->getClass ()) {
576
+ $ class = $ this ->reverseEnvPlaceholders ($ class );
574
577
$ return [] = sprintf ('@return %s A %s instance ' , 0 === strpos ($ class , '% ' ) ? 'object ' : '\\' .ltrim ($ class , '\\' ), ltrim ($ class , '\\' ));
575
578
} elseif ($ definition ->getFactory ()) {
576
579
$ factory = $ definition ->getFactory ();
@@ -594,6 +597,7 @@ private function addService($id, $definition)
594
597
}
595
598
596
599
$ return = str_replace ("\n * \n" , "\n * \n" , implode ("\n * " , $ return ));
600
+ $ return = $ this ->reverseEnvPlaceholders ($ return );
597
601
598
602
$ doc = '' ;
599
603
if ($ definition ->isShared ()) {
@@ -653,7 +657,7 @@ private function addService($id, $definition)
653
657
$ 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 );
654
658
} else {
655
659
if ($ definition ->isDeprecated ()) {
656
- $ code .= sprintf (" @trigger_error(%s, E_USER_DEPRECATED); \n\n" , var_export ($ definition ->getDeprecationMessage ($ id ), true ));
660
+ $ code .= sprintf (" @trigger_error(%s, E_USER_DEPRECATED); \n\n" , $ this -> export ($ definition ->getDeprecationMessage ($ id )));
657
661
}
658
662
659
663
$ code .=
@@ -719,7 +723,7 @@ private function addNewInstance(Definition $definition, $return, $instantiation,
719
723
720
724
$ class = $ this ->dumpValue ($ callable [0 ]);
721
725
// If the class is a string we can optimize call_user_func away
722
- if (strpos ($ class , "' " ) === 0 ) {
726
+ if (0 === strpos ($ class , "' " ) && false === strpos ( $ class , ' $ ' ) ) {
723
727
if ("'' " === $ class ) {
724
728
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 ));
725
729
}
@@ -734,7 +738,7 @@ private function addNewInstance(Definition $definition, $return, $instantiation,
734
738
return sprintf (" $ return {$ instantiation }call_user_func(array(%s, '%s')%s); \n" , $ this ->dumpValue ($ callable [0 ]), $ callable [1 ], $ arguments ? ', ' .implode (', ' , $ arguments ) : '' );
735
739
}
736
740
737
- return sprintf (" $ return {$ instantiation }\\ %s(%s); \n" , $ callable , $ arguments ? implode (', ' , $ arguments ) : '' );
741
+ return sprintf (" $ return {$ instantiation }%s(%s); \n" , $ this -> dumpLiteralClass ( $ this -> dumpValue ( $ callable)) , $ arguments ? implode (', ' , $ arguments ) : '' );
738
742
}
739
743
740
744
if (false !== strpos ($ class , '$ ' )) {
@@ -883,7 +887,7 @@ private function addMethodMap()
883
887
$ code = " \$this->methodMap = array( \n" ;
884
888
ksort ($ definitions );
885
889
foreach ($ definitions as $ id => $ definition ) {
886
- $ code .= ' ' .var_export ($ id, true ).' => ' .var_export ($ this ->generateMethodName ($ id ), true ).", \n" ;
890
+ $ code .= ' ' .$ this -> export ($ id ).' => ' .$ this -> export ($ this ->generateMethodName ($ id )).", \n" ;
887
891
}
888
892
889
893
return $ code ." ); \n" ;
@@ -904,7 +908,7 @@ private function addPrivateServices()
904
908
ksort ($ definitions );
905
909
foreach ($ definitions as $ id => $ definition ) {
906
910
if (!$ definition ->isPublic ()) {
907
- $ code .= ' ' .var_export ($ id, true )." => true, \n" ;
911
+ $ code .= ' ' .$ this -> export ($ id )." => true, \n" ;
908
912
}
909
913
}
910
914
@@ -941,7 +945,7 @@ private function addAliases()
941
945
while (isset ($ aliases [$ id ])) {
942
946
$ id = (string ) $ aliases [$ id ];
943
947
}
944
- $ code .= ' ' .var_export ($ alias, true ).' => ' .var_export ($ id, true ).", \n" ;
948
+ $ code .= ' ' .$ this -> export ($ alias ).' => ' .$ this -> export ($ id ).", \n" ;
945
949
}
946
950
947
951
return $ code ." ); \n" ;
@@ -1060,7 +1064,7 @@ private function exportParameters($parameters, $path = '', $indent = 12)
1060
1064
$ value = $ this ->export ($ value );
1061
1065
}
1062
1066
1063
- $ php [] = sprintf ('%s%s => %s, ' , str_repeat (' ' , $ indent ), var_export ($ key, true ), $ value );
1067
+ $ php [] = sprintf ('%s%s => %s, ' , str_repeat (' ' , $ indent ), $ this -> export ($ key ), $ value );
1064
1068
}
1065
1069
1066
1070
return sprintf ("array( \n%s \n%s) " , implode ("\n" , $ php ), str_repeat (' ' , $ indent - 4 ));
@@ -1262,7 +1266,7 @@ private function dumpValue($value, $interpolate = true)
1262
1266
$ factory = $ value ->getFactory ();
1263
1267
1264
1268
if (is_string ($ factory )) {
1265
- return sprintf ('\\ %s(%s) ' , $ factory , implode (', ' , $ arguments ));
1269
+ return sprintf ('%s(%s) ' , $ this -> dumpLiteralClass ( $ this -> dumpValue ( $ factory)) , implode (', ' , $ arguments ));
1266
1270
}
1267
1271
1268
1272
if (is_array ($ factory )) {
@@ -1337,7 +1341,7 @@ private function dumpValue($value, $interpolate = true)
1337
1341
private function dumpLiteralClass ($ class )
1338
1342
{
1339
1343
if (false !== strpos ($ class , '$ ' )) {
1340
- throw new RuntimeException ( ' Cannot dump definitions which have a variable class name. ' );
1344
+ return sprintf ( ' ${($_ = %s) && false ?: "_"} ' , $ class );
1341
1345
}
1342
1346
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 )) {
1343
1347
throw new RuntimeException (sprintf ('Cannot dump definition because of invalid class name (%s) ' , $ class ?: 'n/a ' ));
@@ -1508,9 +1512,9 @@ private function exportTargetDirs()
1508
1512
private function export ($ value )
1509
1513
{
1510
1514
if (null !== $ this ->targetDirRegex && is_string ($ value ) && preg_match ($ this ->targetDirRegex , $ value , $ matches , PREG_OFFSET_CAPTURE )) {
1511
- $ prefix = $ matches [0 ][1 ] ? var_export (substr ($ value , 0 , $ matches [0 ][1 ]), true ).'. ' : '' ;
1515
+ $ prefix = $ matches [0 ][1 ] ? $ this -> doExport (substr ($ value , 0 , $ matches [0 ][1 ])).'. ' : '' ;
1512
1516
$ suffix = $ matches [0 ][1 ] + strlen ($ matches [0 ][0 ]);
1513
- $ suffix = isset ($ value [$ suffix ]) ? '. ' .var_export (substr ($ value , $ suffix ), true ) : '' ;
1517
+ $ suffix = isset ($ value [$ suffix ]) ? '. ' .$ this -> doExport (substr ($ value , $ suffix )) : '' ;
1514
1518
$ dirname = '__DIR__ ' ;
1515
1519
1516
1520
if (0 < $ offset = 1 + $ this ->targetDirMaxMatches - count ($ matches )) {
@@ -1524,6 +1528,34 @@ private function export($value)
1524
1528
return $ dirname ;
1525
1529
}
1526
1530
1527
- return var_export ($ value , true );
1531
+ return $ this ->doExport ($ value );
1532
+ }
1533
+
1534
+ private function doExport ($ value )
1535
+ {
1536
+ $ export = var_export ($ value , true );
1537
+
1538
+ if ("' " === $ export [0 ] && $ this ->envPlaceholders && false !== strpos ($ export , 'env_ ' )) {
1539
+ foreach ($ this ->envPlaceholders as $ env => $ placeholder ) {
1540
+ $ export = str_replace ($ placeholder , "'. \$this->getEnv(' $ env').' " , $ export );
1541
+ }
1542
+ if ("' " === $ export [1 ]) {
1543
+ $ export = substr ($ export , 3 );
1544
+ }
1545
+ if (".'' " === substr ($ export , -3 )) {
1546
+ $ export = substr ($ export , 0 , -3 );
1547
+ }
1548
+ }
1549
+
1550
+ return $ export ;
1551
+ }
1552
+
1553
+ private function reverseEnvPlaceholders ($ string )
1554
+ {
1555
+ foreach ($ this ->envPlaceholders as $ env => $ placeholder ) {
1556
+ $ string = str_replace ($ placeholder , "%env( $ env)% " , $ string );
1557
+ }
1558
+
1559
+ return $ string ;
1528
1560
}
1529
1561
}
0 commit comments