developer.jelix.org is not used any more and exists only for history. Post new tickets on the Github account.
developer.jelix.org n'est plus utilisée, et existe uniquement pour son historique. Postez les nouveaux tickets sur le compte github.

Ticket #1217: monpatch.diff

File monpatch.diff, 9.0 KB (added by yamsuz, 10 years ago)

Rajout d'un test pour la suppression de zone en fonction d'une période

Line 
1<?php
2/**
3* @package    jelix
4* @subpackage utils
5* @author     Gérald Croes, Laurent Jouanneau
6* @contributor Laurent Jouanneau, Laurent Raufaste, Pulsation, Vincent Panien
7* @copyright  2001-2005 CopixTeam, 2005-2009 Laurent Jouanneau, 2008 Laurent Raufaste, 2008 Pulsation
8*
9* This class was get originally from the Copix project (CopixZone, Copix 2.3dev20050901, http://www.copix.org)
10* Some lines of code are copyrighted 2001-2005 CopixTeam (LGPL licence).
11* Initial authors of this Copix classes are Gerald Croes and Laurent Jouanneau,
12* and this class was adapted/improved for Jelix by Laurent Jouanneau
13*
14* @link        http://www.jelix.org
15* @licence  http://www.gnu.org/licenses/lgpl.html GNU Lesser General Public Licence, see LICENCE file
16*/
17
18/**
19 * jZone is a representation of a zone in an response content, in a html page.
20 * A user zone should inherits from jZone. jZone provide a cache mecanism.
21 * @package    jelix
22 * @subpackage utils
23 */
24class jZone {
25    /**
26    * If we're using cache on this zone
27    * You should override it in your class if you want activate the cache
28    * @var boolean
29    */
30    protected $_useCache = false;
31
32    /**
33     * cache timeout (seconds).
34     * set to 0 if you want to delete cache manually.
35     * @var integer
36     */
37    protected $_cacheTimeout = 0;
38       
39    /**
40     * cache periode (Day, week, month, year)
41     */
42    protected $_formatDate = '';
43   
44        /**
45    * list of zone parameters
46    * @var array
47    */
48    protected $_params;
49
50    /**
51     * template selector
52     * If you want to use a template for your zone, set its name in this property
53     * in your zone, and override _prepareTpl. Else, keep it to empty string, and
54     * override _createContent
55     * @var string
56     */
57    protected $_tplname='';
58
59    /**
60     * says the type of the output of the template, in the case of the result
61     * of the zone is not used in a response in the same output type.
62     * For example, the output type of a ajax response is text, but the template
63     * can contains html, so the template should be treated as html content,
64     * so you should put 'html' here.
65     * If empty, the output type will be the output type of the current response.
66     * @var string
67     * @see jTpl::fetch
68     */
69    protected $_tplOuputType='';
70
71    /**
72     * the jtpl object created automatically by jZone if you set up _tplname
73     * you can use it in _prepareTpl
74     * @var jTpl
75     */
76    protected $_tpl=null;
77
78    /**
79     * When the cache system is activated, says if the cache should be generated or not
80     * you set it to false in _createContent or _prepareTpl, in specific case.
81     * @var boolean
82     */
83    protected $_cancelCache=false;
84
85    /**
86     * constructor.
87     */
88    function __construct($params=array()){
89        $this->_params = $params;
90    }
91
92    /**
93    * get the content of a zone
94    * @param string $name zone selector
95    * @param array $params parameters for the zone
96    * @return string the generated content of the zone
97    * @since 1.0b1
98    */
99    public static function get ($name, $params=array ()){
100        return self::_callZone($name, 'getContent', $params);
101    }
102
103    /**
104    * clear a specific cache of a zone
105    * @param string $name zone selector
106    * @param array $params parameters for the zone
107    * @since 1.0b1
108    */
109    public static function clear ($name, $params=array ()){
110        return self::_callZone($name, 'clearCache', $params);
111    }
112
113    /**
114    * clear all zone cache or all cache of a specific zone
115    * @param string $name zone selector
116    * @since 1.0b1
117    */
118    public static function clearAll($name=''){
119        $dir = JELIX_APP_TEMP_PATH.'zonecache/';
120        if(!file_exists($dir)) return;
121
122        if($name !=''){
123            $sel = new jSelectorZone($name);
124            $fic = '~'.$sel->module.'~'.strtolower($sel->resource).'zone~';
125        }else{
126            $fic = '~';
127        }
128
129        if ($dh = opendir($dir)) {
130           while (($file = readdir($dh)) !== false) {
131               if(strpos($file, $fic) === 0){
132                   unlink($dir.$file);
133               }
134           }
135           closedir($dh);
136       }
137    }
138
139    /**
140    * gets the value of a parameter, if defined. Returns the default value instead.
141    * @param string $paramName the parameter name
142    * @param mixed $defaultValue the parameter default value
143    * @return mixed the param value
144    */
145    public function param ($paramName, $defaultValue=null){
146        return array_key_exists ($paramName, $this->_params) ? $this->_params[$paramName] : $defaultValue;
147    }
148   
149    /**
150     * Same as param(), included for compatibility with older versions
151     * @param string $paramName the parameter name
152     * @param mixed $defaultValue the parameter default value
153     * @return mixed the param value
154     * @deprecated 1.1
155     */
156    public function getParam ($paramName, $defaultValue=null){
157        return $this->param($paramName, $defaultValue);
158    }
159
160    /**
161    * get the zone content
162    * Return the cache content if it is activated and if it's exists, or call _createContent
163    * @return string  zone content
164    */
165    public function getContent (){
166        global $gJConfig;
167       
168        if ($this->_useCache && !$gJConfig->zones['disableCache']){
169            $f = $this->_getCacheFile();
170            if(file_exists($f)){
171                if($this->_cacheTimeout > 0){
172                    clearstatcache();
173                    if(time() - filemtime($f) > $this->_cacheTimeout){
174                        // timeout : regenerate the cache
175                        unlink($f);
176                        $this->_cancelCache=false;
177                        $content=$this->_createContent();
178                        if(!$this->_cancelCache){
179                            jFile::write($f,$content);
180                        }
181                        return $content;
182                    }
183                }
184                if($this->_formatDate != ""){
185                    clearstatcache();
186                    if(date($this->_formatDate,filemtime($f)) != date($this->_formatDate)){
187                        // timeout : regenerate the cache
188                        unlink($f);
189                        $this->_cancelCache=false;
190                        $content=$this->_createContent();
191                        if(!$this->_cancelCache){
192                            jFile::write($f,$content);
193                        }
194                        return $content;
195                    }
196                }
197                if($this->_tplname != ''){
198                    $this->_tpl = new jTpl();
199                    $this->_tpl->assign($this->_params);
200                    $this->_tpl->meta($this->_tplname, $this->_tplOuputType);
201                }
202                $content = file_get_contents($f);
203            }else{
204                $this->_cancelCache=false;
205                $content=$this->_createContent();
206                if(!$this->_cancelCache){
207                    jFile::write($f,$content);
208                }
209            }
210        }else{
211            $content=$this->_createContent();
212        }
213        return $content;
214    }
215
216    /**
217    * Delete the cache of the current zone
218    */
219    public function clearCache (){
220        if ($this->_useCache){
221            $f = $this->_getCacheFile();
222            if(file_exists($f)){
223                unlink($f);
224            }
225        }
226    }
227
228
229    /**
230    * create the content of the zone
231    * by default, it uses a template, and so prepare a jtpl object to use in _prepareTpl.
232    * zone parameters are automatically assigned in the template
233    * If you don't want a template, override it in your class
234    * @return string generated content
235    */
236    protected function _createContent (){
237        $this->_tpl = new jTpl();
238        $this->_tpl->assign($this->_params);
239        $this->_prepareTpl();
240        if($this->_tplname == '') return '';
241        return $this->_tpl->fetch($this->_tplname, $this->_tplOuputType);
242    }
243
244    /**
245     * override this method if you want do additionnal thing on the template object
246     * Example : do access to a dao object.. Note : the template object
247     * is in the _tpl property
248     */
249    protected function _prepareTpl(){
250
251    }
252
253    /**
254    * create the cache filename
255    * @return string the filename
256    */
257    private function _getCacheFile (){
258        $module = jContext::get ();
259        $ar = $this->_params;
260        ksort($ar);
261        $id=md5(serialize($ar));
262        return JELIX_APP_TEMP_PATH.'zonecache/~'.$module.'~'.strtolower(get_class($this)).'~'.$id.'.php';
263    }
264
265   /**
266    * instancy a zone object, and call one of its methods
267    * @param string $name zone selector
268    * @param string $method method name
269    * @param array  $params arguments for the method
270    * @return mixed the result returned by the method
271    */
272    private static function  _callZone($name,$method, &$params){
273
274        $sel = new jSelectorZone($name);
275        jContext::push ($sel->module);
276
277        $fileName = $sel->getPath();
278        require_once($fileName);
279        $className = $sel->resource.'Zone';
280        $zone = new $className ($params);
281        $toReturn = $zone->$method ();
282
283        jContext::pop ();
284        return $toReturn;
285    }
286}
287