Skip to content

Using a service as a router resource #15742

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

Closed
wants to merge 18 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Updating ObjectRouteLoader to use a service_id:methodName format
  • Loading branch information
weaverryan committed Oct 1, 2015
commit 6b0a5c6785a9fbb610130cf7e4c3f59a373f553d
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public function __construct(ContainerInterface $container)
$this->container = $container;
}

protected function getRouteLoaderService($id)
protected function getServiceObject($id)
{
return $this->container->get($id);
}
Expand Down
54 changes: 36 additions & 18 deletions src/Symfony/Component/Routing/Loader/ObjectRouteLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,26 +22,56 @@
*/
abstract class ObjectRouteLoader 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.
*
* @param string $id
*
* @return object
*/
abstract protected function getServiceObject($id);

/**
* Calls the service that will load the routes.
*
* @param string $resource The name of the service to load
* @param mixed $resource Some value that will resolve to a callable
* @param string|null $type The resource type
*
* @return RouteCollection
*/
public function load($resource, $type = null)
{
$routeLoader = $this->getRouteLoaderService($resource);
$parts = explode(':', $resource);
if (count($parts) != 2) {
throw new \InvalidArgumentException(sprintf('Invalid resource "%s" passed to the "service" route loader: use the format "service_name:methodName"', $resource));
}

$serviceString = $parts[0];
$method = $parts[1];

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

if (!is_object($loaderObject)) {
throw new \LogicException(sprintf('%s:getServiceObject() must return an object: %s returned', get_class($this), $loaderObject));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here, $loaderObject can be anything; I would return the type instead.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

very good catch - that's what I had meant to do. Fixed now!

}

if (!$routeLoader instanceof RouteLoaderInterface) {
throw new \LogicException(sprintf('Service "%s" must implement RouteLoaderInterface.', $resource));
if (!method_exists($loaderObject, $method)) {
throw new \BadMethodCallException(sprintf('Method "%s" not found on "%s" when importing routing resource "%s"', $method, get_class($loaderObject), $resource));
}

$routeCollection = $routeLoader->getRouteCollection($this);
$routeCollection = call_user_func(array($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($routeLoader), $routeCollection);
$this->addClassResource(new \ReflectionClass($loaderObject), $routeCollection);

return $routeCollection;
}
Expand All @@ -56,18 +86,6 @@ public function supports($resource, $type = null)
return 'service' === $type;
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was caught between calling this "object" (which would look so generic in a routing.yml file) and service.

}

/**
* Returns a RouteLoaderInterface object matching the id.
*
* For example, if your application uses a service container,
* the $id may be a service id.
*
* @param string $id
*
* @return RouteLoaderInterface
*/
abstract protected function getRouteLoaderService($id);

private function addClassResource(\ReflectionClass $class, RouteCollection $collection)
{
do {
Expand Down
30 changes: 0 additions & 30 deletions src/Symfony/Component/Routing/Loader/RouteLoaderInterface.php

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -19,53 +19,97 @@ class ObjectRouteLoaderTest extends \PHPUnit_Framework_TestCase
{
public function testLoadCallsServiceAndReturnsCollection()
{
$routeLoader = $this->getMock('Symfony\Component\Routing\Loader\RouteLoaderInterface');
$serviceRouteLoader = new ObjectRouteLoaderForTest();

$serviceRouteLoader->loaderMap = array(
'my_route_provider_service' => $routeLoader,
);
$loader = new ObjectRouteLoaderForTest();

// create a basic collection that will be returned
$routes = new RouteCollection();
$routes->add('foo', new Route('/foo'));
$collection = new RouteCollection();
$collection->add('foo', new Route('/foo'));

$routeLoader
->expects($this->any())
->method('getRouteCollection')
// the loader itself is passed
->with($serviceRouteLoader)
->will($this->returnValue($routes));
// create some callable object
$service = $this->getMockBuilder('stdClass')
->setMethods(array('loadRoutes'))
->getMock();
$service->expects($this->once())
->method('loadRoutes')
->with($loader)
->will($this->returnValue($collection));

$actualRoutes = $serviceRouteLoader->load('my_route_provider_service', 'service');
$loader->loaderMap = array(
'my_route_provider_service' => $service,
);

$this->assertSame($routes, $actualRoutes);
$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 \LogicException
* @expectedException \InvalidArgumentException
* @dataProvider getBadResourceStrings
*/
public function testExceptionOnInterfaceNotImplemented()
public function testExceptionWithoutSyntax($resourceString)
{
// anything that doesn't implement the interface
$routeLoader = new \stdClass();
$loader = new ObjectRouteLoaderForTest();
$loader->load($resourceString);
}

$serviceRouteLoader = new ObjectRouteLoaderForTest();
$serviceRouteLoader->loaderMap = array(
'any_service_name' => $routeLoader,
public function getBadResourceStrings()
{
return array(
array('Foo'),
array('Bar::baz'),
array('Foo:Bar:baz'),
);
}

/**
* @expectedException \LogicException
*/
public function testExceptionOnNoObjectReturned()
{
$loader = new ObjectRouteLoaderForTest();
$loader->loaderMap = array('my_service' => 'NOT_AN_OBJECT');
$loader->load('my_service:method');
}

/**
* @expectedException \BadMethodCallException
*/
public function testExceptionOnBadMethod()
{
$loader = new ObjectRouteLoaderForTest();
$loader->loaderMap = array('my_service' => new \stdClass());
$loader->load('my_service:method');
}

/**
* @expectedException \LogicException
*/
public function testExceptionOnMethodNotReturningCollection()
{
$service = $this->getMockBuilder('stdClass')
->setMethods(array('loadRoutes'))
->getMock();
$service->expects($this->once())
->method('loadRoutes')
->will($this->returnValue('NOT_A_COLLECTION'));

$serviceRouteLoader->load('any_service_name', 'service');
$loader = new ObjectRouteLoaderForTest();
$loader->loaderMap = array('my_service' => $service);
$loader->load('my_service:loadRoutes');
}
}

class ObjectRouteLoaderForTest extends ObjectRouteLoader
{
public $loaderMap = array();

protected function getRouteLoaderService($id)
protected function getServiceObject($id)
{
return isset($this->loaderMap[$id]) ? $this->loaderMap[$id] : null;
}
Expand Down
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