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

Commit 3c46ef0

Browse files
committed
Merge pull request #282 from symfony-cmf/block-bundle
block: classes checked + added sonata prepended config
2 parents 7913ffe + cc75b37 commit 3c46ef0

File tree

8 files changed

+429
-351
lines changed

8 files changed

+429
-351
lines changed

bundles/block/cache.rst

Lines changed: 87 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,11 @@
11
Cache
22
=====
33

4-
.. include:: ../_outdate-caution.rst.inc
4+
The Symfony2 CMF BlockBundle integrates with the `SonataCacheBundle`_ to
5+
provide several caching solutions. Have a look at the available adapters in the
6+
`SonataCacheBundle`_ to see all options.
57

6-
The BlockBundle integrates with the `SonataCacheBundle`_ to provide several
7-
caching solutions. Have a look at the available adapters in the
8-
SonataCacheBundle to see all options.
9-
10-
The BlockBundle additionally provides its own adapters for:
8+
The Symfony2 CMF BlockBundle additionally provides its own adapters for:
119

1210
* `ESI`_
1311
* `SSI`_
@@ -58,7 +56,8 @@ CmfBlockBundle and the SonataBlockBundle:
5856
<!-- ... -->
5957
6058
<import resource="@SymfonyCmfBlockBundle/Resources/config/routing/cache.xml"
61-
prefix="/" />
59+
prefix="/"
60+
/>
6261
</routes>
6362
6463
.. code-block:: php
@@ -74,70 +73,82 @@ CmfBlockBundle and the SonataBlockBundle:
7473
3. *SonataBlockBundle* - Use the ``sonata_block`` key to configure the cache
7574
adapter for each block service:
7675

77-
.. configuration-block::
76+
.. configuration-block::
7877

79-
.. code-block:: yaml
78+
.. code-block:: yaml
8079
81-
# app/config/config.yml
82-
sonata_block:
83-
# ...
84-
blocks:
85-
acme_main.block.news:
86-
# use the service id of the cache adapter
87-
cache: cmf.block.cache.js_async
88-
blocks_by_class:
89-
# cache only the RssBlock and not all action blocks
90-
Symfony\Cmf\Bundle\BlockBundle\Doctrine\Phpcr\RssBlock:
80+
# app/config/config.yml
81+
sonata_block:
82+
# ...
83+
blocks:
84+
symfony_cmf.block.action:
85+
# use the service id of the cache adapter
9186
cache: cmf.block.cache.js_async
87+
blocks_by_class:
88+
# cache only the RssBlock and not all action blocks
89+
Symfony\Cmf\Bundle\BlockBundle\Doctrine\Phpcr\RssBlock:
90+
cache: cmf.block.cache.js_async
9291
93-
.. code-block:: xml
92+
.. code-block:: xml
9493
95-
<!-- app/config/config.xml -->
96-
<?xml version="1.0" charset="UTF-8" ?>
97-
<container xmlns="http://symfony.com/schema/dic/services">
94+
<!-- app/config/config.xml -->
95+
<?xml version="1.0" charset="UTF-8" ?>
96+
<container xmlns="http://symfony.com/schema/dic/services">
9897
9998
<config xmlns="http://sonata-project.org/schema/dic/block">
10099
<!-- use the service id of the cache adapter -->
101-
<blocks id="symfony_cmf.block.action"
100+
<block
101+
id="symfony_cmf.block.action"
102+
cache="symfony_cmf.block.cache.js_async"
103+
/>
104+
<block-by-class
105+
class="Symfony\Cmf\Bundle\BlockBundle\Doctrine\Phpcr\RssBlock"
102106
cache="symfony_cmf.block.cache.js_async"
103107
/>
104108
</config>
105109
</container>
106110
107-
.. code-block:: php
108-
109-
// app/config/config.php
110-
$container->loadFromExtension('sonata_block', array(
111-
'blocks' => array(
112-
'symfony_cmf.block.action' => array(
113-
// use the service id of the cache adapter
114-
'cache' => 'symfony_cmf.block.cache.js_async',
115-
),
116-
),
117-
));
111+
.. code-block:: php
112+
113+
// app/config/config.php
114+
$container->loadFromExtension('sonata_block', array(
115+
'blocks' => array(
116+
'symfony_cmf.block.action' => array(
117+
// use the service id of the cache adapter
118+
'cache' => 'symfony_cmf.block.cache.js_async',
119+
),
120+
),
121+
'blocks_by_class' => array(
122+
'Symfony\Cmf\Bundle\BlockBundle\Doctrine\Phpcr\RssBlock' => array(
123+
'cache' => 'symfony_cmf.block.cache.js_async',
124+
),
125+
),
126+
),
127+
));
118128
119129
Workflow
120130
--------
121131

122-
The following happens when a block is rendered using cache:
132+
When a block having a cache configured is rendered, the following process
133+
is triggered:
123134

124-
* A document is loaded based on the name
135+
* The document is loaded based on the name;
125136
* If caching is configured, the cache is checked and content is returned if
126-
found
137+
found.
127138

128139
* Cache keys are computed using:
129140

130-
* The cache keys of the block service
131-
* The extraCacheKeys passed from the template
141+
* The cache keys of the block service;
142+
* The extraCacheKeys passed from the template.
132143

133-
* The cache adapter is asked for a cache element
144+
* The cache adapter is asked for a cache element:
134145

135146
* The ESI and SSI adapter add a specific tag and a url to retrieve the
136-
block content
137-
* The Javascript adapter adds javascript and a url to retrieve the block
138-
content
147+
block content;
148+
* The javascript adapter adds javascript and a url to retrieve the block
149+
content.
139150

140-
* If the cache element is not expired and has data it is returned
151+
* If the cache element is not expired and has data it is returned.
141152
* The template is rendered:
142153

143154
* For ESI and SSI the url is called to retrieve the block content
@@ -150,55 +161,60 @@ The following happens when a block is rendered using cache:
150161
cache is found, have a look at the ``has`` method of the adapters in the
151162
SonataCacheBundle to see how they respond.
152163

153-
If cache is checked and the cache adapter returned that no cache was found,
164+
If the cache is checked and the cache adapter returned that no cache was found,
154165
the workflow proceeds like this:
155166

156-
* Each block document also has a block service, the execute method of it is
157-
called to render the block and return a response
158-
* If the response is cacheable the configured adapter creates a cache element,
159-
it contains
167+
* The block service is asked to render the block
168+
:ref:`as usual <bundle-block-execute>`;
169+
* If the ``Response`` is cacheable, the configured adapter creates a cache
170+
element containing:
160171

161-
* The computed cache keys
162-
* The ttl of the response
163-
* The response
164-
* And additional contextual keys
172+
* The computed cache keys;
173+
* The time to live (TTL) of the response;
174+
* The ``Response``;
175+
* Any additional contextual keys.
165176

166-
* The template is rendered
177+
* The template is rendered.
167178

168179
Cache Keys
169180
----------
170181

171-
The block service has the responsibility to generate the cache keys, the
172-
method ``getCacheKeys`` returns these keys, see :ref:`bundle-block-service`.
182+
It is the responsibility of the :ref:`block service <bundle-block-service>` to generate the cache keys in
183+
the method ``getCacheKeys``.
173184

174-
The block services shipped with the BlockBunde use the ``getCacheKeys`` method
175-
of the ``Sonata\BlockBundle\Block\BaseBlockService``, and return:
185+
The block services shipped with the Symfony CMF BlockBunde use the
186+
``getCacheKeys`` method of the ``Sonata\BlockBundle\Block\BaseBlockService``
187+
which returns:
176188

177189
* ``block_id``
178190
* ``updated_at``
179191

180-
.. note::
192+
.. caution::
181193

182-
If block settings need to be persisted between requests it is advised to
183-
store them in the block document. Alternatively they can be added to the
184-
cache keys. However be very cautious because, depending on the adapter,
185-
the cache keys can be send to the browser and are not secure.
194+
If block settings need to be persisted between requests, it is advised to
195+
store them in the block document. If you add them to the cache keys, you
196+
have to be aware that depending on the adapter, the cache keys can be sent
197+
to the browser and thus are neither hidden nor safe from manipulation by a
198+
client.
186199

187200
Extra Cache Keys
188201
~~~~~~~~~~~~~~~~
189202

190203
The extra cache keys array is used to store metadata along the cache element.
191-
The metadata can be used to invalidate a set of cache elements.
204+
The metadata can be used to invalidate a set of cached elements.
192205

193206
Contextual Keys
194207
~~~~~~~~~~~~~~~
195208

196209
The contextual cache array hold the object class and id used inside the
197210
template. This contextual cache array is then added to the extra cache key.
198211

199-
This feature can be use like this ``$cacheManager->remove(array('objectId' => 'id'))``.
212+
This feature can be use like this::
213+
214+
$cacheManager->remove(array('objectId' => 'id'));
200215

201-
Of course not all cache adapters support this feature, varnish and MongoDB do.
216+
While not all cache adapters support this feature, the Varnish and MongoDB
217+
adapters do.
202218

203219
The BlockBundle also has a cache invalidation listener that calls the
204220
``flush`` method of a cache adapter automatically when a cached block document
@@ -235,7 +251,7 @@ your Twig template when using cache:
235251
),
236252
)) ?>
237253

238-
When using the Esi, Ssi or Js cache adapters the settings passed here are remembered:
254+
When using the Esi, Ssi or Js cache adapters, the settings passed here are remembered:
239255

240256
.. configuration-block::
241257

@@ -269,9 +285,9 @@ also the `SonataBlockBundle Advanced usage`_ documentation.
269285

270286
.. caution::
271287

272-
Because, as mentioned above, settings can be added to the url as parameters
273-
avoid exposing sensitive settings from ``sonata_block_render`` and try to
274-
store them in the block document.
288+
Because, as mentioned above, settings can be added to the URL as
289+
parameters, you have to avoid exposing sensitive settings from
290+
``sonata_block_render`` and store them in the block document instead.
275291

276292
Adapters
277293
--------

0 commit comments

Comments
 (0)