Perforce Chronicle 2012.2/486814
API Documentation

P4Cms_Application Class Reference

Extends Zend_Application to work without a config file. More...

List of all members.

Public Member Functions

 __construct ($environment, $options=null)
 Reimplement Zend_Application constructor to look for a Bootstrap.php if one hasn't been specified and attempt to load default options from it.
 getConfigFile ()
 Get the configuration file this application instance was first constructed with.
 mergeOptions (array $array1, $array2=null, $cache=false)
 Merge options recursively Extended to support caching of merged result in APC.

Protected Member Functions

 _loadConfig ($file)
 Load configuration file of options.

Protected Attributes

 $_configFile = null

Detailed Description

Extends Zend_Application to work without a config file.

P4Cms_Application will look for a Bootstrap.php when options don't explicitly specify one and attempt to load default options from the Bootstrap class via getDefaultOptions().

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_Application::__construct ( environment,
options = null 
)

Reimplement Zend_Application constructor to look for a Bootstrap.php if one hasn't been specified and attempt to load default options from it.

Parameters:
string$environmentapplication environment.
string | array | Zend_Config$optionsString path to configuration file, or array/Zend_Config of configuration options
Exceptions:
Zend_Application_ExceptionWhen invalid options are provided
Returns:
void
    {
        $this->_environment = (string) $environment;

        // if not already present, add P4Cms_Loader to the autoloader stack
        require_once "P4Cms/Loader.php";
        spl_autoload_register(array('P4Cms_Loader', 'autoload'));

        if ($options === null) {
            $options = array();
        } elseif (is_string($options)) {
            $this->_configFile = $options;
            try {
                $options = $this->_loadConfig($options);
            } catch (Zend_Config_Exception $e) {
                $options = array();
            }
        } elseif ($options instanceof Zend_Config) {
            $options = $options->toArray();
        } elseif (!is_array($options)) {
            throw new Zend_Application_Exception(
                "Invalid options provided; must be location of config file, " .
                "a config object, or an array"
            );
        }

        // look for a 'Bootstrap' class if nothing else has been specified.
        if (!isset($options['bootstrap']) && class_exists('Bootstrap')) {

            $options['bootstrap'] = "Bootstrap.php";

            // look for provision of default options in Bootstrap class
            if (method_exists('Bootstrap', 'getDefaultOptions')) {
                $defaults = Bootstrap::getDefaultOptions($environment);
                $options  = $this->mergeOptions($defaults, $options, true);
            }
        }

        $this->setOptions($options);
    }

Member Function Documentation

P4Cms_Application::_loadConfig ( file) [protected]

Load configuration file of options.

Extends Zend's _loadConfig to add caching of config data when APC is enabled.

Parameters:
string$filethe name of the configuration file to load
Exceptions:
Zend_Application_ExceptionWhen invalid configuration file is provided
Returns:
array the loaded configuration array
    {
        // if we have apc and a readable file, we'll attempt to cache the config
        // if not, we'll just call parent and let it deal with everything.
        if (!extension_loaded('apc') || !is_readable($file)) {
            return parent::_loadConfig($file);
        }

        // check if we have a cached version of this config file.
        $cacheKey  = $file . "-" . $this->getEnvironment() . "-" . filemtime($file);
        $cacheData = apc_fetch($cacheKey);
        if (is_array($cacheData)) {
            return $cacheData;
        }

        // no cached copy, lets make one.
        // we catch exceptions here, otherwise we would fail to cache
        // invalid config files and would repeatedly try to parse them.
        try {
            $config = parent::_loadConfig($file);
        } catch (Zend_Config_Exception $e) {
            $config = array();
        }
        apc_store($cacheKey, $config);

        return $config;
    }
P4Cms_Application::getConfigFile ( )

Get the configuration file this application instance was first constructed with.

If no configuration file was used (e.g. an array or config object was injected), throws an exception.

Returns:
string the configuration file used to construct the application
Exceptions:
Zend_Application_Exceptionif no config file was used to construct the application
    {
        if (!$this->_configFile) {
            throw new Zend_Application_Exception(
                "Cannot get the configuration file. No file was passed to application constructor."
            );
        }

        return $this->_configFile;
    }
P4Cms_Application::mergeOptions ( array $  array1,
array2 = null,
cache = false 
)

Merge options recursively Extended to support caching of merged result in APC.

Parameters:
array$array1array to merge into
mixed$array2array to merge from
bool$cacheenable caching of merged result (defaults to false)
Returns:
array the merged result
    {
        // if we have apc and caller asked for caching, we'll attempt
        // to cache the merged options, if not, we'll just call parent.
        // no point caching if array2 is empty or not an array.
        if (!$cache || !is_array($array2) || empty($array2) || !extension_loaded('apc')) {
            return parent::mergeOptions($array1, $array2);
        }

        $cacheKey  = md5(serialize($array1) . serialize($array2));
        $cacheData = apc_fetch($cacheKey);
        if (is_array($cacheData)) {
            return $cacheData;
        }

        $merged = parent::mergeOptions($array1, $array2);
        apc_store($cacheKey, $merged);

        return $merged;
    }

Member Data Documentation

P4Cms_Application::$_configFile = null [protected]

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