8000 feature #27065 [DI][Routing] Allow invokable objects to be used as PH… · symfony/symfony@a9d12d2 · GitHub
[go: up one dir, main page]

Skip to content

Commit a9d12d2

Browse files
committed
feature #27065 [DI][Routing] Allow invokable objects to be used as PHP-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
2 parents 39c7c90 + 662ff7e commit a9d12d2

File tree

7 files changed

+69
-5
lines changed

7 files changed

+69
-5
lines changed

src/Symfony/Component/DependencyInjection/Loader/PhpFileLoader.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ public function load($resource, $type = null)
4343

4444
$callback = $load($path);
4545

46-
if ($callback instanceof \Closure) {
46+
if (\is_object($callback) && \is_callable($callback)) {
4747
$callback(new ContainerConfigurator($this->container, $this, $this->instanceof, $path, $resource), $this->container, $this);
4848
}
4949
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
2+
services:
3+
service_container:
4+
class: Symfony\Component\DependencyInjection\ContainerInterface
5+
public: true
6+
synthetic: true
7+
App\BarService:
8+
class: App\BarService
9+
public: true
10+
arguments: [!service { class: FooClass }]
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php
2+
3+
namespace Symfony\Component\DependencyInjection\Loader\Configurator;
4+
5+
use App\BarService;
6+
7+
return new class() {
8+
public function __invoke(ContainerConfigurator $c)
9+
{
10+
$s = $c->services();
11+
$s->set(BarService::class)
12+
->args(array(inline('FooClass')));
13+
}
14+
};

src/Symfony/Component/DependencyInjection/Tests/Loader/PhpFileLoaderTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ public function testConfig($file)
6868
public function provideConfig()
8000 6969
{
7070
yield array('basic');
71+
yield array('object');
7172
yield array('defaults');
7273
yield array('instanceof');
7374
yield array('prototype');

src/Symfony/Component/Routing/Loader/PhpFileLoader.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ public function load($file, $type = null)
4646

4747
$result = $load($path);
4848

49-
if ($result instanceof \Closure) {
49+
if (\is_object($result) && \is_callable($result)) {
5050
$collection = new RouteCollection();
5151
$result(new RoutingConfigurator($collection, $this, $path, $file), $this);
5252
} else {
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
3+
namespace Symfony\Component\Routing\Loader\Configurator;
4+
5+
return new class() {
6+
public function __invoke(RoutingConfigurator $routes)
7+
{
8+
$routes
9+
->collection()
10+
->add('foo', '/foo')
11+
->condition('abc')
12+
->options(array('utf8' => true))
13+
->add('buz', 'zub')
14+
->controller('foo:act');
15+
16+
$routes->import('php_dsl_sub.php')
17+
->prefix('/sub')
18+
->requirements(array('id' => '\d+'));
19+
20+
$routes->import('php_dsl_sub.php')
21+
->namePrefix('z_')
22+
->prefix('/zub');
23+
24+
$routes->import('php_dsl_sub_root.php')
25+
->prefix('/bus', false);
26+
27+
$routes->add('ouf', '/ouf')
28+
->schemes(array('https'))
29+
->methods(array('GET'))
30+
->defaults(array('id' => 0));
31+
}
32+
};

src/Symfony/Component/Routing/Tests/Loader/PhpFileLoaderTest.php

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,8 @@ public function testRoutingConfigurator()
8888
{
8989
$locator = new FileLocator(array(__DIR__.'/../Fixtures'));
9090
$loader = new PhpFileLoader($locator);
91-
$routeCollection = $loader->load('php_dsl.php');
91+
$routeCollectionClosure = $loader->load('php_dsl.php');
92+
$routeCollectionObject = $loader->load('php_object_dsl.php');
9293

9394
$expectedCollection = new RouteCollection();
9495

@@ -122,9 +123,15 @@ public function testRoutingConfigurator()
122123

123124
$expectedCollection->addResource(new FileResource(realpath(__DIR__.'/../Fixtures/php_dsl_sub.php')));
124125
$expectedCollection->addResource(new FileResource(realpath(__DIR__.'/../Fixtures/php_dsl_sub_root.php')));
125-
$expectedCollection->addResource(new FileResource(realpath(__DIR__.'/../Fixtures/php_dsl.php')));
126126

127-
$this->assertEquals($expectedCollection, $routeCollection);
127+
$expectedCollectionClosure = $expectedCollection;
128+
$expectedCollectionObject = clone $expectedCollection;
129+
130+
$expectedCollectionClosure->addResource(new FileResource(realpath(__DIR__.'/../Fixtures/php_dsl.php')));
131+
$expectedCollectionObject->addResource(new FileResource(realpath(__DIR__.'/../Fixtures/php_object_dsl.php')));
132+
133+
$this->assertEquals($expectedCollectionClosure, $routeCollectionClosure);
134+
$this->assertEquals($expectedCollectionObject, $routeCollectionObject);
128135
}
129136

130137
public function testRoutingConfiguratorCanImportGlobPatterns()

0 commit comments

Comments
 (0)
0