Skip to content

Instantly share code, notes, and snippets.

@jgornick
Forked from robzienert/gist:189307
Created September 18, 2009 21:59
Show Gist options
  • Save jgornick/189319 to your computer and use it in GitHub Desktop.
Save jgornick/189319 to your computer and use it in GitHub Desktop.
ZF: Cache Configs
<?php
require_once 'Zend/Application.php';
class My_Application extends Zend_Application
{
/**
* Flag used when determining if we should cache our configuration.
*/
protected $_cacheConfig = false;
/**
* Our default options which will use File caching
*/
protected $_cacheOptions = array(
'frontendType' => 'File',
'backendType' => 'File',
'frontendOptions' => array(),
'backendOptions' => array()
);
/**
* Constructor
*
* Initialize application. Potentially initializes include_paths, PHP
* settings, and bootstrap class.
*
* When $options is an array with a key of configFile, this will tell the
* class to cache the configuration using the default options or cacheOptions
* passed in.
*
* @param string $environment
* @param string|array|Zend_Config $options String path to configuration file, or array/Zend_Config of configuration options
* @throws Zend_Application_Exception When invalid options are provided
* @return void
*/
public function __construct($environment, $options = null)
{
if (is_array($options) && isset($options['configFile'])) {
$this->_cacheConfig = true;
// First, let's check to see if there are any cache options
if (isset($options['cacheOptions']))
$this->_cacheOptions =
array_merge($this->_cacheOptions, $options['cacheOptions']);
$options = $options['configFile'];
}
parent::__construct($environment, $options);
}
/**
* Load configuration file of options.
*
* Optionally will cache the configuration.
*
* @param string $file
* @throws Zend_Application_Exception When invalid configuration file is provided
* @return array
*/
protected function _loadConfig($file)
{
if (!$this->_cacheConfig)
return parent::_loadConfig($file);
require_once 'Zend/Cache.php';
$cache = Zend_Cache::factory(
$this->_cacheOptions['frontendType'],
$this->_cacheOptions['backendType'],
array_merge(array( // Frontend Default Options
'master_file' => $file,
'automatic_serialization' => true
), $this->_cacheOptions['frontendOptions']),
array_merge(array( // Backend Default Options
'cache_dir' => APPLICATION_PATH . '/data/cache'
), $this->_cacheOptions['backendOptions'])
);
$config = $cache->load('Zend_Application_Config');
if (!$config) {
$config = parent::_loadConfig($file);
$cache->save($config, 'Zend_Application_Config');
}
return $config;
}
}
<?php
class Default_Controller_Action_Helper_FileCache extends
Zend_Controller_Action_Helper_Abstract
{
protected $_cache;
public function load($file, $id)
{
return $this->getCache($file)->load($id);
}
public function save($file, $data, $id)
{
return $this->getCache($file)->save($data, $id);
}
public function getCache($file)
{
// We want to create a new Zend_Cache instance if it hasn't been created
// yet, or if it has been created and the master_file used is different
// than the specified one.
if (is_null($this->_cache) || $this->_cache->getOption('master_file') !== $file) {
$this->_cache = null;
$this->_cache = Zend_Cache::factory(
'File',
'File',
array( // Frontend Default Options
'master_file' => $file,
'automatic_serialization' => true
),
array( // Backend Default Options
'cache_dir' => APPLICATION_PATH . '/data/cache'
)
);
}
return $this->_cache;
}
}
<?php
// Define path to application directory
if (!defined('APPLICATION_PATH'))
define('APPLICATION_PATH', realpath(dirname(__FILE__) . '/../app'));
// Define application environment
if (!defined('APPLICATION_ENV'))
define('APPLICATION_ENV',
(getenv('APPLICATION_ENV') ? getenv('APPLICATION_ENV') : 'production'));
// Add our lib folder to the include paths
set_include_path(implode(PATH_SEPARATOR, array(
realpath(APPLICATION_PATH . '/../lib'),
get_include_path()
)));
/** My_Application */
require_once 'My/Application.php';
// Create application, bootstrap, and run
$application = new My_Application(
APPLICATION_ENV,
array(
'configFile' => APPLICATION_PATH . '/configs/application.ini'
)
);
$application->bootstrap()->run();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment