Perforce Chronicle 2012.2/486814
API Documentation

P4Cms_AssetHandler_File Class Reference

Provide a local file backend for Asset Handling. More...

Inheritance diagram for P4Cms_AssetHandler_File:
P4Cms_AssetHandlerInterface

List of all members.

Public Member Functions

 __construct (array $options=null)
 Constructor allows passing options for outputPath or basePath.
 exists ($id)
 Check if the given ID exists in storage.
 getBasePath ()
 Returns the base path presently in use.
 getOutputPath ()
 Returns the output path presently in use or null.
 isOffsite ()
 Used to determine if the asset handler will be storing assets offsite or not.
 put ($id, $data)
 Store the passed data using indicated ID.
 setBasePath ($path)
 Sets the base path to use.
 setOutputPath ($path)
 Sets the output path to use.
 uri ($id)
 Provide a URI for the indicated asset ID.

Protected Attributes

 $_basePath
 $_outputPath

Detailed Description

Provide a local file backend for Asset Handling.

This is the default backend and is intended for single web server deployments. If you wish to use this backend with horizontally scaled web servers the output path should point to a shared storage location (e.g. nfs).

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

Constructor & Destructor Documentation

P4Cms_AssetHandler_File::__construct ( array $  options = null)

Constructor allows passing options for outputPath or basePath.

Parameters:
array$optionsOptions to use on this instance
    {
        if (isset($options['outputPath'])) {
            $this->setOutputPath($options['outputPath']);
        }
        if (isset($options['basePath'])) {
            $this->setBasePath($options['basePath']);
        }
    }

Member Function Documentation

P4Cms_AssetHandler_File::exists ( id)

Check if the given ID exists in storage.

Parameters:
string$idThe ID to test
Returns:
bool true if exists, false otherwise

Implements P4Cms_AssetHandlerInterface.

    {
        if (!$this->getOutputPath()) {
            return false;
        }
        
        return file_exists($this->getOutputPath() . '/' . $id);
    }
P4Cms_AssetHandler_File::getBasePath ( )

Returns the base path presently in use.

Defaults to the value of BASE_PATH define if not set.

Returns:
string The current base path
    {
        return $this->_basePath ?: BASE_PATH;
    }
P4Cms_AssetHandler_File::getOutputPath ( )

Returns the output path presently in use or null.

Returns:
string|null The current output path
    {
        return $this->_outputPath;
    }
P4Cms_AssetHandler_File::isOffsite ( )

Used to determine if the asset handler will be storing assets offsite or not.

Assets such as CSS need to know this so they can decide if they need to include the site's url when referencing images.

Returns:
bool true if assets stored offsite, false otherwise

Implements P4Cms_AssetHandlerInterface.

    {
        return false;
    }
P4Cms_AssetHandler_File::put ( id,
data 
)

Store the passed data using indicated ID.

Will clobber any existing entry with the same ID.

Parameters:
string$idThe ID to store under
string$dataThe data to store
Returns:
bool True if put was successful, false otherwise

Implements P4Cms_AssetHandlerInterface.

    {
        if (!$this->getOutputPath()) {
            return false;
        }

        $result = @file_put_contents($this->getOutputPath() . '/' . $id, $data);
        
        // if we failed to write, log as a warning and return false.
        if ($result === false) {
            $writable = is_writable($this->getOutputPath());
            $message  = "Failed to put asset '" . $id . "'. Output path is "
                      . ($writable ? '' : 'not ') . 'writable';
            P4Cms_Log::log($message, P4Cms_Log::WARN);
            
            return false;
        } else {
            return true;
        }
    }
P4Cms_AssetHandler_File::setBasePath ( path)

Sets the base path to use.

Set to null to enable default base path.

Parameters:
string | null$pathThe base path to use or null for default
Returns:
P4Cms_AssetHandler_File To maintain a fluent interface
    {
        if (!is_string($path) && !is_null($path)) {
            throw new InvalidArgumentException('Path must be a string or null');
        }

        $this->_basePath = $path ? rtrim($path, '/\\') : $path;

        return $this;
    }
P4Cms_AssetHandler_File::setOutputPath ( path)

Sets the output path to use.

Parameters:
string | null$pathThe output path
Returns:
P4Cms_AssetHandler_File To maintain a fluent interface
    {
        if (!is_string($path) && !is_null($path)) {
            throw new InvalidArgumentException('Path must be a string or null');
        }

        // if we have a path strip trailing slashes and try to ensure it exists
        if ($path) {
            $path = rtrim($path, '/\\');
            @mkdir($path, 0755, true);
        }

        $this->_outputPath = $path;

        return $this;
    }
P4Cms_AssetHandler_File::uri ( id)

Provide a URI for the indicated asset ID.

Parameters:
string$idThe ID to get a URI for
Returns:
string|bool The uri used for the specified asset or false

Implements P4Cms_AssetHandlerInterface.

    {
        if (!$this->getOutputPath()) {
            return false;
        }

        $request = Zend_Controller_Front::getInstance()->getRequest();
        $baseUrl = isset($request)
                 ? $request->getBaseUrl()
                 : '';

        return $baseUrl . str_replace($this->getBasePath(), '', $this->getOutputPath()) . "/" . $id;
    }

Member Data Documentation

P4Cms_AssetHandler_File::$_basePath [protected]
P4Cms_AssetHandler_File::$_outputPath [protected]

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