Skip to content
This repository was archived by the owner on Sep 16, 2021. It is now read-only.

Commit af995a3

Browse files
committed
Merge pull request #449 from symfony-cmf/cmf-request-aware
update to removed cmf_request_aware
2 parents 016e426 + 51f6f75 commit af995a3

File tree

3 files changed

+68
-60
lines changed

3 files changed

+68
-60
lines changed

bundles/core/dependency_injection_tags.rst

Lines changed: 12 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,20 @@
44
Dependency Injection Tags
55
-------------------------
66

7-
.. _bundle-core-tags-request-aware:
8-
97
cmf_request_aware
108
~~~~~~~~~~~~~~~~~
119

12-
If you have services that need the request (e.g. for the publishing workflow
13-
or current menu item voters), you can tag them with ``cmf_request_aware`` to
14-
have a kernel listener inject the request. Any class used in such a tagged
15-
service must have the ``setRequest`` method or you will get a fatal error::
10+
.. caution::
11+
12+
This tag has been deprecated in CoreBundle 1.1 and will be removed
13+
in CoreBundle 1.2. Since Symfony 2.3, you can profit from the fact
14+
that the request is a `synchronized service`_.
15+
16+
When working with the 1.0 version of the CMF in Symfony 2.2 and you have
17+
services that need the request (e.g. for the publishing workflow or current
18+
menu item voters), you can tag services with ``cmf_request_aware`` to have a
19+
kernel listener inject the request. Any class used in such a tagged service
20+
must have the ``setRequest`` method or you will get a fatal error::
1621

1722
use Symfony\Component\HttpFoundation\Request;
1823

@@ -26,20 +31,6 @@ service must have the ``setRequest`` method or you will get a fatal error::
2631
}
2732
}
2833

29-
.. caution::
30-
31-
You should only use this tag on services that will be needed on every
32-
request. If you use this tag excessively you will run into performance
33-
issues. For seldom used services, you can inject the container in the
34-
service definition and call ``$this->container->get('request')`` in your
35-
code when you actually need the request.
36-
37-
For Symfony 2.3, this tag is automatically translated to a
38-
`synchronized service`_ but Symfony 2.2 does not have that feature, so you can
39-
use this tag for bundles that you want to be able to work with Symfony 2.2. In
40-
custom applications that run with Symfony 2.3, there is no need for this tag,
41-
just use the synchronized service feature.
42-
4334
cmf_published_voter
4435
~~~~~~~~~~~~~~~~~~~
4536

@@ -51,4 +42,4 @@ workflow.
5142
This tag has the attribute ``priority``. The lower the priority number, the
5243
earlier the voter gets to vote.
5344

54-
.. _`synchronized service`: http://symfony.com/doc/current/cookbook/service_container/scopes.html#using-a-synchronized-service
45+
.. _`synchronized service`: http://symfony.com/doc/current/cookbook/service_container/scopes.html#a-using-a-synchronized-service

bundles/menu/voters.rst

Lines changed: 21 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ unique page and it is perhaps preferable instead to indicate which menu item mos
1919

2020
For example, you are developing a homepage application which features a blog.
2121
The blog index page has a menu item but there are no menu items for the posts
22-
associated with the blog and so none of the menu items will be highlighted.
22+
associated with the blog and so none of the menu items will be highlighted.
2323

2424
When the user is viewing a blog post you want the menu item for the blog to be
2525
highlighted. The CMF facilitates this with current item *voter* classes.
@@ -47,7 +47,7 @@ RequestContentIdentityVoter
4747
This voter looks at the ``content`` field of the menu item extras and compares
4848
it with the main content attribute of the request. The name for the main
4949
content attribute in the request is configurable with the ``content_key``
50-
option - if not set it defaults to the constant
50+
option - if not set it defaults to the constant
5151
``Symfony\Cmf\Bundle\RoutingBundle\Routing\DynamicRouter::CONTENT_KEY`` (which resolves to ``contentDocument``).
5252

5353
You can enable this voter by adding the ``cmf_menu.voters.content_identity``
@@ -197,9 +197,8 @@ menu item.
197197

198198
To use this voter you need to configure a custom service with the name of the
199199
content in the request and your model class to avoid calling getParent on
200-
objects that do not have that method. You need to tag the service as
201-
``cmf_menu.voter`` and also as ``cmf_request_aware`` because it
202-
depends on the request. The service looks the same as for complete custom
200+
objects that do not have that method. You need to tag the service as
201+
``cmf_menu.voter``. The service looks the same as for complete custom
203202
voters (see below), except you do not need to write your own PHP code:
204203

205204
.. configuration-block::
@@ -212,9 +211,10 @@ voters (see below), except you do not need to write your own PHP code:
212211
arguments:
213212
- contentDocument
214213
- "%my_bundle.my_model_class%"
214+
calls:
215+
- [setRequest, ["@?request="]]
215216
tags:
216217
- { name: "cmf_menu.voter" }
217-
- { name: "cmf_request_aware" }
218218
219219
.. code-block:: xml
220220
@@ -227,8 +227,15 @@ voters (see below), except you do not need to write your own PHP code:
227227
class="Symfony\Cmf\Bundle\MenuBundle\Voter\RequestParentContentIdentityVoter">
228228
<argument>contentDocument</argument>
229229
<argument>%my_bundle.my_model_class%</argument>
230+
<call method="setRequest">
231+
<argument
232+
type="service"
233+
id="request"
234+
on-invalid="null"
235+
strict="false"
236+
/>
237+
</call>
230238
<tag name="cmf_menu.voter"/>
231-
<tag name="cmf_request_aware"/>
232239
</service>
233240
</services>
234241
@@ -240,8 +247,14 @@ voters (see below), except you do not need to write your own PHP code:
240247
'Symfony\Cmf\Bundle\MenuBundle\Voter\RequestParentContentIdentityVoter',
241248
array('contentDocument', '%my_bundle.my_model_class%')
242249
));
250+
$definition->addMethodCall('setRequest', array(
251+
new Reference(
252+
'request',
253+
ContainerInterface::NULL_ON_INVALID_REFERENCE,
254+
false
255+
)
256+
));
243257
$definition->addTag('cmf_menu.voter');
244-
$definition->addTag('cmf_request_aware');
245258
246259
$container->setDefinition('my_bundle.menu_voter.parent', $definition);
247260
@@ -253,15 +266,6 @@ Custom Voter
253266
Voters must implement the ``Symfony\Cmf\MenuBundle\Voter\VoterInterface``. To
254267
make the menu bundle notice the voter, tag it with ``cmf_menu.voter``.
255268

256-
If your voter requires the current ``Request`` object in its decision making
257-
process you can add the :ref:`synchronized <bundle-core-tags-request-aware>`
258-
tag to your service container definition.
259-
260-
.. note::
261-
262-
This tag is not required for versions of Symfony >= 2.3, see the
263-
:ref:`documentation <bundle-core-tags-request-aware>` for more information.
264-
265269
If you need to know the content the menu item points to, look in the
266270
``content`` field of the menu item extras: ``$item->getExtra('content');``.
267271
The ``ContentAwareFactory`` places the content referenced by the route there -
@@ -306,4 +310,3 @@ A voter will look something like this::
306310
return null;
307311
}
308312
}
309-

cookbook/creating_a_cms/the-frontend.rst

Lines changed: 35 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -89,15 +89,15 @@ KnpMenuBundle::
8989
Don't forget to add the ``Knp\Menu\NodeInterface`` use statement!
9090

9191
Menus are hierarchical, PHPCR-ODM is also hierarchical and so lends itself
92-
well to this use case.
92+
well to this use case.
9393

9494
Here you add an additional mapping, ``@Children``, which will cause PHPCR-ODM
9595
to populate the annotated property instance ``$children`` with the child
9696
documents of this document.
9797

9898
The options are the options used by KnpMenu system when rendering the menu.
9999
The menu URL is inferred from the ``content`` option (note that you added the
100-
``RouteReferrersReadInterface`` to ``Page`` earlier).
100+
``RouteReferrersReadInterface`` to ``Page`` earlier).
101101

102102
The attributes apply to the HTML elements. See the `KnpMenu`_ documentation
103103
for more information.
@@ -157,28 +157,29 @@ Now you can register the ``PhpcrMenuProvider`` from the menu bundle in the servi
157157
configuration:
158158

159159
.. configuration-block::
160-
160+
161161
.. code-block:: yaml
162162
163-
# src/Acme/BasicCmsBundle/Resources/config/config.yml
164-
services:
165-
acme.basic_cms.menu_provider:
166-
class: Symfony\Cmf\Bundle\MenuBundle\Provider\PhpcrMenuProvider
167-
arguments:
168-
- '@cmf_menu.factory'
169-
- '@doctrine_phpcr'
170-
- /cms/pages
171-
tags:
172-
- { name: knp_menu.provider }
173-
- { name: cmf_request_aware }
163+
# src/Acme/BasicCmsBundle/Resources/config/config.yml
164+
services:
165+
acme.basic_cms.menu_provider:
166+
class: Symfony\Cmf\Bundle\MenuBundle\Provider\PhpcrMenuProvider
167+
arguments:
168+
- '@cmf_menu.factory'
169+
- '@doctrine_phpcr'
170+
- /cms/pages
171+
calls:
172+
- [setRequest, ["@?request="]]
173+
tags:
174+
- { name: knp_menu.provider }
174175
175176
.. code-block:: xml
176177
177178
<?xml version="1.0" encoding="UTF-8" ?>
178179
<container xmlns="http://symfony.com/schema/dic/services"
179180
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
180181
xmlns:acme_demo="http://www.example.com/symfony/schema/"
181-
xsi:schemaLocation="http://symfony.com/schema/dic/services
182+
xsi:schemaLocation="http://symfony.com/schema/dic/services
182183
http://symfony.com/schema/dic/services/services-1.0.xsd">
183184
184185
<!-- ... -->
@@ -190,18 +191,25 @@ configuration:
190191
<argument type="service" id="cmf_menu.factory"/>
191192
<argument type="service" id="doctrine_phpcr"/>
192193
<argument>/cms/pages</argument>
194+
<call method="setRequest">
195+
<argument
196+
type="service"
197+
id="request"
198+
on-invalid="null"
199+
strict="false"
200+
/>
201+
</call>
193202
<tag name="knp_menu.provider" />
194-
<tag name="cmf_request_aware"/>
195203
</service>
196204
</services>
197205
</container>
198-
206+
199207
.. code-block:: php
200208
201209
// src/Acme/BasicCmsBundle/Resources/config/config.php
202210
use Symfony\Component\DependencyInjection\Reference;
203211
// ...
204-
212+
205213
$container
206214
->register(
207215
'acme.basic_cms.menu_provider',
@@ -210,8 +218,14 @@ configuration:
210218
->addArgument(new Reference('cmf_menu.factory'))
211219
->addArgument(new Reference('doctrine_phpcr'))
212220
->addArgument('/cms/pages')
221+
->addMethodCall('setRequest', array(
222+
new Reference(
223+
'request',
224+
ContainerInterface::NULL_ON_INVALID_REFERENCE,
225+
false
226+
)
227+
))
213228
->addTag('knp_menu.provider')
214-
->addTag('cmf_request_aware')
215229
;
216230
217231
and enable the twig rendering functionality of the KnpMenu bundle:
@@ -246,7 +260,7 @@ and finally you can render the menu!
246260
.. configuration-block::
247261

248262
.. code-block:: jinja
249-
263+
250264
{# src/Acme/BasicCmsBundle/Resources/views/Default/page.html.twig #}
251265
252266
{# ... #}
@@ -255,7 +269,7 @@ and finally you can render the menu!
255269
.. code-block:: html+php
256270

257271
<!-- src/Acme/BasicCmsBundle/Resources/views/Default/page.html.php -->
258-
272+
259273
<!-- ... -->
260274
<?php echo $view['knp_menu']->render('main') ?>
261275

0 commit comments

Comments
 (0)