@@ -58,6 +58,8 @@ class PhpDumper extends Dumper
58
58
private $ targetDirRegex ;
59
59
private $ targetDirMaxMatches ;
60
60
private $ docStar ;
61
+ private $ serviceIdToMethodNameMap ;
62
+ private $ usedMethodNames ;
61
63
62
64
/**
63
65
* @var \Symfony\Component\DependencyInjection\LazyProxy\PhpDumper\DumperInterface
@@ -106,6 +108,9 @@ public function dump(array $options = array())
106
108
'namespace ' => '' ,
107
109
'debug ' => true ,
108
110
), $ options );
111
+
112
+ $ this ->initializeMethodNamesMap ($ options ['base_class ' ]);
113
+
109
114
$ this ->docStar = $ options ['debug ' ] ? '* ' : '' ;
110
115
111
116
if (!empty ($ options ['file ' ]) && is_dir ($ dir = dirname ($ options ['file ' ]))) {
@@ -625,19 +630,20 @@ private function addService($id, $definition)
625
630
// with proxies, for 5.3.3 compatibility, the getter must be public to be accessible to the initializer
626
631
$ isProxyCandidate = $ this ->getProxyDumper ()->isProxyCandidate ($ definition );
627
632
$ visibility = $ isProxyCandidate ? 'public ' : 'protected ' ;
633
+ $ methodName = $ this ->generateMethodName ($ id );
628
634
$ code = <<<EOF
629
635
630
636
/* {$ this ->docStar }
631
637
* Gets the ' $ id' service. $ doc
632
638
* $ lazyInitializationDoc
633
639
* $ return
634
640
*/
635
- {$ visibility } function get { $ this -> camelize ( $ id )} Service ($ lazyInitialization)
641
+ {$ visibility } function { $ methodName } ( $ lazyInitialization)
636
642
{
637
643
638
644
EOF ;
639
645
640
- $ code .= $ isProxyCandidate ? $ this ->getProxyDumper ()->getProxyFactoryCode ($ definition , $ id ) : '' ;
646
+ $ code .= $ isProxyCandidate ? $ this ->getProxyDumper ()->getProxyFactoryCode ($ definition , $ id, $ methodName ) : '' ;
641
647
642
648
if ($ definition ->isSynthetic ()) {
643
649
$ 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 );
@@ -864,7 +870,7 @@ private function addMethodMap()
864
870
$ code = " \$this->methodMap = array( \n" ;
865
871
ksort ($ definitions );
866
872
foreach ($ definitions as $ id => $ definition ) {
867
- $ code .= ' ' .var_export ($ id , true ).' => ' .var_export (' get ' . $ this ->camelize ($ id ). ' Service ' , true ).", \n" ;
873
+ $ code .= ' ' .var_export ($ id , true ).' => ' .var_export ($ this ->generateMethodName ($ id ), true ).", \n" ;
868
874
}
869
875
870
876
return $ code ." ); \n" ;
@@ -1338,6 +1344,25 @@ private function getServiceCall($id, Reference $reference = null)
1338
1344
}
1339
1345
}
1340
1346
1347
+ /**
1348
+ * Initializes the method names map to avoid conflicts with the Container methods.
1349
+ *
1350
+ * @param string $class the container base class
1351
+ */
1352
+ private function initializeMethodNamesMap ($ class )
1353
+ {
1354
+ $ this ->serviceIdToMethodNameMap = array ();
1355
+ $ this ->usedMethodNames = array ();
1356
+
1357
+ try {
1358
+ $ reflectionClass = new \ReflectionClass ($ class );
1359
+ foreach ($ reflectionClass ->getMethods () as $ method ) {
1360
+ $ this ->usedMethodNames [strtolower ($ method ->getName ())] = true ;
1361
+ }
1362
+ } catch (\ReflectionException $ e ) {
1363
+ }
1364
+ }
1365
+
1341
1366
/**
1342
1367
* Convert a service id to a valid PHP method name.
1343
1368
*
@@ -1347,15 +1372,26 @@ private function getServiceCall($id, Reference $reference = null)
1347
1372
*
1348
1373
* @throws InvalidArgumentException
1349
1374
*/
1350
- private function camelize ($ id )
1375
+ private function generateMethodName ($ id )
1351
1376
{
1377
+ if (isset ($ this ->serviceIdToMethodNameMap [$ id ])) {
1378
+ return $ this ->serviceIdToMethodNameMap [$ id ];
1379
+ }
1380
+
1352
1381
$ name = Container::camelize ($ id );
1382
+ $ name = preg_replace ('/[^a-zA-Z0-9_\x7f-\xff]/ ' , '' , $ name );
1383
+ $ methodName = 'get ' .$ name .'Service ' ;
1384
+ $ suffix = 1 ;
1353
1385
1354
- if (!preg_match ('/^[a-zA-Z0-9_\x7f-\xff]+$/ ' , $ name )) {
1355
- throw new InvalidArgumentException (sprintf ('Service id "%s" cannot be converted to a valid PHP method name. ' , $ id ));
1386
+ while (isset ($ this ->usedMethodNames [strtolower ($ methodName )])) {
1387
+ ++$ suffix ;
1388
+ $ methodName = 'get ' .$ name .$ suffix .'Service ' ;
1356
1389
}
1357
1390
1358
- return $ name ;
1391
+ $ this ->serviceIdToMethodNameMap [$ id ] = $ methodName ;
1392
+ $ this ->usedMethodNames [strtolower ($ methodName )] = true ;
1393
+
1394
+ return $ methodName ;
1359
1395
}
1360
1396
1361
1397
/**
0 commit comments