[DI][Routing] Allow invokable objects to be used as PHP-DSL loaders

This commit is contained in:
Aurimas Niekis 2018-04-26 13:47:17 +01:00 committed by Fabien Potencier
parent a0a1029413
commit 662ff7e10b
7 changed files with 69 additions and 5 deletions

View File

@ -43,7 +43,7 @@ class PhpFileLoader extends FileLoader
$callback = $load($path);
if ($callback instanceof \Closure) {
if (\is_object($callback) && \is_callable($callback)) {
$callback(new ContainerConfigurator($this->container, $this, $this->instanceof, $path, $resource), $this->container, $this);
}
}

View File

@ -0,0 +1,10 @@
services:
service_container:
class: Symfony\Component\DependencyInjection\ContainerInterface
public: true
synthetic: true
App\BarService:
class: App\BarService
public: true
arguments: [!service { class: FooClass }]

View File

@ -0,0 +1,14 @@
<?php
namespace Symfony\Component\DependencyInjection\Loader\Configurator;
use App\BarService;
return new class() {
public function __invoke(ContainerConfigurator $c)
{
$s = $c->services();
$s->set(BarService::class)
->args(array(inline('FooClass')));
}
};

View File

@ -68,6 +68,7 @@ class PhpFileLoaderTest extends TestCase
public function provideConfig()
{
yield array('basic');
yield array('object');
yield array('defaults');
yield array('instanceof');
yield array('prototype');

View File

@ -46,7 +46,7 @@ class PhpFileLoader extends FileLoader
$result = $load($path);
if ($result instanceof \Closure) {
if (\is_object($result) && \is_callable($result)) {
$collection = new RouteCollection();
$result(new RoutingConfigurator($collection, $this, $path, $file), $this);
} else {

View File

@ -0,0 +1,32 @@
<?php
namespace Symfony\Component\Routing\Loader\Configurator;
return new class() {
public function __invoke(RoutingConfigurator $routes)
{
$routes
->collection()
->add('foo', '/foo')
->condition('abc')
->options(array('utf8' => true))
->add('buz', 'zub')
->controller('foo:act');
$routes->import('php_dsl_sub.php')
->prefix('/sub')
->requirements(array('id' => '\d+'));
$routes->import('php_dsl_sub.php')
->namePrefix('z_')
->prefix('/zub');
$routes->import('php_dsl_sub_root.php')
->prefix('/bus', false);
$routes->add('ouf', '/ouf')
->schemes(array('https'))
->methods(array('GET'))
->defaults(array('id' => 0));
}
};

View File

@ -88,7 +88,8 @@ class PhpFileLoaderTest extends TestCase
{
$locator = new FileLocator(array(__DIR__.'/../Fixtures'));
$loader = new PhpFileLoader($locator);
$routeCollection = $loader->load('php_dsl.php');
$routeCollectionClosure = $loader->load('php_dsl.php');
$routeCollectionObject = $loader->load('php_object_dsl.php');
$expectedCollection = new RouteCollection();
@ -122,9 +123,15 @@ class PhpFileLoaderTest extends TestCase
$expectedCollection->addResource(new FileResource(realpath(__DIR__.'/../Fixtures/php_dsl_sub.php')));
$expectedCollection->addResource(new FileResource(realpath(__DIR__.'/../Fixtures/php_dsl_sub_root.php')));
$expectedCollection->addResource(new FileResource(realpath(__DIR__.'/../Fixtures/php_dsl.php')));
$this->assertEquals($expectedCollection, $routeCollection);
$expectedCollectionClosure = $expectedCollection;
$expectedCollectionObject = clone $expectedCollection;
$expectedCollectionClosure->addResource(new FileResource(realpath(__DIR__.'/../Fixtures/php_dsl.php')));
$expectedCollectionObject->addResource(new FileResource(realpath(__DIR__.'/../Fixtures/php_object_dsl.php')));
$this->assertEquals($expectedCollectionClosure, $routeCollectionClosure);
$this->assertEquals($expectedCollectionObject, $routeCollectionObject);
}
public function testRoutingConfiguratorCanImportGlobPatterns()