forked from ceesvanegmond/minify
-
Notifications
You must be signed in to change notification settings - Fork 30
Expand file tree
/
Copy pathMinify.php
More file actions
259 lines (216 loc) · 6.16 KB
/
Minify.php
File metadata and controls
259 lines (216 loc) · 6.16 KB
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
<?php namespace Devfactory\Minify;
use Devfactory\Minify\Exceptions\InvalidArgumentException;
use Devfactory\Minify\Providers\JavaScript;
use Devfactory\Minify\Providers\StyleSheet;
use RecursiveIteratorIterator;
use RecursiveDirectoryIterator;
use Request;
class Minify
{
/**
* @var array
*/
protected $config;
/**
* @var array
*/
protected $attributes = array();
/**
* @var string
*/
private $environment;
/**
* @var
*/
private $provider;
/**
* @var
*/
private $buildPath;
/**
* @var bool
*/
private $fullUrl = false;
/**
* @var bool
*/
private $onlyUrl = false;
/**
* @var bool
*/
private $buildExtension;
/**
* @param array $config
* @param string $environment
*/
public function __construct(array $config, $environment) {
$this->checkConfiguration($config);
$this->config = $config;
$this->environment = $environment;
}
/**
* @param $file
* @param array $attributes
* @return string
*/
public function javascript($file, $attributes = array()) {
$this->provider = new JavaScript(public_path(), ['hash_salt' => $this->config['hash_salt'], 'disable_mtime' => $this->config['disable_mtime']]);
$this->buildPath = $this->config['js_build_path'];
$this->attributes = $attributes;
$this->buildExtension = 'js';
$this->process($file);
return $this;
}
/**
* @param $file
* @param array $attributes
* @return string
*/
public function stylesheet($file, $attributes = array()) {
$this->provider = new StyleSheet(public_path(), ['hash_salt' => $this->config['hash_salt'], 'disable_mtime' => $this->config['disable_mtime'], 'disable_url_correction' => $this->config['disable_url_correction']]);
$this->buildPath = $this->config['css_build_path'];
$this->attributes = $attributes;
$this->buildExtension = 'css';
$this->process($file);
return $this;
}
/**
* @param $dir
* @param array $attributes
* @return string
*/
public function stylesheetDir($dir, $attributes = array()) {
$this->provider = new StyleSheet(public_path(), ['hash_salt' => $this->config['hash_salt'], 'disable_mtime' => $this->config['disable_mtime'], 'disable_url_correction' => $this->config['disable_url_correction']]);
$this->buildPath = $this->config['css_build_path'];
$this->attributes = $attributes;
$this->buildExtension = 'css';
return $this->assetDirHelper('css', $dir);
}
/**
* @param $dir
* @param array $attributes
* @return string
*/
public function javascriptDir($dir, $attributes = array()) {
$this->provider = new JavaScript(public_path(), ['hash_salt' => $this->config['hash_salt'], 'disable_mtime' => $this->config['disable_mtime']]);
$this->buildPath = $this->config['js_build_path'];
$this->attributes = $attributes;
$this->buildExtension = 'js';
return $this->assetDirHelper('js', $dir);
}
/**
* @param $ext
* @param $dir
* @return string
*/
private function assetDirHelper($ext, $dir) {
$files = array();
$itr_obj = new RecursiveDirectoryIterator(public_path().$dir);
$itr_obj->setFlags(RecursiveDirectoryIterator::SKIP_DOTS);
$dir_obj = new RecursiveIteratorIterator($itr_obj);
foreach ($dir_obj as $fileinfo)
{
if (!$fileinfo->isDir() && ($filename = $fileinfo->getFilename()) && (pathinfo($filename, PATHINFO_EXTENSION) == $ext) && (strlen($fileinfo->getFilename()) < 30))
{
$files[] = str_replace(public_path(), '', $fileinfo);
}
}
if (count($files) > 0)
{
if($this->config['reverse_sort']) {
rsort($files);
}
else {
sort($files);
}
$this->process($files);
}
return $this;
}
/**
* @param $file
*/
private function process($file) {
$this->provider->add($file);
if($this->minifyForCurrentEnvironment() && $this->provider->make($this->buildPath))
{
$this->provider->minify();
}
$this->fullUrl = false;
}
/**
* @return mixed
*/
protected function render()
{
$baseUrl = $this->fullUrl ? $this->getBaseUrl() : '';
if (!$this->minifyForCurrentEnvironment())
{
return $this->provider->tags($baseUrl, $this->attributes);
}
if( $this->buildExtension == 'js')
{
$buildPath = isset($this->config['js_url_path']) ? $this->config['js_url_path'] : $this->buildPath;
}
else# if( $this->buildExtension == 'css')
{
$buildPath = isset($this->config['css_url_path']) ? $this->config['css_url_path'] : $this->buildPath;
}
$filename = $baseUrl . $buildPath . $this->provider->getFilename();
if ($this->onlyUrl) {
return $filename;
}
return $this->provider->tag($filename, $this->attributes);
}
/**
* @return bool
*/
protected function minifyForCurrentEnvironment()
{
return !in_array($this->environment, $this->config['ignore_environments']);
}
/**
* @return string
*/
public function withFullUrl() {
$this->fullUrl = true;
return $this;
}
/**
* @return mixed
*/
public function onlyUrl()
{
$this->onlyUrl = true;
return $this;
}
/**
* @return string
*/
public function __toString() {
return $this->render();
}
/**
* @param array $config
* @throws Exceptions\InvalidArgumentException
* @return array
*/
private function checkConfiguration(array $config) {
if(!isset($config['css_build_path']) || !is_string($config['css_build_path']))
throw new InvalidArgumentException("Missing css_build_path field");
if(!isset($config['js_build_path']) || !is_string($config['js_build_path']))
throw new InvalidArgumentException("Missing js_build_path field");
if(!isset($config['ignore_environments']) || !is_array($config['ignore_environments']))
throw new InvalidArgumentException("Missing ignore_environments field");
}
/**
* @return string
*/
private function getBaseUrl() {
if (is_null($this->config['base_url']) || (trim($this->config['base_url']) == '')) {
return Request::root();
} else {
return $this->config['base_url'];
}
}
}