Modules.php 4.3 KB

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