]> git.mxchange.org Git - friendica.git/blob - vendor/smarty/smarty/libs/sysplugins/smarty_template_cached.php
Add Smarty to Composer
[friendica.git] / vendor / smarty / smarty / libs / sysplugins / smarty_template_cached.php
1 <?php
2 /**
3  * Created by PhpStorm.
4  * User: Uwe Tews
5  * Date: 04.12.2014
6  * Time: 06:08
7  */
8
9 /**
10  * Smarty Resource Data Object
11  * Cache Data Container for Template Files
12  *
13  * @package    Smarty
14  * @subpackage TemplateResources
15  * @author     Rodney Rehm
16  */
17 class Smarty_Template_Cached extends Smarty_Template_Resource_Base
18 {
19     /**
20      * Cache Is Valid
21      *
22      * @var boolean
23      */
24     public $valid = null;
25
26     /**
27      * CacheResource Handler
28      *
29      * @var Smarty_CacheResource
30      */
31     public $handler = null;
32
33     /**
34      * Template Cache Id (Smarty_Internal_Template::$cache_id)
35      *
36      * @var string
37      */
38     public $cache_id = null;
39
40     /**
41      * saved cache lifetime in seconds
42      *
43      * @var integer
44      */
45     public $cache_lifetime = 0;
46
47     /**
48      * Id for cache locking
49      *
50      * @var string
51      */
52     public $lock_id = null;
53
54     /**
55      * flag that cache is locked by this instance
56      *
57      * @var bool
58      */
59     public $is_locked = false;
60
61     /**
62      * Source Object
63      *
64      * @var Smarty_Template_Source
65      */
66     public $source = null;
67
68     /**
69      * Nocache hash codes of processed compiled templates
70      *
71      * @var array
72      */
73     public $hashes = array();
74
75     /**
76      * Flag if this is a cache resource
77      *
78      * @var bool
79      */
80     public $isCache = true;
81
82     /**
83      * create Cached Object container
84      *
85      * @param Smarty_Internal_Template $_template template object
86      */
87     public function __construct(Smarty_Internal_Template $_template)
88     {
89         $this->compile_id = $_template->compile_id;
90         $this->cache_id = $_template->cache_id;
91         $this->source = $_template->source;
92         if (!class_exists('Smarty_CacheResource', false)) {
93             require SMARTY_SYSPLUGINS_DIR . 'smarty_cacheresource.php';
94         }
95         $this->handler = Smarty_CacheResource::load($_template->smarty);
96     }
97
98     /**
99      * @param Smarty_Internal_Template $_template
100      *
101      * @return Smarty_Template_Cached
102      */
103     static function load(Smarty_Internal_Template $_template)
104     {
105         $_template->cached = new Smarty_Template_Cached($_template);
106         $_template->cached->handler->populate($_template->cached, $_template);
107         // caching enabled ?
108         if (!($_template->caching == Smarty::CACHING_LIFETIME_CURRENT ||
109               $_template->caching == Smarty::CACHING_LIFETIME_SAVED) || $_template->source->handler->recompiled
110         ) {
111             $_template->cached->valid = false;
112         }
113         return $_template->cached;
114     }
115
116     /**
117      * Render cache template
118      *
119      * @param \Smarty_Internal_Template $_template
120      * @param  bool                     $no_output_filter
121      *
122      * @throws \Exception
123      */
124     public function render(Smarty_Internal_Template $_template, $no_output_filter = true)
125     {
126         if ($this->isCached($_template)) {
127             if ($_template->smarty->debugging) {
128                 if (!isset($_template->smarty->_debug)) {
129                     $_template->smarty->_debug = new Smarty_Internal_Debug();
130                 }
131                 $_template->smarty->_debug->start_cache($_template);
132             }
133             if (!$this->processed) {
134                 $this->process($_template);
135             }
136             $this->getRenderedTemplateCode($_template);
137             if ($_template->smarty->debugging) {
138                 $_template->smarty->_debug->end_cache($_template);
139             }
140             return;
141         } else {
142             $_template->smarty->ext->_updateCache->updateCache($this, $_template, $no_output_filter);
143         }
144     }
145
146     /**
147      * Check if cache is valid, lock cache if required
148      *
149      * @param \Smarty_Internal_Template $_template
150      *
151      * @return bool flag true if cache is valid
152      */
153     public function isCached(Smarty_Internal_Template $_template)
154     {
155         if ($this->valid !== null) {
156             return $this->valid;
157         }
158         while (true) {
159             while (true) {
160                 if ($this->exists === false || $_template->smarty->force_compile || $_template->smarty->force_cache) {
161                     $this->valid = false;
162                 } else {
163                     $this->valid = true;
164                 }
165                 if ($this->valid && $_template->caching == Smarty::CACHING_LIFETIME_CURRENT &&
166                     $_template->cache_lifetime >= 0 && time() > ($this->timestamp + $_template->cache_lifetime)
167                 ) {
168                     // lifetime expired
169                     $this->valid = false;
170                 }
171                 if ($this->valid && $_template->smarty->compile_check == 1 &&
172                     $_template->source->getTimeStamp() > $this->timestamp
173                 ) {
174                     $this->valid = false;
175                 }
176                 if ($this->valid || !$_template->smarty->cache_locking) {
177                     break;
178                 }
179                 if (!$this->handler->locked($_template->smarty, $this)) {
180                     $this->handler->acquireLock($_template->smarty, $this);
181                     break 2;
182                 }
183                 $this->handler->populate($this, $_template);
184             }
185             if ($this->valid) {
186                 if (!$_template->smarty->cache_locking || $this->handler->locked($_template->smarty, $this) === null) {
187                     // load cache file for the following checks
188                     if ($_template->smarty->debugging) {
189                         $_template->smarty->_debug->start_cache($_template);
190                     }
191                     if ($this->handler->process($_template, $this) === false) {
192                         $this->valid = false;
193                     } else {
194                         $this->processed = true;
195                     }
196                     if ($_template->smarty->debugging) {
197                         $_template->smarty->_debug->end_cache($_template);
198                     }
199                 } else {
200                     $this->is_locked = true;
201                     continue;
202                 }
203             } else {
204                 return $this->valid;
205             }
206             if ($this->valid && $_template->caching === Smarty::CACHING_LIFETIME_SAVED &&
207                 $_template->cached->cache_lifetime >= 0 &&
208                 (time() > ($_template->cached->timestamp + $_template->cached->cache_lifetime))
209             ) {
210                 $this->valid = false;
211             }
212             if ($_template->smarty->cache_locking) {
213                 if (!$this->valid) {
214                     $this->handler->acquireLock($_template->smarty, $this);
215                 } elseif ($this->is_locked) {
216                     $this->handler->releaseLock($_template->smarty, $this);
217                 }
218             }
219             return $this->valid;
220         }
221         return $this->valid;
222     }
223
224     /**
225      * Process cached template
226      *
227      * @param Smarty_Internal_Template $_template template object
228      * @param bool                     $update    flag if called because cache update
229      */
230     public function process(Smarty_Internal_Template $_template, $update = false)
231     {
232         if ($this->handler->process($_template, $this, $update) === false) {
233             $this->valid = false;
234         }
235         if ($this->valid) {
236             $this->processed = true;
237         } else {
238             $this->processed = false;
239         }
240     }
241
242     /**
243      * Read cache content from handler
244      *
245      * @param Smarty_Internal_Template $_template template object
246      *
247      * @return string|false content
248      */
249     public function read(Smarty_Internal_Template $_template)
250     {
251         if (!$_template->source->handler->recompiled) {
252             return $this->handler->readCachedContent($_template);
253         }
254         return false;
255     }
256 }