1
0
mirror of https://github.com/mrclay/minify.git synced 2025-08-31 09:31:48 +02:00

Large restructuring

Moves all dependency building into App
bootstrap.php returns an App instance
The app loads config files as necessary
Moves logging to Monolog
Moves HTTP digest auth to packagist component
Rely on sys_get_temp_dir
Env hosts $_POST and allows defaults when reading
HTML helper uses the App and can handle less files
Source factory assumes strings are filenames
Fixes JsClosureCompilerTest::test6 (API now handles ES5 by default)
Exclude JsClosureCompilerTest due to API limitations
config.php can now return a Minify\Config object
Variables set in config.php are now moved to a `Minify\Config` object,
allowing better static analysis.
The `zlib.output_compression` set is moved into `Minify::serve`.
This commit is contained in:
Steve Clay
2016-02-27 01:13:28 -05:00
parent 0b466c0892
commit 4710509c68
30 changed files with 629 additions and 637 deletions

View File

@@ -1,26 +1,29 @@
<?php
die('Disabled: use this only for testing');
require __DIR__ . '/../../bootstrap.php';
$app = (require __DIR__ . '/../../bootstrap.php');
/* @var \Minify\App $app */
function getPost($key) {
return get_magic_quotes_gpc()
? stripslashes($_POST[$key])
: $_POST[$key];
// use FirePHP if not already setup
if (!$app->config->errorLogger) {
$app->config->errorLogger = true;
}
$app->cache = new Minify_Cache_Null();
$env = $app->env;
function h($txt) {
return htmlspecialchars($txt, ENT_QUOTES, 'UTF-8');
}
if (isset($_POST['textIn'])) {
require '../config.php';
$textIn = str_replace("\r\n", "\n", getPost('textIn'));
if ($env->post('textIn')) {
$textIn = str_replace("\r\n", "\n", $env->post('textIn'));
}
if (isset($_POST['method']) && $_POST['method'] === 'Minify and serve') {
if ($env->post('method') === 'Minify and serve') {
$base = trim(getPost('base'));
$base = trim($env->post('base'));
if ($base) {
$textIn = preg_replace(
'@(<head\\b[^>]*>)@i'
@@ -38,16 +41,12 @@ if (isset($_POST['method']) && $_POST['method'] === 'Minify and serve') {
$sourceSpec['minifyOptions']['cssMinifier'] = array('Minify_CSSmin', 'minify');
}
$source = new Minify_Source($sourceSpec);
Minify_Logger::setLogger(FirePHP::getInstance(true));
$env = new Minify_Env();
$controller = new Minify_Controller_Files($env, new Minify_Source_Factory($env));
$minify = new Minify(new Minify_Cache_Null());
$controller = new Minify_Controller_Files($env, $app->sourceFactory, $app->logger);
try {
$minify->serve($controller, array(
'files' => $source
,'contentType' => Minify::TYPE_HTML
$app->minify->serve($controller, array(
'files' => $source,
'contentType' => Minify::TYPE_HTML,
));
} catch (Exception $e) {
echo h($e->getMessage());
@@ -58,16 +57,16 @@ if (isset($_POST['method']) && $_POST['method'] === 'Minify and serve') {
$tpl = array();
$tpl['classes'] = array('Minify_HTML', 'JSMin\\JSMin', 'Minify_CSS');
if (isset($_POST['method']) && in_array($_POST['method'], $tpl['classes'])) {
if (in_array($env->post('method'), $tpl['classes'])) {
$args = array($textIn);
if ($_POST['method'] === 'Minify_HTML') {
if ($env->post('method') === 'Minify_HTML') {
$args[] = array(
'cssMinifier' => array('Minify_CSSmin', 'minify')
,'jsMinifier' => array('JSMin\\JSMin', 'minify')
);
}
$func = array($_POST['method'], 'minify');
$func = array($env->post('method'), 'minify');
$tpl['inBytes'] = strlen($textIn);
$startTime = microtime(true);
try {

View File

@@ -5,7 +5,12 @@ die('Disabled: use this only for testing');
* Fetch and minify a URL (auto-detect HTML/JS/CSS)
*/
require __DIR__ . '/../../bootstrap.php';
$app = (require __DIR__ . '/../../bootstrap.php');
/* @var \Minify\App $app */
$app->cache = new Minify_Cache_Null();
$env = $app->env;
function getPost($key) {
if (! isset($_POST[$key])) {
@@ -47,9 +52,9 @@ if (isset($_POST['url'])) {
require '../config.php';
$url = trim(getPost('url'));
$ua = trim(getPost('ua'));
$cook = trim(getPost('cook'));
$url = trim($env->post('url'));
$ua = trim($env->post('ua'));
$cook = trim($env->post('cook'));
if (! preg_match('@^https?://@', $url)) {
die('HTTP(s) only.');
@@ -98,10 +103,10 @@ if (isset($_POST['url'])) {
$sourceSpec['contentType'] = $type['minify'];
if ($type['minify'] === 'text/html') {
if (isset($_POST['minJs'])) {
if ($env->post('minJs')) {
$sourceSpec['minifyOptions']['jsMinifier'] = array('JSMin\\JSMin', 'minify');
}
if (isset($_POST['minCss'])) {
if ($env->post('minCss')) {
$sourceSpec['minifyOptions']['cssMinifier'] = array('Minify_CSSmin', 'minify');
}
}
@@ -109,7 +114,7 @@ if (isset($_POST['url'])) {
$source = new Minify_Source($sourceSpec);
$sendType = 'text/plain';
if ($type['minify'] === 'text/html' && ! isset($_POST['asText'])) {
if ($type['minify'] === 'text/html' && $env->post('asText') === null) {
$sendType = $type['sent'];
}
if ($type['charset']) {
@@ -119,10 +124,8 @@ if (isset($_POST['url'])) {
// using combine instead of serve because it allows us to specify a
// Content-Type like application/xhtml+xml IF we need to
$minify = new Minify(new Minify_Cache_Null());
try {
echo $minify->combine(array($source));
echo $app->minify->combine(array($source));
} catch (Exception $e) {
header('Content-Type: text/html;charset=utf-8');
echo htmlspecialchars($e->getMessage(), ENT_QUOTES, 'UTF-8');
@@ -132,9 +135,7 @@ if (isset($_POST['url'])) {
header('Content-Type: text/html; charset=utf-8');
$ua = get_magic_quotes_gpc()
? stripslashes($_SERVER['HTTP_USER_AGENT'])
: $_SERVER['HTTP_USER_AGENT'];
$ua = $env->server('HTTP_USER_AGENT');
?>
<!DOCTYPE html><head><title>Minify URL</title></head>
@@ -146,7 +147,7 @@ $ua = get_magic_quotes_gpc()
The fetched resource Content-Type will determine the minifier used.</p>
<form action="?2" method="post">
<p><label>URL: <input type="text" name="url" size="60"></label></p>
<p><label>URL: <input type="text" name="url" value="https://code.jquery.com/jquery-2.2.1.js" size="60"></label></p>
<p><input type="submit" value="Fetch and minify"></p>
<fieldset><legend>HTML options</legend>

View File

@@ -1,51 +1,39 @@
<?php
die('Disabled: use this only for testing');
require __DIR__ . '/../../bootstrap.php';
$app = (require __DIR__ . '/../../bootstrap.php');
/* @var \Minify\App $app */
$env = $app->env;
header('Content-Type: text/html;charset=utf-8');
function h($str) { return htmlspecialchars($str, ENT_QUOTES); }
function getPost($name, $default = '') { return isset($_POST[$name]) ? $_POST[$name] : $default; }
function getInput($name, $default = '', $size = 50) {
$val = h(isset($_POST[$name]) ? $_POST[$name] : $default);
return "<input type='text' name='{$name}' value='{$val}' size='{$size}' />";
}
// validate user POST (no arrays and fix slashes)
if (! empty($_POST)) {
foreach ($_POST as $name => $val) {
if (! is_string($val)) {
unset($_POST[$name]);
continue;
}
if (get_magic_quotes_gpc()) {
$_POST[$name] = stripslashes($val);
}
}
global $env;
$val = $env->post($name, $default);
return "<input type='text' name='{$name}' value='" . h($val) . "' size='{$size}' />";
}
$defaultCurrentDir = __DIR__;
$defaultDocRoot = realpath($_SERVER['DOCUMENT_ROOT']);
$defaultDocRoot = realpath($env->getDocRoot());
$defaultSymLink = '//symlinkPath';
$defaultSymTarget = ($defaultCurrentDir[0] === '/') ? '/tmp' : 'C:\\WINDOWS\\Temp';
$defaultCss = "url(hello.gif)\nurl(../hello.gif)\nurl(../../hello.gif)\nurl(up/hello.gif)";
$out = '';
if (isset($_POST['css'])) {
require '../config.php';
if ($env->post('css')) {
$symlinks = array();
if ('' !== ($target = getPost('symTarget'))) {
$symlinks[getPost('symLink')] = $target;
if ('' !== ($target = $env->post('symTarget'))) {
$symlinks[$env->post('symLink')] = $target;
}
$css = Minify_CSS_UriRewriter::rewrite(
getPost('css')
, getPost('currentDir')
, getPost('docRoot')
, $symlinks
$env->post('css'),
$env->post('currentDir'),
$env->post('docRoot'),
$symlinks
);
$out = "<hr /><pre><code>" . h($css) . '</code></pre>';
}
@@ -57,7 +45,7 @@ if (isset($_POST['css'])) {
<div><label>document root: <?php echo getInput('docRoot', $defaultDocRoot); ?></label></div>
<div><label>symlink: <?php echo getInput('symLink', $defaultSymLink); ?> => <?php echo getInput('symTarget', $defaultSymTarget); ?></label></div>
<div><label>current directory: <?php echo getInput('currentDir', $defaultCurrentDir); ?></label></div>
<p><label>input CSS: <textarea name="css" cols="80" rows="5"><?php echo h(getPost('css', $defaultCss)); ?></textarea></label></p>
<p><label>input CSS: <textarea name="css" cols="80" rows="5"><?php echo h($env->post('css', $defaultCss)); ?></textarea></label></p>
<p><input type="submit" value="rewrite()" /></p>
</form>
<?php echo $out; ?>