123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239 |
- <?php
- namespace Cube\Application;
- use Cube\Application,
- Cube\Controller\Front as FrontController;
- class Bootstrap
- {
-
- const RESOURCES_NAMESPACE = 'Resource';
-
- protected $_application;
-
- protected $_resources = array();
-
- final public function __construct()
- {
- $this->setResource('FrontController', FrontController::getInstance()
- ->setOptions(
- $this->getApplication()->getOptions()));
- }
-
- public function getApplication()
- {
- if (!($this->_application instanceof Application)) {
- $this->_application = Application::getInstance();
- }
- return $this->_application;
- }
-
- public function setResource($name, $resource)
- {
- if ($this->hasResource($name) === false) {
- $this->_resources[$name] = $resource;
- }
- return $this;
- }
-
- public function getResource($name)
- {
- if ($this->hasResource($name) === true) {
- return $this->_resources[$name];
- }
- return false;
- }
-
- public function removeResource($name)
- {
- if ($this->hasResource($name) === true) {
- unset($this->_resources[$name]);
- }
- return $this;
- }
-
- public function hasResource($name)
- {
- if (array_key_exists($name, $this->_resources)) {
- return true;
- }
- return false;
- }
-
- protected function _bootstrap($resource)
- {
- if (!$this->hasResource($resource)) {
- $methodName = '_init' . ucfirst($resource);
- $resourceName = '\\' . __NAMESPACE__ . '\\' . self::RESOURCES_NAMESPACE . '\\' . ucfirst($resource);
- if (class_exists($resourceName)) {
- $class = new $resourceName();
- if ($class instanceof Resource\ResourceInterface) {
- $options = $this->getApplication()->getOptions();
- $object = $class->setOptions($options)
- ->init();
- $this->setResource($resource, $object);
- }
- else {
- throw new \DomainException(sprintf("'%s' must be implement the ResourceInterface interface.",
- $resourceName));
- }
- }
- else if (method_exists($this, $methodName)) {
- $result = $this->$methodName();
- $this->setResource($resource, $result);
- }
- }
- }
-
- final public function bootstrap($resource = null)
- {
- if (is_string($resource)) {
- $this->_bootstrap($resource);
- }
- else if (is_array($resource)) {
- foreach ($resource as $res) {
- $this->_bootstrap($res);
- }
- }
- else {
- $options = $this->getApplication()->getOptions();
-
- foreach ((array)$options as $name => $config) {
- $this->_bootstrap($name);
- }
-
- foreach ((array)get_class_methods($this) as $method) {
- if (strpos($method, '_init') === 0) {
- $this->_bootstrap(lcfirst(
- str_replace('_init', '', $method)));
- }
- }
- }
- return $this;
- }
-
- public function run()
- {
- $front = $this->getResource('FrontController');
- return $front->dispatch();
- }
- }
|