Skip to content

[DependencyInjection] Fix a limitation of the PhpDumper #18167

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,12 @@ public function getProxyFactoryCode(Definition $definition, $id)
$instantiation .= " \$this->services['$id'] =";
}

$methodName = 'get'.Container::camelize($id).'Service';
if (func_num_args() >= 3) {
$methodName = func_get_arg(2);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this could be written by adding $methodName = null in the signature of the method, and checking for null.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hmm, actually no. It would break BC for people extending the class. so forget it.

} else {
@trigger_error(sprintf('You must use the third argument of %s to define the method to call to construct your service since version 3.1, not using it won\'t be supported in 4.0.', __METHOD__), E_USER_DEPRECATED);
$methodName = 'get'.Container::camelize($id).'Service';
}
$proxyClass = $this->getProxyClassName($definition);

$generatedClass = $this->generateProxyClass($definition);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,27 @@ public function testGetProxyCode()
);
}

public function testGetProxyFactoryCodeWithCustomMethod()
{
$definition = new Definition(__CLASS__);

$definition->setLazy(true);

$code = $this->dumper->getProxyFactoryCode($definition, 'foo', 'getFoo2Service');

$this->assertStringMatchesFormat(
'%wif ($lazyLoad) {%wreturn $this->services[\'foo\'] =%s'
.'SymfonyBridgeProxyManagerTestsLazyProxyPhpDumperProxyDumperTest_%s(%wfunction '
.'(&$wrappedInstance, \ProxyManager\Proxy\LazyLoadingInterface $proxy) {'
.'%w$wrappedInstance = $this->getFoo2Service(false);%w$proxy->setProxyInitializer(null);'
.'%wreturn true;%w}%w);%w}%w',
$code
);
}

/**
* @group legacy
*/
public function testGetProxyFactoryCode()
{
$definition = new Definition(__CLASS__);
Expand Down
50 changes: 43 additions & 7 deletions src/Symfony/Component/DependencyInjection/Dumper/PhpDumper.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ class PhpDumper extends Dumper
private $targetDirRegex;
private $targetDirMaxMatches;
private $docStar;
private $serviceIdToMethodNameMap;
private $usedMethodNames;

/**
* @var \Symfony\Component\DependencyInjection\LazyProxy\PhpDumper\DumperInterface
Expand Down Expand Up @@ -106,6 +108,9 @@ public function dump(array $options = array())
'namespace' => '',
'debug' => true,
), $options);

$this->initializeMethodNamesMap($options['base_class']);

$this->docStar = $options['debug'] ? '*' : '';

if (!empty($options['file']) && is_dir($dir = dirname($options['file']))) {
Expand Down Expand Up @@ -625,19 +630,20 @@ private function addService($id, $definition)
// with proxies, for 5.3.3 compatibility, the getter must be public to be accessible to the initializer
$isProxyCandidate = $this->getProxyDumper()->isProxyCandidate($definition);
$visibility = $isProxyCandidate ? 'public' : 'protected';
$methodName = $this->generateMethodName($id);
$code = <<<EOF

/*{$this->docStar}
* Gets the '$id' service.$doc
*$lazyInitializationDoc
* $return
*/
{$visibility} function get{$this->camelize($id)}Service($lazyInitialization)
{$visibility} function {$methodName}($lazyInitialization)
{

EOF;

$code .= $isProxyCandidate ? $this->getProxyDumper()->getProxyFactoryCode($definition, $id) : '';
$code .= $isProxyCandidate ? $this->getProxyDumper()->getProxyFactoryCode($definition, $id, $methodName) : '';

if ($definition->isSynthetic()) {
$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);
Expand Down Expand Up @@ -864,7 +870,7 @@ private function addMethodMap()
$code = " \$this->methodMap = array(\n";
ksort($definitions);
foreach ($definitions as $id => $definition) {
$code .= ' '.var_export($id, true).' => '.var_export('get'.$this->camelize($id).'Service', true).",\n";
$code .= ' '.var_export($id, true).' => '.var_export($this->generateMethodName($id), true).",\n";
}

return $code." );\n";
Expand Down Expand Up @@ -1338,6 +1344,25 @@ private function getServiceCall($id, Reference $reference = null)
}
}

/**
* Initializes the method names map to avoid conflicts with the Container methods.
*
* @param string $class the container base class
*/
private function initializeMethodNamesMap($class)
{
$this->serviceIdToMethodNameMap = array();
$this->usedMethodNames = array();

try {
$reflectionClass = new \ReflectionClass($class);
foreach ($reflectionClass->getMethods() as $method) {
$this->usedMethodNames[strtolower($method->getName())] = true;
}
} catch (\ReflectionException $e) {
}
}

/**
* Convert a service id to a valid PHP method name.
*
Expand All @@ -1347,15 +1372,26 @@ private function getServiceCall($id, Reference $reference = null)
*
* @throws InvalidArgumentException
*/
private function camelize($id)
private function generateMethodName($id)
{
if (isset($this->serviceIdToMethodNameMap[$id])) {
return $this->serviceIdToMethodNameMap[$id];
}

$name = Container::camelize($id);
$name = preg_replace('/[^a-zA-Z0-9_\x7f-\xff]/', '', $name);
$methodName = 'get'.$name.'Service';
$suffix = 1;

if (!preg_match('/^[a-zA-Z0-9_\x7f-\xff]+$/', $name)) {
throw new InvalidArgumentException(sprintf('Service id "%s" cannot be converted to a valid PHP method name.', $id));
while (isset($this->usedMethodNames[strtolower($methodName)])) {
++$suffix;
$methodName = 'get'.$name.$suffix.'Service';
}

return $name;
$this->serviceIdToMethodNameMap[$id] = $methodName;
$this->usedMethodNames[strtolower($methodName)] = true;

return $methodName;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,11 @@ public function isProxyCandidate(Definition $definition);
*
* @param Definition $definition
* @param string $id service identifier
* @param string $methodName the method name to get the service, will be added to the interface in 4.0.
*
* @return string
*/
public function getProxyFactoryCode(Definition $definition, $id);
public function getProxyFactoryCode(Definition $definition, $id/**, $methodName = null */);

/**
* Generates the code for the lazy proxy.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -132,16 +132,46 @@ public function testServicesWithAnonymousFactories()
$this->assertStringEqualsFile(self::$fixturesPath.'/php/services19.php', $dumper->dump(), '->dump() dumps services with anonymous factories');
}

/**
* @expectedException \InvalidArgumentException
* @expectedExceptionMessage Service id "bar$" cannot be converted to a valid PHP method name.
*/
public function testAddServiceInvalidServiceId()
public function testAddServiceIdWithUnsupportedCharacters()
{
$class = 'Symfony_DI_PhpDumper_Test_Unsupported_Characters';
$container = new ContainerBuilder();
$container->register('bar$', 'FooClass');
$container->register('bar$!', 'FooClass');
$dumper = new PhpDumper($container);
$dumper->dump();
eval('?>'.$dumper->dump(array('class' => $class)));

$this->assertTrue(method_exists($class, 'getBarService'));
$this->assertTrue(method_exists($class, 'getBar2Service'));
}

public function testConflictingServiceIds()
{
$class = 'Symfony_DI_PhpDumper_Test_Conflicting_Service_Ids';
$container = new ContainerBuilder();
$container->register('foo_bar', 'FooClass');
$container->register('foobar', 'FooClass');
$dumper = new PhpDumper($container);
eval('?>'.$dumper->dump(array('class' => $class)));

$this->assertTrue(method_exists($class, 'getFooBarService'));
$this->assertTrue(method_exists($class, 'getFoobar2Service'));
}

public function testConflictingMethodsWithParent()
{
$class = 'Symfony_DI_PhpDumper_Test_Conflicting_Method_With_Parent';
$container = new ContainerBuilder();
$container->register('bar', 'FooClass');
$container->register('foo_bar', 'FooClass');
$dumper = new PhpDumper($container);
eval('?>'.$dumper->dump(array(
'class' => $class,
'base_class' => 'Symfony\Component\DependencyInjection\Tests\Fixtures\containers\CustomContainer',
)));

$this->assertTrue(method_exists($class, 'getBar2Service'));
$this->assertTrue(method_exists($class, 'getFoobar2Service'));
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

namespace Symfony\Component\DependencyInjection\Tests\Fixtures\containers;

use Symfony\Component\DependencyInjection\Container;
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBag;

class CustomContainer extends Container
{
public function getBarService()
{
}

public function getFoobarService()
{
}
}