PageRenderTime 49ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 0ms

/_examples/mvc2/application/models/view.php

https://bitbucket.org/tumivn/phpexamples
PHP | 437 lines | 185 code | 36 blank | 216 comment | 27 complexity | 0c940eed0ee9a287d447dbc912fa1b4f MD5 | raw file
  1. <?php
  2. /**
  3. *
  4. * @View Class
  5. *
  6. * @copyright Copyright (C) 2009 PHPRO.ORG. All rights reserved.
  7. *
  8. * @license new bsd http://www.opensource.org/licenses/bsd-license.php
  9. * @package Core
  10. * @Author Kevin Waterson
  11. *
  12. */
  13. namespace sevenkevins;
  14. class View
  15. {
  16. /**
  17. *
  18. * The constructor, duh
  19. *
  20. */
  21. public function __construct()
  22. {
  23. // parent::__construct(array(), ArrayObject::ARRAY_AS_PROPS);
  24. // $this->setCacheDir();
  25. }
  26. /**
  27. * The variable property contains the variables
  28. * that can be used inside of the templates.
  29. *
  30. * @access private
  31. * @var array
  32. */
  33. private $variables = array();
  34. /**
  35. * The directory where the templates are stored
  36. *
  37. * @access private
  38. * @var string
  39. */
  40. private $template_dir = null;
  41. /**
  42. * Turns caching on or off
  43. *
  44. * @access private
  45. * @var bool
  46. */
  47. private $caching = false;
  48. /**
  49. * The directory where the cache files will be saved.
  50. *
  51. * @access private
  52. * @var string
  53. */
  54. private $cache_dir = 'cache';
  55. /**
  56. * Lifetime of a cache file in seconds.
  57. * @access private
  58. * @var int
  59. */
  60. private $cache_lifetime = 300;
  61. /**
  62. * Adds a variable that can be used by the templates.
  63. *
  64. * Adds a new array index to the variable property. This
  65. * new array index will be treated as a variable by the templates.
  66. *
  67. * @param string $name The variable name to use in the template
  68. * @param string $value The content you assign to $name
  69. * @access public
  70. * @return void
  71. * @see getVars, $variables
  72. *
  73. */
  74. public function __set($name, $value)
  75. {
  76. $this->variables[$name] = $value;
  77. }
  78. /**
  79. * @Returns names of all the added variables
  80. *
  81. * Returns a numeral array containing the names of all
  82. * added variables.
  83. *
  84. * @access public
  85. * @return array
  86. * @see addVar, $variables
  87. *
  88. */
  89. public function getVars()
  90. {
  91. $variables = array_keys($this->variables);
  92. return !empty($variables) ? $variables : false;
  93. }
  94. /**
  95. *
  96. * Outputs the final template output
  97. *
  98. * Fetches the final template output, and echoes it to the browser.
  99. *
  100. * @param string $file Filename (with path) to the template you want to output
  101. * @param string $id The cache identification number/string of the template you want to fetch
  102. * @access public
  103. * @return void
  104. * @see fetch
  105. *
  106. */
  107. public function render($file, $id = null)
  108. {
  109. echo $this->fetch($file, $id);
  110. }
  111. /**
  112. * Fetch the final template output and returns it
  113. *
  114. * @param string $template_file Filename (with path) to the template you want to fetch
  115. * @param string $id The cache identification number/string of the template you want to fetch
  116. * @access private
  117. * @return string Returns a string on success, FALSE on failure
  118. * @see render
  119. *
  120. */
  121. public function fetch($template_file, $id = null)
  122. {
  123. /*** if the template_dir property is set, add it to the filename ***/
  124. if (!empty($this->template_dir))
  125. {
  126. $template_file = realpath($this->template_dir) . '/' . $template_file;
  127. }
  128. /*** get the cached file contents ***/
  129. if ($this->caching == true && $this->isCached($template_file, $id))
  130. {
  131. $output = $this->getCache($template_file, $id);
  132. }
  133. else
  134. {
  135. $output = $this->getOutput($template_file);
  136. /*** create the cache file ***/
  137. if ($this->caching == true)
  138. {
  139. $this->addCache($output, $template_file, $id);
  140. }
  141. }
  142. return isset($output) ? $output : false;
  143. }
  144. /**
  145. *
  146. * Fetch the template output, and return it
  147. *
  148. * @param string $template_file Filename (with path) to the template to be processed
  149. * @return string Returns a string on success, and FALSE on failure
  150. * @access private
  151. * @see fetch, render
  152. *
  153. */
  154. public function getOutput( $template_file )
  155. {
  156. /*** extract all the variables ***/
  157. extract( $this->variables );
  158. if (file_exists($template_file))
  159. {
  160. ob_start();
  161. include($template_file);
  162. $output = ob_get_contents();
  163. ob_end_clean();
  164. }
  165. else
  166. {
  167. throw new \Exception("The template file '$template_file' does not exist");
  168. }
  169. return !empty($output) ? $output : false;
  170. }
  171. /**
  172. *
  173. * Sets the template directory
  174. *
  175. * @param string $dir Path to the template dir you want to use
  176. * @access public
  177. * @return void
  178. *
  179. */
  180. public function setTemplateDir($dir)
  181. {
  182. $template_dir = realpath($dir);
  183. if (is_dir($template_dir))
  184. {
  185. $this->template_dir = $template_dir;
  186. }
  187. else
  188. {
  189. throw new sevenkevinsException("The template directory '$dir' does not exist", 200);
  190. }
  191. }
  192. /**
  193. *
  194. * Sets the cache directory
  195. *
  196. * @param string $dir Path to the cache dir you want to use
  197. * @access public
  198. * @return void
  199. * @see setCacheLifetime
  200. *
  201. */
  202. function setCacheDir($cacheDir = null)
  203. {
  204. if( is_null( $cacheDir ) )
  205. {
  206. $config = config::getInstance();
  207. $cacheDir = $config->config_values['template']['cache_dir'];
  208. }
  209. if( !is_dir( $cacheDir ) )
  210. {
  211. // TODO log error here FIXME
  212. // try to create the direcory
  213. if( mkdir( $cacheDir ) == false )
  214. {
  215. // unable to create the directory
  216. // option set to false and allow to continue..
  217. // $this->setCaching( false );
  218. throw new \Exception("The cache directory '$cacheDir' does not exist!");
  219. }
  220. }
  221. if ( !is_writable( $cacheDir ) )
  222. {
  223. // TODO log error here FIXME
  224. throw new \Exception("The cache directory '$cacheDir' is not writable");
  225. }
  226. else
  227. {
  228. $config = config::getInstance();
  229. $this->cache_dir = $cacheDir;
  230. }
  231. }
  232. /**
  233. * Sets how long the cache files should survive
  234. *
  235. * @param INT $seconds Number of seconds the cache should survive
  236. * @access public
  237. * @return void
  238. * @see setCacheDir, isCached, setCaching
  239. *
  240. */
  241. public function setCacheLifetime($seconds=0)
  242. {
  243. $this->cache_lifetime = is_numeric($seconds) ? $seconds : 0;
  244. }
  245. /**
  246. * Turn caching on or off
  247. *
  248. * @param bool $state Set TRUE turns caching on, FALSE turns caching off
  249. * @access public
  250. * @return void
  251. * @see setCacheLifetime, isCached, setCacheDir
  252. *
  253. */
  254. public function setCaching($state=false)
  255. {
  256. $this->caching = $state;
  257. }
  258. /**
  259. * Checks if the template in $template is cached
  260. *
  261. * @param string $file Filename of the template
  262. * @param string $id The cache identification number/string of the template you want to fetch
  263. * @access public
  264. * @return bool
  265. * @see setCacheLifetime, setCacheDir, setCaching
  266. *
  267. */
  268. public function isCached($file, $id = null)
  269. {
  270. $cacheId= $id ? md5($id . basename($file)) : md5(basename($file));
  271. $filename = $this->cache_dir . '/' . $cacheId . '/' . basename($file);
  272. if (is_file($filename))
  273. {
  274. clearstatcache();
  275. if (filemtime($filename) > (time() - $this->cache_lifetime))
  276. {
  277. $isCached = true;
  278. }
  279. }
  280. return isset($isCached) ? true : false;
  281. }
  282. /**
  283. * Makes a cache file. Internal method
  284. *
  285. * @param string $content The template output that will be saved in cache
  286. * @param string $file The filename of the template that is being cached
  287. * @param string $id The cache identification number/string of the template you want to fetch
  288. * @access private
  289. * @return void
  290. * @see getCache, clearCache
  291. *
  292. */
  293. private function addCache($content, $file, $id = null)
  294. {
  295. // create the cache id
  296. $cacheId = $id ? md5($id . basename($file)) : md5(basename($file));
  297. // set the cacheDir eg: /tmp/cache
  298. // this will set $this->cache_dir
  299. $this->setCacheDir();
  300. // create the cache filename
  301. $filename = $this->cache_dir . '/' . $cacheId . '/' . basename($file);
  302. // create the directory name for the cache file
  303. $directory = $this->cache_dir . '/' . $cacheId;
  304. // create the cache directory
  305. if( !is_dir( $directory ) )
  306. {
  307. mkdir ( $directory );
  308. }
  309. /*** write to the cache ***/
  310. if( file_put_contents( $filename, $content ) == FALSE )
  311. {
  312. throw new \Exception("Unable to write to cache");
  313. }
  314. }
  315. /**
  316. * Returns the content of a cached file
  317. *
  318. * @param string $file The filename of the template you want to fetch
  319. * @param string $id The cache identification number/string of the template you want to fetch
  320. * @access private
  321. * @return string Cached content on success, FALSE on failure
  322. * @see addCache, clearCache
  323. *
  324. */
  325. private function getCache($file, $id = null)
  326. {
  327. $cacheId= $id ? md5($id . basename($file)) : md5(basename($file));
  328. $filename = $this->cache_dir . '/' . $cacheId . '/' . basename($file);
  329. /*** read the cache file into a variable ***/
  330. $content=file_get_contents($filename);
  331. return isset($content) ? $content : false;
  332. }
  333. /**
  334. * Deletes all of the stored cache files
  335. *
  336. * @access public
  337. *
  338. * @return void
  339. *
  340. * @see addCache, getCache
  341. *
  342. */
  343. public function clearCache()
  344. {
  345. $cacheDir = realpath($this->cache_dir);
  346. $this->delDir($cacheDir);
  347. }
  348. /**
  349. * Remove files and folders recursively.
  350. * WARNING: It does not care what directory $dir is.
  351. *
  352. * @param string $dir directory to remove files and folders from
  353. *
  354. * @access private
  355. *
  356. * @return void
  357. *
  358. * @see clearCache
  359. *
  360. */
  361. private function delDir($dir)
  362. {
  363. /*** perhaps a recursiveDirectoryIteratory here ***/
  364. $deleteDir = realpath($dir);
  365. if ($handle = opendir($deleteDir))
  366. {
  367. while (false !== ($file = readdir($handle)))
  368. {
  369. if ($file != '.' && $file != '..')
  370. {
  371. if (is_dir($deleteDir . '/' . $file))
  372. {
  373. $this->delDir($deleteDir . '/' . $file);
  374. if(is_writable($deleteDir . '/' . $file))
  375. {
  376. rmdir($deleteDir . '/' . $file);
  377. }
  378. else
  379. {
  380. throw new Exception("Unable to remove Directory");
  381. }
  382. }
  383. elseif(is_file($deleteDir . '/' . $file))
  384. {
  385. if(is_writable($deleteDir . '/' . $file))
  386. {
  387. unlink($deleteDir . '/' . $file);
  388. }
  389. else
  390. {
  391. throw new Exception("Unable to unlink $deleteDir".'/'."$file");
  392. }
  393. }
  394. }
  395. }
  396. closedir($handle);
  397. }
  398. }
  399. } /*** end of class ***/
  400. ?>