Modules.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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-2015 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. * @version $Id$
  21. */
  22. /**
  23. * @see Zend_Application_Resource_ResourceAbstract
  24. */
  25. require_once 'Zend/Application/Resource/ResourceAbstract.php';
  26. /**
  27. * Module bootstrapping resource
  28. *
  29. * @category Zend
  30. * @package Zend_Application
  31. * @subpackage Resource
  32. * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
  33. * @license http://framework.zend.com/license/new-bsd New BSD License
  34. */
  35. class Zend_Application_Resource_Modules extends Zend_Application_Resource_ResourceAbstract
  36. {
  37. /**
  38. * @var ArrayObject
  39. */
  40. protected $_bootstraps;
  41. /**
  42. * Constructor
  43. *
  44. * @param mixed $options
  45. */
  46. public function __construct($options = null)
  47. {
  48. $this->_bootstraps = new ArrayObject(array(), ArrayObject::ARRAY_AS_PROPS);
  49. parent::__construct($options);
  50. }
  51. /**
  52. * Initialize modules
  53. *
  54. * @return array
  55. * @throws Zend_Application_Resource_Exception When bootstrap class was not found
  56. */
  57. public function init()
  58. {
  59. $bootstraps = array();
  60. $bootstrap = $this->getBootstrap();
  61. $bootstrap->bootstrap('FrontController');
  62. $front = $bootstrap->getResource('FrontController');
  63. $modules = $front->getControllerDirectory();
  64. $default = $front->getDefaultModule();
  65. $curBootstrapClass = get_class($bootstrap);
  66. foreach ($modules as $module => $moduleDirectory) {
  67. $bootstrapClass = $this->_formatModuleName($module) . '_Bootstrap';
  68. if (!class_exists($bootstrapClass, false)) {
  69. $bootstrapPath = dirname($moduleDirectory) . '/Bootstrap.php';
  70. if (file_exists($bootstrapPath)) {
  71. $eMsgTpl = 'Bootstrap file found for module "%s" but bootstrap class "%s" not found';
  72. include_once $bootstrapPath;
  73. if (($default != $module)
  74. && !class_exists($bootstrapClass, false)
  75. ) {
  76. throw new Zend_Application_Resource_Exception(
  77. sprintf(
  78. $eMsgTpl, $module, $bootstrapClass
  79. )
  80. );
  81. } elseif ($default == $module) {
  82. if (!class_exists($bootstrapClass, false)) {
  83. $bootstrapClass = 'Bootstrap';
  84. if (!class_exists($bootstrapClass, false)) {
  85. throw new Zend_Application_Resource_Exception(
  86. sprintf(
  87. $eMsgTpl, $module, $bootstrapClass
  88. )
  89. );
  90. }
  91. }
  92. }
  93. } else {
  94. continue;
  95. }
  96. }
  97. if ($bootstrapClass == $curBootstrapClass) {
  98. // If the found bootstrap class matches the one calling this
  99. // resource, don't re-execute.
  100. continue;
  101. }
  102. $bootstraps[$module] = $bootstrapClass;
  103. }
  104. return $this->_bootstraps = $this->bootstrapBootstraps($bootstraps);
  105. }
  106. /*
  107. * Bootstraps the bootstraps found. Allows for easy extension.
  108. * @param array $bootstraps Array containing the bootstraps to instantiate
  109. */
  110. protected function bootstrapBootstraps($bootstraps)
  111. {
  112. $bootstrap = $this->getBootstrap();
  113. $out = new ArrayObject(array(), ArrayObject::ARRAY_AS_PROPS);
  114. foreach ($bootstraps as $module => $bootstrapClass) {
  115. $moduleBootstrap = new $bootstrapClass($bootstrap);
  116. $moduleBootstrap->bootstrap();
  117. $out[$module] = $moduleBootstrap;
  118. }
  119. return $out;
  120. }
  121. /**
  122. * Get bootstraps that have been run
  123. *
  124. * @return ArrayObject
  125. */
  126. public function getExecutedBootstraps()
  127. {
  128. return $this->_bootstraps;
  129. }
  130. /**
  131. * Format a module name to the module class prefix
  132. *
  133. * @param string $name
  134. * @return string
  135. */
  136. protected function _formatModuleName($name)
  137. {
  138. $name = strtolower($name);
  139. $name = str_replace(array('-', '.'), ' ', $name);
  140. $name = ucwords($name);
  141. $name = str_replace(' ', '', $name);
  142. return $name;
  143. }
  144. }