Skip to content

Commit 1e4c661

Browse files
committed
renaming DoctrineRouter to DynamicRouter to reflect this does not depend on doctrine. cleanup interfacing with the Mapper
1 parent 8b430b5 commit 1e4c661

18 files changed

+117
-74
lines changed

ContentRepositoryInterface.php

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,11 @@
33
namespace Symfony\Cmf\Component\Routing;
44

55
/**
6-
* Interface used by the DoctrineRouter to retrieve content by it's id.
6+
* Interface used by the DynamicRouter to retrieve content by it's id when
7+
* generating routes from content-id.
78
*
8-
* This can be easily implemented using the DocumentManager.
9+
* This can be easily implemented using i.e. the Doctrine PHPCR-ODM
10+
* DocumentManager.
911
*
1012
* @author Uwe Jäger
1113
*/
@@ -14,9 +16,12 @@ interface ContentRepositoryInterface
1416
/**
1517
* Return a content object by it's id or null if there is none.
1618
*
17-
* @abstract
18-
* @param $id id of the content object
19-
* @return mixed
19+
* If the returned content implements RouteAwareInterface, it will be used
20+
* to get the route from it to generate an URL.
21+
*
22+
* @param string $id id of the content object
23+
*
24+
* @return object A content that matches this id.
2025
*/
2126
function findById($id);
2227
}

DoctrineRouter.php renamed to DynamicRouter.php

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -13,41 +13,42 @@
1313

1414
use Symfony\Cmf\Component\Routing\Mapper\ControllerMapperInterface;
1515

16-
use Doctrine\Common\Persistence\ManagerRegistry;
17-
use Doctrine\Common\Persistence\ObjectManager;
18-
1916
/**
20-
* A router that reads route entries from an Object-Document Mapper store.
17+
* A router that reads route entries from a repository. The repository can
18+
* easily be implemented using an object-document mapper like Doctrine
19+
* PHPCR-ODM but you are free to use something different.
2120
*
22-
* This is basically using the symfony routing matcher and generator. Different
21+
* This router is based on the symfony routing matcher and generator. Different
2322
* to the default router, the route collection is loaded from the injected
2423
* route repository custom per request to not load a potentially large number
2524
* of routes that are known to not match anyways.
2625
*
2726
* If the route provides a content, that content is placed in the defaults
28-
* returned by the match() method in field '_content'.
27+
* returned by the match() method in field RouteObjectInterface::CONTENT_OBJECT.
2928
*
3029
* @author Philippo de Santis
3130
* @author David Buchmann
3231
* @author Uwe Jäger
3332
*/
34-
class DoctrineRouter implements RouterInterface
33+
class DynamicRouter implements RouterInterface
3534
{
3635
/**
3736
* Symfony routes always need a name in the collection. We generate routes
3837
* based on the route object, but need to use a name for example in error
3938
* reporting.
40-
* When generating, we just use this prefix, when matching, we add the full
41-
* repository path with "/" replaced by "_" to get unique names.
39+
* When generating, we just use this prefix, when matching, we append
40+
* whatever the repository returned as ID, replacing anything but
41+
* [^a-z0-9A-Z_.] with "_" to get unique valid route names.
4242
*/
43-
const ROUTE_NAME_PREFIX = 'cmf_routing_doctrine_route';
43+
const ROUTE_NAME_PREFIX = 'cmf_routing_dynamic_route';
4444

4545
/**
4646
* @var array of ControllerMapperInterface
4747
*/
4848
protected $mappers = array();
4949
/**
5050
* The route repository to get routes from
51+
*
5152
* @var RouteRepositoryInterface
5253
*/
5354
protected $routeRepository;
@@ -170,7 +171,7 @@ public function getRouteCollection()
170171
*
171172
* array(
172173
* "_controller" => "NameSpace\Controller::indexAction",
173-
* "reference" => $document,
174+
* "_content" => $document,
174175
* )
175176
*
176177
* The controller can be either the fully qualified class name or the
@@ -217,7 +218,7 @@ public function match($url)
217218
}
218219

219220
if ($route instanceof RouteObjectInterface && $content = $route->getRouteContent()) {
220-
$defaults['_content'] = $content;
221+
$defaults[RouteObjectInterface::CONTENT_OBJECT] = $content;
221222
}
222223
$defaults['path'] = $url; // TODO: get rid of this
223224

@@ -250,7 +251,7 @@ public function getMatcher(RouteCollection $collection)
250251
*
251252
* @param array $parameters which should contain a content field containing a RouteAwareInterface object
252253
*
253-
* @return the route instance
254+
* @return Route the route instance
254255
*
255256
* @throws RouteNotFoundException if there is no content field in the
256257
* parameters or its not possible to build a route from that object

Mapper/ControllerAliasMapper.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
namespace Symfony\Cmf\Component\Routing\Mapper;
44

5-
use Symfony\Cmf\Component\Routing\RouteObjectInterface;
5+
use Symfony\Component\Routing\Route;
66

77
/**
88
* Decide the controller by a map from alias to controller name injected into
@@ -30,7 +30,7 @@ public function __construct(array $controllersByAlias = array())
3030
*
3131
* {@inheritDoc}
3232
*/
33-
public function getController(RouteObjectInterface $document, array &$defaults)
33+
public function getController(Route $route, array &$defaults)
3434
{
3535
if (! isset($defaults['type'])) {
3636
return false;

Mapper/ControllerClassMapper.php

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Symfony\Cmf\Component\Routing\Mapper;
44

5+
use Symfony\Component\Routing\Route;
56
use Symfony\Cmf\Component\Routing\RouteObjectInterface;
67

78
/**
@@ -35,12 +36,17 @@ public function __construct(array $controllersByClass)
3536
*
3637
* @param array $defaults ignored
3738
*/
38-
public function getController(RouteObjectInterface $document, array &$defaults)
39+
public function getController(Route $route, array &$defaults)
3940
{
40-
$content = $document->getRouteContent();
41+
if (! $route instanceof RouteObjectInterface) {
42+
return false;
43+
}
44+
45+
$content = $route->getRouteContent();
4146
if (null == $content) {
4247
return false;
4348
}
49+
4450
// we need to loop over the array in case the content class extends the
4551
// specified class
4652
// i.e. phpcr-odm generates proxy class for the content.

Mapper/ControllerMapperInterface.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@
22

33
namespace Symfony\Cmf\Component\Routing\Mapper;
44

5-
use Symfony\Cmf\Component\Routing\RouteObjectInterface;
5+
use Symfony\Component\Routing\Route;
66

77
/**
8-
* Interface for all controller mappers that work with the DoctrineRouter
8+
* Interface for all controller mappers that work with the DynamicRouter
99
*
1010
* @author David Buchmann
1111
*/
@@ -14,14 +14,14 @@ interface ControllerMapperInterface
1414
/**
1515
* Retrieves the right controller for the given route $document.
1616
*
17-
* @param RouteObjectInterface $document the document or entity for the route
17+
* @param Route $route the document or entity for the route
1818
* @param array $defaults the getRouteDefaults array which may be altered by
1919
* the mapper
2020
*
2121
* @return string the controller to use with this route object including
2222
* the action, i.e. symfony_cmf_content.controller:indexAction
2323
* or false if the mapper can not determine the router
2424
*/
25-
function getController(RouteObjectInterface $document, array &$defaults);
25+
function getController(Route $route, array &$defaults);
2626

2727
}

Mapper/ExplicitTemplateMapper.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
namespace Symfony\Cmf\Component\Routing\Mapper;
44

5+
use Symfony\Component\Routing\Route;
6+
57
use Symfony\Cmf\Component\Routing\RouteObjectInterface;
68

79
/**
@@ -37,7 +39,7 @@ public function __construct($genericController)
3739
*
3840
* {@inheritDoc}
3941
*/
40-
public function getController(RouteObjectInterface $document, array &$defaults)
42+
public function getController(Route $document, array &$defaults)
4143
{
4244
if (! isset($defaults[RouteObjectInterface::TEMPLATE_NAME])) {
4345
return false;

Mapper/TemplateClassMapper.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Symfony\Cmf\Component\Routing\Mapper;
44

5+
use Symfony\Component\Routing\Route;
56
use Symfony\Cmf\Component\Routing\RouteObjectInterface;
67

78
/**
@@ -51,9 +52,12 @@ public function __construct($genericController, array $templatesByClass = array(
5152
*
5253
* {@inheritDoc}
5354
*/
54-
public function getController(RouteObjectInterface $document, array &$defaults)
55+
public function getController(Route $route, array &$defaults)
5556
{
56-
$content = $document->getRouteContent();
57+
if (! $route instanceof RouteObjectInterface) {
58+
return false;
59+
}
60+
$content = $route->getRouteContent();
5761
if (null == $content) {
5862
return false;
5963
}

README.md

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,10 @@ the Symfony2 router instance so you can still use the standard way for some of
1313
your routes.
1414

1515
Additionally, this component is meant to provide useful router implementations.
16-
Currently, there is the *DoctrineRouter* that routes based on doctrine database
17-
entities or documents that extend Symfony2 Route objects.
16+
Currently, there is the *DynamicRouter* that routes based on a implemented
17+
repository that provide Symfony2 Route objects. The repository can be
18+
implemented using a database, for example with doctrine phpcr-odm or doctrine
19+
orm.
1820

1921
**Note**: To use this component outside of the Symfony2 framework context, have
2022
a look at the [Symfony2 Routing component](https://github.com/symfony/Routing)
@@ -28,7 +30,7 @@ This component uses [composer](http://getcomposer.org). It needs the
2830
Symfony2 Routing component and the Symfony2 HttpKernel (for the logger
2931
interface and cache warmup interface).
3032

31-
For the DoctrineRouter you will need something to implement the
33+
For the DynamicRouter you will need something to implement the
3234
RouteRepositoryInterface with. We suggest using Doctrine as this allows to map
3335
any class into a database.
3436

@@ -40,14 +42,14 @@ chained routers. Add your router instances with the ``add`` method, then try
4042
to resolve routes with all added routers using the ``match`` method and
4143
Please refer to the phpdoc comments on the public methods for details.
4244

43-
## Doctrine Router
45+
## Dynamic Router
4446

4547
This implementation of a router loads routes from a RouteRepositoryInterface.
4648
This interface can be easily implemented with doctrine.
4749
The router works with the base UrlMatcher and UrlGenerator classes and only
4850
adds loading routes from the database and the concept of referenced content.
4951

50-
To instantiate a DoctrineRouter, you need an implementation of the
52+
To instantiate a DynamicRouter, you need an implementation of the
5153
RouteRepositoryInterface. See the [Symfony2 RoutingExtraBundle](https://github.com/symfony-cmf/RoutingExtraBundle)
5254
document classes for an example.
5355

@@ -57,7 +59,7 @@ content.
5759

5860
### Match Process
5961

60-
The match method of the DoctrineRouter does the following steps
62+
The match method of the DynamicRouter does the following steps
6163

6264
* Ask the repository for Route documents that could match the requested url
6365
* Build a route collection and let the UrlMatcher find a matching route
@@ -85,7 +87,7 @@ to Symfony\Cmf\Component\Routing\RedirectRouteInterface
8587

8688
You can use the _locale default value in a route to create one route per locale
8789
that all reference the same multilingual content.
88-
The DoctrineRouter respects the _locale when generating routes from content.
90+
The DynamicRouter respects the _locale when generating routes from content.
8991
When resolving the route, the _locale gets into the request and is picked up
9092
by the symfony locale system.
9193

@@ -99,7 +101,7 @@ under the same url is not a good idea.
99101
You can add more ControllerMapperInterface implementations if you have a case
100102
not handled by the provided ones.
101103

102-
For more specific needs, have a look at DoctrineRouter and see if you want to
104+
For more specific needs, have a look at DynamicRouter and see if you want to
103105
extend it. You can also write your own routers to hook into the chain.
104106

105107
### Url generation

RedirectRouteInterface.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,18 +12,18 @@
1212
*
1313
* - uri: an absolute uri
1414
* - routeName and routeParameters: to be used with the standard symfony router
15-
* or a route entry in the routeParameters for the DoctrineRouter. Precedency
15+
* or a route entry in the routeParameters for the DynamicRouter. Precedency
1616
* between these is determined by the order of the routers in the chain
1717
* router.
1818
*
1919
* With standard Symfony routing, you can just use uri / routeName and a
2020
* hashmap of parameters.
2121
*
22-
* For the doctrine router, you can return a RouteInterface instance in the
22+
* For the dynamic router, you can return a RouteInterface instance in the
2323
* field 'route' of the parameters.
2424
*
2525
* Note: getRedirectContent must return the redirect route itself for the
26-
* integration with DoctrineRouter to work.
26+
* integration with DynamicRouter to work.
2727
*
2828
* @author David Buchmann <[email protected]>
2929
*/
@@ -56,7 +56,7 @@ function isPermanent();
5656
/**
5757
* Get the parameters for the target route router::generate()
5858
*
59-
* Note that for the DoctrineRouter, you return the target route
59+
* Note that for the DynamicRouter, you return the target route
6060
* document as field 'route' of the hashmap.
6161
*
6262
* @return array Information to build the route

RouteAwareInterface.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
/**
66
* Interface to be implemented by content that wants to be compatible with the
7-
* DoctrineRouter
7+
* DynamicRouter
88
*/
99
interface RouteAwareInterface
1010
{

RouteObjectInterface.php

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,11 @@
33
namespace Symfony\Cmf\Component\Routing;
44

55
/**
6-
* Documents for entries in the routing table need to implement this interface
7-
* in addition to extending Symfony\Component\Routing\Route for the symfony router
8-
* so the DoctrineRouter can handle them.
6+
* Classes for entries in the routing table may implement this interface in
7+
* addition to extending Symfony\Component\Routing\Route.
8+
*
9+
* If they do, the DynamicRouter will request the route content and put it into
10+
* the RouteObjectInterface::CONTENT_OBJECT field.
911
*
1012
* Some fields in defaults have a special meaning in the getDefaults(). In addition
1113
* to the constants defined in this class, _locale and _controller are also used.
@@ -21,11 +23,16 @@ interface RouteObjectInterface
2123
const CONTROLLER_ALIAS = '_controller_alias';
2224

2325
/**
24-
* An explicit template to be used with this route.
26+
* Field name for an explicit template to be used with this route.
2527
* i.e. SymfonyCmfContentBundle:StaticContent:index.html.twig
2628
*/
2729
const TEMPLATE_NAME = '_template';
2830

31+
/**
32+
* Field name for the content of the current route, if any.
33+
*/
34+
const CONTENT_OBJECT = '_content';
35+
2936
/**
3037
* Get the content document this route entry stands for. If non-null,
3138
* the ControllerClassMapper uses it to identify a controller and

RouteRepositoryInterface.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
namespace Symfony\Cmf\Component\Routing;
44

55
/**
6-
* Interface for the route provider the DoctrineRouter is using.
6+
* Interface for the route provider the DynamicRouter is using.
77
*
88
* Typically this could be a doctrine orm or odm repository, but you can
99
* implement something else if you need to.
@@ -14,7 +14,8 @@ interface RouteRepositoryInterface
1414
* Find routes that could match this absolute path.
1515
*
1616
* This may return a mixed list of class instances, but all routes returned
17-
* must extend the core symfony route.
17+
* must extend the core symfony route. The classes may also implement
18+
* RouteObjectInterface to link to a content document.
1819
*
1920
* This method may not throw an exception based on implementation specific
2021
* restrictions on the url. That case is considered a not found - returning

0 commit comments

Comments
 (0)