Skip to content

Commit

Permalink
Fixing shared proxies into the container
Browse files Browse the repository at this point in the history
  • Loading branch information
Ocramius committed Apr 30, 2013
1 parent 468e92e commit 4ecd5ad
Showing 1 changed file with 31 additions and 11 deletions.
42 changes: 31 additions & 11 deletions src/Symfony/Component/DependencyInjection/Dumper/PhpDumper.php
Expand Up @@ -164,15 +164,16 @@ private function addServiceLocalTempVariables($cId, $definition)
*/
private function addProxyLoading($id, Definition $definition)
{
if (!($definition->isLazy() && $definition->getClass())) {
if (!$this->isProxyCandidate($definition)) {
return '';
}

$class = $this->dumpValue($definition->getClass());
$instantiation = 'return';

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)) {
// provided class name is not valid
return '';
if (ContainerInterface::SCOPE_CONTAINER === $definition->getScope()) {
$instantiation .= " \$this->services['$id'] =";
} elseif (ContainerInterface::SCOPE_PROTOTYPE !== $scope = $definition->getScope()) {
$instantiation .= " \$this->services['$id'] = \$this->scopedServices['$scope']['$id'] =";
}

$methodName = 'get' . Container::camelize($id) . 'Service';
Expand All @@ -182,7 +183,7 @@ private function addProxyLoading($id, Definition $definition)
if (\$lazyLoad) {
\$container = \$this;
return new $proxyClass(
$instantiation new $proxyClass(
function (& \$wrappedInstance, \ProxyManager\Proxy\LazyLoadingInterface \$proxy) use (\$container) {
\$proxy->setProxyInitializer(null);
Expand Down Expand Up @@ -222,13 +223,13 @@ function (Definition $definition) {

/* @var $proxyDefinitions Definition[] */
foreach ($proxyDefinitions as $definition) {
$phpClass = new ClassGenerator(
$generatedClass = new ClassGenerator(
str_replace('\\', '', $definition->getClass()) . '_' . md5(spl_object_hash($definition))
);

$proxyGenerator->generate(new ReflectionClass($definition->getClass()), $phpClass);
$proxyGenerator->generate(new ReflectionClass($definition->getClass()), $generatedClass);

$code .= "\n" . $classGenerator->generate($phpClass);
$code .= "\n" . $classGenerator->generate($generatedClass);
}

return $code;
Expand Down Expand Up @@ -368,9 +369,10 @@ private function addServiceInstance($id, $definition)
$simple = $this->isSimpleInstance($id, $definition);

$instantiation = '';
if (ContainerInterface::SCOPE_CONTAINER === $definition->getScope()) {

if (!$this->isProxyCandidate($definition) && ContainerInterface::SCOPE_CONTAINER === $definition->getScope()) {
$instantiation = "\$this->services['$id'] = ".($simple ? '' : '$instance');
} elseif (ContainerInterface::SCOPE_PROTOTYPE !== $scope = $definition->getScope()) {
} elseif (!$this->isProxyCandidate($definition) && ContainerInterface::SCOPE_PROTOTYPE !== $scope = $definition->getScope()) {
$instantiation = "\$this->services['$id'] = \$this->scopedServices['$scope']['$id'] = ".($simple ? '' : '$instance');
} elseif (!$simple) {
$instantiation = '$instance';
Expand Down Expand Up @@ -1274,6 +1276,24 @@ private function getServiceCall($id, Reference $reference = null)
}
}

/**
* Tells if the given definitions are to be used for proxying
*
* @param Definition $definition
*
* @return bool
*/
private function isProxyCandidate(Definition $definition)
{
if (!($definition->isLazy() && $definition->getClass())) {
return false;
}

$class = $this->dumpValue($definition->getClass());

return (boolean) 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);
}

/**
* Returns the next name to use
*
Expand Down

0 comments on commit 4ecd5ad

Please sign in to comment.