-
Notifications
You must be signed in to change notification settings - Fork 1
/
mini.php
122 lines (108 loc) · 3.72 KB
/
mini.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
<?php
/**
* JS / CSS minifier
*
* PHP versions 4 and 5
*
* CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
* Copyright 2005-2011, Cake Software Foundation, Inc. (http://cakefoundation.org)
*
* @author Pablo Leano
*/
App::uses('File', 'Utility');
class Mini {
const CSS = "css";
const JS = "js";
private $response;
private $filename;
private $path;
private $type;
public function __construct($url,CakeResponse $response,$isCss = false,$isJs = false) {
$this->url = $url;
$this->response = $response;
//get the type of file (js or css)
if($isCss){
$this->type = Mini::CSS;
$this->path = CSS;
}
if($isJs){
$this->type = Mini::JS;
$this->path = JS;
}
if(!$this->type){
throw new Exception("Incorrect type of file");
}
//extract the filename from the url
if (preg_match('|\.\.|', $url) || !preg_match('#^(ccss|cjs)/(.+)$#i', $url, $matches)) {
throw new Exception('Wrong file name');
}
$this->filename = $matches[2];
}
public function process(){
//checks if the file exist in the webroot/css , or if it's in cache (case of merged files)
// if the file is found on cache, changes the $this->cachename
$this->_validates();
$cachefile = CACHE . $this->type . DS . str_replace(array('/','\\'), '-', $this->filename);
//the the compressed file either from cache or from using a compressor
if (file_exists($cachefile)) {
$templateModified = filemtime($this->path . $this->filename);
$cacheModified = filemtime($cachefile);
//if the file is more recent than the cache, compress it
if ($templateModified > $cacheModified) {
$output = $this->_getCompressed();
$this->_writeToCache($cachefile, $output);
} else {
$output = file_get_contents($cachefile);
}
} else {
$output = $this->_getCompressed();
$this->_writeToCache($cachefile, $output);
$templateModified = time();
}
//create the response body and headers
$this->response->header('Date',date("D, j M Y G:i:s ", $templateModified) . 'GMT');
//set the correct content type
//$this->response->header('Content-Type',($this->type == Mini::CSS ? "text/css" : "application/x-javascript"));
$this->response->type(($this->type == Mini::CSS ? "text/css" : "application/x-javascript"));
$this->response->header('Expires', gmdate("D, d M Y H:i:s", time() + DAY) . " GMT");
$this->response->header('Cache-Control','max-age=86400, must-revalidate'); // HTTP/1.1
$this->response->header('Pragma: cache'); // HTTP/1.0
$this->response->body($output);
$this->response->send();
}
/**
* Gets the compressed content of the current file
*/
private function _getCompressed(){
App::import('Vendor', 'Compressor');
// here i'm using csspp but you could also use Yuicompressor
$compressor = new Compressor($this->filename, $this->path, $this->type);
return $compressor->process();
}
/**
* Write the content to a file
* @param string $cachefile path for the cache file
* @param string $content file's content
* @throws Exception
*/
private function _writeToCache($cachefile, $content){
$cache = new File($cachefile);
if(!$cache->write($content)){
throw new Exception('Could not write cache file');
}
}
private function _validates(){
if (($this->type == Mini::CSS && !file_exists(CSS . $this->filename)) || ($this->type == Mini::JS && !file_exists(JS . $this->filename))) {
//check file exists on cache
if(!file_exists(CACHE . $this->type . DS . $this->filename)){
throw new Exception('File not found');
}
}
}
}
$mini = new Mini($url,$response,$isCss,$isJs);
try {
$mini->process();
} catch (Exception $e) {
exit($e->getMessage());
}