/application/library/Thirdpart/Minify/lib/Minify/Source.php

https://gitlab.com/flyhope/Hiblog · PHP · 207 lines · 105 code · 22 blank · 80 comment · 15 complexity · 289b89fb8e0606d0582b8d30a16b237b MD5 · raw file

  1. <?php
  2. /**
  3. * Class Minify_Source
  4. * @package Minify
  5. */
  6. /**
  7. * A content source to be minified by Minify.
  8. *
  9. * This allows per-source minification options and the mixing of files with
  10. * content from other sources.
  11. *
  12. * @package Minify
  13. * @author Stephen Clay <steve@mrclay.org>
  14. */
  15. class Minify_Source implements Minify_SourceInterface {
  16. /**
  17. * @var int time of last modification
  18. */
  19. protected $lastModified = null;
  20. /**
  21. * @var callback minifier function specifically for this source.
  22. */
  23. protected $minifier = null;
  24. /**
  25. * @var array minification options specific to this source.
  26. */
  27. protected $minifyOptions = array();
  28. /**
  29. * @var string full path of file
  30. */
  31. protected $filepath = null;
  32. /**
  33. * @var string HTTP Content Type (Minify requires one of the constants Minify::TYPE_*)
  34. */
  35. protected $contentType = null;
  36. /**
  37. * @var string
  38. */
  39. protected $content = null;
  40. /**
  41. * @var callable
  42. */
  43. protected $getContentFunc = null;
  44. /**
  45. * @var string
  46. */
  47. protected $id = null;
  48. /**
  49. * Create a Minify_Source
  50. *
  51. * In the $spec array(), you can either provide a 'filepath' to an existing
  52. * file (existence will not be checked!) or give 'id' (unique string for
  53. * the content), 'content' (the string content) and 'lastModified'
  54. * (unixtime of last update).
  55. *
  56. * @param array $spec options
  57. */
  58. public function __construct($spec)
  59. {
  60. if (isset($spec['filepath'])) {
  61. $ext = pathinfo($spec['filepath'], PATHINFO_EXTENSION);
  62. switch ($ext) {
  63. case 'js' : $this->contentType = Minify::TYPE_JS;
  64. break;
  65. case 'less' : // fallthrough
  66. case 'css' : $this->contentType = Minify::TYPE_CSS;
  67. break;
  68. case 'htm' : // fallthrough
  69. case 'html' : $this->contentType = Minify::TYPE_HTML;
  70. break;
  71. }
  72. $this->filepath = $spec['filepath'];
  73. $this->id = $spec['filepath'];
  74. // TODO ideally not touch disk in constructor
  75. $this->lastModified = filemtime($spec['filepath']);
  76. if (!empty($spec['uploaderHoursBehind'])) {
  77. // offset for Windows uploaders with out of sync clocks
  78. $this->lastModified += round($spec['uploaderHoursBehind'] * 3600);
  79. }
  80. } elseif (isset($spec['id'])) {
  81. $this->id = 'id::' . $spec['id'];
  82. if (isset($spec['content'])) {
  83. $this->content = $spec['content'];
  84. } else {
  85. $this->getContentFunc = $spec['getContentFunc'];
  86. }
  87. $this->lastModified = isset($spec['lastModified'])
  88. ? $spec['lastModified']
  89. : time();
  90. }
  91. if (isset($spec['contentType'])) {
  92. $this->contentType = $spec['contentType'];
  93. }
  94. if (isset($spec['minifier'])) {
  95. $this->minifier = $spec['minifier'];
  96. }
  97. if (isset($spec['minifyOptions'])) {
  98. $this->minifyOptions = $spec['minifyOptions'];
  99. }
  100. }
  101. /**
  102. * {@inheritdoc}
  103. */
  104. public function getLastModified()
  105. {
  106. return $this->lastModified;
  107. }
  108. /**
  109. * {@inheritdoc}
  110. */
  111. public function getMinifier()
  112. {
  113. return $this->minifier;
  114. }
  115. /**
  116. * {@inheritdoc}
  117. */
  118. public function setMinifier($minifier)
  119. {
  120. $this->minifier = $minifier;
  121. }
  122. /**
  123. * {@inheritdoc}
  124. */
  125. public function getMinifierOptions()
  126. {
  127. return $this->minifyOptions;
  128. }
  129. /**
  130. * {@inheritdoc}
  131. */
  132. public function setMinifierOptions(array $options)
  133. {
  134. $this->minifyOptions = $options;
  135. }
  136. /**
  137. * {@inheritdoc}
  138. */
  139. public function getContentType()
  140. {
  141. return $this->contentType;
  142. }
  143. /**
  144. * {@inheritdoc}
  145. */
  146. public function getContent()
  147. {
  148. if (null === $this->filepath) {
  149. if (null === $this->content) {
  150. $content = call_user_func($this->getContentFunc, $this->id);
  151. } else {
  152. $content = $this->content;
  153. }
  154. } else {
  155. $content = file_get_contents($this->filepath);
  156. }
  157. // remove UTF-8 BOM if present
  158. return ("\xEF\xBB\xBF" === substr($content, 0, 3)) ? substr($content, 3) : $content;
  159. }
  160. /**
  161. * {@inheritdoc}
  162. */
  163. public function getId()
  164. {
  165. return $this->id;
  166. }
  167. /**
  168. * {@inheritdoc}
  169. */
  170. public function getFilePath()
  171. {
  172. return $this->filepath;
  173. }
  174. /**
  175. * {@inheritdoc}
  176. */
  177. public function setupUriRewrites() {
  178. if ($this->filepath
  179. && !isset($this->minifyOptions['currentDir'])
  180. && !isset($this->minifyOptions['prependRelativePath'])
  181. ) {
  182. $this->minifyOptions['currentDir'] = dirname($this->filepath);
  183. }
  184. }
  185. }