Skip to content

Commit

Permalink
Adding combo handling support. Also adding a local combo handling scr…
Browse files Browse the repository at this point in the history
…ipt.
  • Loading branch information
webdevUser committed Jun 5, 2009
1 parent 19a253e commit f28a677
Show file tree
Hide file tree
Showing 3 changed files with 203 additions and 26 deletions.
74 changes: 74 additions & 0 deletions phploader/combo.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
<?php
/*
* This script servers as a local replacement for the remote YUI combo handler
* Each version of YUI you intend to support must be setup in the yui_loader directory
* A valid setup will look something like yui_loader/2.7.0/build, yui_loader/2.6.0/build, etc.
*
* Additional Setup Note:
* If the yui_loader directory does not live webservers root folder then modify $pathToYUILoader accordingly
*/

include("/home/y/share/pear/Yahoo/YUI/loader.php");
$loader = new YAHOO_util_Loader();

$pathToYUILoader = server() . "/yui_loader/"; //Web accessible path to the YUI loader directory (Override as needed)
$base = $pathToYUILoader . $loader->comboDefaultVersion; //Defaults to current version

//server(): Computes the base URL of the current page (protocol, server, path)
//credit: http://code.google.com/p/simple-php-framework/ (modified version of full_url), license: MIT
function server()
{
$s = getenv('HTTPS') ? '' : (getenv('HTTPS') == 'on') ? 's' : '';
$protocol = substr(strtolower(getenv('SERVER_PROTOCOL')), 0, strpos(strtolower(getenv('SERVER_PROTOCOL')), '/')) . $s;
$port = (getenv('SERVER_PORT') == '80') ? '' : (":".getenv('SERVER_PORT'));
return $protocol . "://" . getenv('HTTP_HOST') . $port;
}

$queryString = getenv('QUERY_STRING') ? urldecode(getenv('QUERY_STRING')) : '';
if (isset($queryString) && !empty($queryString)) {
$yuiFiles = explode("&amp;", $queryString);
$contentType = strpos($yuiFiles[0], ".js") ? 'application/x-javascript' : ' text/css';

//Detect and load the required components now
$baseOverrides = array();
$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];
//Check for and setup base overrides as needed
$tmpbase = $pathToYUILoader . $parts[0] . '/' . $parts[1] . '/';
if ($tmpbase != $base) {
$baseOverrides[$tmpbase][$parts[2]] = $parts[2];
}
} else {
die('<!-- Unable to determine module name! -->');
}
}

//Do base overrides if required
if (!empty($baseOverrides)) {
foreach($baseOverrides as $baseOverride=>$modules) {
call_user_func_array(array($loader, 'overrideBase'), array($baseOverride, $modules));
}
}

//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") {
echo $loader->script_raw();
} else {
echo $loader->css_raw();
}

} else {
die('<!-- No YUI modules defined! -->');
}

?>
89 changes: 84 additions & 5 deletions phploader/loader.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@ class YAHOO_util_Loader {
// current target not used
var $target = "";

//If set to true the CSS and JS will be combo loaded from yui.yahooapis.com
var $combine = false;

var $allowRollups = true;

// If set to true to pick up optional modules in addition to required modules
Expand Down Expand Up @@ -112,6 +115,11 @@ class YAHOO_util_Loader {
var $depCache = array();
var $filters = array();

// used to do combo handling
var $comboBase = "";
var $cssComboLocation = null;
var $jsComboLocation = null;

/**
* The constructor needs to be supplied with additional metadata
*/
Expand All @@ -126,6 +134,8 @@ function YAHOO_util_Loader($cacheKey=null, $modules=null, $noYUI=false) {
$this->jsonAvail = function_exists('json_encode');
$this->embedAvail = ($this->curlAvail && $this->apcAvail);
$this->base = $yui_current[YUI_BASE];
$this->comboBase = str_replace('http://yui.yahooapis.com/', 'http://yui.yahooapis.com/combo?', $this->base);
$this->comboDefaultVersion = str_replace('http://yui.yahooapis.com/', '', $this->base); //(ex) 2.7.0/build/

$this->fullCacheKey = null;
$cache = null;
Expand All @@ -140,7 +150,7 @@ function YAHOO_util_Loader($cacheKey=null, $modules=null, $noYUI=false) {
$this->cacheFound = true;

// $this->log("using cache -------------------------------------------------------");
//$this->log(var_export($cache[YUI_DEPCACHE], true));
// $this->log(var_export($cache[YUI_DEPCACHE], true));
// $this->log("----------------------------------------------------------");
$this->modules = $cache[YUI_MODULES];
$this->skin = $cache[YUI_SKIN];
Expand Down Expand Up @@ -376,11 +386,11 @@ function json($moduleType=null, $allowRollups=false, $skipSort=false, $full=fals
}

function script_raw() {
return $this->json(YUI_JS);
return $this->raw(YUI_JS);
}

function css_raw() {
return $this->json(YUI_CSS);
return $this->raw(YUI_CSS);
}

function raw($moduleType=null, $allowRollups=false, $skipSort=false) {
Expand Down Expand Up @@ -1012,7 +1022,12 @@ function processDependencies($outputType, $moduleType, $skipSort=false, $showLoa
break;
case YUI_TAGS:
default:
$html .= $this->getLink($name, $dep[YUI_TYPE])."\n";
if ($this->combine === true) {
$this->addToCombo($name, $dep[YUI_TYPE]);
$html = $this->getComboLink($dep[YUI_TYPE]);
} else {
$html .= $this->getLink($name, $dep[YUI_TYPE])."\n";
}
}
}
}
Expand Down Expand Up @@ -1102,7 +1117,11 @@ function getRemoteContent($url) {

curl_setopt($ch, CURLOPT_URL, $url); // set url to post to
curl_setopt($ch, CURLOPT_FAILONERROR, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);// allow redirects

//Commenting out CURLOPT_FOLLOWLOCATION for now. Doesn't work in safe mode or with openbase_dir enabled.
//See http://au.php.net/manual/ro/function.curl-setopt.php#71313.
//curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);// allow redirects

curl_setopt($ch, CURLOPT_RETURNTRANSFER,1); // return into a variable
// curl_setopt($ch, CURLOPT_TIMEOUT, 3); // times out after 4s

Expand Down Expand Up @@ -1163,6 +1182,66 @@ function getLink($name, $type) {
}
}

function getComboLink($type) {
$url = '';

if ($type == YUI_CSS) {
if ($this->cssComboLocation !== null) {
$url = "<link rel=\"stylesheet\" type=\"text/css\" href=\"{$this->cssComboLocation}\" />";
} else {
$url = "<!-- NO YUI CSS COMPONENTS IDENTIFIED -->";
}
} else if ($type == YUI_JS) {
if ($this->jsComboLocation !== null) {
if ($this->cssComboLocation !== null) {
$url = "\n";
}
$url .= "<script type=\"text/javascript\" src=\"{$this->jsComboLocation}\"></script>";
} else {
$url = "<!-- NO YUI JAVASCRIPT COMPONENTS IDENTIFIED -->";
}
}

//Allow for RAW & DEBUG over minified default
if ($this->filter) {
if (count($this->filterList) > 0 && !isset($this->filterList[$name])) {
// skip the filter
} else if (isset($this->filters[$this->filter])) {
$filter = $this->filters[$this->filter];
$url = ereg_replace($filter[YUI_SEARCH], $filter[YUI_REPLACE], $url);
}
}

return $url;
}

function addToCombo($name, $type) {
//Allow version overrides
$yuiVersion = $this->comboDefaultVersion;
if ($this->version !== null) {
$yuiVersion = $this->version . '/build/';
$this->comboBase = 'http://yui.yahooapis.com/combo?' . $yuiVersion;
}

if ($type == YUI_CSS) {
//If this is the first css component then add the combo base path
if ($this->cssComboLocation === null) {
$this->cssComboLocation = $this->comboBase . $this->modules[$name][YUI_PATH];
} else {
//Prep for next component
$this->cssComboLocation .= '&' . $yuiVersion . $this->modules[$name][YUI_PATH];
}
} else {
//If this is the first js component then add the combo base path
if ($this->jsComboLocation === null) {
$this->jsComboLocation = $this->comboBase . $this->modules[$name][YUI_PATH];
} else {
//Prep for next component
$this->jsComboLocation .= '&' . $yuiVersion . $this->modules[$name][YUI_PATH];
}
}
}

function canJSON() {
return $this->jsonAvail;
}
Expand Down
66 changes: 45 additions & 21 deletions phploader/test/test-loader.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@
$output = "";
$checked = "";
$modules = array();
$canEmbed = false;
$embed = "EMBED";
$embed = false;
$loadOptional = false;
$combine = false;

if (isset($_GET["module"])) {

Expand All @@ -37,32 +37,43 @@
}
}

// content type is "EMBED" to inline the files, otherwise links
// to the files are generated. This is not implemented on the test
// page (we always generate links)
if (isset($_GET["contenttype"])) {
$contenttype = $_GET["contenttype"];
}

// content type is "EMBED" to inline the files, otherwise links to the files are generated.
if (isset($_GET["embed"])) {
$contenttype = "EMBED";
$embed = true;
}

if (isset($_GET["base"])) {
$base= $_GET["base"];
$loader->base = $base;
}

if (isset($_GET["comboBase"]) && !empty($_GET["comboBase"])) {
$comboBase= $_GET["comboBase"];
$loader->comboBase = $comboBase;
} else {
$comboBase= 'http://yui.yahooapis.com/combo?2.7.0/build/';
$loader->comboBase = $comboBase;
}

if (isset($_GET["filter"])) {
$filter= $_GET["filter"];
$loader->filter = $filter;
}

if (isset($_GET["canEmbed"])) {
$canEmbed = true;
if (isset($_GET["combine"])) {
$combine = true;
}

if (isset($_GET["loadOptional"])) {
$loadOptional = true;
}

$loader->canEmbed = $canEmbed;
$loader->combine = $combine;
$loader->loadOptional = $loadOptional;

// tell the loader about each module requested
Expand Down Expand Up @@ -118,16 +129,14 @@

</style>

<!-- The actual output after processing all of the dependencies
<?php echo $output ?>
-->
<?php
//The actual output after processing all of the dependencies
//echo $output;
?>

</head>
<body>
<form name="mainform" action="<?php echo getenv("REQUEST_URI") ?>">

<form name="mainform" action="<?php echo getenv("REQUEST_URI"); ?>">

<!-- All of the modules, check the ones that are required -->
<div class="content" id="modulelist">
Expand All @@ -153,7 +162,8 @@

$prodchecked = "";
$debugchecked = "";
$canEmbedchecked = "";
$combinechecked = "";
$embedchecked = "";
$loadOptionalchecked = "";

// if ($target == $prod) {
Expand All @@ -166,8 +176,12 @@
// $devchecked = "checked";
// }

if ($canEmbed) {
$canEmbedchecked = "checked";
if ($combine) {
$combinechecked = "checked";
}

if ($embed) {
$embedchecked = "checked";
}

if ($loadOptional) {
Expand All @@ -177,7 +191,13 @@
?>

<p>
<input id="base" type="text" name="base" value="<?php echo $loader->base ?>" size="8"/>
<label>Base:</label><br />
<input id="base" type="text" name="base" value="<?php echo $loader->base; ?>" size="15"/>
</p>

<p>
<label>Combo Base:</label><br />
<input id="base" type="text" name="comboBase" value="<?php echo $loader->comboBase; ?>" size="15"/>
</p>

<p>
Expand All @@ -188,14 +208,18 @@
</select>
</p>

<p>
<input type="checkbox" name="combine" value="1" <?php echo $combine; ?> />
Use Combo Loader
</p>

<p>
<input type="checkbox" name="canEmbed" value="1" <?php echo $canEmbedchecked ?> />
<input type="checkbox" name="embed" value="1" <?php echo $embedchecked; ?> />
Embed enabled
</p>

<p>
<input type="checkbox" name="loadOptional" value="1" <?php echo $loadOptionalchecked ?> />
<input type="checkbox" name="loadOptional" value="1" <?php echo $loadOptionalchecked; ?> />
Load Optional
</p>

Expand Down

0 comments on commit f28a677

Please sign in to comment.