Skip to content

Commit

Permalink
Merge pull request #422 from AntonShevchuk/master
Browse files Browse the repository at this point in the history
Controller's refactoring
  • Loading branch information
Anton authored Jul 6, 2017
2 parents 6f56205 + 73e7e96 commit 1a6f490
Show file tree
Hide file tree
Showing 14 changed files with 419 additions and 266 deletions.
103 changes: 57 additions & 46 deletions src/Application/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,14 @@

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;
Expand All @@ -26,6 +31,7 @@
use Bluz\Proxy\Translator;
use Bluz\Request\RequestFactory;
use Bluz\Response\Response as ResponseInstance;
use Zend\Diactoros\ServerRequest;

/**
* Application
Expand All @@ -36,7 +42,7 @@
* @created 06.07.11 16:25
*
* @method Controller error(\Exception $exception)
* @method Controller forbidden(ForbiddenException $exception)
* @method mixed forbidden(ForbiddenException $exception)
* @method null redirect(string $url)
*/
class Application
Expand Down Expand Up @@ -69,7 +75,7 @@ class Application
*
* @return string
*/
public function getEnvironment()
public function getEnvironment() : string
{
return $this->environment;
}
Expand All @@ -79,7 +85,7 @@ public function getEnvironment()
*
* @return string
*/
public function getPath()
public function getPath() : string
{
if (!$this->path) {
if (defined('PATH_APPLICATION')) {
Expand All @@ -98,7 +104,7 @@ public function getPath()
*
* @return bool
*/
public function isDebug()
public function isDebug() : bool
{
return $this->debugFlag;
}
Expand All @@ -110,7 +116,7 @@ public function isDebug()
*
* @return bool
*/
public function useLayout($flag = null)
public function useLayout($flag = null) : bool
{
if (is_bool($flag)) {
$this->layoutFlag = $flag;
Expand Down Expand Up @@ -190,6 +196,7 @@ protected function initConfig()
* Initial Request instance
*
* @return void
* @throws \InvalidArgumentException
*/
protected function initRequest()
{
Expand Down Expand Up @@ -228,7 +235,7 @@ protected function initRouter()
*
* @return \Bluz\Response\Response
*/
public function getResponse()
public function getResponse() : ResponseInstance
{
return Response::getInstance();
}
Expand All @@ -238,7 +245,7 @@ public function getResponse()
*
* @return \Zend\Diactoros\ServerRequest
*/
public function getRequest()
public function getRequest() : ServerRequest
{
return Request::getInstance();
}
Expand All @@ -247,6 +254,7 @@ public function getRequest()
* Run application
*
* @return void
* @throws ApplicationException
*/
public function run()
{
Expand All @@ -263,6 +271,7 @@ public function run()
* - Because it deprecated ({@link http://tools.ietf.org/html/rfc6648})
*
* @return void
* @throws ApplicationException
*/
public function process()
{
Expand Down Expand Up @@ -357,75 +366,77 @@ protected function postProcess()
* @param array $params
*
* @return Controller
* @throws ApplicationException
* @throws ComponentException
* @throws CommonException
* @throws ControllerException
* @throws ForbiddenException
* @throws NotAcceptableException
* @throws NotAllowedException
*/
public function dispatch($module, $controller, $params = [])
public function dispatch($module, $controller, array $params = [])
{
$this->preDispatch($module, $controller, $params);
$result = $this->doDispatch($module, $controller, $params);
$this->postDispatch($module, $controller, $params);
$instance = new Controller($module, $controller, $params);

Logger::info("app:dispatch:>>>: $module/$controller");
$this->preDispatch($instance);

return $result;
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 string $module
* @param string $controller
* @param array $params
* @param Controller $controller
*
* @return void
* @throws ComponentException
* @throws ForbiddenException
* @throws NotAcceptableException
* @throws NotAllowedException
*/
protected function preDispatch($module, $controller, $params = [])
protected function preDispatch($controller)
{
Logger::info("app:dispatch:pre: $module/$controller");
// check HTTP method
$controller->checkHttpMethod();

// check ACL privileges
$controller->checkPrivilege();

// check HTTP Accept header
$controller->checkHttpAccept();
}

/**
* Do dispatch
*
* @param string $module
* @param string $controller
* @param array $params
* @param Controller $controller
*
* @return Controller
* @throws \Bluz\Application\Exception\ForbiddenException
* @throws \Bluz\Application\Exception\NotAllowedException
* @throws \Bluz\Application\Exception\NotAcceptableException
* @return void
* @throws ComponentException
* @throws ControllerException
*/
protected function doDispatch($module, $controller, $params = [])
protected function doDispatch($controller)
{
// @TODO: try to find custom controller class

// create controller controller
$controllerInstance = new Controller($module, $controller);

// check HTTP Accept header
$controllerInstance->checkAccept();
// check HTTP method
$controllerInstance->checkMethod();
// check ACL privileges
$controllerInstance->checkPrivilege();

// run controller
$controllerInstance->run($params);

return $controllerInstance;
$controller->run();
}

/**
* Extension point: post dispatch
*
* @param string $module
* @param string $controller
* @param array $params
* @param Controller $controller
*
* @return void
*/
protected function postDispatch($module, $controller, $params = [])
protected function postDispatch($controller)
{
Logger::info("<<<:dispatch:post: $module/$controller");
// nothing by default
}

/**
Expand Down
Loading

0 comments on commit 1a6f490

Please sign in to comment.