mirror of
https://github.com/mrclay/minify.git
synced 2025-08-24 14:42:50 +02:00
258
lib/Minify/App.php
Normal file
258
lib/Minify/App.php
Normal file
@@ -0,0 +1,258 @@
|
||||
<?php
|
||||
|
||||
namespace Minify;
|
||||
|
||||
use Props\Container;
|
||||
use Psr\Log\LoggerInterface;
|
||||
|
||||
/**
|
||||
* @property \Minify_CacheInterface $cache
|
||||
* @property \Minify\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 \Psr\Log\LoggerInterface $logger
|
||||
* @property \Minify $minify
|
||||
* @property array $serveOptions
|
||||
* @property \Minify_Source_Factory $sourceFactory
|
||||
* @property array $sourceFactoryOptions
|
||||
*/
|
||||
class App extends Container {
|
||||
|
||||
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));
|
||||
|
||||
$varNames = array_map(function ($name) {
|
||||
return "min_$name";
|
||||
}, $propNames);
|
||||
|
||||
$vars = compact($varNames);
|
||||
|
||||
foreach ($varNames as $varName) {
|
||||
if (isset($vars[$varName])) {
|
||||
$config->{substr($varName, 4)} = $vars[$varName];
|
||||
}
|
||||
}
|
||||
|
||||
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 rtrim($config->documentRoot, '/\\');
|
||||
};
|
||||
|
||||
$this->env = function (App $app) {
|
||||
$config = $app->config;
|
||||
$envArgs = empty($config->envArgs) ? array() : $config->envArgs;
|
||||
return new \Minify_Env($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 \Psr\Log\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 (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;
|
||||
}
|
||||
}
|
@@ -4,15 +4,42 @@
|
||||
* @package Minify
|
||||
*/
|
||||
|
||||
use Psr\Log\LoggerInterface;
|
||||
|
||||
class Minify_Cache_File implements Minify_CacheInterface {
|
||||
|
||||
public function __construct($path = '', $fileLocking = false)
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
private $path;
|
||||
|
||||
/**
|
||||
* @var bool
|
||||
*/
|
||||
private $locking;
|
||||
|
||||
/**
|
||||
* @var LoggerInterface
|
||||
*/
|
||||
private $logger;
|
||||
|
||||
/**
|
||||
* @param string $path
|
||||
* @param bool $fileLocking
|
||||
* @param LoggerInterface $logger
|
||||
*/
|
||||
public function __construct($path = '', $fileLocking = false, LoggerInterface $logger = null)
|
||||
{
|
||||
if (! $path) {
|
||||
$path = self::tmp();
|
||||
$path = sys_get_temp_dir();
|
||||
}
|
||||
$this->_locking = $fileLocking;
|
||||
$this->_path = $path;
|
||||
$this->locking = $fileLocking;
|
||||
$this->path = $path;
|
||||
|
||||
if (!$logger) {
|
||||
$logger = new \Monolog\Logger('minify');
|
||||
}
|
||||
$this->logger = $logger;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -26,18 +53,17 @@ class Minify_Cache_File implements Minify_CacheInterface {
|
||||
*/
|
||||
public function store($id, $data)
|
||||
{
|
||||
$flag = $this->_locking
|
||||
$flag = $this->locking
|
||||
? LOCK_EX
|
||||
: null;
|
||||
$file = $this->_path . '/' . $id;
|
||||
$file = $this->path . '/' . $id;
|
||||
if (! @file_put_contents($file, $data, $flag)) {
|
||||
$this->_log("Minify_Cache_File: Write failed to '$file'");
|
||||
$this->logger->warning("Minify_Cache_File: Write failed to '$file'");
|
||||
}
|
||||
// write control
|
||||
if ($data !== $this->fetch($id)) {
|
||||
@unlink($file);
|
||||
$this->_log("Minify_Cache_File: Post-write read failed for '$file'");
|
||||
|
||||
$this->logger->warning("Minify_Cache_File: Post-write read failed for '$file'");
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -53,7 +79,7 @@ class Minify_Cache_File implements Minify_CacheInterface {
|
||||
*/
|
||||
public function getSize($id)
|
||||
{
|
||||
return filesize($this->_path . '/' . $id);
|
||||
return filesize($this->path . '/' . $id);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -67,7 +93,7 @@ class Minify_Cache_File implements Minify_CacheInterface {
|
||||
*/
|
||||
public function isValid($id, $srcMtime)
|
||||
{
|
||||
$file = $this->_path . '/' . $id;
|
||||
$file = $this->path . '/' . $id;
|
||||
|
||||
return (is_file($file) && (filemtime($file) >= $srcMtime));
|
||||
}
|
||||
@@ -79,14 +105,14 @@ class Minify_Cache_File implements Minify_CacheInterface {
|
||||
*/
|
||||
public function display($id)
|
||||
{
|
||||
if ($this->_locking) {
|
||||
$fp = fopen($this->_path . '/' . $id, 'rb');
|
||||
if ($this->locking) {
|
||||
$fp = fopen($this->path . '/' . $id, 'rb');
|
||||
flock($fp, LOCK_SH);
|
||||
fpassthru($fp);
|
||||
flock($fp, LOCK_UN);
|
||||
fclose($fp);
|
||||
} else {
|
||||
readfile($this->_path . '/' . $id);
|
||||
readfile($this->path . '/' . $id);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -99,8 +125,8 @@ class Minify_Cache_File implements Minify_CacheInterface {
|
||||
*/
|
||||
public function fetch($id)
|
||||
{
|
||||
if ($this->_locking) {
|
||||
$fp = fopen($this->_path . '/' . $id, 'rb');
|
||||
if ($this->locking) {
|
||||
$fp = fopen($this->path . '/' . $id, 'rb');
|
||||
if (!$fp) {
|
||||
return false;
|
||||
}
|
||||
@@ -111,7 +137,7 @@ class Minify_Cache_File implements Minify_CacheInterface {
|
||||
|
||||
return $ret;
|
||||
} else {
|
||||
return file_get_contents($this->_path . '/' . $id);
|
||||
return file_get_contents($this->path . '/' . $id);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -122,81 +148,30 @@ class Minify_Cache_File implements Minify_CacheInterface {
|
||||
*/
|
||||
public function getPath()
|
||||
{
|
||||
return $this->_path;
|
||||
return $this->path;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a usable temp directory
|
||||
*
|
||||
* Adapted from Solar/Dir.php
|
||||
* @author Paul M. Jones <pmjones@solarphp.com>
|
||||
* @license http://opensource.org/licenses/bsd-license.php BSD
|
||||
* @link http://solarphp.com/trac/core/browser/trunk/Solar/Dir.php
|
||||
*
|
||||
* @return string
|
||||
* @deprecated
|
||||
*/
|
||||
public static function tmp()
|
||||
{
|
||||
static $tmp = null;
|
||||
if (! $tmp) {
|
||||
$tmp = function_exists('sys_get_temp_dir')
|
||||
? sys_get_temp_dir()
|
||||
: self::_tmp();
|
||||
$tmp = rtrim($tmp, DIRECTORY_SEPARATOR);
|
||||
}
|
||||
|
||||
return $tmp;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the OS-specific directory for temporary files
|
||||
*
|
||||
* @author Paul M. Jones <pmjones@solarphp.com>
|
||||
* @license http://opensource.org/licenses/bsd-license.php BSD
|
||||
* @link http://solarphp.com/trac/core/browser/trunk/Solar/Dir.php
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
protected static function _tmp()
|
||||
{
|
||||
// non-Windows system?
|
||||
if (strtolower(substr(PHP_OS, 0, 3)) != 'win') {
|
||||
$tmp = empty($_ENV['TMPDIR']) ? getenv('TMPDIR') : $_ENV['TMPDIR'];
|
||||
if ($tmp) {
|
||||
return $tmp;
|
||||
} else {
|
||||
return '/tmp';
|
||||
}
|
||||
}
|
||||
// Windows 'TEMP'
|
||||
$tmp = empty($_ENV['TEMP']) ? getenv('TEMP') : $_ENV['TEMP'];
|
||||
if ($tmp) {
|
||||
return $tmp;
|
||||
}
|
||||
// Windows 'TMP'
|
||||
$tmp = empty($_ENV['TMP']) ? getenv('TMP') : $_ENV['TMP'];
|
||||
if ($tmp) {
|
||||
return $tmp;
|
||||
}
|
||||
// Windows 'windir'
|
||||
$tmp = empty($_ENV['windir']) ? getenv('windir') : $_ENV['windir'];
|
||||
if ($tmp) {
|
||||
return $tmp;
|
||||
}
|
||||
// final fallback for Windows
|
||||
return getenv('SystemRoot') . '\\temp';
|
||||
trigger_error(__METHOD__ . ' is deprecated in Minfy 3.0', E_USER_DEPRECATED);
|
||||
return sys_get_temp_dir();
|
||||
}
|
||||
|
||||
/**
|
||||
* Send message to the Minify logger
|
||||
* @param string $msg
|
||||
* @return null
|
||||
* @deprecated Use $this->logger
|
||||
*/
|
||||
protected function _log($msg)
|
||||
{
|
||||
Minify_Logger::log($msg);
|
||||
trigger_error(__METHOD__ . ' is deprecated in Minify 3.0.', E_USER_DEPRECATED);
|
||||
$this->logger->warning($msg);
|
||||
}
|
||||
|
||||
private $_path = null;
|
||||
private $_locking = null;
|
||||
}
|
||||
|
73
lib/Minify/Config.php
Normal file
73
lib/Minify/Config.php
Normal file
@@ -0,0 +1,73 @@
|
||||
<?php
|
||||
|
||||
namespace Minify;
|
||||
|
||||
use Minify_CacheInterface;
|
||||
|
||||
class Config
|
||||
{
|
||||
/**
|
||||
* @var bool
|
||||
*/
|
||||
public $enableBuilder = false;
|
||||
|
||||
/**
|
||||
* @var bool
|
||||
*/
|
||||
public $concatOnly = false;
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
public $builderPassword = 'admin';
|
||||
|
||||
/**
|
||||
* @var bool|object
|
||||
*/
|
||||
public $errorLogger = false;
|
||||
|
||||
/**
|
||||
* @var bool
|
||||
*/
|
||||
public $allowDebugFlag = false;
|
||||
|
||||
/**
|
||||
* @var string|Minify_CacheInterface
|
||||
*/
|
||||
public $cachePath = '';
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
public $documentRoot = '';
|
||||
|
||||
/**
|
||||
* @var bool
|
||||
*/
|
||||
public $cacheFileLocking = true;
|
||||
|
||||
/**
|
||||
* @var array
|
||||
*/
|
||||
public $serveOptions = array();
|
||||
|
||||
/**
|
||||
* @var array
|
||||
*/
|
||||
public $symlinks = array();
|
||||
|
||||
/**
|
||||
* @var int
|
||||
*/
|
||||
public $uploaderHoursBehind = 0;
|
||||
|
||||
/**
|
||||
* @var array
|
||||
*/
|
||||
public $envArgs = array();
|
||||
|
||||
/**
|
||||
* @var callable[]
|
||||
*/
|
||||
public $factories = array();
|
||||
}
|
@@ -4,6 +4,9 @@
|
||||
* @package Minify
|
||||
*/
|
||||
|
||||
use Psr\Log\LoggerInterface;
|
||||
use Monolog\Logger;
|
||||
|
||||
/**
|
||||
* Base class for Minify controller
|
||||
*
|
||||
@@ -24,14 +27,24 @@ abstract class Minify_Controller_Base implements Minify_ControllerInterface {
|
||||
*/
|
||||
protected $sourceFactory;
|
||||
|
||||
/**
|
||||
* @var LoggerInterface
|
||||
*/
|
||||
protected $logger;
|
||||
|
||||
/**
|
||||
* @param Minify_Env $env
|
||||
* @param Minify_Source_Factory $sourceFactory
|
||||
* @param LoggerInterface $logger
|
||||
*/
|
||||
public function __construct(Minify_Env $env, Minify_Source_Factory $sourceFactory)
|
||||
public function __construct(Minify_Env $env, Minify_Source_Factory $sourceFactory, LoggerInterface $logger = null)
|
||||
{
|
||||
$this->env = $env;
|
||||
$this->sourceFactory = $sourceFactory;
|
||||
if (!$logger) {
|
||||
$logger = new Logger('minify');
|
||||
}
|
||||
$this->logger = $logger;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -49,9 +62,11 @@ abstract class Minify_Controller_Base implements Minify_ControllerInterface {
|
||||
* @param string $msg
|
||||
*
|
||||
* @return null
|
||||
* @deprecated use $this->logger
|
||||
*/
|
||||
public function log($msg)
|
||||
{
|
||||
Minify_Logger::log($msg);
|
||||
trigger_error(__METHOD__ . ' is deprecated in Minify 3.0.', E_USER_DEPRECATED);
|
||||
$this->logger->info($msg);
|
||||
}
|
||||
}
|
||||
|
@@ -4,6 +4,9 @@
|
||||
* @package Minify
|
||||
*/
|
||||
|
||||
use Psr\Log\LoggerInterface;
|
||||
use Monolog\Logger;
|
||||
|
||||
/**
|
||||
* Controller class for minifying a set of files
|
||||
*
|
||||
@@ -28,16 +31,6 @@
|
||||
*/
|
||||
class Minify_Controller_Files extends Minify_Controller_Base {
|
||||
|
||||
/**
|
||||
* @param Minify_Env $env Environment
|
||||
* @param Minify_Source_Factory $sourceFactory Source factory. If you need to serve files from any path, this
|
||||
* component must have its "checkAllowDirs" option set to false.
|
||||
*/
|
||||
public function __construct(Minify_Env $env, Minify_Source_Factory $sourceFactory)
|
||||
{
|
||||
parent::__construct($env, $sourceFactory);
|
||||
}
|
||||
|
||||
/**
|
||||
* Set up file sources
|
||||
*
|
||||
@@ -62,16 +55,10 @@ class Minify_Controller_Files extends Minify_Controller_Base {
|
||||
|
||||
$sources = array();
|
||||
foreach ($files as $file) {
|
||||
if ($file instanceof Minify_SourceInterface) {
|
||||
$sources[] = $file;
|
||||
continue;
|
||||
}
|
||||
try {
|
||||
$sources[] = $this->sourceFactory->makeSource(array(
|
||||
'filepath' => $file,
|
||||
));
|
||||
$sources[] = $this->sourceFactory->makeSource($file);
|
||||
} catch (Minify_Source_FactoryException $e) {
|
||||
$this->log($e->getMessage());
|
||||
$this->logger->error($e->getMessage());
|
||||
|
||||
return new Minify_ServeConfiguration($options);
|
||||
}
|
||||
|
@@ -51,7 +51,7 @@ class Minify_Controller_Groups extends Minify_Controller_Files {
|
||||
);
|
||||
if (false === $pathInfo || ! isset($groups[$pathInfo])) {
|
||||
// no PATH_INFO or not a valid group
|
||||
$this->log("Missing PATH_INFO or no group set for \"$pathInfo\"");
|
||||
$this->logger->info("Missing PATH_INFO or no group set for \"$pathInfo\"");
|
||||
|
||||
return new Minify_ServeConfiguration($options);
|
||||
}
|
||||
|
@@ -58,13 +58,13 @@ class Minify_Controller_MinApp extends Minify_Controller_Base {
|
||||
$selectionId .= 'g=' . $get['g'];
|
||||
$keys = explode(',', $get['g']);
|
||||
if ($keys != array_unique($keys)) {
|
||||
$this->log("Duplicate group key found.");
|
||||
$this->logger->info("Duplicate group key found.");
|
||||
|
||||
return new Minify_ServeConfiguration($options);
|
||||
}
|
||||
foreach ($keys as $key) {
|
||||
if (! isset($localOptions['groups'][$key])) {
|
||||
$this->log("A group configuration for \"{$key}\" was not found");
|
||||
$this->logger->info("A group configuration for \"{$key}\" was not found");
|
||||
|
||||
return new Minify_ServeConfiguration($options);
|
||||
}
|
||||
@@ -76,23 +76,17 @@ class Minify_Controller_MinApp extends Minify_Controller_Base {
|
||||
$files = (array)$files;
|
||||
}
|
||||
foreach ($files as $file) {
|
||||
if ($file instanceof Minify_SourceInterface) {
|
||||
$sources[] = $file;
|
||||
continue;
|
||||
}
|
||||
try {
|
||||
$source = $this->sourceFactory->makeSource(array(
|
||||
'filepath' => $file,
|
||||
));
|
||||
$source = $this->sourceFactory->makeSource($file);
|
||||
$sources[] = $source;
|
||||
} catch (Minify_Source_FactoryException $e) {
|
||||
$this->log($e->getMessage());
|
||||
$this->logger->error($e->getMessage());
|
||||
if (null === $firstMissingResource) {
|
||||
$firstMissingResource = basename($file);
|
||||
continue;
|
||||
} else {
|
||||
$secondMissingResource = basename($file);
|
||||
$this->log("More than one file was missing: '$firstMissingResource', '$secondMissingResource'");
|
||||
$this->logger->info("More than one file was missing: '$firstMissingResource', '$secondMissingResource'");
|
||||
|
||||
return new Minify_ServeConfiguration($options);
|
||||
}
|
||||
@@ -112,14 +106,14 @@ class Minify_Controller_MinApp extends Minify_Controller_Base {
|
||||
// no "\"
|
||||
|| strpos($get['f'], '\\') !== false
|
||||
) {
|
||||
$this->log("GET param 'f' was invalid");
|
||||
$this->logger->info("GET param 'f' was invalid");
|
||||
|
||||
return new Minify_ServeConfiguration($options);
|
||||
}
|
||||
$ext = ".{$m[1]}";
|
||||
$files = explode(',', $get['f']);
|
||||
if ($files != array_unique($files)) {
|
||||
$this->log("Duplicate files were specified");
|
||||
$this->logger->info("Duplicate files were specified");
|
||||
|
||||
return new Minify_ServeConfiguration($options);
|
||||
}
|
||||
@@ -131,7 +125,7 @@ class Minify_Controller_MinApp extends Minify_Controller_Base {
|
||||
// valid base
|
||||
$base = "/{$get['b']}/";
|
||||
} else {
|
||||
$this->log("GET param 'b' was invalid");
|
||||
$this->logger->info("GET param 'b' was invalid");
|
||||
|
||||
return new Minify_ServeConfiguration($options);
|
||||
}
|
||||
@@ -153,19 +147,17 @@ class Minify_Controller_MinApp extends Minify_Controller_Base {
|
||||
}
|
||||
|
||||
try {
|
||||
$source = $this->sourceFactory->makeSource(array(
|
||||
'filepath' => $path,
|
||||
));
|
||||
$source = $this->sourceFactory->makeSource($path);
|
||||
$sources[] = $source;
|
||||
$basenames[] = basename($path, $ext);
|
||||
} catch (Minify_Source_FactoryException $e) {
|
||||
$this->log($e->getMessage());
|
||||
$this->logger->error($e->getMessage());
|
||||
if (null === $firstMissingResource) {
|
||||
$firstMissingResource = $uri;
|
||||
continue;
|
||||
} else {
|
||||
$secondMissingResource = $uri;
|
||||
$this->log("More than one file was missing: '$firstMissingResource', '$secondMissingResource`'");
|
||||
$this->logger->info("More than one file was missing: '$firstMissingResource', '$secondMissingResource`'");
|
||||
|
||||
return new Minify_ServeConfiguration($options);
|
||||
}
|
||||
@@ -178,7 +170,7 @@ class Minify_Controller_MinApp extends Minify_Controller_Base {
|
||||
}
|
||||
|
||||
if (!$sources) {
|
||||
$this->log("No sources to serve");
|
||||
$this->logger->info("No sources to serve");
|
||||
|
||||
return new Minify_ServeConfiguration($options);
|
||||
}
|
||||
|
@@ -1,5 +1,7 @@
|
||||
<?php
|
||||
|
||||
use Psr\Log\LoggerInterface;
|
||||
|
||||
interface Minify_ControllerInterface {
|
||||
|
||||
/**
|
||||
|
@@ -23,6 +23,7 @@ class Minify_Env {
|
||||
$options = array_merge(array(
|
||||
'server' => $_SERVER,
|
||||
'get' => $_GET,
|
||||
'post' => $_POST,
|
||||
'cookie' => $_COOKIE,
|
||||
), $options);
|
||||
|
||||
@@ -33,6 +34,7 @@ class Minify_Env {
|
||||
$this->server['DOCUMENT_ROOT'] = rtrim($this->server['DOCUMENT_ROOT'], '/\\');
|
||||
}
|
||||
$this->get = $options['get'];
|
||||
$this->post = $options['post'];
|
||||
$this->cookie = $options['cookie'];
|
||||
}
|
||||
|
||||
@@ -47,30 +49,36 @@ class Minify_Env {
|
||||
: null;
|
||||
}
|
||||
|
||||
public function cookie($key = null)
|
||||
public function cookie($key = null, $default = null)
|
||||
{
|
||||
if (null === $key) {
|
||||
return $this->cookie;
|
||||
}
|
||||
|
||||
return isset($this->cookie[$key])
|
||||
? $this->cookie[$key]
|
||||
: null;
|
||||
return isset($this->cookie[$key]) ? $this->cookie[$key] : $default;
|
||||
}
|
||||
|
||||
public function get($key = null)
|
||||
public function get($key = null, $default = null)
|
||||
{
|
||||
if (null === $key) {
|
||||
return $this->get;
|
||||
}
|
||||
|
||||
return isset($this->get[$key])
|
||||
? $this->get[$key]
|
||||
: null;
|
||||
return isset($this->get[$key]) ? $this->get[$key] : $default;
|
||||
}
|
||||
|
||||
public function post($key = null, $default = null)
|
||||
{
|
||||
if (null === $key) {
|
||||
return $this->post;
|
||||
}
|
||||
|
||||
return isset($this->post[$key]) ? $this->post[$key] : $default;
|
||||
}
|
||||
|
||||
protected $server = null;
|
||||
protected $get = null;
|
||||
protected $post = null;
|
||||
protected $cookie = null;
|
||||
|
||||
/**
|
||||
|
@@ -31,17 +31,19 @@ class Minify_HTML_Helper {
|
||||
public static function getUri($keyOrFiles, $opts = array())
|
||||
{
|
||||
$opts = array_merge(array( // default options
|
||||
'farExpires' => true
|
||||
,'debug' => false
|
||||
,'charset' => 'UTF-8'
|
||||
,'minAppUri' => '/min'
|
||||
,'rewriteWorks' => true
|
||||
,'groupsConfigFile' => ''
|
||||
'farExpires' => true,
|
||||
'debug' => false,
|
||||
'charset' => 'UTF-8',
|
||||
'minAppUri' => '/min',
|
||||
'rewriteWorks' => true,
|
||||
'groupsConfigFile' => self::app()->groupsConfigPath,
|
||||
), $opts);
|
||||
|
||||
$h = new self;
|
||||
$h->minAppUri = $opts['minAppUri'];
|
||||
$h->rewriteWorks = $opts['rewriteWorks'];
|
||||
$h->groupsConfigFile = $opts['groupsConfigFile'];
|
||||
|
||||
if (is_array($keyOrFiles)) {
|
||||
$h->setFiles($keyOrFiles, $opts['farExpires']);
|
||||
} else {
|
||||
@@ -98,7 +100,7 @@ class Minify_HTML_Helper {
|
||||
$file = substr($file, 2);
|
||||
} elseif (0 === strpos($file, '/')
|
||||
|| 1 === strpos($file, ':\\')) {
|
||||
$file = substr($file, strlen($_SERVER['DOCUMENT_ROOT']) + 1);
|
||||
$file = substr($file, strlen(self::app()->env->getDocRoot()) + 1);
|
||||
}
|
||||
$file = strtr($file, '\\', '/');
|
||||
$files[$k] = $file;
|
||||
@@ -117,7 +119,7 @@ class Minify_HTML_Helper {
|
||||
$this->_groupKey = $key;
|
||||
if ($checkLastModified) {
|
||||
if (! $this->groupsConfigFile) {
|
||||
$this->groupsConfigFile = dirname(dirname(dirname(__DIR__))) . '/groupsConfig.php';
|
||||
$this->groupsConfigFile = self::app()->groupsConfigPath;
|
||||
}
|
||||
if (is_file($this->groupsConfigFile)) {
|
||||
$gc = (require $this->groupsConfigFile);
|
||||
@@ -145,23 +147,35 @@ class Minify_HTML_Helper {
|
||||
public static function getLastModified($sources, $lastModified = 0)
|
||||
{
|
||||
$max = $lastModified;
|
||||
$factory = self::app()->sourceFactory;
|
||||
|
||||
/** @var Minify_Source $source */
|
||||
foreach ((array)$sources as $source) {
|
||||
if ($source instanceof Minify_Source) {
|
||||
$max = max($max, $source->getLastModified());
|
||||
} elseif (is_string($source)) {
|
||||
if (0 === strpos($source, '//')) {
|
||||
$source = $_SERVER['DOCUMENT_ROOT'] . substr($source, 1);
|
||||
}
|
||||
if (is_file($source)) {
|
||||
$max = max($max, filemtime($source));
|
||||
}
|
||||
}
|
||||
$source = $factory->makeSource($source);
|
||||
$max = max($max, $source->getLastModified());
|
||||
}
|
||||
|
||||
return $max;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param \Minify\App $app
|
||||
* @return \Minify\App
|
||||
* @internal
|
||||
*/
|
||||
public static function app(\Minify\App $app = null)
|
||||
{
|
||||
static $cached;
|
||||
if ($app) {
|
||||
$cached = $app;
|
||||
return $app;
|
||||
}
|
||||
if ($cached === null) {
|
||||
$cached = (require __DIR__ . '/../../../bootstrap.php');
|
||||
}
|
||||
return $cached;
|
||||
}
|
||||
|
||||
protected $_groupKey = null; // if present, URI will be like g=...
|
||||
protected $_filePaths = array();
|
||||
protected $_lastModified = null;
|
||||
|
@@ -1,45 +0,0 @@
|
||||
<?php
|
||||
/**
|
||||
* Class Minify_Loader
|
||||
* @package Minify
|
||||
*/
|
||||
|
||||
/**
|
||||
* Class autoloader
|
||||
*
|
||||
* @package Minify
|
||||
* @author Stephen Clay <steve@mrclay.org>
|
||||
* @deprecated Use Composer (/vendor/autoload.php)
|
||||
*/
|
||||
class Minify_Loader {
|
||||
public function loadClass($class)
|
||||
{
|
||||
$file = dirname(__DIR__) . DIRECTORY_SEPARATOR;
|
||||
$file .= strtr($class, "\\_", DIRECTORY_SEPARATOR . DIRECTORY_SEPARATOR) . '.php';
|
||||
if (is_readable($file)) {
|
||||
require $file;
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
$map = array(
|
||||
'JavascriptPacker' => 'class.JavaScriptPacker.php',
|
||||
);
|
||||
|
||||
if (!isset($map[$class])) {
|
||||
return;
|
||||
}
|
||||
|
||||
@include $map[$class];
|
||||
}
|
||||
|
||||
public static function register()
|
||||
{
|
||||
$inst = new self();
|
||||
spl_autoload_register(array($inst, 'loadClass'));
|
||||
|
||||
return $inst;
|
||||
}
|
||||
}
|
||||
|
||||
return Minify_Loader::register();
|
@@ -1,47 +0,0 @@
|
||||
<?php
|
||||
/**
|
||||
* Class Minify_Logger
|
||||
* @package Minify
|
||||
*/
|
||||
|
||||
/**
|
||||
* Message logging class
|
||||
*
|
||||
* @package Minify
|
||||
* @author Stephen Clay <steve@mrclay.org>
|
||||
*
|
||||
* @todo lose this singleton! pass log object in Minify::serve and distribute to others
|
||||
*/
|
||||
class Minify_Logger {
|
||||
|
||||
/**
|
||||
* Set logger object.
|
||||
*
|
||||
* The object should have a method "log" that accepts a value as 1st argument and
|
||||
* an optional string label as the 2nd.
|
||||
*
|
||||
* @param mixed $obj or a "falsey" value to disable
|
||||
* @return null
|
||||
*/
|
||||
public static function setLogger($obj = null) {
|
||||
self::$_logger = $obj
|
||||
? $obj
|
||||
: null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Pass a message to the logger (if set)
|
||||
*
|
||||
* @param string $msg message to log
|
||||
* @return null
|
||||
*/
|
||||
public static function log($msg, $label = 'Minify') {
|
||||
if (! self::$_logger) return;
|
||||
self::$_logger->log($msg, $label);
|
||||
}
|
||||
|
||||
/**
|
||||
* @var mixed logger object (like FirePHP) or null (i.e. no logger available)
|
||||
*/
|
||||
private static $_logger = null;
|
||||
}
|
25
lib/Minify/Logger/LegacyHandler.php
Normal file
25
lib/Minify/Logger/LegacyHandler.php
Normal file
@@ -0,0 +1,25 @@
|
||||
<?php
|
||||
|
||||
namespace Minify\Logger;
|
||||
|
||||
use Monolog\Logger;
|
||||
use Monolog\Handler\AbstractProcessingHandler;
|
||||
|
||||
class LegacyHandler extends AbstractProcessingHandler
|
||||
{
|
||||
private $obj;
|
||||
|
||||
public function __construct($obj)
|
||||
{
|
||||
if (!is_callable(array($obj, 'log'))) {
|
||||
throw new \InvalidArgumentException('$obj must have a public log() method');
|
||||
}
|
||||
$this->obj = $obj;
|
||||
parent::__construct();
|
||||
}
|
||||
|
||||
protected function write(array $record)
|
||||
{
|
||||
$this->obj->log((string)$record['formatted']);
|
||||
}
|
||||
}
|
@@ -133,12 +133,16 @@ class Minify_Source_Factory {
|
||||
*/
|
||||
public function makeSource($spec)
|
||||
{
|
||||
$source = null;
|
||||
|
||||
if ($spec instanceof Minify_SourceInterface) {
|
||||
$source = $spec;
|
||||
if (is_string($spec)) {
|
||||
$spec = array(
|
||||
'filepath' => $spec,
|
||||
);
|
||||
} elseif ($spec instanceof Minify_SourceInterface) {
|
||||
return $spec;
|
||||
}
|
||||
|
||||
$source = null;
|
||||
|
||||
if (empty($spec['filepath'])) {
|
||||
// not much we can check
|
||||
return new Minify_Source($spec);
|
||||
|
Reference in New Issue
Block a user