Skip to content

Commit

Permalink
Simply ControllerInterface.
Browse files Browse the repository at this point in the history
Let Controller::invokeAction() itself run startuProcess() and shutdownProcess().
  • Loading branch information
ADmad committed Nov 1, 2019
1 parent 6992d51 commit eb11837
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 62 deletions.
37 changes: 31 additions & 6 deletions src/Controller/Controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -492,12 +492,17 @@ public function setResponse(Response $response)
* Dispatches the controller action. Checks that the action
* exists and isn't private.
*
* @return void
* @return \Psr\Http\Message\ResponseInterface
* @throws \Cake\Controller\Exception\MissingActionException If controller action is not found.
* @throws \UnexpectedValueException If return value of action method is not null or ResponseInterface instance.
*/
public function invokeAction(): void
public function invokeAction(): ResponseInterface
{
$result = $this->startupProcess();
if ($result) {
return $result;
}

$request = $this->request;

if (!$this->isAction($request->getParam('action'))) {
Expand Down Expand Up @@ -526,6 +531,13 @@ public function invokeAction(): void
if ($result) {
$this->response = $result;
}

$result = $this->shutdownProcess();
if ($result) {
return $result;
}

return $this->response;
}

/**
Expand All @@ -545,7 +557,14 @@ public function implementedEvents(): array
}

/**
* @inheritDoc
* Perform the startup process for this controller.
* Fire the Components and Controller callbacks in the correct order.
*
* - Initializes components, which fires their `initialize` callback
* - Calls the controller `beforeFilter`.
* - triggers Component `startup` methods.
*
* @return \Psr\Http\Message\ResponseInterface|null
*/
public function startupProcess(): ?ResponseInterface
{
Expand All @@ -562,16 +581,22 @@ public function startupProcess(): ?ResponseInterface
}

/**
* @inheritDoc
* Perform the various shutdown processes for this controller.
* Fire the Components and Controller callbacks in the correct order.
*
* - triggers the component `shutdown` callback.
* - calls the Controller's `afterFilter` method.
*
* @return \Psr\Http\Message\ResponseInterface|null
*/
public function shutdownProcess(): ResponseInterface
public function shutdownProcess(): ?ResponseInterface
{
$event = $this->dispatchEvent('Controller.shutdown');
if ($event->getResult() instanceof ResponseInterface) {
return $event->getResult();
}

return $this->response;
return null;
}

/**
Expand Down
22 changes: 1 addition & 21 deletions src/Http/ActionDispatcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,26 +58,6 @@ public function dispatch(ServerRequest $request, ?Response $response = null): Re
Router::setRequest($request);
}

$controller = $this->factory->create($request, $response);

return $this->_invoke($controller);
}

/**
* Invoke a controller's action and wrapping methods.
*
* @param \Cake\Http\ControllerInterface $controller The controller to invoke.
* @return \Psr\Http\Message\ResponseInterface The response
*/
protected function _invoke(ControllerInterface $controller): ResponseInterface
{
$result = $controller->startupProcess();
if ($result) {
return $result;
}

$controller->invokeAction();

return $controller->shutdownProcess();
return $this->factory->create($request, $response)->invokeAction();
}
}
30 changes: 2 additions & 28 deletions src/Http/ControllerInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,38 +24,12 @@
*/
interface ControllerInterface
{
/**
* Perform the startup process for this controller.
*
* Fire the Components and Controller callbacks in the correct order.
*
* - Initializes components, which fires their `initialize` callback
* - Calls the controller `beforeFilter`.
* - triggers Component `startup` methods.
*
* @return \Psr\Http\Message\ResponseInterface|null
*/
public function startupProcess(): ?ResponseInterface;

/**
* Perform the various shutdown processes for this controller.
*
* Fire the Components and Controller callbacks in the correct order and
* return the final response instance.
*
* - triggers the component `shutdown` callback.
* - calls the Controller's `afterFilter` method.
*
* @return \Psr\Http\Message\ResponseInterface
*/
public function shutdownProcess(): ResponseInterface;

/**
* Dispatches the controller action. Checks that the action exists and isn't private.
*
* @return void
* @return \Psr\Http\Message\ResponseInterface
* @throws \Cake\Controller\Exception\MissingActionException If controller action is not found.
* @throws \UnexpectedValueException If return value of action method is not null or ResponseInterface instance.
*/
public function invokeAction(): void;
public function invokeAction(): ResponseInterface;
}
11 changes: 5 additions & 6 deletions tests/test_app/TestApp/Controller/CakesController.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,16 +67,15 @@ public function startupProcess(): ?ResponseInterface
/**
* Shutdown process
*
* @return \Psr\Http\Message\ResponseInterface
* @return \Psr\Http\Message\ResponseInterface|null
*/
public function shutdownProcess(): ResponseInterface
public function shutdownProcess(): ?ResponseInterface
{
$response = parent::shutdownProcess();

parent::shutdownProcess();
if ($this->request->getParam('stop') === 'shutdown') {
$response = $response->withStringBody('shutdown stop');
return $this->response->withStringBody('shutdown stop');
}

return $response;
return null;
}
}
2 changes: 1 addition & 1 deletion tests/test_app/TestApp/Controller/TestController.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ class TestController extends ControllerTestAppController
* @param \Cake\Event\EventInterface $event
* @return \Cake\Http\Response|null
*/
public function beforeFilter(EventInterface $event): ?Response
public function beforeFilter(EventInterface $event)
{
}

Expand Down

0 comments on commit eb11837

Please sign in to comment.