Skip to content

Commit

Permalink
Replaced all class require's with autoloader
Browse files Browse the repository at this point in the history
  • Loading branch information
mrclay committed Sep 30, 2012
1 parent db00fa6 commit c196dd3
Show file tree
Hide file tree
Showing 39 changed files with 46 additions and 168 deletions.
20 changes: 3 additions & 17 deletions min/builder/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@

require dirname(__FILE__) . '/../config.php';

require "$min_libPath/Minify/Loader.php";
Minify_Loader::register();

if (! $min_enableBuilder) {
header('Content-Type: text/plain');
die('This application is not enabled. See http://code.google.com/p/minify/wiki/BuilderApp');
Expand All @@ -32,26 +35,9 @@
if (isset($min_builderPassword)
&& is_string($min_builderPassword)
&& $min_builderPassword !== '') {
require dirname(dirname(__FILE__)) . '/lib/DooDigestAuth.php';
DooDigestAuth::http_auth('Minify Builder', array('admin' => $min_builderPassword));
}

$setIncludeSuccess = set_include_path(dirname(__FILE__) . '/../lib' . PATH_SEPARATOR . get_include_path());
// we do it this way because we want the builder to work after the user corrects
// include_path. (set_include_path returning FALSE is OK).
try {
require_once 'Minify/Cache/File.php';
} catch (Exception $e) {
if (! $setIncludeSuccess) {
echo "Minify: set_include_path() failed. You may need to set your include_path "
."outside of PHP code, e.g., in php.ini.";
} else {
echo $e->getMessage();
}
exit();
}
require 'Minify.php';

$cachePathCode = '';
if (! isset($min_cachePath) && ! function_exists('sys_get_temp_dir')) {
$detectedTmp = Minify_Cache_File::tmp();
Expand Down
10 changes: 2 additions & 8 deletions min/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,8 @@
include MINIFY_MIN_DIR . '/config-test.php';
}

// setup include path
set_include_path($min_libPath . PATH_SEPARATOR . get_include_path());

require 'Minify.php';
require "$min_libPath/Minify/Loader.php";
Minify_Loader::register();

Minify::$uploaderHoursBehind = $min_uploaderHoursBehind;
Minify::setCache(
Expand All @@ -39,14 +37,11 @@
}

if ($min_allowDebugFlag) {
require_once 'Minify/DebugDetector.php';
$min_serveOptions['debug'] = Minify_DebugDetector::shouldDebugRequest($_COOKIE, $_GET, $_SERVER['REQUEST_URI']);
}

if ($min_errorLogger) {
require_once 'Minify/Logger.php';
if (true === $min_errorLogger) {
require_once 'FirePHP.php';
$min_errorLogger = FirePHP::getInstance(true);
}
Minify_Logger::setLogger($min_errorLogger);
Expand All @@ -64,7 +59,6 @@
// serve!

if (! isset($min_serveController)) {
require 'Minify/Controller/MinApp.php';
$min_serveController = new Minify_Controller_MinApp();
}
Minify::serve($min_serveController, $min_serveOptions);
Expand Down
15 changes: 1 addition & 14 deletions min/lib/Minify.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,6 @@
* Class Minify
* @package Minify
*/

/**
* Minify_Source
*/
require_once 'Minify/Source.php';

/**
* Minify - Combines, minifies, and caches JavaScript and CSS files on demand.
Expand Down Expand Up @@ -85,7 +80,6 @@ class Minify {
public static function setCache($cache = '', $fileLocking = true)
{
if (is_string($cache)) {
require_once 'Minify/Cache/File.php';
self::$_cache = new Minify_Cache_File($cache, $fileLocking);
} else {
self::$_cache = $cache;
Expand Down Expand Up @@ -176,10 +170,6 @@ public static function serve($controller, $options = array())
if (is_string($controller)) {
// make $controller into object
$class = 'Minify_Controller_' . $controller;
if (! class_exists($class, false)) {
require_once "Minify/Controller/"
. str_replace('_', '/', $controller) . ".php";
}
$controller = new $class();
/* @var Minify_Controller_Base $controller */
}
Expand Down Expand Up @@ -221,8 +211,7 @@ public static function serve($controller, $options = array())
$contentEncoding = self::$_options['encodeMethod'];
} else {
// sniff request header
require_once 'HTTP/Encoder.php';
// depending on what the client accepts, $contentEncoding may be
// depending on what the client accepts, $contentEncoding may be
// 'x-gzip' while our internal encodeMethod is 'gzip'. Calling
// getAcceptedEncoding(false, false) leaves out compress and deflate as options.
list(self::$_options['encodeMethod'], $contentEncoding) = HTTP_Encoder::getAcceptedEncoding(false, false);
Expand All @@ -233,7 +222,6 @@ public static function serve($controller, $options = array())
}

// check client cache
require_once 'HTTP/ConditionalGet.php';
$cgOptions = array(
'lastModifiedTime' => self::$_options['lastModifiedTime']
,'isPublic' => self::$_options['isPublic']
Expand Down Expand Up @@ -528,7 +516,6 @@ protected static function _combineMinify()
$imploded = implode($implodeSeparator, $groupToProcessTogether);
$groupToProcessTogether = array();
if ($lastMinifier) {
self::$_controller->loadMinifier($lastMinifier);
try {
$content[] = call_user_func($lastMinifier, $imploded, $lastOptions);
} catch (Exception $e) {
Expand Down
2 changes: 0 additions & 2 deletions min/lib/Minify/Build.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@
* @package Minify
*/

require_once 'Minify/Source.php';

/**
* Maintain a single last modification time for a group of Minify sources to
* allow use of far off Expires headers in Minify.
Expand Down
3 changes: 0 additions & 3 deletions min/lib/Minify/CSS.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,11 +67,9 @@ public static function minify($css, $options = array())
if ($options['removeCharsets']) {
$css = preg_replace('/@charset[^;]+;\\s*/', '', $css);
}
require_once 'Minify/CSS/Compressor.php';
if (! $options['preserveComments']) {
$css = Minify_CSS_Compressor::process($css, $options);
} else {
require_once 'Minify/CommentPreserver.php';
$css = Minify_CommentPreserver::process(
$css
,array('Minify_CSS_Compressor', 'process')
Expand All @@ -81,7 +79,6 @@ public static function minify($css, $options = array())
if (! $options['currentDir'] && ! $options['prependRelativePath']) {
return $css;
}
require_once 'Minify/CSS/UriRewriter.php';
if ($options['currentDir']) {
return Minify_CSS_UriRewriter::rewrite(
$css
Expand Down
1 change: 0 additions & 1 deletion min/lib/Minify/Cache/File.php
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,6 @@ protected static function _tmp()
*/
protected function _log($msg)
{
require_once 'Minify/Logger.php';
Minify_Logger::log($msg);
}

Expand Down
28 changes: 0 additions & 28 deletions min/lib/Minify/Controller/Base.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,33 +78,6 @@ public function getDefaultMinifers() {
return $ret;
}

/**
* Load any code necessary to execute the given minifier callback.
*
* The controller is responsible for loading minification code on demand
* via this method. This built-in function will only load classes for
* static method callbacks where the class isn't already defined. It uses
* the PEAR convention, so, given array('Jimmy_Minifier', 'minCss'), this
* function will include 'Jimmy/Minifier.php'.
*
* If you need code loaded on demand and this doesn't suit you, you'll need
* to override this function in your subclass.
* @see Minify_Controller_Page::loadMinifier()
*
* @param callback $minifierCallback callback of minifier function
*
* @return null
*/
public function loadMinifier($minifierCallback)
{
if (is_array($minifierCallback)
&& is_string($minifierCallback[0])
&& !class_exists($minifierCallback[0], false)) {

require str_replace('_', '/', $minifierCallback[0]) . '.php';
}
}

/**
* Is a user-given file within an allowable directory, existing,
* and having an extension js/css/html/txt ?
Expand Down Expand Up @@ -244,7 +217,6 @@ public final function analyzeSources($options = array())
* @return null
*/
public function log($msg) {
require_once 'Minify/Logger.php';
Minify_Logger::log($msg);
}
}
2 changes: 0 additions & 2 deletions min/lib/Minify/Controller/Files.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@
* @package Minify
*/

require_once 'Minify/Controller/Base.php';

/**
* Controller class for minifying a set of files
*
Expand Down
2 changes: 0 additions & 2 deletions min/lib/Minify/Controller/Groups.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@
* @package Minify
*/

require_once 'Minify/Controller/Base.php';

/**
* Controller class for serving predetermined groups of minimized sets, selected
* by PATH_INFO
Expand Down
2 changes: 0 additions & 2 deletions min/lib/Minify/Controller/MinApp.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@
* @package Minify
*/

require_once 'Minify/Controller/Base.php';

/**
* Controller class for requests to /min/index.php
*
Expand Down
19 changes: 0 additions & 19 deletions min/lib/Minify/Controller/Page.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@
* @package Minify
*/

require_once 'Minify/Controller/Base.php';

/**
* Controller class for serving a single HTML page
*
Expand Down Expand Up @@ -59,29 +57,12 @@ public function setupSources($options) {
'cssMinifier' => array('Minify_CSS', 'minify')
,'jsMinifier' => array('JSMin', 'minify')
);
$this->_loadCssJsMinifiers = true;
unset($options['minifyAll']);
}
$this->sources[] = new Minify_Source($sourceSpec);

$options['contentType'] = Minify::TYPE_HTML;
return $options;
}

protected $_loadCssJsMinifiers = false;

/**
* @see Minify_Controller_Base::loadMinifier()
*/
public function loadMinifier($minifierCallback)
{
if ($this->_loadCssJsMinifiers) {
// Minify will not call for these so we must manually load
// them when Minify/HTML.php is called for.
require_once 'Minify/CSS.php';
require_once 'JSMin.php';
}
parent::loadMinifier($minifierCallback); // load Minify/HTML.php
}
}

2 changes: 0 additions & 2 deletions min/lib/Minify/Controller/Version1.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@
* @package Minify
*/

require_once 'Minify/Controller/Base.php';

/**
* Controller class for emulating version 1 of minify.php (mostly a proof-of-concept)
*
Expand Down
1 change: 0 additions & 1 deletion min/lib/Minify/JS/ClosureCompiler.php
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,6 @@ protected function _buildPostBody($js, $returnErrors = false)
*/
protected function _fallback($js)
{
require_once 'JSMin.php';
return JSMin::minify($js);
}
}
Expand Down
1 change: 0 additions & 1 deletion min/lib/Minify/Lines.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,6 @@ public static function minify($content, $options = array())

// check for desired URI rewriting
if (isset($options['currentDir'])) {
require_once 'Minify/CSS/UriRewriter.php';
Minify_CSS_UriRewriter::$debugText = '';
$content = Minify_CSS_UriRewriter::rewrite(
$content
Expand Down
28 changes: 28 additions & 0 deletions min/lib/Minify/Loader.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php
/**
* Class Minify_Loader
* @package Minify
*/

/**
* Class autoloader
*
* @package Minify
* @author Stephen Clay <[email protected]>
*/
class Minify_Loader {
public function loadClass($class)
{
$file = dirname(dirname(__FILE__)) . DIRECTORY_SEPARATOR;
$file .= strtr($class, "\\_", DIRECTORY_SEPARATOR . DIRECTORY_SEPARATOR) . '.php';
if (is_readable($file)) {
require $file;
}
}

static public function register()
{
$inst = new self();
spl_autoload_register(array($inst, 'loadClass'));
}
}
6 changes: 4 additions & 2 deletions min/utils.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,10 @@
* @package Minify
*/

require_once dirname(__FILE__) . '/lib/Minify/HTML/Helper.php';

if (! class_exists('Minify_Loader', false)) {
require dirname(__FILE__) . '/lib/Minify/Loader.php';
Minify_Loader::register();
}

/*
* Get an HTML-escaped Minify URI for a group or set of files. By default, URIs
Expand Down
10 changes: 2 additions & 8 deletions min_extras/cli/minify.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,8 @@

$pathToLib = dirname(dirname(__DIR__)) . '/min/lib';

// needed because of dumb require statements in class files :(
set_include_path($pathToLib . PATH_SEPARATOR . get_include_path());

// barebones autoloader
spl_autoload_register(function ($class) use ($pathToLib) {
$file = $pathToLib . '/' . str_replace(array('_', '\\'), DIRECTORY_SEPARATOR, $class) . '.php';
return is_file($file) ? ((require $file) || true) : false;
});
require "$min_libPath/Minify/Loader.php";
Minify_Loader::register();

$cli = new MrClay\Cli;

Expand Down
10 changes: 2 additions & 8 deletions min_extras/cli/rewrite-uris.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,8 @@

$pathToLib = dirname(dirname(__DIR__)) . '/min/lib';

// needed because of dumb require statements in class files :(
set_include_path($pathToLib . PATH_SEPARATOR . get_include_path());

// barebones autoloader
spl_autoload_register(function ($class) use ($pathToLib) {
$file = $pathToLib . '/' . str_replace(array('_', '\\'), DIRECTORY_SEPARATOR, $class) . '.php';
return is_file($file) ? ((require $file) || true) : false;
});
require "$min_libPath/Minify/Loader.php";
Minify_Loader::register();

$cli = new MrClay\Cli;

Expand Down
8 changes: 2 additions & 6 deletions min_extras/config.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,9 @@

require dirname(__FILE__) . '/../min/config.php';

set_include_path($min_libPath . PATH_SEPARATOR . get_include_path());
require "$min_libPath/Minify/Loader.php";
Minify_Loader::register();

$minifyCachePath = isset($min_cachePath)
? $min_cachePath
: '';

function min_autoload($name) {
require str_replace(array('_', '\\'), DIRECTORY_SEPARATOR, $name) . '.php';
}
spl_autoload_register('min_autoload');
Loading

0 comments on commit c196dd3

Please sign in to comment.