Modules.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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-2012 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-2012 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. * @return void
  46. */
  47. public function __construct($options = null)
  48. {
  49. $this->_bootstraps = new ArrayObject(array(), ArrayObject::ARRAY_AS_PROPS);
  50. parent::__construct($options);
  51. }
  52. /**
  53. * Initialize modules
  54. *
  55. * @return array
  56. * @throws Zend_Application_Resource_Exception When bootstrap class was not found
  57. */
  58. public function init()
  59. {
  60. $bootstraps = array();
  61. $bootstrap = $this->getBootstrap();
  62. $bootstrap->bootstrap('FrontController');
  63. $front = $bootstrap->getResource('FrontController');
  64. $modules = $front->getControllerDirectory();
  65. $default = $front->getDefaultModule();
  66. $curBootstrapClass = get_class($bootstrap);
  67. foreach ($modules as $module => $moduleDirectory) {
  68. $bootstrapClass = $this->_formatModuleName($module) . '_Bootstrap';
  69. if (!class_exists($bootstrapClass, false)) {
  70. $bootstrapPath = dirname($moduleDirectory) . '/Bootstrap.php';
  71. if (file_exists($bootstrapPath)) {
  72. $eMsgTpl = 'Bootstrap file found for module "%s" but bootstrap class "%s" not found';
  73. include_once $bootstrapPath;
  74. if (($default != $module)
  75. && !class_exists($bootstrapClass, false)
  76. ) {
  77. throw new Zend_Application_Resource_Exception(sprintf(
  78. $eMsgTpl, $module, $bootstrapClass
  79. ));
  80. } elseif ($default == $module) {
  81. if (!class_exists($bootstrapClass, false)) {
  82. $bootstrapClass = 'Bootstrap';
  83. if (!class_exists($bootstrapClass, false)) {
  84. throw new Zend_Application_Resource_Exception(sprintf(
  85. $eMsgTpl, $module, $bootstrapClass
  86. ));
  87. }
  88. }
  89. }
  90. } else {
  91. continue;
  92. }
  93. }
  94. if ($bootstrapClass == $curBootstrapClass) {
  95. // If the found bootstrap class matches the one calling this
  96. // resource, don't re-execute.
  97. continue;
  98. }
  99. $bootstraps[$module] = $bootstrapClass;
  100. }
  101. return $this->_bootstraps = $this->bootstrapBootstraps($bootstraps);
  102. }
  103. /*
  104. * Bootstraps the bootstraps found. Allows for easy extension.
  105. * @param array $bootstraps Array containing the bootstraps to instantiate
  106. */
  107. protected function bootstrapBootstraps($bootstraps)
  108. {
  109. $bootstrap = $this->getBootstrap();
  110. $out = new ArrayObject(array(), ArrayObject::ARRAY_AS_PROPS);
  111. foreach($bootstraps as $module => $bootstrapClass) {
  112. $moduleBootstrap = new $bootstrapClass($bootstrap);
  113. $moduleBootstrap->bootstrap();
  114. $out[$module] = $moduleBootstrap;
  115. }
  116. return $out;
  117. }
  118. /**
  119. * Get bootstraps that have been run
  120. *
  121. * @return ArrayObject
  122. */
  123. public function getExecutedBootstraps()
  124. {
  125. return $this->_bootstraps;
  126. }
  127. /**
  128. * Format a module name to the module class prefix
  129. *
  130. * @param string $name
  131. * @return string
  132. */
  133. protected function _formatModuleName($name)
  134. {
  135. $name = strtolower($name);
  136. $name = str_replace(array('-', '.'), ' ', $name);
  137. $name = ucwords($name);
  138. $name = str_replace(' ', '', $name);
  139. return $name;
  140. }
  141. }