| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440 | 
							- <?php
 
- /**
 
-  *
 
-  * Cube Framework $Id$ dV48TyHotkxfH/69Qj9xzd7RdkGKdkqq8gzk841Pho8=
 
-  *
 
-  * @link        http://codecu.be/framework
 
-  * @copyright   Copyright (c) 2015 CodeCube SRL
 
-  * @license     http://codecu.be/framework/license Commercial License
 
-  *
 
-  * @version     1.6
 
-  */
 
- /**
 
-  * front controller implementation
 
-  */
 
- namespace Cube\Controller;
 
- use Cube\Application,
 
-     Cube\ModuleManager,
 
-     Cube\Controller\Request,
 
-     Cube\Controller\Request\AbstractRequest,
 
-     Cube\Controller\Plugin\AbstractPlugin,
 
-     Cube\Controller\Response\ResponseInterface,
 
-     Cube\Http\Response,
 
-     Cube\Controller\Router,
 
-     Cube\Controller\Dispatcher,
 
-     Cube\Controller\Dispatcher\DispatcherInterface,
 
-     Cube\Debug,
 
-     Cube\Exception\Dispatcher as DispatcherException;
 
- class Front
 
- {
 
-     const ERROR_CONTROLLER = 'error';
 
-     const ERROR_ACTION = 'not-found';
 
-     /**
 
-      *
 
-      * will hold an array of front controller plugin objects
 
-      *
 
-      * @var array
 
-      */
 
-     protected $_plugins;
 
-     /**
 
-      *
 
-      * request object
 
-      *
 
-      * @var \Cube\Controller\Request\AbstractRequest
 
-      */
 
-     protected $_request;
 
-     /**
 
-      *
 
-      * response object
 
-      *
 
-      * @var \Cube\Http\Response
 
-      */
 
-     protected $_response;
 
-     /**
 
-      *
 
-      * router object
 
-      *
 
-      * @var \Cube\Controller\Router
 
-      */
 
-     protected $_router;
 
-     /**
 
-      *
 
-      * dispatcher object
 
-      *
 
-      * @var \Cube\Controller\Dispatcher
 
-      */
 
-     protected $_dispatcher;
 
-     /**
 
-      *
 
-      * holds an instance of the object
 
-      *
 
-      * @var \Cube\Controller\Front
 
-      */
 
-     protected static $_instance;
 
-     /**
 
-      *
 
-      * application config options
 
-      *
 
-      * @var array
 
-      */
 
-     protected $_options;
 
-     /**
 
-      *
 
-      * bootstrap object, in order to be available in the application
 
-      *
 
-      * @var \Cube\Application\Bootstrap
 
-      */
 
-     protected $_bootstrap;
 
-     /**
 
-      *
 
-      * class constructor
 
-      */
 
-     protected function __construct()
 
-     {
 
-         $this->_plugins = new Plugin\Broker();
 
-     }
 
-     /**
 
-      *
 
-      * returns an instance of the object and creates it if it wasnt instantiated yet
 
-      *
 
-      * @return \Cube\Controller\Front
 
-      */
 
-     public static function getInstance()
 
-     {
 
-         if (!self::$_instance instanceof self) {
 
-             self::$_instance = new self();
 
-         }
 
-         return self::$_instance;
 
-     }
 
-     /**
 
-      *
 
-      * register plugin
 
-      *
 
-      * @param \Cube\Controller\Plugin\AbstractPlugin $plugin
 
-      *
 
-      * @return \Cube\Controller\Front
 
-      */
 
-     public function registerPlugin(AbstractPlugin $plugin)
 
-     {
 
-         $this->_plugins->registerPlugin($plugin);
 
-         return $this;
 
-     }
 
-     /**
 
-      *
 
-      * check if the plugin has been registered
 
-      *
 
-      * @param string|AbstractPlugin $plugin
 
-      *
 
-      * @return bool
 
-      */
 
-     public function isRegisteredPlugin($plugin)
 
-     {
 
-         return $this->_plugins->isRegisteredPlugin($plugin);
 
-     }
 
-     /**
 
-      *
 
-      * get the request object, if not set create a new object of type \Cube\Controller\Request
 
-      *
 
-      * @return \Cube\Controller\Request\AbstractRequest
 
-      */
 
-     public function getRequest()
 
-     {
 
-         if (!$this->_request instanceof AbstractRequest) {
 
-             // initialize a routed request
 
-             $this->setRequest(
 
-                 new Request());
 
-         }
 
-         return $this->_request;
 
-     }
 
-     /**
 
-      *
 
-      * set the request object
 
-      *
 
-      * @param \Cube\Controller\Request\AbstractRequest $request
 
-      *
 
-      * @return \Cube\Controller\Front
 
-      */
 
-     public function setRequest(AbstractRequest $request)
 
-     {
 
-         $this->_request = $request;
 
-         return $this;
 
-     }
 
-     /**
 
-      *
 
-      * get the response object, if not set create a new object of type \Cube\Controller\Response
 
-      *
 
-      * @return \Cube\Controller\Response\ResponseInterface
 
-      */
 
-     public function getResponse()
 
-     {
 
-         if (!($this->_response instanceof ResponseInterface)) {
 
-             $this->setResponse(
 
-                 new Response());
 
-         }
 
-         return $this->_response;
 
-     }
 
-     /**
 
-      *
 
-      * set response object
 
-      *
 
-      * @param \Cube\Controller\Response\ResponseInterface $response
 
-      *
 
-      * @return \Cube\Controller\Front
 
-      */
 
-     public function setResponse(ResponseInterface $response)
 
-     {
 
-         $this->_response = $response;
 
-         return $this;
 
-     }
 
-     /**
 
-      *
 
-      * get router object, if not set create a new object of type \Cube\Controller\Router
 
-      *
 
-      * @return \Cube\Controller\Router
 
-      */
 
-     public function getRouter()
 
-     {
 
-         if (!($this->_router instanceof Router\AbstractRouter)) {
 
-             // add routes from module manager singleton
 
-             $routes = ModuleManager::getInstance()->getRoutes();
 
-             $this->setRouter(
 
-                 new Router($routes));
 
-         }
 
-         return $this->_router;
 
-     }
 
-     /**
 
-      *
 
-      * set router object
 
-      *
 
-      * @param \Cube\Controller\Router\AbstractRouter $router
 
-      *
 
-      * @return \Cube\Controller\Front
 
-      */
 
-     public function setRouter(Router\AbstractRouter $router)
 
-     {
 
-         $this->_router = $router;
 
-         return $this;
 
-     }
 
-     /**
 
-      *
 
-      * get dispatcher object, if not set create a new object of type \Cube\Controller\Dispatcher
 
-      *
 
-      * @return \Cube\Controller\Dispatcher\DispatcherInterface
 
-      */
 
-     public function getDispatcher()
 
-     {
 
-         if (!($this->_dispatcher instanceof DispatcherInterface)) {
 
-             $this->setDispatcher(
 
-                 new Dispatcher());
 
-         }
 
-         return $this->_dispatcher;
 
-     }
 
-     /**
 
-      *
 
-      * set dispatcher object
 
-      *
 
-      * @param \Cube\Controller\Dispatcher\DispatcherInterface $dispatcher
 
-      *
 
-      * @return \Cube\Controller\Front
 
-      */
 
-     public function setDispatcher(DispatcherInterface $dispatcher)
 
-     {
 
-         $this->_dispatcher = $dispatcher;
 
-         return $this;
 
-     }
 
-     /**
 
-      *
 
-      * get options array
 
-      *
 
-      * @return array
 
-      */
 
-     public function getOptions()
 
-     {
 
-         return $this->_options;
 
-     }
 
-     /**
 
-      *
 
-      * set options array
 
-      *
 
-      * @param array $options
 
-      *
 
-      * @return \Cube\Controller\Front
 
-      */
 
-     public function setOptions(array $options)
 
-     {
 
-         if (is_array($this->_options)) {
 
-             $this->_options = array_merge($this->_options, $options);
 
-         }
 
-         else {
 
-             $this->_options = $options;
 
-         }
 
-         return $this;
 
-     }
 
-     /**
 
-      *
 
-      * get a key from the options array
 
-      *
 
-      * @param string $key
 
-      *
 
-      * @return mixed|null
 
-      */
 
-     public function getOption($key)
 
-     {
 
-         if (isset($this->_options[$key])) {
 
-             return $this->_options[$key];
 
-         }
 
-         return null;
 
-     }
 
-     /**
 
-      *
 
-      * set or unset a key in the options array
 
-      *
 
-      * @param string     $key
 
-      * @param mixed|null $value
 
-      *
 
-      * @return \Cube\Controller\Front
 
-      */
 
-     public function setOption($key, $value = null)
 
-     {
 
-         if ($value === null && isset($this->_options[$key])) {
 
-             unset($this->_options[$key]);
 
-         }
 
-         else {
 
-             $this->_options[$key] = $value;
 
-         }
 
-         return $this;
 
-     }
 
-     /**
 
-      *
 
-      * get the bootstrap object
 
-      *
 
-      * @return \Cube\Application\Bootstrap
 
-      */
 
-     public function getBootstrap()
 
-     {
 
-         return Application::getInstance()->getBootstrap();
 
-     }
 
-     /**
 
-      *
 
-      * dispatch request
 
-      *
 
-      * @param \Cube\Controller\Request\AbstractRequest    $request
 
-      * @param \Cube\Controller\Response\ResponseInterface $response
 
-      */
 
-     public function dispatch(AbstractRequest $request = null, ResponseInterface $response = null)
 
-     {
 
-         if ($request instanceof AbstractRequest) {
 
-             $this->setRequest($request);
 
-         }
 
-         if ($response instanceof ResponseInterface) {
 
-             $this->setResponse($response);
 
-         }
 
-         $request = $this->getRequest();
 
-         $response = $this->getResponse();
 
-         // set request 
 
-         $this->_plugins->setRequest($request)
 
-             ->setResponse($response);
 
-         $router = $this->getRouter();
 
-         // run pre route plugins
 
-         $this->_plugins->preRoute();
 
-         // route request
 
-         $this->_plugins->setRequest(
 
-             $router->route($request));
 
-         // run post route plugins
 
-         $this->_plugins->postRoute();
 
-         // run pre dispatcher plugins
 
-         $this->_plugins->preDispatcher();
 
-         do {
 
-             // set dispatched flag to true
 
-             $request->setDispatched(true);
 
-             // run pre dispatch plugins
 
-             $this->_plugins->preDispatch();
 
-             try {
 
-                 $response = $this->getDispatcher()->dispatch($request, $response);
 
-             } catch (DispatcherException $e) {
 
-                 $request->setController(self::ERROR_CONTROLLER)
 
-                     ->setAction(self::ERROR_ACTION);
 
-                 $request->setDispatched(false);
 
-             }
 
-             // dispatch request
 
-             $this->setResponse(
 
-                 $response);
 
-             // run post dispatch plugins
 
-             $this->_plugins->postDispatch();
 
-         } while ($request->isDispatched() !== true);
 
-         // run post dispatcher plugins
 
-         $this->_plugins->postDispatcher();
 
-         $this->setRequest($request)
 
-             ->setResponse($response);
 
-         Debug::setMemoryEnd();
 
-         Debug::setTimeEnd();
 
-         Debug::setCpuUsageEnd();
 
-         $this->getResponse()->send();
 
-     }
 
- }
 
 
  |