-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[DependencyInjection] Added closure service factories #12115
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
unkind
wants to merge
1
commit into
symfony:master
from
unkind:feature-closures-as-service-factories
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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,3 @@ | ||
vendor/ | ||
composer.lock | ||
phpunit.xml |
This file contains hidden or 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.6.0 | ||
----- | ||
|
||
* Added the `Symfony\Bridge\SuperClosure` bridge |
41 changes: 41 additions & 0 deletions
41
src/Symfony/Bridge/SuperClosure/ClosureDumper/SuperClosureDumper.php
This file contains hidden or 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,41 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <[email protected]> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Bridge\SuperClosure\ClosureDumper; | ||
|
||
use Jeremeamia\SuperClosure\ClosureParser; | ||
use Symfony\Component\DependencyInjection\Dumper\ClosureDumper\ClosureDumperInterface; | ||
use Symfony\Component\DependencyInjection\Exception\DumpingClosureException; | ||
|
||
/** | ||
* @author Nikita Konstantinov <[email protected]> | ||
*/ | ||
class SuperClosureDumper implements ClosureDumperInterface | ||
{ | ||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function dump(\Closure $closure) | ||
{ | ||
$parser = ClosureParser::fromClosure($closure); | ||
|
||
if ($parser->getUsedVariables()) { | ||
throw new DumpingClosureException($closure, 'Closure must not depend on context (use statement)'); | ||
} | ||
|
||
try { | ||
// Remove trailing ";" | ||
return substr($parser->getCode(), 0, -1); | ||
} catch (\Exception $e) { | ||
throw new DumpingClosureException($closure, $e->getMessage(), 0, $e); | ||
} | ||
} | ||
} |
This file contains hidden or 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-2014 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. |
129 changes: 129 additions & 0 deletions
129
src/Symfony/Bridge/SuperClosure/Tests/Fixtures/php/services_with_closure_factory.php
This file contains hidden or 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,129 @@ | ||
<?php | ||
|
||
use Symfony\Component\DependencyInjection\ContainerInterface; | ||
use Symfony\Component\DependencyInjection\Container; | ||
use Symfony\Component\DependencyInjection\Exception\InactiveScopeException; | ||
use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException; | ||
use Symfony\Component\DependencyInjection\Exception\LogicException; | ||
use Symfony\Component\DependencyInjection\Exception\RuntimeException; | ||
use Symfony\Component\DependencyInjection\ParameterBag\FrozenParameterBag; | ||
|
||
/** | ||
* ProjectServiceContainerWithClosures | ||
* | ||
* This class has been auto-generated | ||
* by the Symfony Dependency Injection Component. | ||
*/ | ||
class ProjectServiceContainerWithClosures extends Container | ||
{ | ||
private static $parameters = array( | ||
'baz' => 42, | ||
); | ||
|
||
/** | ||
* Constructor. | ||
*/ | ||
public function __construct() | ||
{ | ||
$this->services = | ||
$this->scopedServices = | ||
$this->scopeStacks = array(); | ||
|
||
$this->set('service_container', $this); | ||
|
||
$this->scopes = array(); | ||
$this->scopeChildren = array(); | ||
$this->methodMap = array( | ||
'bar' => 'getBarService', | ||
'foo' => 'getFooService', | ||
); | ||
|
||
$this->aliases = array(); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function compile() | ||
{ | ||
throw new LogicException('You cannot compile a dumped frozen container.'); | ||
} | ||
|
||
/** | ||
* Gets the 'bar' service. | ||
* | ||
* This service is shared. | ||
* This method always returns the same instance of the service. | ||
* | ||
* @return \stdClass A stdClass instance. | ||
*/ | ||
protected function getBarService() | ||
{ | ||
return $this->services['bar'] = call_user_func(function (\stdClass $foo) { | ||
$bar = clone $foo; | ||
$bar->bar = 'bar'; | ||
return $bar; | ||
}, $this->get('foo')); | ||
} | ||
|
||
/** | ||
* Gets the 'foo' service. | ||
* | ||
* This service is shared. | ||
* This method always returns the same instance of the service. | ||
* | ||
* @return \stdClass A stdClass instance. | ||
*/ | ||
protected function getFooService() | ||
{ | ||
return $this->services['foo'] = call_user_func(function ($baz) { | ||
$foo = new \stdClass(); | ||
$foo->foo = $baz; | ||
return $foo; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. add a blank line before There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I can't do it as it is code generated by SuperClosure library. |
||
}, 42); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function getParameter($name) | ||
{ | ||
$name = strtolower($name); | ||
|
||
if (!(isset(self::$parameters[$name]) || array_key_exists($name, self::$parameters))) { | ||
throw new InvalidArgumentException(sprintf('The parameter "%s" must be defined.', $name)); | ||
} | ||
|
||
return self::$parameters[$name]; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function hasParameter($name) | ||
{ | ||
$name = strtolower($name); | ||
|
||
return isset(self::$parameters[$name]) || array_key_exists($name, self::$parameters); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function setParameter($name, $value) | ||
{ | ||
throw new LogicException('Impossible to call set() on a frozen ParameterBag.'); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function getParameterBag() | ||
{ | ||
if (null === $this->parameterBag) { | ||
$this->parameterBag = new FrozenParameterBag(self::$parameters); | ||
} | ||
|
||
return $this->parameterBag; | ||
} | ||
} |
This file contains hidden or 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,76 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <[email protected]> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Bridge\SuperClosure\Tests; | ||
|
||
use Symfony\Bridge\SuperClosure\ClosureDumper\SuperClosureDumper; | ||
use Symfony\Component\DependencyInjection\ContainerBuilder; | ||
use Symfony\Component\DependencyInjection\Dumper\PhpDumper; | ||
use Symfony\Component\DependencyInjection\Reference; | ||
|
||
/** | ||
* Integration tests of {@see \Symfony\Component\DependencyInjection\Dumper\PhpDumper} and SuperClosureDumper | ||
* | ||
* @author Nikita Konstantinov <[email protected]> | ||
*/ | ||
class PhpDumperTest extends \PHPUnit_Framework_TestCase | ||
{ | ||
public function testThatPhpDumperCanDumpClosure() | ||
{ | ||
$container = new ContainerBuilder(); | ||
|
||
$container->setParameter('baz', 42); | ||
|
||
$container | ||
->register('foo', 'stdClass') | ||
->setFactory(function ($baz) { | ||
$foo = new \stdClass(); | ||
$foo->foo = $baz; | ||
|
||
return $foo; | ||
}) | ||
->addArgument('%baz%') | ||
; | ||
|
||
$container | ||
->register('bar', 'stdClass') | ||
->setFactory(function (\stdClass $foo) { | ||
$bar = clone $foo; | ||
$bar->bar = 'bar'; | ||
|
||
return $bar; | ||
}) | ||
->addArgument(new Reference('foo')) | ||
; | ||
|
||
$container->compile(); | ||
|
||
$dumper = new PhpDumper($container); | ||
$dumper->setClosureDumper(new SuperClosureDumper()); | ||
|
||
$options = array('class' => 'ProjectServiceContainerWithClosures'); | ||
|
||
$this->assertStringEqualsFile(__DIR__.'/Fixtures/php/services_with_closure_factory.php', $dumper->dump($options)); | ||
} | ||
|
||
public function testThatDumpedContainerWorks() | ||
{ | ||
require_once __DIR__.'/Fixtures/php/services_with_closure_factory.php'; | ||
|
||
$container = new \ProjectServiceContainerWithClosures(); | ||
|
||
$expectedBar = new \stdClass(); | ||
$expectedBar->foo = 42; | ||
$expectedBar->bar = 'bar'; | ||
|
||
$this->assertEquals($expectedBar, $container->get('bar')); | ||
} | ||
} |
50 changes: 50 additions & 0 deletions
50
src/Symfony/Bridge/SuperClosure/Tests/SuperClosureDumperTest.php
This file contains hidden or 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,50 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <[email protected]> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Bridge\SuperClosure\Tests; | ||
|
||
use Symfony\Bridge\SuperClosure\ClosureDumper\SuperClosureDumper; | ||
use Symfony\Component\DependencyInjection\ContainerInterface; | ||
|
||
/** | ||
* @author Nikita Konstantinov <[email protected]> | ||
*/ | ||
class SuperClosureDumperTest extends \PHPUnit_Framework_TestCase | ||
{ | ||
public function testThatClosureDumps() | ||
{ | ||
$dumper = new SuperClosureDumper(); | ||
|
||
$expectedCode = <<<'CODE' | ||
function (\Symfony\Component\DependencyInjection\ContainerInterface $container) { | ||
return $container->get('foo'); | ||
} | ||
CODE; | ||
|
||
$actualCode = $dumper->dump(function (ContainerInterface $container) { | ||
return $container->get('foo'); | ||
}); | ||
|
||
$this->assertSame($expectedCode, $actualCode); | ||
} | ||
|
||
/** | ||
* @expectedException \Symfony\Component\DependencyInjection\Exception\DumpingClosureException | ||
*/ | ||
public function testThatContextDependentClosureCannotBeDumped() | ||
{ | ||
$dumper = new SuperClosureDumper(); | ||
|
||
$dumper->dump(function () use ($dumper) { | ||
return new \stdClass(); | ||
}); | ||
} | ||
} |
This file contains hidden or 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,33 @@ | ||
{ | ||
"name": "symfony/superclosure-bridge", | ||
"type": "symfony-bridge", | ||
"description": "Symfony SuperClosure Bridge", | ||
"keywords": [], | ||
"homepage": "http://symfony.com", | ||
"license": "MIT", | ||
"authors": [ | ||
{ | ||
"name": "Fabien Potencier", | ||
"email": "[email protected]" | ||
}, | ||
{ | ||
"name": "Symfony Community", | ||
"homepage": "http://symfony.com/contributors" | ||
} | ||
], | ||
"require": { | ||
"php": ">=5.3.3", | ||
"symfony/dependency-injection": "~2.6", | ||
"jeremeamia/superclosure": "~1.0" | ||
}, | ||
"autoload": { | ||
"psr-0": { "Symfony\\Bridge\\SuperClosure\\": "" } | ||
}, | ||
"target-dir": "Symfony/Bridge/SuperClosure", | ||
"minimum-stability": "dev", | ||
"extra": { | ||
"branch-alias": { | ||
"dev-master": "2.6-dev" | ||
} | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
add a blank line before
return