-
Notifications
You must be signed in to change notification settings - Fork 0
/
css.php
177 lines (149 loc) · 3.55 KB
/
css.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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
<?php
/**
* Easy Min
*
* Simple minification for better website performance
*
* @author Timothy J. Warren
* @copyright Copyright (c) 2012-2016
* @link https://git.timshomepage.net/aviat4ion/Easy-Min
* @license http://philsturgeon.co.uk/code/dbad-license
*/
// --------------------------------------------------------------------------
namespace Aviat\EasyMin;
require_once('./min.php');
/**
* Simple CSS Minifier
*/
class CSSMin extends BaseMin {
protected $css_root;
protected $path_from;
protected $path_to;
protected $group;
protected $last_modified;
protected $requested_time;
public function __construct(array $config, array $groups)
{
$group = $_GET['g'];
$this->css_root = $config['css_root'];
$this->path_from = $config['path_from'];
$this->path_to = $config['path_to'];
$this->group = $groups[$group];
$this->last_modified = $this->get_last_modified();
$this->send();
}
/**
* Send the CSS
*
* @return void
*/
protected function send()
{
if($this->last_modified >= $this->get_if_modified() && $this->is_not_debug())
{
throw new FileNotChangedException();
}
$css = ( ! array_key_exists('debug', $_GET))
? $this->compress($this->get_css())
: $this->get_css();
$this->output($css);
}
/**
* Function for compressing the CSS as tightly as possible
*
* @param string $buffer
* @return string
*/
protected function compress($buffer)
{
//Remove CSS comments
$buffer = preg_replace('!/\*[^*]*\*+([^/][^*]*\*+)*/!', '', $buffer);
//Remove tabs, spaces, newlines, etc.
$buffer = preg_replace('`\s+`', ' ', $buffer);
$replace = array(
' )' => ')',
') ' => ')',
' }' => '}',
'} ' => '}',
' {' => '{',
'{ ' => '{',
', ' => ',',
': ' => ':',
'; ' => ';',
);
//Eradicate every last space!
$buffer = trim(strtr($buffer, $replace));
$buffer = str_replace('{ ', '{', $buffer);
$buffer = str_replace('} ', '}', $buffer);
return $buffer;
}
/**
* Get the most recent file modification date
*
* @return int
*/
protected function get_last_modified()
{
$modified = array();
// Get all the css files, and concatenate them together
if(isset($this->group))
{
foreach($this->group as $file)
{
$new_file = realpath("{$this->css_root}{$file}");
$modified[] = filemtime($new_file);
}
}
//Add this page for last modified check
$modified[] = filemtime(__FILE__);
//Get the latest modified date
rsort($modified);
return array_shift($modified);
}
/**
* Get the css to display
*
* @return string
*/
protected function get_css()
{
$css = '';
foreach($this->group as $file)
{
$new_file = realpath("{$this->css_root}{$file}");
$css .= file_get_contents($new_file);
}
// Correct paths that have changed due to concatenation
// based on rules in the config file
$css = str_replace($this->path_from, $this->path_to, $css);
return $css;
}
/**
* Output the CSS
*
* @return void
*/
protected function output($css)
{
$this->send_final_output($css, 'text/css', $this->last_modified);
}
}
// --------------------------------------------------------------------------
// ! Start Minifying
// --------------------------------------------------------------------------
//Get config files
$config = require('config/config.php');
$groups = require($config['css_groups_file']);
if ( ! array_key_exists($_GET['g'], $groups))
{
throw new InvalidArgumentException('You must specify a css group that exists');
}
try
{
new CSSMin($config, $groups);
}
catch (FileNotChangedException $e)
{
BaseMin::send304();
}
//End of css.php