Modules.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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-2008 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-2008 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. foreach (array_keys($modules) as $module) {
  62. if ($module === $default) {
  63. continue;
  64. }
  65. $bootstrapClass = $this->_formatModuleName($module) . '_Bootstrap';
  66. if (!class_exists($bootstrapClass, false)) {
  67. $bootstrapPath = $front->getModuleDirectory($module) . '/Bootstrap.php';
  68. if (file_exists($bootstrapPath)) {
  69. include_once $bootstrapPath;
  70. if (!class_exists($bootstrapClass, false)) {
  71. throw new Zend_Application_Resource_Exception('Bootstrap file found for module "' . $module . '" but bootstrap class "' . $bootstrapClass . '" not found');
  72. }
  73. } else {
  74. continue;
  75. }
  76. }
  77. $moduleBootstrap = new $bootstrapClass($bootstrap);
  78. $moduleBootstrap->bootstrap();
  79. $this->_bootstraps[$module] = $moduleBootstrap;
  80. }
  81. return $this->_bootstraps;
  82. }
  83. /**
  84. * Get bootstraps that have been run
  85. *
  86. * @return ArrayObject
  87. */
  88. public function getExecutedBootstraps()
  89. {
  90. return $this->_bootstraps;
  91. }
  92. /**
  93. * Format a module name to the module class prefix
  94. *
  95. * @param string $name
  96. * @return string
  97. */
  98. protected function _formatModuleName($name)
  99. {
  100. $name = strtolower($name);
  101. $name = str_replace(array('-', '.'), ' ', $name);
  102. $name = ucwords($name);
  103. $name = str_replace(' ', '', $name);
  104. return $name;
  105. }
  106. }