These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
1 | <?php |
||
2 | namespace Elgg\Cache; |
||
3 | |||
4 | use Elgg\Config; |
||
5 | use Elgg\ViewsService; |
||
6 | |||
7 | |||
8 | /** |
||
9 | * WARNING: API IN FLUX. DO NOT USE DIRECTLY. |
||
10 | * |
||
11 | * @access private |
||
12 | * |
||
13 | * @since 1.10.0 |
||
14 | */ |
||
15 | class SimpleCache { |
||
16 | |||
17 | /** @var Config */ |
||
18 | private $config; |
||
19 | |||
20 | /** |
||
21 | * Constructor |
||
22 | * |
||
23 | * @param Config $config Elgg's global configuration |
||
24 | */ |
||
25 | public function __construct(Config $config) { |
||
26 | $this->config = $config; |
||
27 | } |
||
28 | |||
29 | 3 | /** |
|
30 | 3 | * Registers a view to simple cache. |
|
31 | 3 | * |
|
32 | 3 | * Simple cache is a caching mechanism that saves the output of |
|
33 | * a view and its extensions into a file. |
||
34 | * |
||
35 | * @warning Simple cached views must take no parameters and return |
||
36 | * the same content no matter who is logged in. |
||
37 | * |
||
38 | * @param string $view_name View name |
||
39 | * |
||
40 | * @return void |
||
41 | * @see elgg_get_simplecache_url() |
||
42 | */ |
||
43 | function registerView($view_name) { |
||
0 ignored issues
–
show
|
|||
44 | $view_name = ViewsService::canonicalizeViewName($view_name); |
||
45 | elgg_register_external_view($view_name, true); |
||
1 ignored issue
–
show
It seems like
$view_name defined by \Elgg\ViewsService::cano...izeViewName($view_name) on line 44 can also be of type false ; however, elgg_register_external_view() does only seem to accept string , did you maybe forget to handle an error condition?
This check looks for type mismatches where the missing type is Consider the follow example <?php
function getDate($date)
{
if ($date !== null) {
return new DateTime($date);
}
return false;
}
This function either returns a new
Loading history...
|
|||
46 | } |
||
47 | |||
48 | /** |
||
49 | * Get the URL for the cached view. |
||
50 | * |
||
51 | * Recommended usage is to just pass the entire view name as the first and only arg: |
||
52 | * |
||
53 | * ``` |
||
54 | * $blog_js = $simpleCache->getUrl('blog/save_draft.js'); |
||
55 | * $favicon = $simpleCache->getUrl('graphics/favicon.ico'); |
||
56 | * ``` |
||
57 | * |
||
58 | * For backwards compatibility with older versions of Elgg, you can also pass |
||
59 | * "js" or "css" as the first arg, with the rest of the view name as the second arg: |
||
60 | * |
||
61 | * ``` |
||
62 | * $blog_js = $simpleCache->getUrl('js', 'blog/save_draft.js'); |
||
63 | * ``` |
||
64 | * |
||
65 | * This automatically registers the view with Elgg's simplecache. |
||
66 | * |
||
67 | * @param string $view The full view name |
||
68 | * @param string $subview If the first arg is "css" or "js", the rest of the view name |
||
69 | * |
||
70 | * @return string |
||
71 | */ |
||
72 | function getUrl($view, $subview = '') { |
||
0 ignored issues
–
show
|
|||
73 | // handle `getUrl('js', 'js/blog/save_draft')` |
||
74 | if (($view === 'js' || $view === 'css') && 0 === strpos($subview, $view . '/')) { |
||
75 | $view = $subview; |
||
76 | $subview = ''; |
||
77 | } |
||
78 | |||
79 | // handle `getUrl('js', 'blog/save_draft')` |
||
80 | if (!empty($subview)) { |
||
81 | $view = "$view/$subview"; |
||
82 | } |
||
83 | |||
84 | $view = ViewsService::canonicalizeViewName($view); |
||
85 | |||
86 | // should be normalized to canonical form by now: `getUrl('blog/save_draft.js')` |
||
87 | $this->registerView($view); |
||
88 | return $this->getRoot() . $view; |
||
89 | } |
||
90 | |||
91 | /** |
||
92 | * Get the base url for simple cache requests |
||
93 | * |
||
94 | * @return string The simplecache root url for the current viewtype. |
||
95 | * @access private |
||
96 | */ |
||
97 | function getRoot() { |
||
0 ignored issues
–
show
|
|||
98 | $viewtype = elgg_get_viewtype(); |
||
99 | if ($this->isEnabled()) { |
||
100 | $lastcache = (int) $this->config->lastcache; |
||
101 | } else { |
||
102 | 3 | $lastcache = 0; |
|
103 | 3 | } |
|
104 | 3 | ||
105 | return elgg_normalize_url("/cache/$lastcache/$viewtype/"); |
||
106 | } |
||
107 | 3 | ||
108 | /** |
||
109 | * Is simple cache enabled |
||
110 | 3 | * |
|
111 | * @return bool |
||
112 | */ |
||
113 | function isEnabled() { |
||
0 ignored issues
–
show
|
|||
114 | return (bool) $this->config->simplecache_enabled; |
||
115 | } |
||
116 | |||
117 | /** |
||
118 | 3 | * Enables the simple cache. |
|
119 | 3 | * |
|
120 | * @see elgg_register_simplecache_view() |
||
121 | * @return void |
||
122 | */ |
||
123 | function enable() { |
||
0 ignored issues
–
show
|
|||
124 | $this->config->save('simplecache_enabled', 1); |
||
125 | $this->invalidate(); |
||
126 | } |
||
127 | |||
128 | /** |
||
129 | * Disables the simple cache. |
||
130 | * |
||
131 | * @warning Simplecache is also purged when disabled. |
||
132 | * |
||
133 | * @see elgg_register_simplecache_view() |
||
134 | * @return void |
||
135 | */ |
||
136 | function disable() { |
||
0 ignored issues
–
show
|
|||
137 | if ($this->config->simplecache_enabled) { |
||
138 | $this->config->save('simplecache_enabled', 0); |
||
139 | |||
140 | $this->invalidate(); |
||
141 | } |
||
142 | } |
||
143 | |||
144 | /** |
||
145 | * Returns the path to where views are simplecached. |
||
146 | * |
||
147 | * @return string |
||
148 | */ |
||
149 | private function getPath() { |
||
150 | $realpath = realpath($this->config->cacheroot); |
||
151 | return rtrim($realpath, DIRECTORY_SEPARATOR) . "/views_simplecache"; |
||
152 | } |
||
153 | |||
154 | /** |
||
155 | * Deletes all cached views in the simplecache and sets the lastcache and |
||
156 | * lastupdate time to 0 for every valid viewtype. |
||
157 | * |
||
158 | * @return bool |
||
159 | */ |
||
160 | function invalidate() { |
||
0 ignored issues
–
show
|
|||
161 | _elgg_rmdir($this->getPath(), true); |
||
162 | |||
163 | $time = time(); |
||
164 | $this->config->save("simplecache_lastupdate", $time); |
||
165 | $this->config->lastcache = $time; |
||
166 | |||
167 | return true; |
||
168 | } |
||
169 | |||
170 | /** |
||
171 | * Set up config appropriately on engine boot. |
||
172 | * |
||
173 | * @return void |
||
174 | */ |
||
175 | function init() { |
||
0 ignored issues
–
show
|
|||
176 | $lastcache = $this->config->lastcache; |
||
177 | if (!defined('UPGRADING') && empty($lastcache)) { |
||
178 | $this->config->lastcache = (int) $this->config->simplecache_lastupdate; |
||
179 | } |
||
180 | } |
||
181 | } |
||
182 |
Adding explicit visibility (
private
,protected
, orpublic
) is generally recommend to communicate to other developers how, and from where this method is intended to be used.