Minify_Source

A content source to be minified by Minify.

Defined (1)

The class is defined in the following location(s).

/lib/Minify/Minify/Source.php  
  1. class Minify_Source { 
  2.  
  3. /** 
  4. * @var int time of last modification 
  5. */ 
  6. public $lastModified = null; 
  7.  
  8. /** 
  9. * @var callback minifier function specifically for this source. 
  10. */ 
  11. public $minifier = null; 
  12.  
  13. /** 
  14. * @var array minification options specific to this source. 
  15. */ 
  16. public $minifyOptions = null; 
  17.  
  18. /** 
  19. * @var string full path of file 
  20. */ 
  21. public $filepath = null; 
  22.  
  23. /** 
  24. * @var string HTTP Content Type (Minify requires one of the constants Minify::TYPE_*) 
  25. */ 
  26. public $contentType = null; 
  27.  
  28. /** 
  29. * Create a Minify_Source 
  30. *  
  31. * In the $spec array(), you can either provide a 'filepath' to an existing 
  32. * file (existence will not be checked!) or give 'id' (unique string for  
  33. * the content), 'content' (the string content) and 'lastModified'  
  34. * (unixtime of last update). 
  35. *  
  36. * As a shortcut, the controller will replace "//" at the beginning 
  37. * of a filepath with $_SERVER['DOCUMENT_ROOT'] . '/'. 
  38. * @param array $spec options 
  39. */ 
  40. public function __construct($spec) 
  41. if (isset($spec['filepath'])) { 
  42. if (0 === strpos($spec['filepath'], '//')) { 
  43. $spec['filepath'] = $_SERVER['DOCUMENT_ROOT'] . substr($spec['filepath'], 1); 
  44. $segments = explode('.', $spec['filepath']); 
  45. $ext = strtolower(array_pop($segments)); 
  46. switch ($ext) { 
  47. case 'js' : $this->contentType = 'application/x-javascript'; 
  48. break; 
  49. case 'css' : $this->contentType = 'text/css'; 
  50. break; 
  51. case 'htm' : // fallthrough 
  52. case 'html' : $this->contentType = 'text/html'; 
  53. break; 
  54. $this->filepath = $spec['filepath']; 
  55. $this->_id = $spec['filepath']; 
  56. $this->lastModified = filemtime($spec['filepath']) 
  57. // offset for Windows uploaders with out of sync clocks 
  58. + round(Minify0_Minify::$uploaderHoursBehind * 3600); 
  59. } elseif (isset($spec['id'])) { 
  60. $this->_id = 'id::' . $spec['id']; 
  61. if (isset($spec['content'])) { 
  62. $this->_content = $spec['content']; 
  63. } else { 
  64. $this->_getContentFunc = $spec['getContentFunc']; 
  65. $this->lastModified = isset($spec['lastModified']) 
  66. ? $spec['lastModified'] 
  67. : time(); 
  68. if (isset($spec['contentType'])) { 
  69. $this->contentType = $spec['contentType']; 
  70. if (isset($spec['minifier'])) { 
  71. $this->minifier = $spec['minifier']; 
  72. if (isset($spec['minifyOptions'])) { 
  73. $this->minifyOptions = $spec['minifyOptions']; 
  74.  
  75. /** 
  76. * Get content 
  77. * @return string 
  78. */ 
  79. public function getContent() 
  80. if (isset($this->minifyOptions['processCssImports']) && $this->minifyOptions['processCssImports']) { 
  81. $content = Minify_ImportProcessor::process($this->filepath); 
  82. } else { 
  83. $content = (null !== $this->filepath) 
  84. ? file_get_contents($this->filepath) 
  85. : ((null !== $this->_content) 
  86. ? $this->_content 
  87. : call_user_func($this->_getContentFunc, $this->_id) 
  88. ); 
  89.  
  90. // remove UTF-8 BOM if present 
  91. return (pack("CCC", 0xef, 0xbb, 0xbf) === substr($content, 0, 3)) 
  92. ? substr($content, 3) 
  93. : $content; 
  94.  
  95. /** 
  96. * Get id 
  97. * @return string 
  98. */ 
  99. public function getId() 
  100. return $this->_id; 
  101.  
  102. /** 
  103. * Verifies a single minification call can handle all sources 
  104. * @param array $sources Minify_Source instances 
  105. *  
  106. * @return bool true iff there no sources with specific minifier preferences. 
  107. */ 
  108. public static function haveNoMinifyPrefs($sources) 
  109. foreach ($sources as $source) { 
  110. if (null !== $source->minifier 
  111. || null !== $source->minifyOptions) { 
  112. return false; 
  113. return true; 
  114.  
  115. /** 
  116. * Get unique string for a set of sources 
  117. * @param array $sources Minify_Source instances 
  118. *  
  119. * @return string 
  120. */ 
  121. public static function getDigest($sources) 
  122. foreach ($sources as $source) { 
  123. $info[] = array( 
  124. $source->_id, $source->minifier, $source->minifyOptions 
  125. ); 
  126. return md5(serialize($info)); 
  127.  
  128. /** 
  129. * Get content type from a group of sources 
  130. *  
  131. * This is called if the user doesn't pass in a 'contentType' options  
  132. *  
  133. * @param array $sources Minify_Source instances 
  134. *  
  135. * @return string content type. e.g. 'text/css' 
  136. */ 
  137. public static function getContentType($sources) 
  138. foreach ($sources as $source) { 
  139. if ($source->contentType !== null) { 
  140. return $source->contentType; 
  141. return 'text/plain'; 
  142.  
  143. protected $_content = null; 
  144. protected $_getContentFunc = null; 
  145. protected $_id = null;