<?php
/**
 * Application bootstrap
 *
 * @package home page
 * @author Miroslav Kubelik (koubel@volny.cz)
 * @copyright Copyright (c) 2007-2009 Miroslav Kubelik
 * @license http://opensource.org/licenses/gpl-license.php GNU GPL
 */

/**
 * Application bootstrap.
 *
 * Initializes application resources and settings.
 *
 * @package koubel homepage
 * @author Miroslav Kubelik (koubel@volny.cz)
 * @copyright Copyright (c) 2007-2009 Miroslav Kubelik
 * @license http://opensource.org/licenses/gpl-license.php GNU GPL
 */
class Bootstrap extends Zend_Application_Bootstrap_Bootstrap
{
    /**
     * Bootstrap autoloader for application resources
     * @return Zend_Application_Module_Autoloader
     */
    protected function _initAutoload()
    {
        /** @var $appAutoloader Zend_Loader_Autoloader */
        $appAutoloader = $this->getApplication()->getAutoloader();
        //enable classic include path directory loader
        $appAutoloader->setFallbackAutoloader(true);
        //enable warnings for autoloading
        $appAutoloader->suppressNotFoundWarnings($this->getApplication()->getEnvironment() == "production");
        
        // resource autoloader
        $autoloader = new Zend_Application_Module_Autoloader(array(
            'namespace' => 'HomeWebApp',
            'basePath'  => dirname(__FILE__),
        ));
    
        return $autoloader;
    }
    
    /**
     * Adds some dirs into includePath
     * @return void
     */
    protected function _initIncludePath()
    {
        IncludePath::add(APPLICATION_PATH."/models");
    }

    protected function _initResponse()
    {
        // Ensure the front controller is initialized
        $this->bootstrap('FrontController');

        // Retrieve the front controller from the bootstrap registry
        $front = $this->getResource('FrontController');

        $response = new HttpResponse();
        $front->setResponse($response);

        // Ensure the resource is stored in the bootstrap registry
        return $response;
    }

    /**
     * Gets application cache.
     * @return Zend_Cache
     */
    protected function _initCache()
    {
        $cacheOption = $this->getOption("cache");
        if (isset($cacheOption["dir"])) {
            $cacheDir = realpath($cacheOption["dir"]);
        } else {
            throw new Exception("cache resource isn't set");
        }

        $frontendOptions = array("automatic_serialization" => true);
        $backendOptions  = array("cache_dir" => $cacheDir);
        $cache = Zend_Cache::factory('Core', 'File', $frontendOptions, $backendOptions);

        //sets cache for some Zend classes, for speed up
        Zend_Date::setOptions(array('cache' => $cache));
        Zend_Locale::setCache($cache);
        Zend_Translate::setCache($cache);
        Zend_Db_Table::setDefaultMetadataCache($cache);

        return $cache;
    }

    /**
     * Inits application log
     * @return Zend_Log
     */
    protected function _initLog()
    {
        $log = new Zend_Log();
        
        $logOption = $this->getOption("log");
        $log->addWriter(isset($logOption["file"])
            ? new Zend_Log_Writer_Stream(realpath($logOption["file"]))
            : new Zend_Log_Writer_Null()
        );

        return $log;
    }

    /**
     * Adds resource type for action helpers into resource loader
     * @param Zend_Loader_Autoloader_Resource $resourceLoader
     */
    public static function addActionHelperLoaderResource(Zend_Loader_Autoloader_Resource $resourceLoader)
    {
        $resourceLoader->addResourceType("controllerHelper", "controllers/helpers/", "Controller_Helper");
    }

    /**
     * Adds resource type for action helpers into resource loader
     * @see Bootstrap::addActionHelperLoaderResource()
     */
    protected function _initActionHelperLoaderResource()
    {
        $loader = $this->getResource("autoload");
        self::addActionHelperLoaderResource($loader);
    }

    /**
     * Inits some database parameters, nowadays timezone only.
     */
    protected function _initDbParams()
    {
        $this->bootstrap('Db');
        
        $dbAdapter = $this->getResource("Db");
        $dbAdapter->query("SET time_zone = '+2:00'");
    }
}
