Caching application.ini for Zend Framework apps

In most of the time we build our application using Zend Framework, we make index.php as the entry which creates the object of Zend_Application and it’s configurations such as Application.ini.

 

But it’s very expensive when reading and parsing the Application.ini for every request.

 

It gave me inspiration to prevent parsing the ini for every request when I take a look into function Zend_Application::_loadConfig(). This function has allowed to pass an array as it’s parameters.

 

Here is the code in index.php :

 

<?php
// ......

// Application.ini.inc cache file
defined('CONFIG_INC')
    || define('CONFIG_INC',
              PROJECT_ROOT . '/library/Kbs/Config/Application.ini.inc');

// We use default config if no cache
$configFile = CONFIG_INC;
$noConfigCache = false;
if (false == is_file(CONFIG_INC)) {
    $configFile = PROJECT_ROOT . '/library/Kbs/Config/Application.ini';
    $noConfigCache = true;
}

// Zend_Application
require_once 'Zend/Application.php';

// Application object
$application = new Zend_Application(
    APPLICATION_ENV,
    $configFile
);

// Create the cache of config if no
// Only for production
if ($noConfigCache and ('production' == APPLICATION_ENV)) {
    $configs = '<?php' . PHP_EOL
             . 'return '
             . var_export($application->getOptions(), true) . PHP_EOL
             . '?>';
    file_put_contents(CONFIG_INC, $configs);
}

// ......

 

Now the cache called Application.ini.inc will be loaded and parsed when the application find it.

Posted in Zend Framework | Tagged , | Leave a comment