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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
|
<?php
namespace Minify;
use Minify_Cache_File;
use Minify_CacheInterface;
use Minify_Controller_MinApp;
use Minify_ControllerInterface;
use Minify_DebugDetector;
use Minify_Env;
use Minify_Source_Factory;
use Props\Container;
use Psr\Log\LoggerInterface;
use RuntimeException;
use Monolog;
use Minify;
/**
* @property Minify_CacheInterface $cache
* @property Config $config
* @property string $configPath
* @property Minify_ControllerInterface $controller
* @property string $dir
* @property string $docRoot
* @property Minify_Env $env
* @property Monolog\Handler\ErrorLogHandler $errorLogHandler
* @property array $groupsConfig
* @property string $groupsConfigPath
* @property LoggerInterface $logger
* @property \Minify $minify
* @property array $serveOptions
* @property Minify_Source_Factory $sourceFactory
* @property array $sourceFactoryOptions
*/
class App extends Container
{
/**
* Constructor
*
* @param string $dir Directory containing config files
*/
public function __construct($dir)
{
$that = $this;
$this->dir = rtrim($dir, '/\\');
$this->cache = function (App $app) use ($that) {
$config = $app->config;
if ($config->cachePath instanceof Minify_CacheInterface) {
return $config->cachePath;
}
if (!$config->cachePath || is_string($config->cachePath)) {
return new Minify_Cache_File($config->cachePath, $config->cacheFileLocking, $app->logger);
}
$type = $that->typeOf($config->cachePath);
throw new RuntimeException('$min_cachePath must be a path or implement Minify_CacheInterface.'
. " Given $type");
};
$this->config = function (App $app) {
$config = (require $app->configPath);
if ($config instanceof Minify\Config) {
return $config;
}
// copy from vars into properties
$config = new Minify\Config();
$propNames = array_keys(get_object_vars($config));
$prefixer = function ($name) {
return "min_$name";
};
$varNames = array_map($prefixer, $propNames);
$varDefined = get_defined_vars();
$varNames = array_filter($varNames, function ($name) use ($varDefined) {
return array_key_exists($name, $varDefined);
});
$vars = compact($varNames);
foreach ($varNames as $varName) {
if (isset($vars[$varName])) {
$config->{substr($varName, 4)} = $vars[$varName];
}
}
if ($config->documentRoot) {
// copy into env
if (empty($config->envArgs['server'])) {
$config->envArgs['server'] = $_SERVER;
}
$config->envArgs['server']['DOCUMENT_ROOT'] = $config->documentRoot;
}
return $config;
};
$this->configPath = "{$this->dir}/config.php";
$this->controller = function (App $app) use ($that) {
$config = $app->config;
if (empty($config->factories['controller'])) {
$ctrl = new Minify_Controller_MinApp($app->env, $app->sourceFactory, $app->logger);
} else {
$ctrl = call_user_func($config->factories['controller'], $app);
}
if ($ctrl instanceof Minify_ControllerInterface) {
return $ctrl;
}
$type = $that->typeOf($ctrl);
throw new RuntimeException('$min_factories["controller"] callable must return an implementation'
." of Minify_CacheInterface. Returned $type");
};
$this->docRoot = function (App $app) {
$config = $app->config;
if (empty($config->documentRoot)) {
return $app->env->getDocRoot();
}
return $app->env->normalizePath($config->documentRoot);
};
$this->env = function (App $app) {
return new Minify_Env($app->config->envArgs);
};
$this->errorLogHandler = function (App $app) {
$format = "%channel%.%level_name%: %message% %context% %extra%";
$handler = new Monolog\Handler\ErrorLogHandler();
$handler->setFormatter(new Monolog\Formatter\LineFormatter($format));
return $handler;
};
$this->groupsConfig = function (App $app) {
return (require $app->groupsConfigPath);
};
$this->groupsConfigPath = "{$this->dir}/groupsConfig.php";
$this->logger = function (App $app) use ($that) {
$value = $app->config->errorLogger;
if ($value instanceof LoggerInterface) {
return $value;
}
$logger = new Monolog\Logger('minify');
if (!$value) {
return $logger;
}
if ($value === true || $value instanceof \FirePHP) {
$logger->pushHandler($app->errorLogHandler);
$logger->pushHandler(new Monolog\Handler\FirePHPHandler());
return $logger;
}
if ($value instanceof Monolog\Handler\HandlerInterface) {
$logger->pushHandler($value);
return $logger;
}
// BC
if (is_object($value) && is_callable(array($value, 'log'))) {
$handler = new Minify\Logger\LegacyHandler($value);
$logger->pushHandler($handler);
return $logger;
}
$type = $that->typeOf($value);
throw new RuntimeException('If set, $min_errorLogger must be a PSR-3 logger or a Monolog handler.'
." Given $type");
};
$this->minify = function (App $app) use ($that) {
$config = $app->config;
if (empty($config->factories['minify'])) {
return new \Minify($app->cache, $app->logger);
}
$minify = call_user_func($config->factories['minify'], $app);
if ($minify instanceof \Minify) {
return $minify;
}
$type = $that->typeOf($minify);
throw new RuntimeException('$min_factories["minify"] callable must return a Minify object.'
." Returned $type");
};
$this->serveOptions = function (App $app) {
$config = $app->config;
$env = $app->env;
$ret = $config->serveOptions;
$ret['minifierOptions']['text/css']['docRoot'] = $app->docRoot;
$ret['minifierOptions']['text/css']['symlinks'] = $config->symlinks;
$ret['minApp']['symlinks'] = $config->symlinks;
// auto-add targets to allowDirs
foreach ($config->symlinks as $uri => $target) {
$ret['minApp']['allowDirs'][] = $target;
}
if ($config->allowDebugFlag) {
$ret['debug'] = Minify_DebugDetector::shouldDebugRequest($env);
}
if ($config->concatOnly) {
$ret['concatOnly'] = true;
}
// check for URI versioning
if ($env->get('v') !== null || preg_match('/&\\d/', $app->env->server('QUERY_STRING'))) {
$ret['maxAge'] = 31536000;
}
// need groups config?
if ($env->get('g') !== null) {
$ret['minApp']['groups'] = $app->groupsConfig;
}
return $ret;
};
$this->sourceFactory = function (App $app) {
return new Minify_Source_Factory($app->env, $app->sourceFactoryOptions, $app->cache);
};
$this->sourceFactoryOptions = function (App $app) {
$serveOptions = $app->serveOptions;
$ret = array();
// translate legacy setting to option for source factory
if (isset($serveOptions['minApp']['noMinPattern'])) {
$ret['noMinPattern'] = $serveOptions['minApp']['noMinPattern'];
}
if (isset($serveOptions['minApp']['allowDirs'])) {
$ret['allowDirs'] = $serveOptions['minApp']['allowDirs'];
}
if (isset($serveOptions['checkAllowDirs'])) {
$ret['checkAllowDirs'] = $serveOptions['checkAllowDirs'];
}
if (is_numeric($app->config->uploaderHoursBehind)) {
$ret['uploaderHoursBehind'] = $app->config->uploaderHoursBehind;
}
return $ret;
};
}
public function runServer()
{
if (!$this->env->get('f') && $this->env->get('g') === null) {
// no spec given
$msg = '<p>No "f" or "g" parameters were detected.</p>';
$url = 'https://github.com/mrclay/minify/blob/master/docs/CommonProblems.wiki.md#long-url-parameters-are-ignored';
$defaults = $this->minify->getDefaultOptions();
$this->minify->errorExit($defaults['badRequestHeader'], $url, $msg);
}
$this->minify->serve($this->controller, $this->serveOptions);
}
/**
* @param mixed $var
* @return string
*/
private function typeOf($var)
{
$type = gettype($var);
return $type === 'object' ? get_class($var) : $type;
}
}
|