PageRenderTime 50ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/jelix/utils/jZone.class.php

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