8000 [Routing][ObjectRouteLoader] Allow invokable route loader services · symfony/symfony@5bf7ad4 · GitHub
[go: up one dir, main page]

Skip to content

Commit 5bf7ad4

Browse files
committed
[Routing][ObjectRouteLoader] Allow invokable route loader services
1 parent f551f2d commit 5bf7ad4

File tree

3 files changed

+13
-8
lines changed

3 files changed

+13
-8
lines changed

src/Symfony/Component/Routing/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ CHANGELOG
1111
* deprecated implementing `Serializable` for `Route` and `CompiledRoute`; if you serialize them, please
1212
ensure your unserialization logic can recover from a failure related to an updated serialization format
1313
* exposed `utf8` Route option, defaults "locale" and "format" in configuration loaders and configurators
14+
* added support for invokable route loader services
1415

1516
4.2.0
1617
-----

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -37,25 +37,25 @@ abstract protected function getServiceObject($id);
3737
/**
3838
* Calls the service that will load the routes.
3939
*
40-
* @param mixed $resource Some value that will resolve to a callable
40+
* @param string $resource Some value that will resolve to a callable
4141
* @param string|null $type The resource type
4242
*
4343
* @return RouteCollection
4444
*/
4545
public function load($resource, $type = null)
4646
{
47+
if (!preg_match('/^[^\:]+(?:::?(?:[^\:]+))?$/', $resource)) {
48+
throw new \InvalidArgumentException(sprintf('Invalid resource "%s" passed to the "service" route loader: use the format "service::method" or "service" if your service has an "__invoke" method.', $resource));
49+
}
50+
4751
if (1 === substr_count($resource, ':')) {
4852
$resource = str_replace(':', '::', $resource);
4953
@trigger_error(sprintf('Referencing service route loaders with a single colon is deprecated since Symfony 4.1. Use %s instead.', $resource), E_USER_DEPRECATED);
5054
}
5155

5256
$parts = explode('::', $resource);
53-
if (2 != \count($parts)) {
54-
throw new \InvalidArgumentException(sprintf('Invalid resource "%s" passed to the "service" route loader: use the format "service::method"', $resource));
55-
}
56-
5757
$serviceString = $parts[0];
58-
$method = $parts[1];
58+
$method = $parts[1] ?? '__invoke';
5959

6060
$loaderObject = $this->getServiceObject($serviceString);
6161

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

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ public function testLoadCallsServiceAndReturnsCollection()
7070
* @expectedException \InvalidArgumentException
7171
* @dataProvider getBadResourceStrings
7272
*/
73-
public function testExceptionWithoutSyntax($resourceString)
73+
public function testExceptionWithoutSyntax(string $resourceString): void
7474
{
7575
$loader = new ObjectRouteLoaderForTest();
7676
$loader->load($resourceString);
@@ -79,8 +79,12 @@ public function testExceptionWithoutSyntax($resourceString)
7979
public function getBadResourceStrings()
8080
{
8181
return [
82-
['Foo'],
8382
['Foo:Bar:baz'],
83+
['Foo::Bar::baz'],
84+
['Foo:'],
85+
['Foo::'],
86+
[':Foo'],
87+
['::Foo'],
8488
];
8589
}
8690

0 commit comments

Comments
 (0)
0