Skip to content

Commit

Permalink
ProxyManager Bridge
Browse files Browse the repository at this point in the history
  • Loading branch information
Ocramius authored and fabpot committed May 6, 2013
1 parent c8f95b5 commit 78e3710
Show file tree
Hide file tree
Showing 38 changed files with 1,445 additions and 29 deletions.
3 changes: 2 additions & 1 deletion composer.json
Expand Up @@ -66,7 +66,8 @@
"doctrine/orm": "~2.2,>=2.2.3",
"monolog/monolog": "~1.3",
"propel/propel1": "1.6.*",
"ircmaxell/password-compat": "1.0.*"
"ircmaxell/password-compat": "1.0.*",
"ocramius/proxy-manager": ">=0.3.1,<0.4-dev"
},
"autoload": {
"psr-0": { "Symfony\\": "src/" },
Expand Down
4 changes: 4 additions & 0 deletions src/Symfony/Bridge/ProxyManager/.gitignore
@@ -0,0 +1,4 @@
vendor/
composer.lock
phpunit.xml

7 changes: 7 additions & 0 deletions src/Symfony/Bridge/ProxyManager/CHANGELOG.md
@@ -0,0 +1,7 @@
CHANGELOG
=========

2.3.0
-----

* First introduction of `Symfony\Bridge\ProxyManager`
19 changes: 19 additions & 0 deletions src/Symfony/Bridge/ProxyManager/LICENSE
@@ -0,0 +1,19 @@
Copyright (c) 2004-2013 Fabien Potencier

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is furnished
to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
@@ -0,0 +1,62 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Bridge\ProxyManager\LazyProxy\Instantiator;

use ProxyManager\Configuration;
use ProxyManager\Factory\LazyLoadingValueHolderFactory;
use ProxyManager\GeneratorStrategy\EvaluatingGeneratorStrategy;
use ProxyManager\Proxy\LazyLoadingInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\DependencyInjection\Definition;
use Symfony\Component\DependencyInjection\LazyProxy\Instantiator\InstantiatorInterface;

/**
* Runtime lazy loading proxy generator
*
* @author Marco Pivetta <ocramius@gmail.com>
*/
class RuntimeInstantiator implements InstantiatorInterface
{
/**
* @var \ProxyManager\Factory\LazyLoadingValueHolderFactory
*/
private $factory;

/**
* Constructor
*/
public function __construct()
{
$config = new Configuration();

$config->setGeneratorStrategy(new EvaluatingGeneratorStrategy());

$this->factory = new LazyLoadingValueHolderFactory($config);
}

/**
* {@inheritDoc}
*/
public function instantiateProxy(ContainerInterface $container, Definition $definition, $id, $realInstantiator)
{
return $this->factory->createProxy(
$definition->getClass(),
function (& $wrappedInstance, LazyLoadingInterface $proxy) use ($realInstantiator) {
$proxy->setProxyInitializer(null);

$wrappedInstance = call_user_func($realInstantiator);

return true;
}
);
}
}
116 changes: 116 additions & 0 deletions src/Symfony/Bridge/ProxyManager/LazyProxy/PhpDumper/ProxyDumper.php
@@ -0,0 +1,116 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Bridge\ProxyManager\LazyProxy\PhpDumper;

use ProxyManager\Generator\ClassGenerator;
use ProxyManager\GeneratorStrategy\BaseGeneratorStrategy;
use ProxyManager\ProxyGenerator\LazyLoadingValueHolderGenerator;
use Symfony\Component\DependencyInjection\Container;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\DependencyInjection\Definition;
use Symfony\Component\DependencyInjection\LazyProxy\PhpDumper\DumperInterface;

/**
* Generates dumped php code of proxies via reflection
*
* @author Marco Pivetta <ocramius@gmail.com>
*/
class ProxyDumper implements DumperInterface
{
/**
* @var \ProxyManager\ProxyGenerator\LazyLoadingValueHolderGenerator
*/
private $proxyGenerator;

/**
* @var \ProxyManager\GeneratorStrategy\BaseGeneratorStrategy
*/
private $classGenerator;

/**
* Constructor
*/
public function __construct()
{
$this->proxyGenerator = new LazyLoadingValueHolderGenerator();
$this->classGenerator = new BaseGeneratorStrategy();
}

/**
* {@inheritDoc}
*/
public function isProxyCandidate(Definition $definition)
{
return $definition->isLazy()
&& ($class = $definition->getClass())
&& class_exists($class);
}

/**
* {@inheritDoc}
*/
public function getProxyFactoryCode(Definition $definition, $id)
{
$instantiation = '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';
$proxyClass = $this->getProxyClassName($definition);

return <<<EOF
if (\$lazyLoad) {
\$container = \$this;
$instantiation new $proxyClass(
function (& \$wrappedInstance, \ProxyManager\Proxy\LazyLoadingInterface \$proxy) use (\$container) {
\$proxy->setProxyInitializer(null);
\$wrappedInstance = \$container->$methodName(false);
return true;
}
);
}
EOF;
}

/**
* {@inheritDoc}
*/
public function getProxyCode(Definition $definition)
{
$generatedClass = new ClassGenerator($this->getProxyClassName($definition));

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

return $this->classGenerator->generate($generatedClass);
}

/**
* Produces the proxy class name for the given definition
*
* @param Definition $definition
*
* @return string
*/
private function getProxyClassName(Definition $definition)
{
return str_replace('\\', '', $definition->getClass()) . '_' . spl_object_hash($definition);
}
}
13 changes: 13 additions & 0 deletions src/Symfony/Bridge/ProxyManager/README.md
@@ -0,0 +1,13 @@
ProxyManager Bridge
===================

Provides integration for [ProxyManager](https://github.com/Ocramius/ProxyManager) with various Symfony2 components.

Resources
---------

You can run the unit tests with the following command:

$ cd path/to/Symfony/Bridge/ProxyManager/
$ composer.phar install --dev
$ phpunit
@@ -0,0 +1,56 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Bridge\ProxyManager\LazyProxy\Tests;

require_once __DIR__ . '/Fixtures/includes/foo.php';

use ProxyManager\Configuration;
use Symfony\Bridge\ProxyManager\LazyProxy\Instantiator\RuntimeInstantiator;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\ContainerInterface;

/**
* Integration tests for {@see \Symfony\Component\DependencyInjection\ContainerBuilder} combined
* with the ProxyManager bridge
*
* @author Marco Pivetta <ocramius@gmail.com>
*/
class ContainerBuilderTest extends \PHPUnit_Framework_TestCase
{
/**
* @covers Symfony\Component\DependencyInjection\ContainerBuilder::createService
*/
public function testCreateProxyServiceWithRuntimeInstantiator()
{
$builder = new ContainerBuilder();

$builder->setProxyInstantiator(new RuntimeInstantiator());

$builder->register('foo1', 'ProxyManagerBridgeFooClass')->setFile(__DIR__.'/Fixtures/includes/foo.php');
$builder->getDefinition('foo1')->setLazy(true);

/* @var $foo1 \ProxyManager\Proxy\LazyLoadingInterface|\ProxyManager\Proxy\ValueHolderInterface */
$foo1 = $builder->get('foo1');

$this->assertSame($foo1, $builder->get('foo1'), 'The same proxy is retrieved on multiple subsequent calls');
$this->assertInstanceOf('\ProxyManagerBridgeFooClass', $foo1);
$this->assertInstanceOf('\ProxyManager\Proxy\LazyLoadingInterface', $foo1);
$this->assertFalse($foo1->isProxyInitialized());

$foo1->initializeProxy();

$this->assertSame($foo1, $builder->get('foo1'), 'The same proxy is retrieved after initialization');
$this->assertTrue($foo1->isProxyInitialized());
$this->assertInstanceOf('\ProxyManagerBridgeFooClass', $foo1->getWrappedValueHolderValue());
$this->assertNotInstanceOf('\ProxyManager\Proxy\LazyLoadingInterface', $foo1->getWrappedValueHolderValue());
}
}
@@ -0,0 +1,72 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Bridge\ProxyManager\LazyProxy\Tests\Dumper;

use ProxyManager\Configuration;
use Symfony\Bridge\ProxyManager\LazyProxy\PhpDumper\ProxyDumper;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\DependencyInjection\Dumper\PhpDumper;

/**
* Integration tests for {@see \Symfony\Component\DependencyInjection\Dumper\PhpDumper} combined
* with the ProxyManager bridge
*
* @author Marco Pivetta <ocramius@gmail.com>
*/
class PhpDumperTest extends \PHPUnit_Framework_TestCase
{
public function testDumpContainerWithProxyService()
{
$container = new ContainerBuilder();

$container->register('foo', 'stdClass');
$container->getDefinition('foo')->setLazy(true);
$container->compile();

$dumper = new PhpDumper($container);

$dumper->setProxyDumper(new ProxyDumper());

$dumpedString = $dumper->dump();

$this->assertStringMatchesFormatFile(
__DIR__ . '/../Fixtures/php/lazy_service_structure.txt',
$dumpedString,
'->dump() does generate proxy lazy loading logic.'
);
}


/**
* Verifies that the generated container retrieves the same proxy instance on multiple subsequent requests
*/
public function testDumpContainerWithProxyServiceWillShareProxies()
{
require_once __DIR__ . '/../Fixtures/php/lazy_service.php';

$container = new \LazyServiceProjectServiceContainer();

/* @var $proxy \stdClass_c1d194250ee2e2b7d2eab8b8212368a8 */
$proxy = $container->get('foo');

$this->assertInstanceOf('stdClass_c1d194250ee2e2b7d2eab8b8212368a8', $proxy);
$this->assertSame($proxy, $container->get('foo'));

$this->assertFalse($proxy->isProxyInitialized());

$proxy->initializeProxy();

$this->assertTrue($proxy->isProxyInitialized());
$this->assertSame($proxy, $container->get('foo'));
}
}

5 comments on commit 78e3710

@lsmith77
Copy link
Contributor

Choose a reason for hiding this comment

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

@fabpot I guess this needs to be added to some script for the subtree split to work?

@fabpot
Copy link
Member

@fabpot fabpot commented on 78e3710 May 8, 2013

Choose a reason for hiding this comment

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

@lsmith77
Copy link
Contributor

Choose a reason for hiding this comment

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

merci!

@wouterj
Copy link
Member

@wouterj wouterj commented on 78e3710 May 8, 2013

Choose a reason for hiding this comment

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

@fabpot and a Composer package should be created

@fabpot
Copy link
Member

@fabpot fabpot commented on 78e3710 May 8, 2013

Choose a reason for hiding this comment

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

Done

Please sign in to comment.