PageRenderTime 39ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/src/libraries/default/base/template/helper/javascript/file.php

https://github.com/bhar1red/anahita
PHP | 195 lines | 82 code | 22 blank | 91 comment | 12 complexity | f2ac03094d99bb3964ac7a152cf36b24 MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1
  1. <?php
  2. /**
  3. * LICENSE: ##LICENSE##
  4. *
  5. * @category Anahita
  6. * @package Lib_Base
  7. * @subpackage Template_Helper
  8. * @author Arash Sanieyan <ash@anahitapolis.com>
  9. * @author Rastin Mehr <rastin@anahitapolis.com>
  10. * @copyright 2008 - 2010 rmdStudio Inc./Peerglobe Technology Inc
  11. * @license GNU GPLv3 <http://www.gnu.org/licenses/gpl-3.0.html>
  12. * @version SVN: $Id$
  13. * @link http://www.anahitapolis.com
  14. */
  15. /**
  16. *
  17. *
  18. * @category Anahita
  19. * @package Lib_Base
  20. * @subpackage Template_Helper
  21. * @author Arash Sanieyan <ash@anahitapolis.com>
  22. * @author Rastin Mehr <rastin@anahitapolis.com>
  23. * @license GNU GPLv3 <http://www.gnu.org/licenses/gpl-3.0.html>
  24. * @link http://www.anahitapolis.com
  25. */
  26. class LibBaseTemplateHelperJavascriptFile extends KTemplateHelperAbstract
  27. {
  28. /**
  29. * Cache dir
  30. *
  31. * @var string
  32. */
  33. protected $_cache_dir;
  34. /**
  35. * Cached data
  36. *
  37. * @var array
  38. */
  39. protected $_cache_data;
  40. /**
  41. * File
  42. *
  43. * @var string
  44. */
  45. protected $_file;
  46. /**
  47. * Constructor.
  48. *
  49. * @param KConfig $config An optional KConfig object with configuration options.
  50. *
  51. * @return void
  52. */
  53. public function __construct(KConfig $config)
  54. {
  55. $this->_file = $config->file;
  56. parent::__construct($config);
  57. $this->_cache_file = $config->cache_file;
  58. if ( is_readable($this->_cache_file)) {
  59. $this->_cache_data = unserialize(file_get_contents($this->_cache_file));
  60. }
  61. if ( !is_array($this->_cache_data) ) {
  62. $this->_cache_data = array();
  63. }
  64. if ( !isset($this->_cache_data['timestamp']) ) {
  65. $this->_cache_data = array('timetamp' => time());
  66. }
  67. }
  68. /**
  69. * Initializes the default configuration for the object
  70. *
  71. * Called from {@link __construct()} as a first step of object instantiation.
  72. *
  73. * @param KConfig $config An optional KConfig object with configuration options.
  74. *
  75. * @return void
  76. */
  77. protected function _initialize(KConfig $config)
  78. {
  79. $config->append(array(
  80. 'cache_file' => JPATH_CACHE.'/'.basename($this->_file).'.'.md5($this->_file)
  81. ));
  82. parent::_initialize($config);
  83. }
  84. /**
  85. * Return the content of the file and all of the files included in it
  86. *
  87. * @return string
  88. */
  89. public function getData()
  90. {
  91. return $this->_parseFile($this->_file);
  92. }
  93. /**
  94. * Write the output of the file into and output
  95. *
  96. * @param string $output Compress file
  97. * @param boolean $force Determine whether to foce the compression regardless of the
  98. * cache value
  99. *
  100. * @return string
  101. */
  102. public function write($output)
  103. {
  104. //lets parse the file
  105. if ( $this->_hasBeenModified($this->_file) || !file_exists($output)) {
  106. //get the data
  107. file_put_contents($output, $this->getData());
  108. //update the timestamp
  109. $this->_cache_data['timestamp'] = time();
  110. file_put_contents($this->_cache_file, serialize($this->_cache_data));
  111. }
  112. }
  113. /**
  114. * Parses a file and return its content. The method traverse through
  115. * all the imported javascript file within the file and append their
  116. * content to the content of the $file
  117. *
  118. * @param string $file The file to parse
  119. *
  120. * @return string
  121. */
  122. protected function _parseFile($file)
  123. {
  124. //if modified parse it again
  125. if ( $this->_hasBeenModified($file) )
  126. {
  127. $imports = array();
  128. $content = file_get_contents($file);
  129. //lets compress the file
  130. $matches = array();
  131. $dir = dirname($file);
  132. if ( preg_match_all('/\/\/@depends (.*)/', $content, $matches) ) {
  133. foreach($matches[1] as $i => $match) {
  134. $imported_file = $dir.'/'.$match;
  135. $imports[] = $imported_file;
  136. if ( file_exists($imported_file) ) {
  137. $data = $this->_parseFile($imported_file);
  138. $content = str_replace($matches[0][$i], $data, $content);
  139. }
  140. }
  141. }
  142. $content = "//".str_replace(JPATH_ROOT,'',$file)."\n".$content;
  143. $this->_cache_data[$file] = array('data'=> $content,'imports' => $imports);
  144. } else {
  145. $content = $this->_cache_data[$file]['data'];
  146. }
  147. return $content;
  148. }
  149. /**
  150. * Check if the file or any of the imported files within it has been modified since
  151. * the last time it was cached
  152. *
  153. * @param string $file The file to check
  154. *
  155. * @return boolean
  156. */
  157. protected function _hasBeenModified($file)
  158. {
  159. if ( !isset($this->_cache_data[$file]) ) {
  160. return true;
  161. }
  162. if ( filemtime($file) > $this->_cache_data['timestamp'] ) {
  163. return true;
  164. }
  165. $imports = $this->_cache_data[$file]['imports'];
  166. foreach($imports as $import) {
  167. if ( $this->_hasBeenModified($import) ) {
  168. return true;
  169. }
  170. }
  171. return false;
  172. }
  173. }