Application.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. <?php
  2. /**
  3. *
  4. * Cube Framework $Id$ +WZi6nampCuXzvKFXCrgiWwuuVA4sBB9qgNtaNbX24c=
  5. *
  6. * @link http://codecu.be/framework
  7. * @copyright Copyright (c) 2015 CodeCube SRL
  8. * @license http://codecu.be/framework/license Commercial License
  9. *
  10. * @version 1.6
  11. */
  12. /**
  13. * application class
  14. */
  15. namespace Cube;
  16. use Cube\Loader\Autoloader;
  17. class Application
  18. {
  19. /**
  20. *
  21. * the name of the module bootstrap files
  22. */
  23. const BOOTSTRAP = 'Bootstrap';
  24. /**
  25. *
  26. * holds the configuration options array
  27. *
  28. * @var array
  29. */
  30. protected $_options;
  31. /**
  32. * autoloader object
  33. *
  34. * @var \Cube\Loader\Autoloader
  35. */
  36. protected $_autoloader;
  37. /**
  38. *
  39. * requested module name
  40. *
  41. * @var \Cube\ModuleManager
  42. */
  43. protected $_moduleManager;
  44. /**
  45. *
  46. * bootstrap
  47. *
  48. * @var \Cube\Application\Bootstrap
  49. */
  50. protected $_bootstrap;
  51. /**
  52. *
  53. * returns an instance of the object and creates it if it wasnt instantiated yet
  54. *
  55. * @return \Cube\Application
  56. */
  57. private static $_instance;
  58. /**
  59. *
  60. * class constructor
  61. *
  62. * initialize autoloader
  63. *
  64. * @param array $options configuration array
  65. */
  66. protected function __construct($options = array())
  67. {
  68. require_once 'Debug.php';
  69. Debug::setTimeStart();
  70. Debug::setMemoryStart();
  71. Debug::setCpuUsageStart();
  72. require_once 'Loader/Autoloader.php';
  73. $this->_autoloader = Autoloader::getInstance()
  74. ->register();
  75. $this->setOptions($options);
  76. }
  77. /**
  78. *
  79. * initialize application as singleton
  80. *
  81. * @param array $options configuration array
  82. * @return \Cube\Application
  83. */
  84. public static function init($options = array())
  85. {
  86. if (!self::$_instance instanceof self) {
  87. self::$_instance = new self($options);
  88. }
  89. return self::$_instance;
  90. }
  91. /**
  92. *
  93. * returns an instance of the application object
  94. *
  95. * @return \Cube\Application
  96. */
  97. public static function getInstance()
  98. {
  99. return self::$_instance;
  100. }
  101. /**
  102. *
  103. * get options array
  104. *
  105. * @return array
  106. */
  107. public function getOptions()
  108. {
  109. return $this->_options;
  110. }
  111. /**
  112. *
  113. * set options array
  114. *
  115. * @param array $options
  116. * @return \Cube\Application
  117. */
  118. public function setOptions(array $options)
  119. {
  120. if (!empty($this->_options)) {
  121. $this->_options = array_replace_recursive(
  122. array_merge_recursive($this->_options, $options), $options);
  123. }
  124. else {
  125. $this->_options = $options;
  126. }
  127. return $this;
  128. }
  129. /**
  130. *
  131. * get a key from the options array
  132. *
  133. * @param string $key
  134. * @return mixed|null
  135. */
  136. public function getOption($key)
  137. {
  138. if (isset($this->_options[$key])) {
  139. return $this->_options[$key];
  140. }
  141. return null;
  142. }
  143. /**
  144. *
  145. * set or unset a key in the options array
  146. *
  147. * @param string $key
  148. * @param mixed|null $value
  149. * @return \Cube\Application
  150. */
  151. public function setOption($key, $value = null)
  152. {
  153. $this->_options[$key] = $value;
  154. return $this;
  155. }
  156. /**
  157. * get bootstrap object
  158. * we will first search for the requested module bootstrap and only if the file doesnt exist will we call the default bootstrap class from the library
  159. * the bootstrap class will only be created once
  160. *
  161. * @return \Cube\Application\Bootstrap
  162. */
  163. public function getBootstrap()
  164. {
  165. if (!($this->_bootstrap instanceof Application\Bootstrap)) {
  166. $bootstrap = $this->_moduleManager->getActiveModule() . '\\' . self::BOOTSTRAP;
  167. if (class_exists($bootstrap)) {
  168. $this->_bootstrap = new $bootstrap($this);
  169. }
  170. if ($this->_bootstrap === null) {
  171. $this->_bootstrap = new Application\Bootstrap($this);
  172. }
  173. }
  174. return $this->_bootstrap;
  175. }
  176. /**
  177. * initialize module manager and bootstrap application
  178. *
  179. * @throws \InvalidArgumentException
  180. * @return \Cube\Application
  181. */
  182. public function bootstrap()
  183. {
  184. if (!empty($this->_options['modules'])) {
  185. $this->_moduleManager = ModuleManager::getInstance()
  186. ->setModules($this->_options['modules']);
  187. $this->setOptions(
  188. $this->_moduleManager->getConfig(
  189. $this->_moduleManager->getActiveModule()));
  190. $this->_autoloader->addPaths(
  191. $this->_moduleManager->getPaths());
  192. }
  193. else {
  194. throw new \InvalidArgumentException('At least one module needs to be created in order for the application to function.');
  195. }
  196. $this->getBootstrap()->bootstrap();
  197. return $this;
  198. }
  199. /**
  200. * Run the application
  201. *
  202. * @return void
  203. */
  204. public function run()
  205. {
  206. $this->getBootstrap()->run();
  207. }
  208. }