123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246 |
- <?php
- namespace Cube;
- use Cube\Loader\Autoloader;
- class Application
- {
-
- const BOOTSTRAP = 'Bootstrap';
-
- protected $_options;
-
- protected $_autoloader;
-
- protected $_moduleManager;
-
- protected $_bootstrap;
-
- private static $_instance;
-
- protected function __construct($options = array())
- {
- require_once 'Debug.php';
- Debug::setTimeStart();
- Debug::setMemoryStart();
- Debug::setCpuUsageStart();
- require_once 'Loader/Autoloader.php';
- $this->_autoloader = Autoloader::getInstance()
- ->register();
- $this->setOptions($options);
- }
-
- public static function init($options = array())
- {
- if (!self::$_instance instanceof self) {
- self::$_instance = new self($options);
- }
- return self::$_instance;
- }
-
- public static function getInstance()
- {
- return self::$_instance;
- }
-
- public function getOptions()
- {
- return $this->_options;
- }
-
- public function setOptions(array $options)
- {
- if (!empty($this->_options)) {
- $this->_options = array_replace_recursive(
- array_merge_recursive($this->_options, $options), $options);
- }
- else {
- $this->_options = $options;
- }
- return $this;
- }
-
- public function getOption($key)
- {
- if (isset($this->_options[$key])) {
- return $this->_options[$key];
- }
- return null;
- }
-
- public function setOption($key, $value = null)
- {
- $this->_options[$key] = $value;
- return $this;
- }
-
- public function getBootstrap()
- {
- if (!($this->_bootstrap instanceof Application\Bootstrap)) {
- $bootstrap = $this->_moduleManager->getActiveModule() . '\\' . self::BOOTSTRAP;
- if (class_exists($bootstrap)) {
- $this->_bootstrap = new $bootstrap($this);
- }
- if ($this->_bootstrap === null) {
- $this->_bootstrap = new Application\Bootstrap($this);
- }
- }
- return $this->_bootstrap;
- }
-
- public function bootstrap()
- {
- if (!empty($this->_options['modules'])) {
- $this->_moduleManager = ModuleManager::getInstance()
- ->setModules($this->_options['modules']);
- $this->setOptions(
- $this->_moduleManager->getConfig(
- $this->_moduleManager->getActiveModule()));
- $this->_autoloader->addPaths(
- $this->_moduleManager->getPaths());
- }
- else {
- throw new \InvalidArgumentException('At least one module needs to be created in order for the application to function.');
- }
- $this->getBootstrap()->bootstrap();
- return $this;
- }
-
- public function run()
- {
-
- $this->getBootstrap()->run();
- }
- }
|