File indexing completed on 2024-06-23 05:55:02

0001 <?php
0002 /**
0003  * Zend Framework
0004  *
0005  * LICENSE
0006  *
0007  * This source file is subject to the new BSD license that is bundled
0008  * with this package in the file LICENSE.txt.
0009  * It is also available through the world-wide-web at this URL:
0010  * http://framework.zend.com/license/new-bsd
0011  * If you did not receive a copy of the license and are unable to
0012  * obtain it through the world-wide-web, please send an email
0013  * to license@zend.com so we can send you a copy immediately.
0014  *
0015  * @category   Zend
0016  * @package    Zend_Application
0017  * @subpackage Resource
0018  * @copyright  Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
0019  * @license    http://framework.zend.com/license/new-bsd     New BSD License
0020  * @version    $Id$
0021  */
0022 
0023 /**
0024  * @see Zend_Application_Resource_ResourceAbstract
0025  */
0026 // require_once 'Zend/Application/Resource/ResourceAbstract.php';
0027 
0028 
0029 /**
0030  * Module bootstrapping resource
0031  *
0032  * @category   Zend
0033  * @package    Zend_Application
0034  * @subpackage Resource
0035  * @copyright  Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
0036  * @license    http://framework.zend.com/license/new-bsd     New BSD License
0037  */
0038 class Zend_Application_Resource_Modules extends Zend_Application_Resource_ResourceAbstract
0039 {
0040     /**
0041      * @var ArrayObject
0042      */
0043     protected $_bootstraps;
0044 
0045     /**
0046      * Constructor
0047      *
0048      * @param  mixed $options
0049      */
0050     public function __construct($options = null)
0051     {
0052         $this->_bootstraps = new ArrayObject(array(), ArrayObject::ARRAY_AS_PROPS);
0053         parent::__construct($options);
0054     }
0055 
0056     /**
0057      * Initialize modules
0058      *
0059      * @return array
0060      * @throws Zend_Application_Resource_Exception When bootstrap class was not found
0061      */
0062     public function init()
0063     {
0064         $bootstraps = array();
0065         $bootstrap = $this->getBootstrap();
0066         $bootstrap->bootstrap('FrontController');
0067         $front = $bootstrap->getResource('FrontController');
0068 
0069         $modules = $front->getControllerDirectory();
0070         $default = $front->getDefaultModule();
0071         $curBootstrapClass = get_class($bootstrap);
0072         foreach ($modules as $module => $moduleDirectory) {
0073             $bootstrapClass = $this->_formatModuleName($module) . '_Bootstrap';
0074             if (!class_exists($bootstrapClass, false)) {
0075                 $bootstrapPath = dirname($moduleDirectory) . '/Bootstrap.php';
0076                 if (file_exists($bootstrapPath)) {
0077                     $eMsgTpl = 'Bootstrap file found for module "%s" but bootstrap class "%s" not found';
0078                     include_once $bootstrapPath;
0079                     if (($default != $module)
0080                         && !class_exists($bootstrapClass, false)
0081                     ) {
0082                         throw new Zend_Application_Resource_Exception(
0083                             sprintf(
0084                                 $eMsgTpl, $module, $bootstrapClass
0085                             )
0086                         );
0087                     } elseif ($default == $module) {
0088                         if (!class_exists($bootstrapClass, false)) {
0089                             $bootstrapClass = 'Bootstrap';
0090                             if (!class_exists($bootstrapClass, false)) {
0091                                 throw new Zend_Application_Resource_Exception(
0092                                     sprintf(
0093                                         $eMsgTpl, $module, $bootstrapClass
0094                                     )
0095                                 );
0096                             }
0097                         }
0098                     }
0099                 } else {
0100                     continue;
0101                 }
0102             }
0103 
0104             if ($bootstrapClass == $curBootstrapClass) {
0105                 // If the found bootstrap class matches the one calling this
0106                 // resource, don't re-execute.
0107                 continue;
0108             }
0109 
0110             $bootstraps[$module] = $bootstrapClass;
0111         }
0112 
0113         return $this->_bootstraps = $this->bootstrapBootstraps($bootstraps);
0114     }
0115 
0116     /*
0117      * Bootstraps the bootstraps found. Allows for easy extension.
0118      * @param array $bootstraps Array containing the bootstraps to instantiate
0119      */
0120     protected function bootstrapBootstraps($bootstraps)
0121     {
0122         $bootstrap = $this->getBootstrap();
0123         $out       = new ArrayObject(array(), ArrayObject::ARRAY_AS_PROPS);
0124 
0125         foreach ($bootstraps as $module => $bootstrapClass) {
0126             $moduleBootstrap = new $bootstrapClass($bootstrap);
0127             $moduleBootstrap->bootstrap();
0128             $out[$module] = $moduleBootstrap;
0129         }
0130 
0131         return $out;
0132     }
0133 
0134     /**
0135      * Get bootstraps that have been run
0136      *
0137      * @return ArrayObject
0138      */
0139     public function getExecutedBootstraps()
0140     {
0141         return $this->_bootstraps;
0142     }
0143 
0144     /**
0145      * Format a module name to the module class prefix
0146      *
0147      * @param  string $name
0148      * @return string
0149      */
0150     protected function _formatModuleName($name)
0151     {
0152         $name = strtolower($name);
0153         $name = str_replace(array('-', '.'), ' ', $name);
0154         $name = ucwords($name);
0155         $name = str_replace(' ', '', $name);
0156         return $name;
0157     }
0158 }