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

Skip to content

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

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

Merged
merged 1 commit into from
Apr 27, 2018
Merged
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

extra unneeded line

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

services:
service_container:
class: Symfony\Component\DependencyInjection\ContainerInterface
public: true
synthetic: true
App\BarService:
class: App\BarService
public: true
arguments: [!service { class: FooClass }]
Original file line number Diff line number Diff line change
@@ -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')));
}
};
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
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