/combine.class.php

https://github.com/lucascolette/Combine-files · PHP · 125 lines · 47 code · 30 blank · 48 comment · 6 complexity · 686364cd7452d76d05baa3ed63d72813 MD5 · raw file

  1. <?php
  2. /*
  3. * Combine js and css files with cache system
  4. * Based on http://www.chrisrenner.com/2011/02/minify-and-compress-all-your-javascript-files-into-one-on-the-fly/
  5. *
  6. * @version 1.0
  7. * @author Lucas Colette <eu@colet.me>
  8. *
  9. */
  10. class CombineFiles {
  11. public function __construct () {
  12. }
  13. /*
  14. * Concatenate and minify multiple files, js or css,
  15. * defined on param $type
  16. *
  17. * @uses CombineFiles::getFileName() to get the hash of the file
  18. * @uses CombineFiles::compareFiles() to verify if the cache file already exists
  19. *
  20. * @param $filesDir string Source dir of original files
  21. * @param $cacheDir string
  22. * @param $files array List of files to be combined
  23. */
  24. public function Fetch ( $filesDir, $cacheDir, $files, $type ) {
  25. $cacheFile = self::getFileName($files, $type);
  26. $hasCacheFile = self::compareFiles($filesDir, $cacheDir, $files, $type, $cacheFile);
  27. /* Verify the cache file */
  28. if ( !$hasCacheFile ) {
  29. $content = null;
  30. foreach ( $files as $script ) {
  31. $extensionFile = ( $type == 'js' ) ? '.js' : '.css';
  32. $content .= file_get_contents($filesDir . '/' . $script . $extensionFile);
  33. }
  34. $minify = ($type == 'js') ? JSMin::minify($content) : self::minifyCSS($content);
  35. $openFile = fopen($cacheDir . '/' . $cacheFile, "w");
  36. fwrite($openFile, $minify);
  37. fclose($openFile);
  38. }
  39. return $cacheDir . '/' . $cacheFile;
  40. }
  41. /*
  42. * Get the hash of the cache file
  43. *
  44. * @param $files string
  45. * @param $type string css or js
  46. * @return string
  47. */
  48. public function getFileName ( $files, $type ) {
  49. $hashFile = md5( implode('_', $files) ).'.'.$type;
  50. return $hashFile;
  51. }
  52. /*
  53. * Compare the modified date of the source files
  54. * against the hash file if it exists and return true if the hash
  55. * file is newer and
  56. * return false if its older or if hash file doesn't exist
  57. *
  58. * @param $filesDir string
  59. * @param $cacheDir string
  60. * @param $files array
  61. * @param $type string
  62. * @param $cacheFile string
  63. * @return boolean
  64. */
  65. public function compareFiles ( $filesDir, $cacheDir, $files, $type, $cacheFile ) {
  66. if ( !file_exists($cacheDir . '/' . $cacheFile) ) {
  67. return false;
  68. }
  69. $cacheModified = filemtime($cacheDir . '/' . $cacheFile);
  70. $extensionFile = ($type == 'js') ? '.js' : '.css';
  71. foreach ($files as $script) {
  72. $sourceModified = filemtime($filesDir . '/' . $script . $extensionFile);
  73. if ( $sourceModified > $cacheModified ) {
  74. return false;
  75. }
  76. }
  77. return true;
  78. }
  79. /*
  80. * Minify CSS
  81. *
  82. * @param $content string
  83. * @return string
  84. */
  85. public function minifyCSS ( $content ) {
  86. $content = preg_replace('!/\*[^*]*\*+([^/][^*]*\*+)*/!', '', $content);
  87. /* Remove tabs, spaces, newlines, etc. */
  88. $content = str_replace(array("\r\n","\r","\n","\t",' ',' ',' '), '', $content);
  89. /* Remove other spaces before/after ; */
  90. $content = preg_replace(array('(( )+{)','({( )+)'), '{', $content);
  91. $content = preg_replace(array('(( )+})','(}( )+)','(;( )*})'), '}', $content);
  92. $content = preg_replace(array('(;( )+)','(( )+;)'), ';', $content);
  93. return $content;
  94. }
  95. }