Permalink
Fetching contributors…
Cannot retrieve contributors at this time
167 lines (145 sloc) 6.29 KB
<?PHP
/**
* Copyright (c) 2009, Yahoo! Inc. All rights reserved.
* Code licensed under the BSD License:
* http://developer.yahoo.net/yui/license.html
* version: 1.0.0b2
*/
/*
This feature will allow YUI PHP Loader to combine files without relying
on a remote combo-service. The key use case here would be someone
writing their own website/application in PHP.
1. The main endpoint for combo requests in this case is combo.php. Place
this file in the same location as loader.php.
Note: If the phploader directory does not live in the webserver's root
folder then modify the PATH_TO_LIB variable accordingly
2. Download and extract each version of YUI you intend to support into
the phploader/lib directory.
A valid setup would look something like:
htdocs/phploader/lib/2.8.0r4/build
htdocs/phploader/lib/2.7.0/build
etc...
*/
require "./combo_functions.inc.php";
//Web accessible path to the YUI PHP loader lib directory (Override as needed)
define("PATH_TO_LIB", server() . "/phploader/lib/");
//APC Configuration
define("APC_AVAIL", function_exists('apc_fetch') ? true : false);
define("APC_TTL", 0);
$queryString = getenv('QUERY_STRING') ? urldecode(getenv('QUERY_STRING')) : '';
if (isset($queryString) && !empty($queryString)) {
$yuiFiles = explode("&", $queryString);
$contentType = strpos($yuiFiles[0], ".js") ? 'application/x-javascript' :
' text/css';
$cache = false;
if (APC_AVAIL === true) {
$cache = apc_fetch(server(true));
}
if ($cache) {
//Set cache headers and output cache content
header("Cache-Control: max-age=315360000");
header(
"Expires: " . date("D, j M Y H:i:s", strtotime("now + 10 years"))
." GMT"
);
header("Content-Type: " . $contentType);
echo $cache;
} else {
//Use the first module to determine which version of the YUI meta info
//to load
if (isset($yuiFiles) && !empty($yuiFiles)) {
$metaInfo = explode("/", $yuiFiles[0]);
$yuiVersion = $metaInfo[0];
}
include("./loader.php");
$loader = new YAHOO_util_Loader($yuiVersion);
$base = PATH_TO_LIB . $yuiVersion . "/build/";
$loader->base = $base;
//Detect and set a filter as needed (defaults to minified version)
if (strpos($queryString, "-debug.js") !== false) {
$loader->filter = YUI_DEBUG;
} else if (
strpos($queryString, "-min.js") === false &&
strpos($queryString, "-debug.js") === false
) {
$loader->filter = YUI_RAW;
}
//Verify this version of the library exists locally
$localPathToBuild = "../lib/" . $yuiVersion . "/build/";
if (
file_exists($localPathToBuild) === false ||
is_readable($localPathToBuild) === false
) {
throw new Exception('Unable to locate the YUI build directory!');
}
//Detect and load the required components now
$yuiComponents = array();
foreach ($yuiFiles as $yuiFile) {
$parts = explode("/", $yuiFile);
if (isset($parts[0]) && isset($parts[1]) && isset($parts[2])) {
//Add module to array for loading
$yuiComponents[] = $parts[2];
} else {
throw new Exception('Unable to determine module name!');
}
}
//Load the components
call_user_func_array(array($loader, 'load'), $yuiComponents);
//Set cache headers and output raw file content
header("Cache-Control: max-age=315360000");
header(
"Expires: " . date(
"D, j M Y H:i:s", strtotime("now + 10 years")
) ." GMT"
);
header("Content-Type: " . $contentType);
if ($contentType == "application/x-javascript") {
$rawScript = $loader->script_raw();
if (APC_AVAIL === true) {
apc_store(server(true), $rawScript, APC_TTL);
}
echo $rawScript;
} else {
$rawCss = '';
$cssResourceList = $loader->css_data();
foreach ($cssResourceList["css"] as $cssResource=>$val) {
foreach (
$cssResourceList["css"][$cssResource] as $key=>$value
) {
$crtResourceBase = substr($key, 0, strrpos($key, "/") + 1);
$crtResourceContent = $loader->getRemoteContent($key);
//Handle image path corrections (order is important)
$crtResourceContent = preg_replace(
'/((url\()([^\.\.|^http]\S+)(\)))/', '${2}'.
$crtResourceBase . '${3}${4}', $crtResourceContent
); // just filename or subdirs/filename (e.g) url(foo.png),
// url(foo/foo.png)
$crtResourceContent = str_replace(
"url(/",
"url($crtResourceBase", $crtResourceContent
); // slash filename (e.g.) url(/whatever)
$crtResourceContent = preg_replace(
'/(url\()(\.\.\/)+/',
'url(' . $base, $crtResourceContent
); // relative paths (e.g.) url(../../foo.png)
$crtResourceContent = preg_replace_callback(
'/AlphaImageLoader\(src=[\'"](.*?)[\'"]/',
'alphaImageLoaderPathCorrection',
$crtResourceContent
); // AlphaImageLoader relative paths (e.g.)
// AlphaImageLoader(src='../../foo.png')
$rawCss .= $crtResourceContent;
}
}
//Cleanup build path dups caused by relative paths that already
//included the build directory
$rawCss = str_replace("/build/build/", "/build/", $rawCss);
if (APC_AVAIL === true) {
apc_store(server(true), $rawCss, APC_TTL);
}
echo $rawCss;
}
}
} else {
throw new Exception('No YUI modules defined!');
}