Perforce Chronicle 2012.2/486814
API Documentation

P4Cms_View_Helper_HeadScript Class Reference

Aggregating version of HeadScript helper (combines JS files) More...

List of all members.

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

Detailed Description

Aggregating version of HeadScript helper (combines JS files)

Copyright:
2011-2012 Perforce Software. All rights reserved
License:
Please see LICENSE.txt in top-level folder of this distribution.
Version:
2012.2/486814

Member Function Documentation

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.

Returns:
bool true if this PHP has gzip support.
    {
        return function_exists('gzencode');
    }
P4Cms_View_Helper_HeadScript::_clientAcceptsGzip ( ) [protected]

Check if the client can accept gzip encoded content.

Returns:
bool true if the client supports gzip; false otherwise.
    {
        $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).

Returns:
P4Cms_AssetHandlerInterface|null the handler to use or null
    {
        return $this->_assetHandler;
    }
P4Cms_View_Helper_HeadScript::getDocumentRoot ( )

Get the file-system path to the document root.

Returns:
string the location of the public folder.
    {
        return $this->_documentRoot;
    }
P4Cms_View_Helper_HeadScript::setAggregateJs ( aggregate)

Enable or disable JS aggregation.

Parameters:
bool$aggregateset to true to enable, false to disable.
Returns:
P4Cms_View_Helper_HeadScript provides fluent interface.
    {
        $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).

Parameters:
P4Cms_AssetHandlerInterface | null$handlerThe handler to use or null
Returns:
P4Cms_View_Helper_HeadScript provides fluent interface.
    {
        $this->_assetHandler = $handler;

        return $this;
    }
P4Cms_View_Helper_HeadScript::setDocumentRoot ( path)

Set the file-system path to the document root.

Parameters:
string$paththe location of the public folder.
Returns:
P4Cms_View_Helper_HeadScript provides fluent interface.
    {
        $this->_documentRoot = rtrim($path, '/\\');
        return $this;
    }
P4Cms_View_Helper_HeadScript::toString ( indent = null)

Extend toString to aggregate JS files where possible.

Parameters:
string | int$indentZend provides no documentation for this param.
Returns:
string
    {
        // aggregate JS files (but only once).
        if ($this->_aggregate && !$this->_aggregated) {
            $this->_aggregateJs();
        }

        return parent::toString();
    }

Member Data Documentation

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]

The documentation for this class was generated from the following file: