@@ -51,6 +51,8 @@ class PhpDumper extends Dumper
51
51
private $ referenceVariables ;
52
52
private $ variableCount ;
53
53
private $ reservedVariables = array ('instance ' , 'class ' );
54
+ private $ targetDirRx ;
55
+ private $ targetDirMaxMatches ;
54
56
55
57
/**
56
58
* @var \Symfony\Component\DependencyInjection\LazyProxy\PhpDumper\DumperInterface
@@ -95,11 +97,32 @@ public function setProxyDumper(ProxyDumper $proxyDumper)
95
97
*/
96
98
public function dump (array $ options = array ())
97
99
{
100
+ $ this ->targetDirRx = null ;
101
+ $ this ->targetDirMaxMatches = 0 ;
102
+
98
103
$ options = array_merge (array (
99
104
'class ' => 'ProjectServiceContainer ' ,
100
105
'base_class ' => 'Container ' ,
101
106
), $ options );
102
107
108
+ if (isset ($ options ['file ' ][0 ]) && '/ ' === $ options ['file ' ][0 ]) {
109
+ if (is_dir ($ dir = dirname ($ options ['file ' ]))) {
110
+ $ dir = explode ('/ ' , realpath ($ dir ));
111
+ $ i = count ($ dir );
112
+ if (3 <= $ i ) {
113
+ $ rx = '' ;
114
+ while (2 < --$ i ) {
115
+ $ rx = sprintf ('(/%s%s)? ' , preg_quote ($ dir [$ i ], '# ' ), $ rx );
116
+ ++$ this ->targetDirMaxMatches ;
117
+ }
118
+ do {
119
+ $ rx = preg_quote ('/ ' .$ dir [$ i ], '# ' ).$ rx ;
120
+ } while (0 < --$ i );
121
+ $ this ->targetDirRx = '# ' .preg_quote ($ dir [0 ], '# ' ).$ rx .'# ' ;
122
+ }
123
+ }
124
+ }
125
+
103
126
$ code = $ this ->startClass ($ options ['class ' ], $ options ['base_class ' ]);
104
127
105
128
if ($ this ->container ->isFrozen ()) {
@@ -841,7 +864,7 @@ private function addMethodMap()
841
864
$ code = " \$this->methodMap = array( \n" ;
842
865
ksort ($ definitions );
843
866
foreach ($ definitions as $ id => $ definition ) {
844
- $ code .= ' ' .var_export ($ id, true ).' => ' .var_export ('get ' .$ this ->camelize ($ id ).'Service ' , true ).", \n" ;
867
+ $ code .= ' ' .$ this -> export ($ id ).' => ' .$ this -> export ('get ' .$ this ->camelize ($ id ).'Service ' ).", \n" ;
845
868
}
846
869
847
870
return $ code ." ); \n" ;
@@ -869,7 +892,7 @@ private function addAliases()
869
892
while (isset ($ aliases [$ id ])) {
870
893
$ id = (string ) $ aliases [$ id ];
871
894
}
872
- $ code .= ' ' .var_export ($ alias, true ).' => ' .var_export ($ id, true ).", \n" ;
895
+ $ code .= ' ' .$ this -> export ($ alias ).' => ' .$ this -> export ($ id ).", \n" ;
873
896
}
874
897
875
898
return $ code ." ); \n" ;
@@ -979,10 +1002,10 @@ private function exportParameters($parameters, $path = '', $indent = 12)
979
1002
} elseif ($ value instanceof Reference) {
980
1003
throw new InvalidArgumentException (sprintf ('You cannot dump a container with parameters that contain references to other services (reference to service "%s" found in "%s"). ' , $ value , $ path .'/ ' .$ key ));
981
1004
} else {
982
- $ value = var_export ($ value, true );
1005
+ $ value = $ this -> export ($ value );
983
1006
}
984
1007
985
- $ php [
A935
] = sprintf ('%s%s => %s, ' , str_repeat (' ' , $ indent ), var_export ($ key, true ), $ value );
1008
+ $ php [] = sprintf ('%s%s => %s, ' , str_repeat (' ' , $ indent ), $ this -> export ($ key ), $ value );
986
1009
}
987
1010
988
1011
return sprintf ("array( \n%s \n%s) " , implode ("\n" , $ php ), str_repeat (' ' , $ indent - 4 ));
@@ -1214,14 +1237,14 @@ private function dumpValue($value, $interpolate = true)
1214
1237
return "'. " .$ that ->dumpParameter (strtolower ($ match [2 ])).".' " ;
1215
1238
};
1216
1239
1217
- $ code = str_replace ('%% ' , '% ' , preg_replace_callback ('/(?<!%)(%)([^%]+)\1/ ' , $ replaceParameters , var_export ($ value, true )));
1240
+ $ code = str_replace ('%% ' , '% ' , preg_replace_callback ('/(?<!%)(%)([^%]+)\1/ ' , $ replaceParameters , $ this -> export ($ value )));
1218
1241
1219
1242
return $ code ;
1220
1243
}
1221
1244
} elseif (is_object ($ value ) || is_resource ($ value )) {
1222
1245
throw new RuntimeException ('Unable to dump a service container if a parameter is an object or a resource. ' );
1223
1246
} else {
1224
- return var_export ($ value, true );
1247
+ return $ this -> export ($ value );
1225
1248
}
1226
1249
}
1227
1250
@@ -1323,4 +1346,24 @@ private function getNextVariableName()
1323
1346
return $ name ;
1324
1347
}
1325
1348
}
1349
+
1350
+ private function export ($ value )
1351
+ {
1352
+ if (null !== $ this ->targetDirRx && is_string ($ value ) && preg_match ($ this ->targetDirRx , $ value , $ matches , PREG_OFFSET_CAPTURE )) {
1353
+ $ prefix = $ matches [0 ][1 ] ? var_export (substr ($ value , 0 , $ matches [0 ][1 ]), true ).'. ' : '' ;
1354
+ $ suffix = $ matches [0 ][1 ] + strlen ($ matches [0 ][0 ]);
1355
+ $ suffix = isset ($ value [$ suffix ]) ? '. ' .var_export (substr ($ value , $ suffix ), true ) : '' ;
1356
+ $ dirname = '__DIR__ ' ;
1357
+ for ($ i = $ this ->targetDirMaxMatches - count ($ matches ); 0 <= $ i ; --$ i ) {
1358
+ $ dirname = sprintf ('dirname(%s) ' , $ dirname );
1359
+ }
1360
+ if ($ prefix || $ suffix ) {
1361
+ return sprintf ('(%s%s%s) ' , $ prefix , $ dirname , $ suffix );
1362
+ } else {
1363
+ return $ dirname ;
1364
+ }
1365
+ }
1366
+
1367
+ return var_export ($ value , true );
1368
+ }
1326
1369
}
0 commit comments