3 /** This file is part of KCFinder project
5 * @desc Minify JS & CSS
8 * @author Pavel Tzonkov <sunhater@sunhater.com>
9 * @copyright 2010-2014 KCFinder Project
10 * @license http://opensource.org/licenses/GPL-3.0 GPLv3
11 * @license http://opensource.org/licenses/LGPL-3.0 LGPLv3
12 * @link http://kcfinder.sunhater.com
20 protected $type = "js";
21 protected $minCmd = "";
22 protected $mime = array(
23 'js' => "text/javascript",
27 public function __construct($type=null) {
28 require "conf/config.php";
29 $this->config
= $_CONFIG;
30 $type = strtolower($type);
31 if (isset($this->mime
[$type]))
33 if (isset($_CONFIG["_{$this->type}MinCmd"]))
34 $this->minCmd
= $_CONFIG["_{$this->type}MinCmd"];
37 public function minify($cacheFile=null, $dir=null) {
39 $dir = dirname($_SERVER['SCRIPT_FILENAME']);
41 // MODIFICATION TIME FILES
44 $_SERVER['SCRIPT_FILENAME'],
48 // GET SOURCE CODE FILES
49 $files = dir
::content($dir, array(
51 'pattern' => '/^.*\.' . $this->type
. '$/'
54 // GET NEWEST MODIFICATION TIME
56 foreach (array_merge($mtFiles, $files) as $file) {
57 $fmtime = filemtime($file);
62 $header = "Content-Type: {$this->mime[$this->type]}";
64 // GET SOURCE CODE FROM CLIENT HTTP CACHE IF EXISTS
65 httpCache
::checkMTime($mtime, $header);
70 // GET SOURCE CODE FROM SERVER-SIDE CACHE
71 if (($cacheFile !== null) &&
72 file_exists($cacheFile) &&
74 (filemtime($cacheFile) >= $mtime) ||
75 !is_writable($cacheFile) // if cache file cannot be modified
76 ) // the script will output it always
77 ) { // with its distribution content
82 // MINIFY AND JOIN SOURCE CODE
84 foreach ($files as $file) {
86 if (strlen($this->minCmd
) && (substr($file, 4, 1) != "_")) {
87 $cmd = str_replace("{file}", $file, $this->minCmd
);
91 $source .= file_get_contents($file);
94 // UPDATE SERVER-SIDE CACHE
95 if (($cacheFile !== null) &&
97 is_writable($cacheFile) ||
99 !file_exists($cacheFile) &&
100 dir
::isWritable(dirname($cacheFile))
104 file_put_contents($cacheFile, $source);
105 touch($cacheFile, $mtime);
108 // OUTPUT SOURCE CODE