diff options
author | Frederic Guillot <fred@kanboard.net> | 2016-05-15 18:31:47 -0400 |
---|---|---|
committer | Frederic Guillot <fred@kanboard.net> | 2016-05-15 18:31:47 -0400 |
commit | 67b836164997527b91452b19adbcb8aa3c5decf1 (patch) | |
tree | b5876d311912e97b0592c7e208639f7b52813a75 /app/Core/Controller | |
parent | 108e867605dbc7ece4cbcbecc89a674e9c154a9b (diff) |
Refactoring: added controlled middleware and changed response class
Diffstat (limited to 'app/Core/Controller')
-rw-r--r-- | app/Core/Controller/AccessForbiddenException.php | 14 | ||||
-rw-r--r-- | app/Core/Controller/BaseException.php | 52 | ||||
-rw-r--r-- | app/Core/Controller/BaseMiddleware.php | 58 | ||||
-rw-r--r-- | app/Core/Controller/PageNotFoundException.php | 14 | ||||
-rw-r--r-- | app/Core/Controller/Runner.php | 102 |
5 files changed, 240 insertions, 0 deletions
diff --git a/app/Core/Controller/AccessForbiddenException.php b/app/Core/Controller/AccessForbiddenException.php new file mode 100644 index 00000000..b5dccb78 --- /dev/null +++ b/app/Core/Controller/AccessForbiddenException.php @@ -0,0 +1,14 @@ +<?php + +namespace Kanboard\Core\Controller; + +/** + * Class AccessForbiddenException + * + * @package Kanboard\Core\Controller + * @author Frederic Guillot + */ +class AccessForbiddenException extends BaseException +{ + +} diff --git a/app/Core/Controller/BaseException.php b/app/Core/Controller/BaseException.php new file mode 100644 index 00000000..13836d2c --- /dev/null +++ b/app/Core/Controller/BaseException.php @@ -0,0 +1,52 @@ +<?php + +namespace Kanboard\Core\Controller; + +use Exception; + +/** + * Class AccessForbiddenException + * + * @package Kanboard\Core\Controller + * @author Frederic Guillot + */ +class BaseException extends Exception +{ + protected $withoutLayout = false; + + /** + * Get object instance + * + * @static + * @access public + * @param string $message + * @return static + */ + public static function getInstance($message = '') + { + return new static($message); + } + + /** + * There is no layout + * + * @access public + * @return BaseException + */ + public function withoutLayout() + { + $this->withoutLayout = true; + return $this; + } + + /** + * Return true if no layout + * + * @access public + * @return boolean + */ + public function hasLayout() + { + return $this->withoutLayout; + } +} diff --git a/app/Core/Controller/BaseMiddleware.php b/app/Core/Controller/BaseMiddleware.php new file mode 100644 index 00000000..f2862d13 --- /dev/null +++ b/app/Core/Controller/BaseMiddleware.php @@ -0,0 +1,58 @@ +<?php + +namespace Kanboard\Core\Controller; + +use Kanboard\Core\Base; + +/** + * Class BaseMiddleware + * + * @package Kanboard\Core\Controller + * @author Frederic Guillot + */ +abstract class BaseMiddleware extends Base +{ + /** + * @var BaseMiddleware + */ + protected $nextMiddleware = null; + + /** + * Execute middleware + */ + abstract public function execute(); + + /** + * Set next middleware + * + * @param BaseMiddleware $nextMiddleware + * @return BaseMiddleware + */ + public function setNextMiddleware($nextMiddleware) + { + $this->nextMiddleware = $nextMiddleware; + return $this; + } + + /** + * @return BaseMiddleware + */ + public function getNextMiddleware() + { + return $this->nextMiddleware; + } + + /** + * Move to next middleware + */ + public function next() + { + if ($this->nextMiddleware !== null) { + if (DEBUG) { + $this->logger->debug(__METHOD__.' => ' . get_class($this->nextMiddleware)); + } + + $this->nextMiddleware->execute(); + } + } +} diff --git a/app/Core/Controller/PageNotFoundException.php b/app/Core/Controller/PageNotFoundException.php new file mode 100644 index 00000000..e96a2057 --- /dev/null +++ b/app/Core/Controller/PageNotFoundException.php @@ -0,0 +1,14 @@ +<?php + +namespace Kanboard\Core\Controller; + +/** + * Class PageNotFoundException + * + * @package Kanboard\Core\Controller + * @author Frederic Guillot + */ +class PageNotFoundException extends BaseException +{ + +} diff --git a/app/Core/Controller/Runner.php b/app/Core/Controller/Runner.php new file mode 100644 index 00000000..b973c098 --- /dev/null +++ b/app/Core/Controller/Runner.php @@ -0,0 +1,102 @@ +<?php + +namespace Kanboard\Core\Controller; + +use Kanboard\Controller\AppController; +use Kanboard\Core\Base; +use Kanboard\Middleware\ApplicationAuthorizationMiddleware; +use Kanboard\Middleware\AuthenticationMiddleware; +use Kanboard\Middleware\BootstrapMiddleware; +use Kanboard\Middleware\PostAuthenticationMiddleware; +use Kanboard\Middleware\ProjectAuthorizationMiddleware; +use RuntimeException; + +/** + * Class Runner + * + * @package Kanboard\Core\Controller + * @author Frederic Guillot + */ +class Runner extends Base +{ + /** + * Execute middleware and controller + */ + public function execute() + { + try { + $this->executeMiddleware(); + $this->executeController(); + } catch (PageNotFoundException $e) { + $controllerObject = new AppController($this->container); + $controllerObject->notFound($e->hasLayout()); + } catch (AccessForbiddenException $e) { + $controllerObject = new AppController($this->container); + $controllerObject->accessForbidden($e->hasLayout()); + } + } + + /** + * Execute all middleware + */ + protected function executeMiddleware() + { + if (DEBUG) { + $this->logger->debug(__METHOD__); + } + + $bootstrapMiddleware = new BootstrapMiddleware($this->container); + $authenticationMiddleware = new AuthenticationMiddleware($this->container); + $postAuthenticationMiddleware = new PostAuthenticationMiddleware($this->container); + $appAuthorizationMiddleware = new ApplicationAuthorizationMiddleware($this->container); + $projectAuthorizationMiddleware = new ProjectAuthorizationMiddleware($this->container); + + $bootstrapMiddleware->setNextMiddleware($authenticationMiddleware); + $authenticationMiddleware->setNextMiddleware($postAuthenticationMiddleware); + $postAuthenticationMiddleware->setNextMiddleware($appAuthorizationMiddleware); + $appAuthorizationMiddleware->setNextMiddleware($projectAuthorizationMiddleware); + + $bootstrapMiddleware->execute(); + } + + /** + * Execute the controller + */ + protected function executeController() + { + $className = $this->getControllerClassName(); + + if (DEBUG) { + $this->logger->debug(__METHOD__.' => '.$className.'::'.$this->router->getAction()); + } + + $controllerObject = new $className($this->container); + $controllerObject->{$this->router->getAction()}(); + } + + /** + * Get controller class name + * + * @access protected + * @return string + * @throws RuntimeException + */ + protected function getControllerClassName() + { + if ($this->router->getPlugin() !== '') { + $className = '\Kanboard\Plugin\\'.$this->router->getPlugin().'\Controller\\'.$this->router->getController(); + } else { + $className = '\Kanboard\Controller\\'.$this->router->getController(); + } + + if (! class_exists($className)) { + throw new RuntimeException('Controller not found'); + } + + if (! method_exists($className, $this->router->getAction())) { + throw new RuntimeException('Action not implemented'); + } + + return $className; + } +} |