|
Perforce Chronicle 2012.2/486814
API Documentation
|
Aggregating version of HeadScript helper (combines JS files) More...
Public Member Functions | |
| getAssetHandler () | |
| Get the asset handler used to store the aggregated JS file(s). | |
| getDocumentRoot () | |
| Get the file-system path to the document root. | |
| setAggregateJs ($aggregate) | |
| Enable or disable JS aggregation. | |
| setAssetHandler (P4Cms_AssetHandlerInterface $handler=null) | |
| Set the asset handler used to store the aggregated JS file(s). | |
| setDocumentRoot ($path) | |
| Set the file-system path to the document root. | |
| toString ($indent=null) | |
| Extend toString to aggregate JS files where possible. | |
Protected Member Functions | |
| _aggregateJs () | |
| Aggregate local unconditional JS files. | |
| _canGzipCompress () | |
| Check if this PHP can generate gzip compressed data. | |
| _clientAcceptsGzip () | |
| Check if the client can accept gzip encoded content. | |
Protected Attributes | |
| $_aggregate = false | |
| $_aggregated = false | |
| $_assetHandler = null | |
| $_documentRoot = null | |
Aggregating version of HeadScript helper (combines JS files)
| P4Cms_View_Helper_HeadScript::_aggregateJs | ( | ) | [protected] |
Aggregate local unconditional JS files.
Rebuilds whenever a file changes.
{
// bail out if no asset handler is configured
if (!$this->getAssetHandler()) {
P4Cms_Log::log(
"Failed to aggregate JS. Asset Handler is unset.",
P4Cms_Log::ERR
);
return;
}
// bail out if document root is unset.
if (!$this->getDocumentRoot()) {
P4Cms_Log::log(
"Failed to aggregate JS. Document root is unset.",
P4Cms_Log::ERR
);
return;
}
// identify JS we can aggregate.
$time = 0;
$files = array();
$ignore = array();
$this->getContainer()->ksort();
foreach ($this as $item) {
if (!$this->_isValid($item)) {
continue;
}
// normalize the attributes.
$attributes = $item->attributes + array(
'src' => null,
'conditional' => null,
'charset' => null,
'defer' => null
);
// only aggregate scripts that:
// - are type text/javascript
// - have a 'src' attribute
// - are local
// - are not conditional
// - have no explicit charset
// - are not deferred
// - exist in the document root
$file = $this->getDocumentRoot() . $attributes['src'];
if (($item->type !== 'text/javascript'
|| !$attributes['src']
|| P4Cms_Uri::hasScheme($attributes['src']))
|| $attributes['conditional']
|| $attributes['charset']
|| $attributes['defer']
|| !file_exists($file)
) {
$ignore[] = $item;
continue;
}
$files[] = $file;
$time = max($time, filemtime($file));
}
// nothing to do if there are no files to aggregate.
if (!$files) {
return;
}
// determine if compression should be enabled
$compressed = $this->_canGzipCompress() && $this->_clientAcceptsGzip();
// generate build filename.
$buildFile = md5(implode(',', $files) . $time)
. ($compressed ? '.jsgz' : '.js');
// rebuild if file does not exist.
if (!$this->getAssetHandler()->exists($buildFile)) {
$js = "";
foreach ($files as $file) {
$js .= file_get_contents($file) . "\n";
}
// also compress if possible.
if ($compressed) {
$js = gzencode($js, 9);
}
// write out aggregate file - if it fails, abort aggregation.
if (!$this->getAssetHandler()->put($buildFile, $js)) {
return;
}
}
// if we made it this far aggregation worked; update the
// list to only contain ignored plus the aggregated scripts.
$this->getContainer()->exchangeArray($ignore);
$this->appendFile($this->getAssetHandler()->uri($buildFile));
$this->_aggregated = true;
}
| P4Cms_View_Helper_HeadScript::_canGzipCompress | ( | ) | [protected] |
Check if this PHP can generate gzip compressed data.
{
return function_exists('gzencode');
}
| P4Cms_View_Helper_HeadScript::_clientAcceptsGzip | ( | ) | [protected] |
Check if the client can accept gzip encoded content.
{
$front = Zend_Controller_Front::getInstance();
$request = $front->getRequest();
$accepts = isset($request)
? $request->getHeader('Accept-Encoding')
: '';
return strpos($accepts, 'gzip') !== false;
}
| P4Cms_View_Helper_HeadScript::getAssetHandler | ( | ) |
Get the asset handler used to store the aggregated JS file(s).
{
return $this->_assetHandler;
}
| P4Cms_View_Helper_HeadScript::getDocumentRoot | ( | ) |
Get the file-system path to the document root.
{
return $this->_documentRoot;
}
| P4Cms_View_Helper_HeadScript::setAggregateJs | ( | $ | aggregate | ) |
Enable or disable JS aggregation.
| bool | $aggregate | set to true to enable, false to disable. |
{
$this->_aggregate = (bool) $aggregate;
return $this;
}
| P4Cms_View_Helper_HeadScript::setAssetHandler | ( | P4Cms_AssetHandlerInterface $ | handler = null | ) |
Set the asset handler used to store the aggregated JS file(s).
| P4Cms_AssetHandlerInterface | null | $handler | The handler to use or null |
{
$this->_assetHandler = $handler;
return $this;
}
| P4Cms_View_Helper_HeadScript::setDocumentRoot | ( | $ | path | ) |
Set the file-system path to the document root.
| string | $path | the location of the public folder. |
{
$this->_documentRoot = rtrim($path, '/\\');
return $this;
}
| P4Cms_View_Helper_HeadScript::toString | ( | $ | indent = null | ) |
Extend toString to aggregate JS files where possible.
| string | int | $indent | Zend provides no documentation for this param. |
{
// aggregate JS files (but only once).
if ($this->_aggregate && !$this->_aggregated) {
$this->_aggregateJs();
}
return parent::toString();
}
P4Cms_View_Helper_HeadScript::$_aggregate = false [protected] |
P4Cms_View_Helper_HeadScript::$_aggregated = false [protected] |
P4Cms_View_Helper_HeadScript::$_assetHandler = null [protected] |
P4Cms_View_Helper_HeadScript::$_documentRoot = null [protected] |