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