-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[Routing] Deprecate ServiceRouterLoader and ObjectRouteLoader in favor of ContainerLoader and ObjectLoader #32582
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
nicolas-grekas
merged 1 commit into
symfony:4.4
from
fancyweb:deprecate-service-router-loader
Jul 23, 2019
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\Routing\Loader; | ||
|
||
use Psr\Container\ContainerInterface; | ||
|
||
/** | ||
* A route loader that executes a service from a PSR-11 container to load the routes. | ||
* | ||
* @author Ryan Weaver <ryan@knpuniversity.com> | ||
*/ | ||
class ContainerLoader extends ObjectLoader | ||
{ | ||
private $container; | ||
|
||
public function __construct(ContainerInterface $container) | ||
{ | ||
$this->container = $container; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function supports($resource, $type = null) | ||
{ | ||
return 'service' === $type; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
protected function getObject(string $id) | ||
{ | ||
return $this->container->get($id); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\Routing\Loader; | ||
|
||
use Symfony\Component\Config\Loader\Loader; | ||
use Symfony\Component\Config\Resource\FileResource; | ||
use Symfony\Component\Routing\RouteCollection; | ||
|
||
/** | ||
* A route loader that calls a method on an object to load the routes. | ||
* | ||
* @author Ryan Weaver <ryan@knpuniversity.com> | ||
*/ | ||
abstract class ObjectLoader extends Loader | ||
{ | ||
/** | ||
* Returns the object that the method will be called on to load routes. | ||
* | ||
* For example, if your application uses a service container, | ||
* the $id may be a service id. | ||
* | ||
* @return object | ||
*/ | ||
abstract protected function getObject(string $id); | ||
|
||
/** | ||
* Calls the object method that will load the routes. | ||
* | ||
* @param string $resource object_id::method | ||
* @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 %s route loader: use the format "object_id::method" or "object_id" if your object class has an "__invoke" method.', $resource, \is_string($type) ? '"'.$type.'"' : 'object')); | ||
} | ||
|
||
$parts = explode('::', $resource); | ||
$method = $parts[1] ?? '__invoke'; | ||
|
||
$loaderObject = $this->getObject($parts[0]); | ||
|
||
if (!\is_object($loaderObject)) { | ||
throw new \LogicException(sprintf('%s:getObject() must return an object: %s returned', \get_class($this), \gettype($loaderObject))); | ||
} | ||
|
||
if (!\is_callable([$loaderObject, $method])) { | ||
throw new \BadMethodCallException(sprintf('Method "%s" not found on "%s" when importing routing resource "%s"', $method, \get_class($loaderObject), $resource)); | ||
} | ||
|
||
$routeCollection = $loaderObject->$method($this); | ||
|
||
if (!$routeCollection instanceof RouteCollection) { | ||
$type = \is_object($routeCollection) ? \get_class($routeCollection) : \gettype($routeCollection); | ||
|
||
throw new \LogicException(sprintf('The %s::%s method must return a RouteCollection: %s returned', \get_class($loaderObject), $method, $type)); | ||
} | ||
|
||
// make the object file tracked so that if it changes, the cache rebuilds | ||
$this->addClassResource(new \ReflectionClass($loaderObject), $routeCollection); | ||
|
||
return $routeCollection; | ||
} | ||
|
||
private function addClassResource(\ReflectionClass $class, RouteCollection $collection) | ||
{ | ||
do { | ||
if (is_file($class->getFileName())) { | ||
$collection->addResource(new FileResource($class->getFileName())); | ||
} | ||
} while ($class = $class->getParentClass()); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
24 changes: 24 additions & 0 deletions
24
src/Symfony/Component/Routing/Tests/Fixtures/TestObjectRouteLoader.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\Routing\Tests\Fixtures; | ||
|
||
use Symfony\Component\Routing\Loader\ObjectRouteLoader; | ||
|
||
class TestObjectRouteLoader extends ObjectRouteLoader | ||
fancyweb marked this conversation as resolved.
Show resolved
Hide resolved
|
||
{ | ||
public $loaderMap = []; | ||
|
||
protected function getServiceObject($id) | ||
{ | ||
return $this->loaderMap[$id] ?? null; | ||
} | ||
} |
36 changes: 36 additions & 0 deletions
36
src/Symfony/Component/Routing/Tests/Loader/ContainerLoaderTest.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\Routing\Tests\Loader; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
use Symfony\Component\DependencyInjection\Container; | ||
use Symfony\Component\Routing\Loader\ContainerLoader; | ||
|
||
class ContainerLoaderTest extends TestCase | ||
{ | ||
/** | ||
* @dataProvider supportsProvider | ||
*/ | ||
public function testSupports(bool $expected, string $type = null) | ||
{ | ||
$this->assertSame($expected, (new ContainerLoader(new Container()))->supports('foo', $type)); | ||
} | ||
|
||
public function supportsProvider() | ||
{ | ||
return [ | ||
[true, 'service'], | ||
[false, 'bar'], | ||
[false, null], | ||
]; | ||
} | ||
} |
29 changes: 29 additions & 0 deletions
29
src/Symfony/Component/Routing/Tests/Loader/DependencyInjection/ServiceRouterLoaderTest.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\Routing\Tests\Loader; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
use Symfony\Component\DependencyInjection\Container; | ||
use Symfony\Component\Routing\Loader\DependencyInjection\ServiceRouterLoader; | ||
|
||
class ServiceRouterLoaderTest extends TestCase | ||
{ | ||
/** | ||
* @group legacy | ||
* @expectedDeprecation The "Symfony\Component\Routing\Loader\DependencyInjection\ServiceRouterLoader" class is deprecated since Symfony 4.4, use "Symfony\Component\Routing\Loader\ContainerLoader" instead. | ||
* @expectedDeprecation The "Symfony\Component\Routing\Loader\ObjectRouteLoader" class is deprecated since Symfony 4.4, use "Symfony\Component\Routing\Loader\ObjectLoader" instead. | ||
*/ | ||
public function testDeprecationWarning() | ||
{ | ||
new ServiceRouterLoader(new Container()); | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.