Bootstrap.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. <?php
  2. /**
  3. *
  4. * Cube Framework $Id$ ywBq0GD/Wfo6NkXOTpHP+JXJC2leduaTfdJqu899Nds=
  5. *
  6. * @link http://codecu.be/framework
  7. * @copyright Copyright (c) 2014 CodeCube SRL
  8. * @license http://codecu.be/framework/license Commercial License
  9. *
  10. * @version 1.0
  11. */
  12. /**
  13. * bootstrap class
  14. */
  15. namespace Cube\Application;
  16. use Cube\Application,
  17. Cube\Controller\Front as FrontController;
  18. class Bootstrap
  19. {
  20. /**
  21. * default name of resources namespace
  22. */
  23. const RESOURCES_NAMESPACE = 'Resource';
  24. /**
  25. *
  26. * application object
  27. *
  28. * @var \Cube\Application
  29. */
  30. protected $_application;
  31. /**
  32. *
  33. * resources array
  34. *
  35. * @var array
  36. */
  37. protected $_resources = array();
  38. /**
  39. *
  40. * bootstrap front controller resource
  41. * final class so it cannot be overridden by module bootstraps
  42. */
  43. final public function __construct()
  44. {
  45. $this->setResource('FrontController', FrontController::getInstance()
  46. ->setOptions(
  47. $this->getApplication()->getOptions()));
  48. }
  49. /**
  50. *
  51. * get the application object
  52. *
  53. * @return \Cube\Application
  54. */
  55. public function getApplication()
  56. {
  57. if (!($this->_application instanceof Application)) {
  58. $this->_application = Application::getInstance();
  59. }
  60. return $this->_application;
  61. }
  62. /**
  63. *
  64. * add a resource to the resources array
  65. *
  66. * @param string $name
  67. * @param mixed $resource
  68. *
  69. * @return \Cube\Application\Bootstrap
  70. */
  71. public function setResource($name, $resource)
  72. {
  73. if ($this->hasResource($name) === false) {
  74. $this->_resources[$name] = $resource;
  75. }
  76. // else {
  77. // throw new \InvalidArgumentException(sprintf("A resource with the name '%s' already exists", $name));
  78. // }
  79. return $this;
  80. }
  81. /**
  82. *
  83. * get a resource
  84. *
  85. * @param string $name
  86. *
  87. * @return mixed|bool
  88. */
  89. public function getResource($name)
  90. {
  91. if ($this->hasResource($name) === true) {
  92. return $this->_resources[$name];
  93. }
  94. return false;
  95. }
  96. /**
  97. *
  98. * remove a resource
  99. *
  100. * @param string $name
  101. *
  102. * @return $this
  103. */
  104. public function removeResource($name)
  105. {
  106. if ($this->hasResource($name) === true) {
  107. unset($this->_resources[$name]);
  108. }
  109. return $this;
  110. }
  111. /**
  112. *
  113. * check if a resource exists
  114. *
  115. * @param string $name
  116. *
  117. * @return bool
  118. */
  119. public function hasResource($name)
  120. {
  121. if (array_key_exists($name, $this->_resources)) {
  122. return true;
  123. }
  124. return false;
  125. }
  126. /**
  127. *
  128. * bootstrap a single resource
  129. * we will search for the resource either in the default resources folder or in the bootstrap of the requested module
  130. * a resource will only be bootstrapped once
  131. *
  132. * @param string $resource
  133. *
  134. * @throws \DomainException
  135. */
  136. protected function _bootstrap($resource)
  137. {
  138. if (!$this->hasResource($resource)) {
  139. $methodName = '_init' . ucfirst($resource);
  140. $resourceName = '\\' . __NAMESPACE__ . '\\' . self::RESOURCES_NAMESPACE . '\\' . ucfirst($resource);
  141. if (class_exists($resourceName)) {
  142. $class = new $resourceName();
  143. if ($class instanceof Resource\ResourceInterface) {
  144. $options = $this->getApplication()->getOptions();
  145. $object = $class->setOptions($options)
  146. ->init();
  147. $this->setResource($resource, $object);
  148. }
  149. else {
  150. throw new \DomainException(sprintf("'%s' must be implement the ResourceInterface interface.",
  151. $resourceName));
  152. }
  153. }
  154. else if (method_exists($this, $methodName)) {
  155. $result = $this->$methodName();
  156. $this->setResource($resource, $result);
  157. }
  158. }
  159. }
  160. /**
  161. *
  162. * bootstrap one, multiple (array) or all resources if no parameters are set
  163. * final method
  164. *
  165. * @param null|string|array $resource
  166. *
  167. * @return \Cube\Application\Bootstrap
  168. */
  169. final public function bootstrap($resource = null)
  170. {
  171. if (is_string($resource)) {
  172. $this->_bootstrap($resource);
  173. }
  174. else if (is_array($resource)) {
  175. foreach ($resource as $res) {
  176. $this->_bootstrap($res);
  177. }
  178. }
  179. else {
  180. $options = $this->getApplication()->getOptions();
  181. // bootstrap default application resources that are initialized in the application configuration
  182. foreach ((array)$options as $name => $config) {
  183. $this->_bootstrap($name);
  184. }
  185. // bootstrap resources defined in the active module bootstrap file
  186. foreach ((array)get_class_methods($this) as $method) {
  187. if (strpos($method, '_init') === 0) {
  188. $this->_bootstrap(lcfirst(
  189. str_replace('_init', '', $method)));
  190. }
  191. }
  192. }
  193. return $this;
  194. }
  195. /**
  196. *
  197. *
  198. * run the application
  199. */
  200. public function run()
  201. {
  202. $front = $this->getResource('FrontController');
  203. return $front->dispatch();
  204. }
  205. }