|
4 | 4 | Customization
|
5 | 5 | -------------
|
6 | 6 |
|
7 |
| -.. _routingauto_customization_pathproviders: |
| 7 | +.. _routingauto_customization_tokenproviders: |
8 | 8 |
|
9 |
| -Adding Path Providers |
10 |
| -~~~~~~~~~~~~~~~~~~~~~ |
| 9 | +Token Providers |
| 10 | +~~~~~~~~~~~~~~~ |
11 | 11 |
|
12 |
| -The goal of a ``PathProvider`` class is to add one or several path elements to |
13 |
| -the route stack. For example, the following provider will add the path |
14 |
| -``foo/bar`` to the route stack:: |
| 12 | +The goal of a ``TokenProvider`` class is to provide values for tokens in the |
| 13 | +URI schema. Such values can be derived form the object for which the route |
| 14 | +is being generated, or from the environment (e.g. the you could use the |
| 15 | +current locale in the route). |
15 | 16 |
|
16 |
| - // src/Acme/CmsBundle/RoutingAuto/PathProvider/FoobarProvider.php |
17 |
| - namespace Acme\CmsBundle\RoutingAuto\PathProvider; |
| 17 | +The following token provider will simply provide the value "foobar":: |
18 | 18 |
|
19 |
| - use Symfony\Cmf\Bundle\RoutingAutoBundle\AutoRoute\PathProviderInterface; |
20 |
| - use Symfony\Cmf\Bundle\RoutingAutoBundle\AutoRoute\RouteStack; |
| 19 | + // src/Acme/CmsBundle/RoutingAuto/PathProvider/FoobarTokenProvider.php |
| 20 | + namespace Symfony\Cmf\Component\RoutingAuto\TokenProvider; |
21 | 21 |
|
22 |
| - class FoobarProvider implements PathProviderInterface |
| 22 | + use Symfony\Cmf\Component\RoutingAuto\TokenProviderInterface; |
| 23 | + use Symfony\Component\OptionsResolver\OptionsResolverInterface; |
| 24 | + use Symfony\Cmf\Component\RoutingAuto\UriContext; |
| 25 | + |
| 26 | + class FoobarTokenProvider implements TokenProviderInterface |
23 | 27 | {
|
24 |
| - public function providePath(RouteStack $routeStack) |
| 28 | + /** |
| 29 | + * {@inheritDoc} |
| 30 | + */ |
| 31 | + public function provideValue(UriContext $uriContext, $options) |
| 32 | + { |
| 33 | + return 'foobar'; |
| 34 | + } |
| 35 | + |
| 36 | + /** |
| 37 | + * {@inheritDoc} |
| 38 | + */ |
| 39 | + public function configureOptions(OptionsResolverInterface $optionsResolver) |
25 | 40 | {
|
26 |
| - $routeStack->addPathElements(array('foo', 'bar')); |
27 | 41 | }
|
28 | 42 | }
|
29 | 43 |
|
30 | 44 | To use the path provider you must register it in the container and add the
|
31 |
| -``cmf_routing_auto.provider`` tag and set the **alias** accordingly: |
| 45 | +``cmf_routing_auto.token_provider`` tag and set the **alias** accordingly: |
32 | 46 |
|
33 | 47 | .. configuration-block::
|
34 | 48 |
|
35 | 49 | .. code-block:: yaml
|
36 | 50 |
|
37 | 51 | services:
|
38 |
| - acme_cms.path_provider.foobar: |
39 |
| - class: Acme\CmsBundle\RoutingAuto\PathProvider\FoobarProvider |
40 |
| - scope: prototype |
| 52 | + acme_cms.token_provider.foobar: |
| 53 | + class: Acme\CmsBundle\RoutingAuto\PathProvider\FoobarTokenProvider |
41 | 54 | tags:
|
42 |
| - - { name: cmf_routing_auto.provider, alias: "foobar"} |
| 55 | + - { name: cmf_routing_auto.token_provider, alias: "foobar" } |
43 | 56 |
|
44 | 57 | .. code-block:: xml
|
45 | 58 |
|
46 | 59 | <?xml version="1.0" encoding="UTF-8" ?>
|
47 | 60 | <container xmlns="http://symfony.com/schema/dic/services">
|
48 | 61 | <service
|
49 |
| - id="acme_cms.path_provider.foobar" |
50 |
| - class="Acme\CmsBundle\RoutingAuto\PathProvider\FoobarProvider" |
51 |
| - scope="prototype" |
| 62 | + id="acme_cms.token_provider.foobar" |
| 63 | + class="Acme\CmsBundle\RoutingAuto\PathProvider\FoobarTokenProvider" |
52 | 64 | >
|
53 |
| - <tag name="cmf_routing_auto.provider" alias="foobar"/> |
| 65 | + <tag name="cmf_routing_auto.token_provider" alias="foobar"/> |
54 | 66 | </service>
|
55 | 67 | </container>
|
56 | 68 |
|
57 | 69 | .. code-block:: php
|
58 | 70 |
|
59 | 71 | use Symfony\Component\DependencyInjection\Definition;
|
60 | 72 |
|
61 |
| - $definition = new Definition('Acme\CmsBundle\RoutingAuto\PathProvider\FoobarProvider'); |
62 |
| - $definition->addTag('cmf_routing_auto.provider', array('alias' => 'foobar')); |
63 |
| - $definition->setScope('prototype'); |
| 73 | + $definition = new Definition('Acme\CmsBundle\RoutingAuto\PathProvider\FoobarTokenProvider'); |
| 74 | + $definition->addTag('cmf_routing_auto.token_provider', array('alias' => 'foobar')); |
64 | 75 |
|
65 |
| - $container->setDefinition('acme_cms.path_provider.foobar', $definition); |
| 76 | + $container->setDefinition('acme_cms.token_provider.foobar', $definition); |
66 | 77 |
|
67 |
| -The ``FoobarProvider`` is now available as **foobar** in the routing auto |
| 78 | +The ``FoobarTokenProvider`` is now available as **foobar** in the routing auto |
68 | 79 | configuration.
|
69 | 80 |
|
70 |
| -.. caution:: |
| 81 | +Conflict Resolvers |
| 82 | +~~~~~~~~~~~~~~~~~~ |
| 83 | + |
| 84 | +Conflict resolvers decide what happens if a generated route already exists in |
| 85 | +the route repository and is not related to the context object. |
| 86 | + |
| 87 | +The following example will append an unique string to the URI to resolve a |
| 88 | +conflict:: |
| 89 | + |
| 90 | + namespace Symfony\Cmf\Component\RoutingAuto\ConflictResolver; |
| 91 | + |
| 92 | + use Symfony\Cmf\Component\RoutingAuto\ConflictResolverInterface; |
| 93 | + use Symfony\Cmf\Component\RoutingAuto\UriContext; |
| 94 | + use Symfony\Cmf\Component\RoutingAuto\Adapter\AdapterInterface; |
| 95 | + |
| 96 | + class UniqidConflictResolver implements ConflictResolverInterface |
| 97 | + { |
| 98 | + public function resolveConflict(UriContext $uriContext) |
| 99 | + { |
| 100 | + $uri = $uriContext->getUri(); |
| 101 | + return sprintf('%s-%s', uniqid()); |
| 102 | + } |
| 103 | + } |
| 104 | + |
| 105 | +It is registered in the DI configuration as follows: |
| 106 | + |
| 107 | +.. configuration-block:: |
| 108 | + |
| 109 | + .. code-block:: yaml |
| 110 | +
|
| 111 | + services: |
| 112 | + acme_cms.conflict_resolver.foobar: |
| 113 | + class: Acme\CmsBundle\RoutingAuto\ConflictResolver\UniqidConflictResolver |
| 114 | + tags: |
| 115 | + - { name: cmf_routing_auto.conflict_resolver, alias: "uniqid"} |
| 116 | +
|
| 117 | + .. code-block:: xml |
| 118 | +
|
| 119 | + <?xml version="1.0" encoding="UTF-8" ?> |
| 120 | + <container xmlns="http://symfony.com/schema/dic/services"> |
| 121 | + <service |
| 122 | + id="acme_cms.conflict_resolver.foobar" |
| 123 | + class="Acme\CmsBundle\RoutingAuto\ConflictResolver\UniqidConflictResolver" |
| 124 | + > |
| 125 | + <tag name="cmf_routing_auto.conflict_resolver" alias="uniqid"/> |
| 126 | + </service> |
| 127 | + </container> |
| 128 | +
|
| 129 | + .. code-block:: php |
| 130 | +
|
| 131 | + use Symfony\Component\DependencyInjection\Definition; |
71 | 132 |
|
72 |
| - Both path providers and path actions need to be defined with a scope of |
73 |
| - "prototype". This ensures that each time the auto routing system requests |
74 |
| - the class a new one is given and you do not have any state problems. |
| 133 | + $definition = new Definition('Acme\CmsBundle\RoutingAuto\ConflictResolver\UniqidConflictResolver'); |
| 134 | + $definition->addTag('cmf_routing_auto.conflict_resolver', array('alias' => 'foobar')); |
75 | 135 |
|
76 |
| -Adding Path Actions |
77 |
| -~~~~~~~~~~~~~~~~~~~ |
| 136 | + $container->setDefinition('acme_cms.conflict_resolver.uniqid', $definition); |
78 | 137 |
|
79 |
| -In the auto routing system, a "path action" is an action to take if the path |
80 |
| -provided by the "path provider" exists or not. |
| 138 | +Defunct Route Handlers |
| 139 | +~~~~~~~~~~~~~~~~~~~~~~ |
81 | 140 |
|
82 |
| -You can add a path action by extending the ``PathActionInterface`` and |
83 |
| -registering your new class correctly in the DI configuration. |
| 141 | +Defunct Route Handlers decide what happens to old routes when an object is |
| 142 | +updated and its generated URI changes. |
84 | 143 |
|
85 |
| -This is a very simple implementation from the bundle - it is used to throw an |
86 |
| -exception when a path already exists:: |
| 144 | +They are not all-together trivial - the following handler removes old routes and is |
| 145 | +the default handler:: |
87 | 146 |
|
88 |
| - namespace Symfony\Cmf\Bundle\RoutingAutoBundle\RoutingAuto\PathNotExists; |
| 147 | + namespace Symfony\Cmf\Component\RoutingAuto\DefunctRouteHandler; |
89 | 148 |
|
90 |
| - use Symfony\Cmf\Bundle\RoutingAutoBundle\AutoRoute\PathActionInterface; |
91 |
| - use Symfony\Cmf\Bundle\RoutingAutoBundle\AutoRoute\Exception\CouldNotFindRouteException; |
92 |
| - use Symfony\Cmf\Bundle\RoutingAutoBundle\AutoRoute\RouteStack; |
| 149 | + use Symfony\Cmf\Component\RoutingAuto\DefunctRouteHandlerInterface; |
| 150 | + use Symfony\Cmf\Component\RoutingAuto\UriContextCollection; |
| 151 | + use Symfony\Cmf\Component\RoutingAuto\Adapter\AdapterInterface; |
93 | 152 |
|
94 |
| - class ThrowException implements PathActionInterface |
| 153 | + class RemoveDefunctRouteHandler implements DefunctRouteHandlerInterface |
95 | 154 | {
|
96 |
| - public function init(array $options) |
| 155 | + protected $adapter; |
| 156 | + |
| 157 | + public function __construct(AdapterInterface $adapter) |
97 | 158 | {
|
| 159 | + $this->adapter = $adapter; |
98 | 160 | }
|
99 | 161 |
|
100 |
| - public function execute(RouteStack $routeStack) |
| 162 | + public function handleDefunctRoutes(UriContextCollection $uriContextCollection) |
101 | 163 | {
|
102 |
| - throw new CouldNotFindRouteException('/'.$routeStack->getFullPath()); |
| 164 | + $referringAutoRouteCollection = $this->adapter->getReferringAutoRoutes($uriContextCollection->getSubjectObject()); |
| 165 | + |
| 166 | + foreach ($referringAutoRouteCollection as $referringAutoRoute) { |
| 167 | + if (false === $uriContextCollection->containsAutoRoute($referringAutoRoute)) { |
| 168 | + $newRoute = $uriContextCollection->getAutoRouteByTag($referringAutoRoute->getAutoRouteTag()); |
| 169 | + |
| 170 | + $this->adapter->migrateAutoRouteChildren($referringAutoRoute, $newRoute); |
| 171 | + $this->adapter->removeAutoRoute($referringAutoRoute); |
| 172 | + } |
| 173 | + } |
103 | 174 | }
|
104 | 175 | }
|
105 | 176 |
|
106 |
| -The ``init()`` method configures the provider (throwing errors when required |
107 |
| -options do not exists) and the ``execute()`` method executes the action. |
108 |
| - |
109 | 177 | It is registered in the DI configuration as follows:
|
110 | 178 |
|
111 | 179 | .. configuration-block::
|
112 | 180 |
|
113 | 181 | .. code-block:: yaml
|
114 | 182 |
|
115 | 183 | services:
|
116 |
| - cmf_routing_auto.not_exists_action.throw_exception: |
117 |
| - class: Symfony\Cmf\Bundle\RoutingAutoBundle\RoutingAuto\PathNotExists\ThrowException |
118 |
| - scope: prototype |
| 184 | + acme_cms.defunct_route_handler.foobar: |
| 185 | + class: Acme\CmsBundle\RoutingAuto\DefunctRouteHandler\RemoveConflictResolver |
119 | 186 | tags:
|
120 |
| - - { name: cmf_routing_auto.not_exists_action, alias: "throw_exception"} |
| 187 | + - { name: cmf_routing_auto.defunct_route_handler, alias: "remove"} |
121 | 188 |
|
122 | 189 | .. code-block:: xml
|
123 | 190 |
|
124 | 191 | <?xml version="1.0" encoding="UTF-8" ?>
|
125 | 192 | <container xmlns="http://symfony.com/schema/dic/services">
|
126 | 193 | <service
|
127 |
| - id="cmf_routing_auto.not_exists_action.throw_exception" |
128 |
| - class="Symfony\Cmf\Bundle\RoutingAutoBundle\RoutingAuto\PathNotExists\ThrowException" |
129 |
| - scope="prototype" |
130 |
| - > |
131 |
| - <tag name="cmf_routing_auto.not_exists_action" alias="throw_exception"/> |
| 194 | + id="acme_cms.defunct_route_handler.foobar" |
| 195 | + class="Acme\CmsBundle\RoutingAuto\DefunctRouteHandler\RemoveConflictResolver" |
| 196 | + > |
| 197 | + <tag name="cmf_routing_auto.defunct_route_handler" alias="remove"/> |
132 | 198 | </service>
|
133 | 199 | </container>
|
134 | 200 |
|
135 | 201 | .. code-block:: php
|
136 | 202 |
|
137 | 203 | use Symfony\Component\DependencyInjection\Definition;
|
138 | 204 |
|
139 |
| - $definition = new Definition('Symfony\Cmf\Bundle\RoutingAutoBundle\RoutingAuto\PathNotExists\ThrowException'); |
140 |
| - $definition->addTag('cmf_routing_auto.provider', array('alias' => 'throw_exception')); |
141 |
| - $definition->setScope('prototype'); |
142 |
| -
|
143 |
| - $container->setDefinition('cmf_routing_auto.not_exists_action.throw_exception', $definition); |
144 |
| -
|
145 |
| -Note the following: |
146 |
| - |
147 |
| -* **Scope**: Must *always* be set to *prototype*; |
148 |
| -* **Tag**: The tag registers the service with the auto routing system, it can |
149 |
| - be one of the following: |
150 |
| - |
151 |
| - * ``cmf_routing_auto.exists.action`` - if the action is to be used when a |
152 |
| - path exists; |
153 |
| - * ``cmf_routing_auto.not_exists.action`` - if the action is to be used when |
154 |
| - a path does not exist; |
| 205 | + $definition = new Definition('Acme\CmsBundle\RoutingAuto\DefunctRouteHandler\RemoveConflictResolver'); |
| 206 | + $definition->addTag('cmf_routing_auto.defunct_route_handler', array('alias' => 'foobar')); |
155 | 207 |
|
156 |
| -* **Alias**: The alias of the tag is the name by which you will reference this |
157 |
| - action in the auto routing configuration. |
| 208 | + $container->setDefinition('acme_cms.defunct_route_handler.remove', $definition); |
0 commit comments