forked from jarves/jarves
-
Notifications
You must be signed in to change notification settings - Fork 0
/
PluginController.php
149 lines (139 loc) · 5.26 KB
/
PluginController.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
<?php
/**
* This file is part of Jarves.
*
* (c) Marc J. Schmidt <[email protected]>
*
* J.A.R.V.E.S - Just A Rather Very Easy [content management] System.
*
* http://jarves.io
*
* To get the full copyright and license information, please view the
* LICENSE file, that was distributed with this source code.
*/
namespace Jarves;
use Jarves\Cache\Cacher;
use Jarves\Cache\ResponseCacher;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\Templating\EngineInterface;
class PluginController extends Controller
{
/**
* Replaces all falsy (0, '', null) values in $values with the $default value.
*
* @param array $values
* @param array $defaults
*/
protected function setOptions(array &$values, array $defaults)
{
foreach ($defaults as $key => $default) {
if (!isset($values[$key]) || !$values[$key]) {
$values[$key] = $default;
}
}
}
/**
* Returns whether this cache is valid(exists) or not.
*
* @param string $cacheKey
*
* @return boolean
*/
protected function isValidCache($cacheKey)
{
/** @var Cacher $cacher */
$cacher = $this->get('jarves.cache.cacher');
return $cacher->getDistributedCache($cacheKey) !== null;
}
/**
* Renders a view and wrap it with a PluginResponse, so the result will
* be rendered at the plugin's position.
*
* If you use the regular `render` method, its Symfony response will
* be passed through the HttpKernel and will be displayed without the PageResponse.
*
* @param string $view The view name
* @param array $parameters An array of parameters to pass to the view
*
* @return string will be transformed into a PluginResponse automatically by Jarves
*/
protected function renderPluginView($view, array $parameters = array())
{
/** @var EngineInterface $templating */
$templating = $this->get('templating');
return $templating->render($view, $parameters);
}
/**
* Returns a rendered view. If we find data behind the given cache
* it uses this data instead of calling $data. So this function
* does not cache the whole rendered html. To do so use renderFullCache().
*
* In this cache method, the template engine is always called. If you want to cache
* this as well, use renderFullCached().
*
* Example:
*
* return $this->renderCache('myCache', 'plugin1/default.tpl', function(){
* return array('items' => heavyDbQuery());
* });
*
* Note: The $data callable is only called if the cache needs to regenerate (when it has been
* invalidated or empty, or the view file changed).
*
* If the callable $data returns NULL, then this will return NULL, too.
*
* @param string $cacheKey
* @param string $view
* @param array|callable $data Pass the data as array or a data provider function.
*
* @see method `render` to get more information.
*
* @return string
*/
protected function renderCached($cacheKey, $view, $data = null)
{
/** @var ResponseCacher $responseCacher */
$responseCacher = $this->get('jarves.cache.response_cacher');
return $responseCacher->renderCached($cacheKey, $view, $data);
}
/**
* Returns a rendered view. If we find html behind the given cache
* it returns this directly. This is a couple os ms faster than `renderCached`
* since the template engine is never used when there's a valid cache.
*
* Example:
*
* return $this->renderFullCached('myCache', 'plugin1/default.tpl', function(){
* return array('items' => heavyDbQuery());
* });
*
* Note: The $data callable is only called if the cache needs to regenerate (when it has been
* invalidated or empty, or the view file changed).
*
* If the callable $data returns NULL, then this will return NULL, too, without entering
* the actual rendering process.
*
* You should use this method in your plugins instead of writing your own cache mechanism,
* because this method handles PageResponse merging. Means: If templates used in this
* $view are changing somehow the PageResponse ({{loadAsset('style.css')}} calls) then
* this information (diff to current PageResponse) is stored and restored when we found
* a html cache. The diff is beside the actual rendered HTML also stored in the cache
* to keep this possible.
*
* @param string $cacheKey
* @param string $view
* @param array|callable $data Pass the data as array or a data provider function.
* @param integer $lifeTime In seconds. Default is one hour/3600 seconds.
* @param bool $force Force to bypass the cache and always call $data. For debuggin purposes.
*
* @see method `render` to get more information.
*
* @return string
*/
protected function renderFullCached($cacheKey, $view, $data = null, $lifeTime = null, $force = false)
{
/** @var ResponseCacher $responseCacher */
$responseCacher = $this->get('jarves.cache.response_cacher');
return $responseCacher->renderFullCached($cacheKey, $view, $data, $lifeTime, $force);
}
}