web/lib/Zend/Application/Resource/Modules.php
changeset 64 162c1de6545a
parent 19 1c2f13fd785c
child 68 ecaf28ffe26e
equal deleted inserted replaced
63:5b37998e522e 64:162c1de6545a
       
     1 <?php
       
     2 /**
       
     3  * Zend Framework
       
     4  *
       
     5  * LICENSE
       
     6  *
       
     7  * This source file is subject to the new BSD license that is bundled
       
     8  * with this package in the file LICENSE.txt.
       
     9  * It is also available through the world-wide-web at this URL:
       
    10  * http://framework.zend.com/license/new-bsd
       
    11  * If you did not receive a copy of the license and are unable to
       
    12  * obtain it through the world-wide-web, please send an email
       
    13  * to license@zend.com so we can send you a copy immediately.
       
    14  *
       
    15  * @category   Zend
       
    16  * @package    Zend_Application
       
    17  * @subpackage Resource
       
    18  * @copyright  Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
       
    19  * @license    http://framework.zend.com/license/new-bsd     New BSD License
       
    20  * @version    $Id: Modules.php 20814 2010-02-01 20:13:08Z freak $
       
    21  */
       
    22 
       
    23 /**
       
    24  * @see Zend_Application_Resource_ResourceAbstract
       
    25  */
       
    26 require_once 'Zend/Application/Resource/ResourceAbstract.php';
       
    27 
       
    28 
       
    29 /**
       
    30  * Module bootstrapping resource
       
    31  *
       
    32  * @category   Zend
       
    33  * @package    Zend_Application
       
    34  * @subpackage Resource
       
    35  * @copyright  Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
       
    36  * @license    http://framework.zend.com/license/new-bsd     New BSD License
       
    37  */
       
    38 class Zend_Application_Resource_Modules extends Zend_Application_Resource_ResourceAbstract
       
    39 {
       
    40     /**
       
    41      * @var ArrayObject
       
    42      */
       
    43     protected $_bootstraps;
       
    44 
       
    45     /**
       
    46      * Constructor
       
    47      *
       
    48      * @param  mixed $options
       
    49      * @return void
       
    50      */
       
    51     public function __construct($options = null)
       
    52     {
       
    53         $this->_bootstraps = new ArrayObject(array(), ArrayObject::ARRAY_AS_PROPS);
       
    54         parent::__construct($options);
       
    55     }
       
    56 
       
    57     /**
       
    58      * Initialize modules
       
    59      *
       
    60      * @return array
       
    61      * @throws Zend_Application_Resource_Exception When bootstrap class was not found
       
    62      */
       
    63     public function init()
       
    64     {
       
    65         $bootstrap = $this->getBootstrap();
       
    66         $bootstrap->bootstrap('FrontController');
       
    67         $front = $bootstrap->getResource('FrontController');
       
    68 
       
    69         $modules = $front->getControllerDirectory();
       
    70         $default = $front->getDefaultModule();
       
    71         $curBootstrapClass = get_class($bootstrap);
       
    72         foreach ($modules as $module => $moduleDirectory) {
       
    73             $bootstrapClass = $this->_formatModuleName($module) . '_Bootstrap';
       
    74             if (!class_exists($bootstrapClass, false)) {
       
    75                 $bootstrapPath  = dirname($moduleDirectory) . '/Bootstrap.php';
       
    76                 if (file_exists($bootstrapPath)) {
       
    77                     $eMsgTpl = 'Bootstrap file found for module "%s" but bootstrap class "%s" not found';
       
    78                     include_once $bootstrapPath;
       
    79                     if (($default != $module)
       
    80                         && !class_exists($bootstrapClass, false)
       
    81                     ) {
       
    82                         throw new Zend_Application_Resource_Exception(sprintf(
       
    83                             $eMsgTpl, $module, $bootstrapClass
       
    84                         ));
       
    85                     } elseif ($default == $module) {
       
    86                         if (!class_exists($bootstrapClass, false)) {
       
    87                             $bootstrapClass = 'Bootstrap';
       
    88                             if (!class_exists($bootstrapClass, false)) {
       
    89                                 throw new Zend_Application_Resource_Exception(sprintf(
       
    90                                     $eMsgTpl, $module, $bootstrapClass
       
    91                                 ));
       
    92                             }
       
    93                         }
       
    94                     }
       
    95                 } else {
       
    96                     continue;
       
    97                 }
       
    98             }
       
    99 
       
   100             if ($bootstrapClass == $curBootstrapClass) {
       
   101                 // If the found bootstrap class matches the one calling this
       
   102                 // resource, don't re-execute.
       
   103                 continue;
       
   104             }
       
   105 
       
   106             $moduleBootstrap = new $bootstrapClass($bootstrap);
       
   107             $moduleBootstrap->bootstrap();
       
   108             $this->_bootstraps[$module] = $moduleBootstrap;
       
   109         }
       
   110 
       
   111         return $this->_bootstraps;
       
   112     }
       
   113 
       
   114     /**
       
   115      * Get bootstraps that have been run
       
   116      *
       
   117      * @return ArrayObject
       
   118      */
       
   119     public function getExecutedBootstraps()
       
   120     {
       
   121         return $this->_bootstraps;
       
   122     }
       
   123 
       
   124     /**
       
   125      * Format a module name to the module class prefix
       
   126      *
       
   127      * @param  string $name
       
   128      * @return string
       
   129      */
       
   130     protected function _formatModuleName($name)
       
   131     {
       
   132         $name = strtolower($name);
       
   133         $name = str_replace(array('-', '.'), ' ', $name);
       
   134         $name = ucwords($name);
       
   135         $name = str_replace(' ', '', $name);
       
   136         return $name;
       
   137     }
       
   138 }