PageRenderTime 60ms CodeModel.GetById 25ms RepoModel.GetById 1ms app.codeStats 0ms

/application/libraries/Minify/Minify.php

https://bitbucket.org/rcasares/alianza
PHP | 236 lines | 100 code | 29 blank | 107 comment | 18 complexity | eefc73d3139fa53d81596e57df040759 MD5 | raw file
  1. <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
  2. /**
  3. * CodeIgniter Minify
  4. *
  5. * A minification driver system for CodeIgniter
  6. *
  7. * NOTICE OF LICENSE
  8. *
  9. * Licensed under the Open Software License version 3.0
  10. *
  11. * This source file is subject to the Open Software License (OSL 3.0) that is
  12. * bundled with this package in the files license.txt / license.rst. It is
  13. * also available through the world wide web at this URL:
  14. * http://opensource.org/licenses/OSL-3.0
  15. *
  16. * @package ci-minify
  17. * @author Eric Barnes
  18. * @copyright Copyright (c) Eric Barnes. (http://ericlbarnes.com/)
  19. * @license http://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0)
  20. * @link http://ericlbarnes.com
  21. * @since Version 1.0
  22. * @filesource
  23. */
  24. // ------------------------------------------------------------------------
  25. /**
  26. * Minify Driver
  27. *
  28. * @subpackage Drivers
  29. */
  30. class Minify extends CI_Driver_Library {
  31. /**
  32. * CI Object
  33. *
  34. * @var object
  35. */
  36. protected $_ci = '';
  37. /**
  38. * valid drivers
  39. *
  40. * @var array
  41. */
  42. public $valid_drivers = array('minify_css', 'minify_js');
  43. // ------------------------------------------------------------------------
  44. /**
  45. * Construct
  46. *
  47. * Initialize params
  48. *
  49. * @return \Minify
  50. */
  51. public function __construct()
  52. {
  53. $this->_ci =& get_instance();
  54. log_message('debug', 'CI-Minify: Library initialized.');
  55. }
  56. // ------------------------------------------------------------------------
  57. /**
  58. * Combine Files
  59. *
  60. * Pass an array of files and combine them.
  61. * @param array $files
  62. * @param string $type
  63. * @param bool $compact
  64. * @param string $css_charset
  65. * @return mixed
  66. */
  67. public function combine_files($files = array(), $type = '', $compact = TRUE, $css_charset = 'utf-8')
  68. {
  69. if ( ! is_array($files) OR count($files) < 1)
  70. {
  71. log_message('error', 'Minify->combine_files missing files array');
  72. return FALSE;
  73. }
  74. return $this->_do_combine($files, $type, $compact, $css_charset);
  75. }
  76. // ------------------------------------------------------------------------
  77. /**
  78. * Combine Directory
  79. *
  80. * Pass a directory and combine all the files into one string.
  81. *
  82. * @param string $directory
  83. * @param array $ignore
  84. * @param string $type
  85. * @param bool $compact
  86. * @param string $css_charset
  87. * @return string
  88. */
  89. public function combine_directory($directory = '', $ignore = array(), $type = '', $compact = TRUE, $css_charset = 'utf-8')
  90. {
  91. $available = array();
  92. if ($directory == '' OR ! is_dir($directory))
  93. {
  94. log_message('error', 'Minify->combine_directory missing files array');
  95. return FALSE;
  96. }
  97. $this->_ci->load->helper('directory');
  98. foreach (directory_map($directory, TRUE) as $dir => $file)
  99. {
  100. if ($this->_get_type($file) == 'js' OR $this->_get_type($file) == 'css')
  101. {
  102. $available[$file] = $directory.'/'.$file;
  103. }
  104. }
  105. // Finally get ignored files
  106. if (count($ignore) > 0)
  107. {
  108. foreach ($available AS $key => $file)
  109. {
  110. if (in_array($key, $ignore))
  111. {
  112. unset($available[$key]);
  113. }
  114. }
  115. }
  116. return $this->_do_combine($available, $type, $compact, $css_charset);
  117. }
  118. // ------------------------------------------------------------------------
  119. /**
  120. * Do combine
  121. *
  122. * Combine all the files and return a string.
  123. *
  124. * @param array $files
  125. * @param string $type
  126. * @param bool $compact
  127. * @param string $css_charset
  128. * @return string
  129. */
  130. private function _do_combine($files, $type, $compact = TRUE, $css_charset = 'utf-8')
  131. {
  132. $contents = '';
  133. $file_count = 0;
  134. foreach ($files AS $file)
  135. {
  136. if ( ! file_exists($file))
  137. {
  138. log_message('error', 'Minify->_do_combine missing file '.$file);
  139. continue;
  140. }
  141. $file_count++;
  142. if ($type == '')
  143. {
  144. $type = $this->_get_type($file);
  145. }
  146. $path_info = pathinfo($file, PATHINFO_BASENAME); // Referal File and path
  147. if ($type == 'css')
  148. {
  149. // only one charset placed at the beginning of the document is allowed
  150. // in order to keep standars compliance and fixing Webkit problems
  151. // Note: Minify_css driver yet remove all charsets previously
  152. if ($file_count == 1)
  153. {
  154. $contents .= '@charset "'.$css_charset.'";'."\n";
  155. }
  156. $contents .= "\n".'/* @fileRef '.$path_info.' */'."\n";
  157. $contents .= $this->css->min($file, $compact, $is_aggregated = TRUE);
  158. }
  159. elseif ($type == 'js')
  160. {
  161. unset($css_charset);
  162. $contents .= "\n".'// @fileRef '.$path_info.' '."\n";
  163. $contents .= $this->js->min($file, $compact);
  164. }
  165. else
  166. {
  167. $contents .= $file."\n\n";
  168. }
  169. }
  170. return $contents;
  171. }
  172. // ------------------------------------------------------------------------
  173. /**
  174. * Save File
  175. *
  176. * Save a file
  177. *
  178. * @param string $contents
  179. * @param string $full_path
  180. * @return bool
  181. */
  182. public function save_file($contents = '', $full_path = '')
  183. {
  184. $this->_ci->load->helper('file');
  185. if ( ! write_file($full_path, $contents))
  186. {
  187. log_message('error', 'Minify->save_file could not write file');
  188. return FALSE;
  189. }
  190. return TRUE;
  191. }
  192. // ------------------------------------------------------------------------
  193. /**
  194. * Get Type
  195. *
  196. * Get the file extension to determine file type
  197. *
  198. * @param string $file
  199. * @return string
  200. */
  201. private function _get_type($file)
  202. {
  203. return pathinfo($file, PATHINFO_EXTENSION);
  204. }
  205. }
  206. /* End of file Minify.php */
  207. /* Location: ./application/libraries/Minify/Minify.php */