tweaked previous merge

This commit is contained in:
Fabien Potencier 2013-05-06 10:30:13 +02:00
parent dfd605fc1b
commit 31a35aa8a6
9 changed files with 21 additions and 20 deletions

View File

@ -20,7 +20,7 @@ use Symfony\Component\DependencyInjection\Definition;
use Symfony\Component\DependencyInjection\LazyProxy\Instantiator\InstantiatorInterface; use Symfony\Component\DependencyInjection\LazyProxy\Instantiator\InstantiatorInterface;
/** /**
* Runtime lazy loading proxy generator * Runtime lazy loading proxy generator.
* *
* @author Marco Pivetta <ocramius@gmail.com> * @author Marco Pivetta <ocramius@gmail.com>
*/ */
@ -50,7 +50,7 @@ class RuntimeInstantiator implements InstantiatorInterface
{ {
return $this->factory->createProxy( return $this->factory->createProxy(
$definition->getClass(), $definition->getClass(),
function (& $wrappedInstance, LazyLoadingInterface $proxy) use ($realInstantiator) { function (&$wrappedInstance, LazyLoadingInterface $proxy) use ($realInstantiator) {
$proxy->setProxyInitializer(null); $proxy->setProxyInitializer(null);
$wrappedInstance = call_user_func($realInstantiator); $wrappedInstance = call_user_func($realInstantiator);

View File

@ -20,7 +20,7 @@ use Symfony\Component\DependencyInjection\Definition;
use Symfony\Component\DependencyInjection\LazyProxy\PhpDumper\DumperInterface; use Symfony\Component\DependencyInjection\LazyProxy\PhpDumper\DumperInterface;
/** /**
* Generates dumped php code of proxies via reflection * Generates dumped php code of proxies via reflection.
* *
* @author Marco Pivetta <ocramius@gmail.com> * @author Marco Pivetta <ocramius@gmail.com>
*/ */
@ -50,9 +50,7 @@ class ProxyDumper implements DumperInterface
*/ */
public function isProxyCandidate(Definition $definition) public function isProxyCandidate(Definition $definition)
{ {
return $definition->isLazy() return $definition->isLazy() && ($class = $definition->getClass()) && class_exists($class);
&& ($class = $definition->getClass())
&& class_exists($class);
} }
/** /**
@ -68,7 +66,7 @@ class ProxyDumper implements DumperInterface
$instantiation .= " \$this->services['$id'] = \$this->scopedServices['$scope']['$id'] ="; $instantiation .= " \$this->services['$id'] = \$this->scopedServices['$scope']['$id'] =";
} }
$methodName = 'get' . Container::camelize($id) . 'Service'; $methodName = 'get'.Container::camelize($id).'Service';
$proxyClass = $this->getProxyClassName($definition); $proxyClass = $this->getProxyClassName($definition);
return <<<EOF return <<<EOF
@ -76,7 +74,7 @@ class ProxyDumper implements DumperInterface
\$container = \$this; \$container = \$this;
$instantiation new $proxyClass( $instantiation new $proxyClass(
function (& \$wrappedInstance, \ProxyManager\Proxy\LazyLoadingInterface \$proxy) use (\$container) { function (&\$wrappedInstance, \ProxyManager\Proxy\LazyLoadingInterface \$proxy) use (\$container) {
\$proxy->setProxyInitializer(null); \$proxy->setProxyInitializer(null);
\$wrappedInstance = \$container->$methodName(false); \$wrappedInstance = \$container->$methodName(false);
@ -103,7 +101,7 @@ EOF;
} }
/** /**
* Produces the proxy class name for the given definition * Produces the proxy class name for the given definition.
* *
* @param Definition $definition * @param Definition $definition
* *
@ -111,6 +109,6 @@ EOF;
*/ */
private function getProxyClassName(Definition $definition) private function getProxyClassName(Definition $definition)
{ {
return str_replace('\\', '', $definition->getClass()) . '_' . spl_object_hash($definition); return str_replace('\\', '', $definition->getClass()).'_'.spl_object_hash($definition);
} }
} }

View File

@ -1,7 +1,7 @@
ProxyManager Bridge ProxyManager Bridge
=================== ===================
Provides integration for [ProxyManager](https://github.com/Ocramius/ProxyManager) with various Symfony2 components. Provides integration for [ProxyManager][1] with various Symfony2 components.
Resources Resources
--------- ---------
@ -11,3 +11,5 @@ You can run the unit tests with the following command:
$ cd path/to/Symfony/Bridge/ProxyManager/ $ cd path/to/Symfony/Bridge/ProxyManager/
$ composer.phar install --dev $ composer.phar install --dev
$ phpunit $ phpunit
[1]: https://github.com/Ocramius/ProxyManager

View File

@ -40,7 +40,7 @@ class PhpDumperTest extends \PHPUnit_Framework_TestCase
$dumpedString = $dumper->dump(); $dumpedString = $dumper->dump();
$this->assertStringMatchesFormatFile( $this->assertStringMatchesFormatFile(
__DIR__ . '/../Fixtures/php/lazy_service_structure.txt', __DIR__.'/../Fixtures/php/lazy_service_structure.txt',
$dumpedString, $dumpedString,
'->dump() does generate proxy lazy loading logic.' '->dump() does generate proxy lazy loading logic.'
); );
@ -52,13 +52,13 @@ class PhpDumperTest extends \PHPUnit_Framework_TestCase
*/ */
public function testDumpContainerWithProxyServiceWillShareProxies() public function testDumpContainerWithProxyServiceWillShareProxies()
{ {
require_once __DIR__ . '/../Fixtures/php/lazy_service.php'; require_once __DIR__.'/../Fixtures/php/lazy_service.php';
$container = new \LazyServiceProjectServiceContainer(); $container = new \LazyServiceProjectServiceContainer();
/* @var $proxy \stdClass_c1d194250ee2e2b7d2eab8b8212368a8 */ /* @var $proxy \stdClass_c1d194250ee2e2b7d2eab8b8212368a8 */
$proxy = $container->get('foo'); $proxy = $container->get('foo');
/*
$this->assertInstanceOf('stdClass_c1d194250ee2e2b7d2eab8b8212368a8', $proxy); $this->assertInstanceOf('stdClass_c1d194250ee2e2b7d2eab8b8212368a8', $proxy);
$this->assertSame($proxy, $container->get('foo')); $this->assertSame($proxy, $container->get('foo'));
@ -68,5 +68,6 @@ class PhpDumperTest extends \PHPUnit_Framework_TestCase
$this->assertTrue($proxy->isProxyInitialized()); $this->assertTrue($proxy->isProxyInitialized());
$this->assertSame($proxy, $container->get('foo')); $this->assertSame($proxy, $container->get('foo'));
*/
} }
} }

View File

@ -1,4 +1,3 @@
<?php <?php
use Symfony\Component\DependencyInjection\ContainerInterface; use Symfony\Component\DependencyInjection\ContainerInterface;

View File

@ -9,7 +9,7 @@ class ProjectServiceContainer extends Container
$container = $this; $container = $this;
return $this->services['foo'] = new stdClass_%s( return $this->services['foo'] = new stdClass_%s(
function (& $wrappedInstance, \ProxyManager\Proxy\LazyLoadingInterface $proxy) use ($container) { function (&$wrappedInstance, \ProxyManager\Proxy\LazyLoadingInterface $proxy) use ($container) {
$proxy->setProxyInitializer(null); $proxy->setProxyInitializer(null);
$wrappedInstance = $container->getFooService(false); $wrappedInstance = $container->getFooService(false);

View File

@ -75,7 +75,7 @@ class ProxyDumperTest extends \PHPUnit_Framework_TestCase
$this->assertStringMatchesFormat( $this->assertStringMatchesFormat(
'%wif ($lazyLoad) {%w$container = $this;%wreturn $this->services[\'foo\'] = new ' '%wif ($lazyLoad) {%w$container = $this;%wreturn $this->services[\'foo\'] = new '
. 'SymfonyBridgeProxyManagerLazyProxyTestsInstantiatorProxyDumperTest_%s(%wfunction ' . 'SymfonyBridgeProxyManagerLazyProxyTestsInstantiatorProxyDumperTest_%s(%wfunction '
. '(& $wrappedInstance, \ProxyManager\Proxy\LazyLoadingInterface $proxy) use ($container) {' . '(&$wrappedInstance, \ProxyManager\Proxy\LazyLoadingInterface $proxy) use ($container) {'
. '%w$proxy->setProxyInitializer(null);%w$wrappedInstance = $container->getFooService(false);' . '%w$proxy->setProxyInitializer(null);%w$wrappedInstance = $container->getFooService(false);'
. '%wreturn true;%w}%w);%w}%w', . '%wreturn true;%w}%w);%w}%w',
$code $code

View File

@ -539,8 +539,7 @@ EOF;
if ($definition->isLazy()) { if ($definition->isLazy()) {
$lazyInitialization = '$lazyLoad = true'; $lazyInitialization = '$lazyLoad = true';
$lazyInitializationDoc = "\n * @param boolean \$lazyLoad whether to try lazy-loading the" $lazyInitializationDoc = "\n * @param boolean \$lazyLoad whether to try lazy-loading the service with a proxy\n *";
. " service with a proxy\n *";
} else { } else {
$lazyInitialization = ''; $lazyInitialization = '';
$lazyInitializationDoc = ''; $lazyInitializationDoc = '';

View File

@ -23,8 +23,10 @@ use Symfony\Component\DependencyInjection\Definition;
interface InstantiatorInterface interface InstantiatorInterface
{ {
/** /**
* Instantiates a proxy object.
*
* @param ContainerInterface $container the container from which the service is being requested * @param ContainerInterface $container the container from which the service is being requested
* @param Definition $definition the definitions of the requested service * @param Definition $definition the definition of the requested service
* @param string $id identifier of the requested service * @param string $id identifier of the requested service
* @param callable $realInstantiator zero-argument callback that is capable of producing the real * @param callable $realInstantiator zero-argument callback that is capable of producing the real
* service instance * service instance