Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
38 changed files
with
1,445 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
vendor/ | ||
composer.lock | ||
phpunit.xml | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
CHANGELOG | ||
========= | ||
|
||
2.3.0 | ||
----- | ||
|
||
* First introduction of `Symfony\Bridge\ProxyManager` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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. |
62 changes: 62 additions & 0 deletions
62
src/Symfony/Bridge/ProxyManager/LazyProxy/Instantiator/RuntimeInstantiator.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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
116
src/Symfony/Bridge/ProxyManager/LazyProxy/PhpDumper/ProxyDumper.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
56 changes: 56 additions & 0 deletions
56
src/Symfony/Bridge/ProxyManager/Tests/LazyProxy/ContainerBuilderTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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()); | ||
} | ||
} |
72 changes: 72 additions & 0 deletions
72
src/Symfony/Bridge/ProxyManager/Tests/LazyProxy/Dumper/PhpDumperTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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')); | ||
} | ||
} |
Oops, something went wrong.
78e3710
There was a problem hiding this comment.
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?
78e3710
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@lsmith77 done: https://github.com/symfony/ProxyManagerBridge
78e3710
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
merci!
78e3710
There was a problem hiding this comment.
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
78e3710
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done