8000 [Routing][ObjectRouteLoader] Allow invokable route loader services by fancyweb · Pull Request #30933 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[Routing][ObjectRouteLoader] Allow invokable route loader services #30933

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
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
1 change: 1 addition & 0 deletions src/Symfony/Component/Routing/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ CHANGELOG
* deprecated implementing `Serializable` for `Route` and `CompiledRoute`; if you serialize them, please
ensure your unserialization logic can recover from a failure related to an updated serialization format
* exposed `utf8` Route option, defaults "locale" and "format" in configuration loaders and configurators
* added support for invokable route loader services

4.2.0
-----
Expand Down
12 changes: 6 additions & 6 deletions src/Symfony/Component/Routing/Loader/ObjectRouteLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,25 +37,25 @@ abstract protected function getServiceObject($id);
/**
* Calls the service that will load the routes.
*
* @param mixed $resource Some value that will resolve to a callable
* @param string $resource Some value that will resolve to a callable
* @param string|null $type The resource type
*
* @return RouteCollection
*/
public function load($resource, $type = null)
{
if (!preg_match('/^[^\:]+(?:::?(?:[^\:]+))?$/', $resource)) {
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));
}

if (1 === substr_count($resource, ':')) {
$resource = str_replace(':', '::', $resource);
@trigger_error(sprintf('Referencing service route loaders with a single colon is deprecated since Symfony 4.1. Use %s instead.', $resource), E_USER_DEPRECATED);
}

$parts = explode('::', $resource);
if (2 != \count($parts)) {
throw new \InvalidArgumentException(sprintf('Invalid resource "%s" passed to the "service" route loader: use the format "service::method"', $resource));
}

$serviceString = $parts[0];
$method = $parts[1];
$method = $parts[1] ?? '__invoke';

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ public function testLoadCallsServiceAndReturnsCollection()
* @expectedException \InvalidArgumentException
* @dataProvider getBadResourceStrings
*/
public function testExceptionWithoutSyntax($resourceString)
public function testExceptionWithoutSyntax(string $resourceString): void
{
$loader = new ObjectRouteLoaderForTest();
$loader->load($resourceString);
Expand All @@ -79,8 +79,12 @@ public function testExceptionWithoutSyntax($resourceString)
public function getBadResourceStrings()
{
return [
['Foo'],
['Foo:Bar:baz'],
['Foo::Bar::baz'],
['Foo:'],
['Foo::'],
[':Foo'],
['::Foo'],
];
}

Expand Down
0