Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[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
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@
"symfony/security-bundle": "self.version",
"symfony/serializer": "self.version",
"symfony/stopwatch": "self.version",
"symfony/superclosure-bridge": "self.version",
"symfony/swiftmailer-bridge": "self.version",
"symfony/templating": "self.version",
"symfony/translation": "self.version",
Expand All @@ -76,7 +77,8 @@
"propel/propel1": "1.6.*",
"ircmaxell/password-compat": "1.0.*",
"ocramius/proxy-manager": ">=0.3.1,<0.6-dev",
"egulias/email-validator": "~1.2"
"egulias/email-validator": "~1.2",
"jeremeamia/superclosure": "~1.0"
},
"autoload": {
"psr-0": { "Symfony\\": "src/" },
Expand Down
3 changes: 3 additions & 0 deletions src/Symfony/Bridge/SuperClosure/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
vendor/
composer.lock
phpunit.xml
7 changes: 7 additions & 0 deletions src/Symfony/Bridge/SuperClosure/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
CHANGELOG
=========

2.6.0
-----

* Added the `Symfony\Bridge\SuperClosure` bridge
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);
}
}
}
19 changes: 19 additions & 0 deletions src/Symfony/Bridge/SuperClosure/LICENSE
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.
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;
Copy link
Member

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

}, $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;
Copy link
Member

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

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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;
}
}
76 changes: 76 additions & 0 deletions src/Symfony/Bridge/SuperClosure/Tests/PhpDumperTest.php
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 src/Symfony/Bridge/SuperClosure/Tests/SuperClosureDumperTest.php
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();
});
}
}
33 changes: 33 additions & 0 deletions src/Symfony/Bridge/SuperClosure/composer.json
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"
}
}
}
Loading