PluginProbe
WP-Optimize – Cache, Compress images, Minify & Clean database to boost page speed & performance / 4.7.0
WP-Optimize – Cache, Compress images, Minify & Clean database to boost page speed & performance v4.7.0
4.7.0 4.6.1 4.6.0 4.5.5 4.5.4 4.5.3 4.5.2 3.2.20 3.2.21 3.2.22 3.2.3 3.2.5 3.2.6 3.2.7 3.2.9 3.3.0 3.3.1 3.3.2 3.4.0 3.4.1 3.4.2 3.5.0 3.6.0 3.7.0 3.7.1 All 111 releases
wp-optimize / vendor / mrclay / minify / lib / Minify / App.php

App.php in WP-Optimize – Cache, Compress images, Minify & Clean database to boost page speed & performance 4.7.0, at vendor/mrclay/minify/lib/Minify/App.php

300 lines 9.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace Minify;
4
5 use Minify_Cache_File;
6 use Minify_CacheInterface;
7 use Minify_Controller_MinApp;
8 use Minify_ControllerInterface;
9 use Minify_DebugDetector;
10 use Minify_Env;
11 use Minify_Source_Factory;
12 use Props\Container;
13 use Psr\Log\LoggerInterface;
14 use RuntimeException;
15 use Monolog;
16 use Minify;
17
18 /**
19 * @property Minify_CacheInterface $cache
20 * @property Config $config
21 * @property string $configPath
22 * @property Minify_ControllerInterface $controller
23 * @property string $dir
24 * @property string $docRoot
25 * @property Minify_Env $env
26 * @property Monolog\Handler\ErrorLogHandler $errorLogHandler
27 * @property array $groupsConfig
28 * @property string $groupsConfigPath
29 * @property LoggerInterface $logger
30 * @property \Minify $minify
31 * @property array $serveOptions
32 * @property Minify_Source_Factory $sourceFactory
33 * @property array $sourceFactoryOptions
34 */
35 class App extends Container
36 {
37
38 /**
39 * Constructor
40 *
41 * @param string $dir Directory containing config files
42 */
43 public function __construct($dir)
44 {
45 $that = $this;
46
47 $this->dir = rtrim($dir, '/\\');
48
49 $this->cache = function (App $app) use ($that) {
50 $config = $app->config;
51
52 if ($config->cachePath instanceof Minify_CacheInterface) {
53 return $config->cachePath;
54 }
55
56 if (!$config->cachePath || is_string($config->cachePath)) {
57 return new Minify_Cache_File($config->cachePath, $config->cacheFileLocking, $app->logger);
58 }
59
60 $type = $that->typeOf($config->cachePath);
61 throw new RuntimeException('$min_cachePath must be a path or implement Minify_CacheInterface.'
62 . " Given $type");
63 };
64
65 $this->config = function (App $app) {
66 $config = (require $app->configPath);
67
68 if ($config instanceof Minify\Config) {
69 return $config;
70 }
71
72 // copy from vars into properties
73
74 $config = new Minify\Config();
75
76 $propNames = array_keys(get_object_vars($config));
77
78 $prefixer = function ($name) {
79 return "min_$name";
80 };
81 $varNames = array_map($prefixer, $propNames);
82
83 $varDefined = get_defined_vars();
84
85 $varNames = array_filter($varNames, function ($name) use ($varDefined) {
86 return array_key_exists($name, $varDefined);
87 });
88
89 $vars = compact($varNames);
90
91 foreach ($varNames as $varName) {
92 if (isset($vars[$varName])) {
93 $config->{substr($varName, 4)} = $vars[$varName];
94 }
95 }
96
97 if ($config->documentRoot) {
98 // copy into env
99 if (empty($config->envArgs['server'])) {
100 $config->envArgs['server'] = $_SERVER;
101 }
102 $config->envArgs['server']['DOCUMENT_ROOT'] = $config->documentRoot;
103 }
104
105 return $config;
106 };
107
108 $this->configPath = "{$this->dir}/config.php";
109
110 $this->controller = function (App $app) use ($that) {
111 $config = $app->config;
112
113 if (empty($config->factories['controller'])) {
114 $ctrl = new Minify_Controller_MinApp($app->env, $app->sourceFactory, $app->logger);
115 } else {
116 $ctrl = call_user_func($config->factories['controller'], $app);
117 }
118
119 if ($ctrl instanceof Minify_ControllerInterface) {
120 return $ctrl;
121 }
122
123 $type = $that->typeOf($ctrl);
124 throw new RuntimeException('$min_factories["controller"] callable must return an implementation'
125 ." of Minify_CacheInterface. Returned $type");
126 };
127
128 $this->docRoot = function (App $app) {
129 $config = $app->config;
130 if (empty($config->documentRoot)) {
131 return $app->env->getDocRoot();
132 }
133
134 return $app->env->normalizePath($config->documentRoot);
135 };
136
137 $this->env = function (App $app) {
138 return new Minify_Env($app->config->envArgs);
139 };
140
141 $this->errorLogHandler = function (App $app) {
142 $format = "%channel%.%level_name%: %message% %context% %extra%";
143 $handler = new Monolog\Handler\ErrorLogHandler();
144 $handler->setFormatter(new Monolog\Formatter\LineFormatter($format));
145
146 return $handler;
147 };
148
149 $this->groupsConfig = function (App $app) {
150 return (require $app->groupsConfigPath);
151 };
152
153 $this->groupsConfigPath = "{$this->dir}/groupsConfig.php";
154
155 $this->logger = function (App $app) use ($that) {
156 $value = $app->config->errorLogger;
157
158 if ($value instanceof LoggerInterface) {
159 return $value;
160 }
161
162 $logger = new Monolog\Logger('minify');
163
164 if (!$value) {
165 return $logger;
166 }
167
168 if ($value === true || $value instanceof \FirePHP) {
169 $logger->pushHandler($app->errorLogHandler);
170 $logger->pushHandler(new Monolog\Handler\FirePHPHandler());
171
172 return $logger;
173 }
174
175 if ($value instanceof Monolog\Handler\HandlerInterface) {
176 $logger->pushHandler($value);
177
178 return $logger;
179 }
180
181 // BC
182 if (is_object($value) && is_callable(array($value, 'log'))) {
183 $handler = new Minify\Logger\LegacyHandler($value);
184 $logger->pushHandler($handler);
185
186 return $logger;
187 }
188
189 $type = $that->typeOf($value);
190 throw new RuntimeException('If set, $min_errorLogger must be a PSR-3 logger or a Monolog handler.'
191 ." Given $type");
192 };
193
194 $this->minify = function (App $app) use ($that) {
195 $config = $app->config;
196
197 if (empty($config->factories['minify'])) {
198 return new \Minify($app->cache, $app->logger);
199 }
200
201 $minify = call_user_func($config->factories['minify'], $app);
202 if ($minify instanceof \Minify) {
203 return $minify;
204 }
205
206 $type = $that->typeOf($minify);
207 throw new RuntimeException('$min_factories["minify"] callable must return a Minify object.'
208 ." Returned $type");
209 };
210
211 $this->serveOptions = function (App $app) {
212 $config = $app->config;
213 $env = $app->env;
214
215 $ret = $config->serveOptions;
216
217 $ret['minifierOptions']['text/css']['docRoot'] = $app->docRoot;
218 $ret['minifierOptions']['text/css']['symlinks'] = $config->symlinks;
219 $ret['minApp']['symlinks'] = $config->symlinks;
220
221 // auto-add targets to allowDirs
222 foreach ($config->symlinks as $uri => $target) {
223 $ret['minApp']['allowDirs'][] = $target;
224 }
225
226 if ($config->allowDebugFlag) {
227 $ret['debug'] = Minify_DebugDetector::shouldDebugRequest($env);
228 }
229
230 if ($config->concatOnly) {
231 $ret['concatOnly'] = true;
232 }
233
234 // check for URI versioning
235 if ($env->get('v') !== null || preg_match('/&\\d/', $app->env->server('QUERY_STRING'))) {
236 $ret['maxAge'] = 31536000;
237 }
238
239 // need groups config?
240 if ($env->get('g') !== null) {
241 $ret['minApp']['groups'] = $app->groupsConfig;
242 }
243
244 return $ret;
245 };
246
247 $this->sourceFactory = function (App $app) {
248 return new Minify_Source_Factory($app->env, $app->sourceFactoryOptions, $app->cache);
249 };
250
251 $this->sourceFactoryOptions = function (App $app) {
252 $serveOptions = $app->serveOptions;
253 $ret = array();
254
255 // translate legacy setting to option for source factory
256 if (isset($serveOptions['minApp']['noMinPattern'])) {
257 $ret['noMinPattern'] = $serveOptions['minApp']['noMinPattern'];
258 }
259
260 if (isset($serveOptions['minApp']['allowDirs'])) {
261 $ret['allowDirs'] = $serveOptions['minApp']['allowDirs'];
262 }
263
264 if (isset($serveOptions['checkAllowDirs'])) {
265 $ret['checkAllowDirs'] = $serveOptions['checkAllowDirs'];
266 }
267
268 if (is_numeric($app->config->uploaderHoursBehind)) {
269 $ret['uploaderHoursBehind'] = $app->config->uploaderHoursBehind;
270 }
271
272 return $ret;
273 };
274 }
275
276 public function runServer()
277 {
278 if (!$this->env->get('f') && $this->env->get('g') === null) {
279 // no spec given
280 $msg = '<p>No "f" or "g" parameters were detected.</p>';
281 $url = 'https://github.com/mrclay/minify/blob/master/docs/CommonProblems.wiki.md#long-url-parameters-are-ignored';
282 $defaults = $this->minify->getDefaultOptions();
283 $this->minify->errorExit($defaults['badRequestHeader'], $url, $msg);
284 }
285
286 $this->minify->serve($this->controller, $this->serveOptions);
287 }
288
289 /**
290 * @param mixed $var
291 * @return string
292 */
293 private function typeOf($var)
294 {
295 $type = gettype($var);
296
297 return $type === 'object' ? get_class($var) : $type;
298 }
299 }
300