/library/Zend/Application/Resource/Modules.php

https://github.com/grjones/qframe · PHP · 138 lines · 67 code · 11 blank · 60 comment · 11 complexity · 858266e728c91eb686cc0a5ac9b05fab MD5 · raw file

  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 20816 2010-02-01 21:13:54Z freak $
  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-2010 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. $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(sprintf(
  77. $eMsgTpl, $module, $bootstrapClass
  78. ));
  79. } elseif ($default == $module) {
  80. if (!class_exists($bootstrapClass, false)) {
  81. $bootstrapClass = 'Bootstrap';
  82. if (!class_exists($bootstrapClass, false)) {
  83. throw new Zend_Application_Resource_Exception(sprintf(
  84. $eMsgTpl, $module, $bootstrapClass
  85. ));
  86. }
  87. }
  88. }
  89. } else {
  90. continue;
  91. }
  92. }
  93. if ($bootstrapClass == $curBootstrapClass) {
  94. // If the found bootstrap class matches the one calling this
  95. // resource, don't re-execute.
  96. continue;
  97. }
  98. $moduleBootstrap = new $bootstrapClass($bootstrap);
  99. $moduleBootstrap->bootstrap();
  100. $this->_bootstraps[$module] = $moduleBootstrap;
  101. }
  102. return $this->_bootstraps;
  103. }
  104. /**
  105. * Get bootstraps that have been run
  106. *
  107. * @return ArrayObject
  108. */
  109. public function getExecutedBootstraps()
  110. {
  111. return $this->_bootstraps;
  112. }
  113. /**
  114. * Format a module name to the module class prefix
  115. *
  116. * @param string $name
  117. * @return string
  118. */
  119. protected function _formatModuleName($name)
  120. {
  121. $name = strtolower($name);
  122. $name = str_replace(array('-', '.'), ' ', $name);
  123. $name = ucwords($name);
  124. $name = str_replace(' ', '', $name);
  125. return $name;
  126. }
  127. }