@@ -211,6 +211,8 @@ public function dump(array $options = array())
211
211
array_pop ($ code );
212
212
$ code ["Container {$ hash }/ {$ options ['class ' ]}.php " ] = substr_replace ($ files [$ options ['class ' ].'.php ' ], "<?php \n\nnamespace Container {$ hash }; \n" , 0 , 6 );
213
213
$ namespaceLine = $ this ->namespace ? "\nnamespace {$ this ->namespace }; \n" : '' ;
214
+ $ time = time ();
215
+ $ id = hash ('crc32 ' , $ hash .$ time );
214
216
215
217
$ code [$ options ['class ' ].'.php ' ] = <<<EOF
216
218
<?php
@@ -229,7 +231,11 @@ public function dump(array $options = array())
229
231
\\class_alias(\\Container {$ hash }\\ {$ options ['class ' ]}::class, {$ options ['class ' ]}::class, false);
230
232
}
231
233
232
- return new \\Container {$ hash }\\ {$ options ['class ' ]}();
234
+ return new \\Container {$ hash }\\ {$ options ['class ' ]}(array(
235
+ 'container.build_hash' => ' $ hash',
236
+ 'container.build_id' => ' $ id',
237
+ 'container.build_time' => $ time,
238
+ ));
233
239
234
240
EOF ;
235
241
} else {
@@ -564,15 +570,15 @@ private function isTrivialInstance(Definition $definition)
564
570
}
565
571
566
572
foreach ($ definition ->getArguments () as $ arg ) {
567
- if (!$ arg ) {
573
+ if (!$ arg || $ arg instanceof Parameter ) {
568
574
continue ;
569
575
}
570
576
if (is_array ($ arg ) && 3 >= count ($ arg )) {
571
577
foreach ($ arg as $ k => $ v ) {
572
578
if ($ this ->dumpValue ($ k ) !== $ this ->dumpValue ($ k , false )) {
573
579
return false ;
574
580
}
575
- if (!$ v ) {
581
+ if (!$ v || $ v instanceof Parameter ) {
576
582
continue ;
577
583
}
578
584
if ($ v instanceof Reference && $ this ->container ->has ($ id = (string ) $ v ) && $ this ->container ->findDefinition ($ id )->isSynthetic ()) {
@@ -892,10 +898,10 @@ private function addNewInstance(Definition $definition, $return, $instantiation,
892
898
}
893
899
894
900
if (0 === strpos ($ class , 'new ' )) {
895
- return $ return .sprintf ("(%s)->%s(%s); \n" , $ this -> dumpValue ( $ callable [ 0 ]) , $ callable [1 ], $ arguments ? implode (', ' , $ arguments ) : '' );
901
+ return $ return .sprintf ("(%s)->%s(%s); \n" , $ class , $ callable [1 ], $ arguments ? implode (', ' , $ arguments ) : '' );
896
902
}
897
903
898
- return $ return .sprintf ("\\call_user_func(array(%s, '%s')%s); \n" , $ this -> dumpValue ( $ callable [ 0 ]) , $ callable [1 ], $ arguments ? ', ' .implode (', ' , $ arguments ) : '' );
904
+ return $ return .sprintf ("\\call_user_func(array(%s, '%s')%s); \n" , $ class , $ callable [1 ], $ arguments ? ', ' .implode (', ' , $ arguments ) : '' );
899
905
}
900
906
901
907
return $ return .sprintf ("%s(%s); \n" , $ this ->dumpLiteralClass ($ this ->dumpValue ($ callable )), $ arguments ? implode (', ' , $ arguments ) : '' );
@@ -957,6 +963,11 @@ public function __construct()
957
963
958
964
EOF ;
959
965
}
966
+ if ($ this ->asFiles ) {
967
+ $ code = str_replace ('$parameters ' , "\$buildParameters; \n private \$parameters " , $ code );
968
+ $ code = str_replace ('__construct() ' , '__construct(array $buildParameters = array()) ' , $ code );
969
+ $ code .= " \$this->buildParameters = \$buildParameters; \n" ;
970
+ }
960
971
961
972
if ($ this ->container ->isCompiled ()) {
962
973
if ($ this ->container ->getParameterBag ()->all ()) {
@@ -1283,6 +1294,9 @@ private function addDefaultParametersMethod()
1283
1294
1284
1295
public function getParameter($name)
1285
1296
{
1297
+ if (isset($this->buildParameters[$name])) {
1298
+ return $this->buildParameters[$name];
1299
+ }
1286
1300
if (!(isset($this->parameters[$name]) || isset($this->loadedDynamicParameters[$name]) || array_key_exists($name, $this->parameters))) {
1287
1301
$name = $this->normalizeParameterName($name);
1288
1302
@@ -1299,6 +1313,9 @@ public function getParameter($name)
1299
1313
1300
1314
public function hasParameter($name)
1301
1315
{
1316
+ if (isset($this->buildParameters[$name])) {
1317
+ return true;
1318
+ }
1302
1319
$name = $this->normalizeParameterName($name);
1303
1320
1304
1321
return isset($this->parameters[$name]) || isset($this->loadedDynamicParameters[$name]) || array_key_exists($name, $this->parameters);
@@ -1316,13 +1333,19 @@ public function getParameterBag()
1316
1333
foreach ($this->loadedDynamicParameters as $name => $loaded) {
1317
1334
$parameters[$name] = $loaded ? $this->dynamicParameters[$name] : $this->getDynamicParameter($name);
1318
1335
}
1336
+ foreach ($this->buildParameters as $name => $value) {
1337
+ $parameters[$name] = $value;
1338
+ }
1319
1339
$this->parameterBag = new FrozenParameterBag($parameters);
1320
1340
}
1321
1341
1322
1342
return $this->parameterBag;
1323
1343
}
1324
1344
1325
1345
EOF;
1346
+ if (!$ this ->asFiles ) {
1347
+ $ code = preg_replace ('/^.*buildParameters.*\n.*\n.*\n/m ' , '' , $ code );
1348
+ }
1326
1349
1327
1350
if ($ dynamicPhp ) {
1328
1351
$ loadedDynamicParameters = $ this ->exportParameters (array_combine (array_keys ($ dynamicPhp ), array_fill (0 , count ($ dynamicPhp ), false )), '' , 8 );
@@ -1717,16 +1740,21 @@ private function dumpValue($value, $interpolate = true)
1717
1740
throw new RuntimeException (sprintf ('Cannot dump definition because of invalid factory method (%s) ' , $ factory [1 ] ?: 'n/a ' ));
1718
1741
}
1719
1742
1743
+ $ class = $ this ->dumpValue ($ factory [0 ]);
1720
1744
if (is_string ($ factory [0 ])) {
1721
- return sprintf ('%s::%s(%s) ' , $ this ->dumpLiteralClass ($ this -> dumpValue ( $ factory [ 0 ]) ), $ factory [1 ], implode (', ' , $ arguments ));
1745
+ return sprintf ('%s::%s(%s) ' , $ this ->dumpLiteralClass ($ class ), $ factory [1 ], implode (', ' , $ arguments ));
1722
1746
}
1723
1747
1724
1748
if ($ factory [0 ] instanceof Definition) {
1725
- return sprintf ("\\call_user_func(array(%s, '%s')%s) " , $ this ->dumpValue ($ factory [0 ]), $ factory [1 ], count ($ arguments ) > 0 ? ', ' .implode (', ' , $ arguments ) : '' );
1749
+ if (0 === strpos ($ class , 'new ' )) {
1750
+ return sprintf ('(%s)->%s(%s) ' , $ class , $ factory [1 ], implode (', ' , $ arguments ));
1751
+ }
1752
+
1753
+ return sprintf ("\\call_user_func(array(%s, '%s')%s) " , $ class , $ factory [1 ], count ($ arguments ) > 0 ? ', ' .implode (', ' , $ arguments ) : '' );
1726
1754
}
1727
1755
1728
1756
if ($ factory [0 ] instanceof Reference) {
1729
- return sprintf ('%s->%s(%s) ' , $ this -> dumpValue ( $ factory [ 0 ]) , $ factory [1 ], implode (', ' , $ arguments ));
1757
+ return sprintf ('%s->%s(%s) ' , $ class , $ factory [1 ], implode (', ' , $ arguments ));
1730
1758
}
1731
1759
}
1732
1760
0 commit comments