+ *
+ * @deprecated since Symfony 4.4, use ObjectLoader instead.
*/
-abstract class ObjectRouteLoader extends Loader
+abstract class ObjectRouteLoader extends ObjectLoader
{
/**
* Returns the object that the method will be called on to load routes.
@@ -53,32 +55,7 @@ public function load($resource, $type = null)
@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);
- $serviceString = $parts[0];
- $method = $parts[1] ?? '__invoke';
-
- $loaderObject = $this->getServiceObject($serviceString);
-
- if (!\is_object($loaderObject)) {
- throw new \LogicException(sprintf('%s:getServiceObject() 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 service file tracked so that if it changes, the cache rebuilds
- $this->addClassResource(new \ReflectionClass($loaderObject), $routeCollection);
-
- return $routeCollection;
+ return parent::load($resource, $type);
}
/**
@@ -89,12 +66,11 @@ public function supports($resource, $type = null)
return 'service' === $type;
}
- private function addClassResource(\ReflectionClass $class, RouteCollection $collection)
+ /**
+ * {@inheritdoc}
+ */
+ protected function getObject(string $id)
{
- do {
- if (is_file($class->getFileName())) {
- $collection->addResource(new FileResource($class->getFileName()));
- }
- } while ($class = $class->getParentClass());
+ return $this->getServiceObject($id);
}
}
diff --git a/src/Symfony/Component/Routing/Tests/Fixtures/TestObjectRouteLoader.php b/src/Symfony/Component/Routing/Tests/Fixtures/TestObjectRouteLoader.php
new file mode 100644
index 0000000000000..d272196dd6f10
--- /dev/null
+++ b/src/Symfony/Component/Routing/Tests/Fixtures/TestObjectRouteLoader.php
@@ -0,0 +1,24 @@
+
+ *
+ * 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
+{
+ public $loaderMap = [];
+
+ protected function getServiceObject($id)
+ {
+ return $this->loaderMap[$id] ?? null;
+ }
+}
diff --git a/src/Symfony/Component/Routing/Tests/Loader/ContainerLoaderTest.php b/src/Symfony/Component/Routing/Tests/Loader/ContainerLoaderTest.php
new file mode 100644
index 0000000000000..5f74111d1b092
--- /dev/null
+++ b/src/Symfony/Component/Routing/Tests/Loader/ContainerLoaderTest.php
@@ -0,0 +1,36 @@
+
+ *
+ * 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],
+ ];
+ }
+}
diff --git a/src/Symfony/Component/Routing/Tests/Loader/DependencyInjection/ServiceRouterLoaderTest.php b/src/Symfony/Component/Routing/Tests/Loader/DependencyInjection/ServiceRouterLoaderTest.php
new file mode 100644
index 0000000000000..497ce2f3b3658
--- /dev/null
+++ b/src/Symfony/Component/Routing/Tests/Loader/DependencyInjection/ServiceRouterLoaderTest.php
@@ -0,0 +1,29 @@
+
+ *
+ * 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());
+ }
+}
diff --git a/src/Symfony/Component/Routing/Tests/Loader/ObjectLoaderTest.php b/src/Symfony/Component/Routing/Tests/Loader/ObjectLoaderTest.php
new file mode 100644
index 0000000000000..1267f540d07b4
--- /dev/null
+++ b/src/Symfony/Component/Routing/Tests/Loader/ObjectLoaderTest.php
@@ -0,0 +1,131 @@
+
+ *
+ * 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\Routing\Loader\ObjectLoader;
+use Symfony\Component\Routing\Route;
+use Symfony\Component\Routing\RouteCollection;
+
+class ObjectLoaderTest extends TestCase
+{
+ public function testLoadCallsServiceAndReturnsCollection()
+ {
+ $loader = new TestObjectLoader();
+
+ // create a basic collection that will be returned
+ $collection = new RouteCollection();
+ $collection->add('foo', new Route('/foo'));
+
+ $loader->loaderMap = [
+ 'my_route_provider_service' => new TestObjectLoaderRouteService($collection),
+ ];
+
+ $actualRoutes = $loader->load(
+ 'my_route_provider_service::loadRoutes',
+ 'service'
+ );
+
+ $this->assertSame($collection, $actualRoutes);
+ // the service file should be listed as a resource
+ $this->assertNotEmpty($actualRoutes->getResources());
+ }
+
+ /**
+ * @expectedException \InvalidArgumentException
+ * @dataProvider getBadResourceStrings
+ */
+ public function testExceptionWithoutSyntax(string $resourceString): void
+ {
+ $loader = new TestObjectLoader();
+ $loader->load($resourceString);
+ }
+
+ public function getBadResourceStrings()
+ {
+ return [
+ ['Foo:Bar:baz'],
+ ['Foo::Bar::baz'],
+ ['Foo:'],
+ ['Foo::'],
+ [':Foo'],
+ ['::Foo'],
+ ];
+ }
+
+ /**
+ * @expectedException \LogicException
+ */
+ public function testExceptionOnNoObjectReturned()
+ {
+ $loader = new TestObjectLoader();
+ $loader->loaderMap = ['my_service' => 'NOT_AN_OBJECT'];
+ $loader->load('my_service::method');
+ }
+
+ /**
+ * @expectedException \BadMethodCallException
+ */
+ public function testExceptionOnBadMethod()
+ {
+ $loader = new TestObjectLoader();
+ $loader->loaderMap = ['my_service' => new \stdClass()];
+ $loader->load('my_service::method');
+ }
+
+ /**
+ * @expectedException \LogicException
+ */
+ public function testExceptionOnMethodNotReturningCollection()
+ {
+ $service = $this->getMockBuilder('stdClass')
+ ->setMethods(['loadRoutes'])
+ ->getMock();
+ $service->expects($this->once())
+ ->method('loadRoutes')
+ ->willReturn('NOT_A_COLLECTION');
+
+ $loader = new TestObjectLoader();
+ $loader->loaderMap = ['my_service' => $service];
+ $loader->load('my_service::loadRoutes');
+ }
+}
+
+class TestObjectLoader extends ObjectLoader
+{
+ public $loaderMap = [];
+
+ public function supports($resource, $type = null)
+ {
+ return 'service';
+ }
+
+ protected function getObject(string $id)
+ {
+ return $this->loaderMap[$id] ?? null;
+ }
+}
+
+class TestObjectLoaderRouteService
+{
+ private $collection;
+
+ public function __construct($collection)
+ {
+ $this->collection = $collection;
+ }
+
+ public function loadRoutes()
+ {
+ return $this->collection;
+ }
+}
diff --git a/src/Symfony/Component/Routing/Tests/Loader/ObjectRouteLoaderTest.php b/src/Symfony/Component/Routing/Tests/Loader/ObjectRouteLoaderTest.php
index a286436de5c0b..52e4be8157e3a 100644
--- a/src/Symfony/Component/Routing/Tests/Loader/ObjectRouteLoaderTest.php
+++ b/src/Symfony/Component/Routing/Tests/Loader/ObjectRouteLoaderTest.php
@@ -12,26 +12,28 @@
namespace Symfony\Component\Routing\Tests\Loader;
use PHPUnit\Framework\TestCase;
-use Symfony\Component\Routing\Loader\ObjectRouteLoader;
use Symfony\Component\Routing\Route;
use Symfony\Component\Routing\RouteCollection;
+use Symfony\Component\Routing\Tests\Fixtures\TestObjectRouteLoader;
+/**
+ * @group legacy
+ */
class ObjectRouteLoaderTest extends TestCase
{
/**
- * @group legacy
* @expectedDeprecation Referencing service route loaders with a single colon is deprecated since Symfony 4.1. Use my_route_provider_service::loadRoutes instead.
*/
public function testLoadCallsServiceAndReturnsCollectionWithLegacyNotation()
{
- $loader = new ObjectRouteLoaderForTest();
+ $loader = new TestObjectRouteLoader();
// create a basic collection that will be returned
$collection = new RouteCollection();
$collection->add('foo', new Route('/foo'));
$loader->loaderMap = [
- 'my_route_provider_service' => new RouteService($collection),
+ 'my_route_provider_service' => new TestObjectRouteLoaderRouteService($collection),
];
$actualRoutes = $loader->load(
@@ -46,14 +48,14 @@ public function testLoadCallsServiceAndReturnsCollectionWithLegacyNotation()
public function testLoadCallsServiceAndReturnsCollection()
{
- $loader = new ObjectRouteLoaderForTest();
+ $loader = new TestObjectRouteLoader();
// create a basic collection that will be returned
$collection = new RouteCollection();
$collection->add('foo', new Route('/foo'));
$loader->loaderMap = [
- 'my_route_provider_service' => new RouteService($collection),
+ 'my_route_provider_service' => new TestObjectRouteLoaderRouteService($collection),
];
$actualRoutes = $loader->load(
@@ -72,7 +74,7 @@ public function testLoadCallsServiceAndReturnsCollection()
*/
public function testExceptionWithoutSyntax(string $resourceString): void
{
- $loader = new ObjectRouteLoaderForTest();
+ $loader = new TestObjectRouteLoader();
$loader->load($resourceString);
}
@@ -93,7 +95,7 @@ public function getBadResourceStrings()
*/
public function testExceptionOnNoObjectReturned()
{
- $loader = new ObjectRouteLoaderForTest();
+ $loader = new TestObjectRouteLoader();
$loader->loaderMap = ['my_service' => 'NOT_AN_OBJECT'];
$loader->load('my_service::method');
}
@@ -103,7 +105,7 @@ public function testExceptionOnNoObjectReturned()
*/
public function testExceptionOnBadMethod()
{
- $loader = new ObjectRouteLoaderForTest();
+ $loader = new TestObjectRouteLoader();
$loader->loaderMap = ['my_service' => new \stdClass()];
$loader->load('my_service::method');
}
@@ -120,23 +122,13 @@ public function testExceptionOnMethodNotReturningCollection()
->method('loadRoutes')
->willReturn('NOT_A_COLLECTION');
- $loader = new ObjectRouteLoaderForTest();
+ $loader = new TestObjectRouteLoader();
$loader->loaderMap = ['my_service' => $service];
$loader->load('my_service::loadRoutes');
}
}
-class ObjectRouteLoaderForTest extends ObjectRouteLoader
-{
- public $loaderMap = [];
-
- protected function getServiceObject($id)
- {
- return isset($this->loaderMap[$id]) ? $this->loaderMap[$id] : null;
- }
-}
-
-class RouteService
+class TestObjectRouteLoaderRouteService
{
private $collection;
pFad - Phonifier reborn
Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.
Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.
Alternative Proxies:
Alternative Proxy
pFad Proxy
pFad v3 Proxy
pFad v4 Proxy