Skip to content

Commit

Permalink
feature #27065 [DI][Routing] Allow invokable objects to be used as PH…
Browse files Browse the repository at this point in the history
…P-DSL loaders (aurimasniekis)

This PR was squashed before being merged into the 4.1-dev branch (closes #27065).

Discussion
----------

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

| Q             | A
| ------------- | ---
| Branch?       | master
| Bug fix?      | no
| New feature?  | no
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | #26583, #25630
| License       | MIT
| Doc PR        | none

Changed DI/Router PHPFileLoader to check is_object && is_callable instead of instance of Closure

Commits
-------

662ff7e [DI][Routing] Allow invokable objects to be used as PHP-DSL loaders
  • Loading branch information
fabpot committed Apr 27, 2018
2 parents 39c7c90 + 662ff7e commit a9d12d2
Show file tree
Hide file tree
Showing 7 changed files with 69 additions and 5 deletions.
Expand Up @@ -43,7 +43,7 @@ public function load($resource, $type = null)

$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);
}
}
Expand Down
@@ -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 }]
@@ -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')));
}
};
Expand Up @@ -68,6 +68,7 @@ public function testConfig($file)
public function provideConfig()
{
yield array('basic');
yield array('object');
yield array('defaults');
yield array('instanceof');
yield array('prototype');
Expand Down
2 changes: 1 addition & 1 deletion src/Symfony/Component/Routing/Loader/PhpFileLoader.php
Expand Up @@ -46,7 +46,7 @@ public function load($file, $type = null)

$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 {
Expand Down
32 changes: 32 additions & 0 deletions src/Symfony/Component/Routing/Tests/Fixtures/php_object_dsl.php
@@ -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));
}
};
13 changes: 10 additions & 3 deletions src/Symfony/Component/Routing/Tests/Loader/PhpFileLoaderTest.php
Expand Up @@ -88,7 +88,8 @@ public function testRoutingConfigurator()
{
$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();

Expand Down Expand Up @@ -122,9 +123,15 @@ public function testRoutingConfigurator()

$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()
Expand Down

0 comments on commit a9d12d2

Please sign in to comment.