PageRenderTime 48ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/www/libs/components/WebLoader/WebLoader.php

https://github.com/bazo/Mokuji
PHP | 234 lines | 117 code | 38 blank | 79 comment | 12 complexity | 7cc3419ccd8a4318ad26db4034b37337 MD5 | raw file
Possible License(s): BSD-3-Clause, MIT
  1. <?php
  2. /**
  3. * Web loader
  4. *
  5. * @author Jan Marek
  6. * @license MIT
  7. */
  8. abstract class WebLoader extends Control
  9. {
  10. /** @var string */
  11. public $sourcePath;
  12. /** @var string */
  13. public $sourceUri;
  14. /** @var string */
  15. public $tempPath;
  16. /** @var string */
  17. public $tempUri;
  18. /** @var bool */
  19. public $joinFiles = true;
  20. /** @var string */
  21. public $generatedFileNamePrefix = "";
  22. /** @var array */
  23. public $filters = array();
  24. /** @var array */
  25. private $files = array();
  26. /** @var string */
  27. private $module = null;
  28. /**
  29. * Get html element including generated content
  30. * @param string $source
  31. * @return Html
  32. */
  33. abstract public function getElement($source);
  34. /**
  35. * Generate compiled file(s) and render link(s)
  36. */
  37. public function render()
  38. {
  39. $hasArgs = func_num_args() > 0;
  40. if ($hasArgs) {
  41. $backup = $this->files;
  42. $this->clear();
  43. $this->addFiles(func_get_args());
  44. }
  45. // joined files
  46. if ($this->joinFiles) {
  47. $file = $this->generate($this->files);
  48. echo $this->getElement($this->tempUri . "/" . $file);
  49. // separated files
  50. } else {
  51. foreach ($this->files as $file) {
  52. $file = $this->generate(array($file));
  53. echo $this->getElement($this->tempUri . "/" . $file);
  54. }
  55. }
  56. if ($hasArgs) {
  57. $this->files = $backup;
  58. }
  59. }
  60. public function setModule($module)
  61. {
  62. $this->module = $module;
  63. }
  64. /**
  65. * Get file list
  66. * @return array
  67. */
  68. public function getFiles() {
  69. return $this->files;
  70. }
  71. /**
  72. * Add file
  73. * @param string $file filename
  74. */
  75. public function addFile($file)
  76. {
  77. if (in_array($file, $this->files) || !file_exists($this->sourcePath . "/" . $file)) {
  78. return;
  79. }
  80. $this->files[] = $file;
  81. }
  82. /**
  83. * Add files
  84. * @param array $files list of files
  85. */
  86. public function addFiles(array $files)
  87. {
  88. foreach ($files as $file) {
  89. $this->addFile($file);
  90. }
  91. }
  92. /**
  93. * Remove file
  94. * @param string $file filename
  95. */
  96. public function removeFile($file)
  97. {
  98. $this->removeFiles(array($file));
  99. }
  100. /**
  101. * Remove files
  102. * @param array $files list of files
  103. */
  104. public function removeFiles(array $files)
  105. {
  106. $this->files = array_diff($this->files, $files);
  107. }
  108. /**
  109. * Remove all files
  110. */
  111. public function clear() {
  112. $this->files = array();
  113. }
  114. /**
  115. * Get last modified timestamp of newest file
  116. * @param array $files
  117. * @return int
  118. */
  119. public function getLastModified(array $files = null)
  120. {
  121. if ($files === null) {
  122. $files = $this->files;
  123. }
  124. $modified = 0;
  125. foreach ($files as $file) {
  126. $modified = max($modified, filemtime($this->sourcePath . "/" . $file));
  127. }
  128. return $modified;
  129. }
  130. /**
  131. * Filename of generated file
  132. * @param array $files
  133. * @return string
  134. */
  135. public function getGeneratedFilename(array $files = null)
  136. {
  137. if ($files === null) {
  138. $files = $this->files;
  139. }
  140. //return implode("|", $files);
  141. return $this->generatedFileNamePrefix . md5(implode("|", $files) . $this->getLastModified($files) .$this->module);
  142. }
  143. /**
  144. * Get joined content of all files
  145. * @param array $files
  146. * @return string
  147. */
  148. public function getContent(array $files = null)
  149. {
  150. if ($files === null) {
  151. $files = $this->files;
  152. }
  153. $content = "";
  154. foreach ($files as $file) {
  155. $content .= $this->loadFile($file);
  156. }
  157. return $this->applyFilters($content);
  158. }
  159. /**
  160. * Load content and save file
  161. * @param array $files
  162. * @return string filename of generated file
  163. */
  164. protected function generate($files)
  165. {
  166. $name = $this->getGeneratedFilename($files);
  167. $path = $this->tempPath . "/" . $name;
  168. if (!file_exists($path)) {
  169. if (!in_array(SafeStream::PROTOCOL, stream_get_wrappers())) {
  170. SafeStream::register();
  171. }
  172. if( !file_exists(dirname($path))) mkdir($path, 0775, true);
  173. file_put_contents("safe://" . $path, $this->getContent($files));
  174. }
  175. return $name;
  176. }
  177. /**
  178. * Apply filters to a string
  179. * @param string $s
  180. * @return string
  181. */
  182. protected function applyFilters($s) {
  183. foreach ($this->filters as $filter) {
  184. $s = call_user_func($filter, $s);
  185. }
  186. return $s;
  187. }
  188. /**
  189. * Load file
  190. * @param string $path
  191. * @return string
  192. */
  193. protected function loadFile($file) {
  194. return file_get_contents($this->sourcePath . "/" . $file);
  195. }
  196. }