Source of file Application.php
Size: 10,496 Bytes - Last Modified: 2017-11-08T13:54:24+00:00
/home/travis/build/bluzphp/framework/src/Application/Application.php
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462 | <?php /** * Bluz Framework Component * * @copyright Bluz PHP Team * @link https://github.com/bluzphp/framework */ declare(strict_types=1); namespace Bluz\Application; use Bluz\Application\Exception\ApplicationException; use Bluz\Application\Exception\ForbiddenException; use Bluz\Application\Exception\NotAcceptableException; use Bluz\Application\Exception\NotAllowedException; use Bluz\Application\Exception\RedirectException; use Bluz\Common; use Bluz\Common\Exception\CommonException; use Bluz\Common\Exception\ComponentException; use Bluz\Controller\Controller; use Bluz\Controller\ControllerException; use Bluz\Proxy\Config; use Bluz\Proxy\Layout; use Bluz\Proxy\Logger; use Bluz\Proxy\Messages; use Bluz\Proxy\Request; use Bluz\Proxy\Response; use Bluz\Proxy\Router; use Bluz\Proxy\Session; use Bluz\Proxy\Translator; use Bluz\Request\RequestFactory; use Bluz\Response\Response as ResponseInstance; use Zend\Diactoros\ServerRequest; /** * Application * * @package Bluz\Application * @link https://github.com/bluzphp/framework/wiki/Application * @author Anton Shevchuk * @created 06.07.11 16:25 * * @method Controller error(\Exception $exception) * @method mixed forbidden(ForbiddenException $exception) * @method null redirect(string $url) */ class Application { use Common\Helper; use Common\Singleton; /** * @var string Environment name */ protected $environment = 'production'; /** * @var string Application path */ protected $path; /** * @var bool Debug application flag */ protected $debugFlag = false; /** * @var bool Layout usage flag */ protected $layoutFlag = true; /** * Get application environment * * @return string */ public function getEnvironment() : string { return $this->environment; } /** * Get path to Application * * @return string */ public function getPath() : string { if (!$this->path) { if (defined('PATH_APPLICATION')) { $this->path = PATH_APPLICATION; } else { $reflection = new \ReflectionClass($this); // 3 level up $this->path = dirname($reflection->getFileName(), 3); } } return $this->path; } /** * Return Debug flag * * @return bool */ public function isDebug() : bool { return $this->debugFlag; } /** * Return/setup Layout Flag * * @param bool|null $flag * * @return bool */ public function useLayout($flag = null) : bool { if (is_bool($flag)) { $this->layoutFlag = $flag; } return $this->layoutFlag; } /** * Initialize system packages * * @param string $environment * * @throws ApplicationException * @return void */ public function init($environment = 'production') { $this->environment = $environment; try { // initial default helper path $this->addHelperPath(__DIR__ . '/Helper/'); // init Config $this->initConfig(); // first log message Logger::info('app:init'); // init Session, start inside class (if needed) Session::getInstance(); // init Messages Messages::getInstance(); // init Translator Translator::getInstance(); // init Request $this->initRequest(); // init Response $this->initResponse(); // init Router $this->initRouter(); } catch (\Exception $e) { throw new ApplicationException("Application can't be loaded: " . $e->getMessage()); } } /** * Initial Request instance * * @return void */ protected function initConfig() { Config::getInstance(); // setup configuration for current environment if ($debug = Config::getData('debug')) { $this->debugFlag = (bool)$debug; } // initial php settings if ($ini = Config::getData('php')) { foreach ($ini as $key => $value) { $result = ini_set($key, $value); Logger::info('app:init:php:' . $key . ':' . ($result ?: '---')); } } } /** * Initial Request instance * * @return void * @throws \InvalidArgumentException */ protected function initRequest() { $request = RequestFactory::fromGlobals(); Request::setInstance($request); } /** * Initial Response instance * * @return void */ protected function initResponse() { $response = new ResponseInstance(); Response::setInstance($response); } /** * Initial Router instance * * @return void */ protected function initRouter() { $router = new \Bluz\Router\Router(); $router->setOptions(Config::getData('router')); Router::setInstance($router); } /** * Get Response instance * * @return \Bluz\Response\Response */ public function getResponse() : ResponseInstance { return Response::getInstance(); } /** * Get Request instance * * @return \Zend\Diactoros\ServerRequest */ public function getRequest() : ServerRequest { return Request::getInstance(); } /** * Run application * * @return void * @throws ApplicationException */ public function run() { $this->process(); $this->render(); $this->end(); } /** * Process application * * Note: * - Why you don't use "X-" prefix for custom headers? * - Because it deprecated ({@link http://tools.ietf.org/html/rfc6648}) * * @return void * @throws ApplicationException */ public function process() { $this->preProcess(); $this->doProcess(); $this->postProcess(); } /** * Extension point: pre process * * - Router processing * - Analyse request headers * * @return void * @throws ApplicationException */ protected function preProcess() { Router::process(); // disable Layout for XmlHttpRequests if (Request::isXmlHttpRequest()) { $this->layoutFlag = false; } // switch to JSON response based on Accept header if (Request::checkAccept([Request::TYPE_HTML, Request::TYPE_JSON]) === Request::TYPE_JSON) { $this->layoutFlag = false; Response::setType('JSON'); } } /** * Do process * * - Dispatch controller * - Exceptions handling * - Setup layout * - Setup response body * * @return void */ protected function doProcess() { $module = Request::getModule(); $controller = Request::getController(); $params = Request::getParams(); // try to dispatch controller try { // dispatch controller $result = $this->dispatch($module, $controller, $params); } catch (ForbiddenException $e) { $result = $this->forbidden($e); } catch (RedirectException $e) { // redirect to URL $result = $this->redirect($e->getUrl()); } catch (\Exception $e) { $result = $this->error($e); } // setup layout, if needed if ($this->useLayout()) { // render view to layout // needed for headScript and headStyle helpers Layout::setContent($result->render()); Response::setBody(Layout::getInstance()); } else { Response::setBody($result); } } /** * Extension point: post process * * @return void */ protected function postProcess() { // nothing } /** * Dispatch controller with params * * Call dispatch from any \Bluz\Package * Application::getInstance()->dispatch($module, $controller, array $params); * * @param string $module * @param string $controller * @param array $params * * @return Controller * @throws ComponentException * @throws CommonException * @throws ControllerException * @throws ForbiddenException * @throws NotAcceptableException * @throws NotAllowedException */ public function dispatch($module, $controller, array $params = []) { $instance = new Controller($module, $controller, $params); Logger::info("app:dispatch:>>>: $module/$controller"); $this->preDispatch($instance); Logger::info("app:dispatch:===: $module/$controller"); $this->doDispatch($instance); Logger::info("app:dispatch:<<<: $module/$controller"); $this->postDispatch($instance); return $instance; } /** * Extension point: pre dispatch * * @param Controller $controller * * @return void * @throws ComponentException * @throws ForbiddenException * @throws NotAcceptableException * @throws NotAllowedException */ protected function preDispatch($controller) { // check HTTP method $controller->checkHttpMethod(); // check ACL privileges $controller->checkPrivilege(); // check HTTP Accept header $controller->checkHttpAccept(); } /** * Do dispatch * * @param Controller $controller * * @return void * @throws ComponentException * @throws ControllerException */ protected function doDispatch($controller) { // run controller $controller->run(); } /** * Extension point: post dispatch * * @param Controller $controller * * @return void */ protected function postDispatch($controller) { // nothing by default } /** * Render, is send Response * * @return void */ public function render() { Response::send(); } /** * Extension point: finally method * * @return void */ public function end() { // nothing } } |