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 | 3 | public function __construct(Config $config) { |
|
26 | 3 | $this->config = $config; |
|
27 | 3 | } |
|
28 | |||
29 | /** |
||
30 | * Registers a view to simple cache. |
||
31 | * |
||
32 | * 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) { |
||
44 | $view_name = ViewsService::canonicalizeViewName($view_name); |
||
45 | elgg_register_external_view($view_name, true); |
||
1 ignored issue
–
show
|
|||
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 = '') { |
||
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 | 3 | function getRoot() { |
|
98 | 3 | $viewtype = elgg_get_viewtype(); |
|
99 | 3 | if ($this->isEnabled()) { |
|
100 | $lastcache = (int) $this->config->lastcache; |
||
101 | } else { |
||
102 | 3 | $lastcache = 0; |
|
103 | } |
||
104 | |||
105 | 3 | return elgg_normalize_url("/cache/$lastcache/$viewtype/"); |
|
106 | } |
||
107 | |||
108 | /** |
||
109 | * Is simple cache enabled |
||
110 | * |
||
111 | * @return bool |
||
112 | */ |
||
113 | 3 | function isEnabled() { |
|
114 | 3 | return (bool) $this->config->simplecache_enabled; |
|
115 | } |
||
116 | |||
117 | /** |
||
118 | * Enables the simple cache. |
||
119 | * |
||
120 | * @see elgg_register_simplecache_view() |
||
121 | * @return void |
||
122 | */ |
||
123 | function enable() { |
||
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() { |
||
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() { |
||
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() { |
||
176 | $lastcache = $this->config->lastcache; |
||
177 | if (!defined('UPGRADING') && empty($lastcache)) { |
||
178 | $this->config->lastcache = (int) $this->config->simplecache_lastupdate; |
||
179 | } |
||
180 | } |
||
181 | } |
||
182 |
This check looks for type mismatches where the missing type is
false
. This is usually indicative of an error condtion.Consider the follow example
This function either returns a new
DateTime
object or false, if there was an error. This is a typical pattern in PHP programming to show that an error has occurred without raising an exception. The calling code should check for this returnedfalse
before passing on the value to another function or method that may not be able to handle afalse
.