@@ -759,6 +759,10 @@ private function addNewInstance($id, Definition $definition, $return, $instantia
759
759
if (null !== $ definition ->getFactory ()) {
760
760
$ callable = $ definition ->getFactory ();
761
761
if (is_array ($ callable )) {
762
+ if (!preg_match ('/^[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*$/ ' , $ callable [1 ])) {
763
+ throw new RuntimeException (sprintf ('Cannot dump definition because of invalid factory method (%s) ' , $ callable [1 ] ?: 'n/a ' ));
764
+ }
765
+
762
766
if ($ callable [0 ] instanceof Reference
763
767
|| ($ callable [0 ] instanceof Definition && $ this ->definitionVariables ->contains ($ callable [0 ]))) {
764
768
return sprintf (" $ return {$ instantiation }%s->%s(%s); \n" , $ this ->dumpValue ($ callable [0 ]), $ callable [1 ], $ arguments ? implode (', ' , $ arguments ) : '' );
@@ -1310,8 +1314,12 @@ private function dumpValue($value, $interpolate = true)
1310
1314
}
1311
1315
1312
1316
if (is_array ($ factory )) {
1317
+ if (!preg_match ('/^[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*$/ ' , $ factory [1 ])) {
1318
+ throw new RuntimeException (sprintf ('Cannot dump definition because of invalid factory method (%s) ' , $ factory [1 ] ?: 'n/a ' ));
1319
+ }
1320
+
1313
1321
if (is_string ($ factory [0 ])) {
1314
- return sprintf ('\\ %s::%s(%s) ' , $ factory [0 ], $ factory [1 ], implode (', ' , $ arguments ));
1322
+ return sprintf ('%s::%s(%s) ' , $ this -> dumpLiteralClass ( $ this -> dumpValue ( $ factory [0 ])) , $ factory [1 ], implode (', ' , $ arguments ));
1315
1323
}
1316
1324
1317
1325
if ($ factory [0 ] instanceof Definition) {
@@ -1342,12 +1350,8 @@ private function dumpValue($value, $interpolate = true)
1342
1350
if (null === $ class ) {
1343
1351
throw new RuntimeException ('Cannot dump definitions which have no class nor factory. ' );
1344
1352
}
1345
- $ class = $ this ->dumpValue ($ class );
1346
- if (false !== strpos ($ class , '$ ' )) {
1347
- throw new RuntimeException ('Cannot dump definitions which have a variable class name. ' );
1348
- }
1349
1353
1350
- return sprintf ('new \\ %s(%s) ' , substr ( str_replace ( '\\\\' , '\\' , $ class ), 1 , - 1 ), implode (', ' , $ arguments ));
1354
+ return sprintf ('new %s(%s) ' , $ this -> dumpLiteralClass ( $ this -> dumpValue ( $ class )), implode (', ' , $ arguments ));
1351
1355
} elseif ($ value instanceof Variable) {
1352
1356
return '$ ' .$ value ;
1353
1357
} elseif ($ value instanceof Reference) {
@@ -1388,9 +1392,18 @@ private function dumpValue($value, $interpolate = true)
1388
1392
* @param string $class
1389
1393
*
1390
1394
* @return string
1395
+ *
1396
+ * @throws RuntimeException
1391
1397
*/
1392
1398
private function dumpLiteralClass ($ class )
1393
1399
{
1400
+ if (false !== strpos ($ class , '$ ' )) {
1401
+ throw new RuntimeException ('Cannot dump definitions which have a variable class name. ' );
1402
+ }
1403
+ 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 )) {
1404
+ throw new RuntimeException (sprintf ('Cannot dump definition because of invalid class name (%s) ' , $ class ?: 'n/a ' ));
1405
+ }
1406
+
1394
1407
return '\\' .substr (str_replace ('\\\\' , '\\' , $ class ), 1 , -1 );
1395
1408
}
1396
1409
0 commit comments