-
Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy pathAbstractAction.php
More file actions
56 lines (48 loc) · 1.34 KB
/
AbstractAction.php
File metadata and controls
56 lines (48 loc) · 1.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
<?php
declare(strict_types=1);
namespace Lit\Voltage;
use Lit\Nimo\Handlers\AbstractHandler;
use Lit\Voltage\Interfaces\ViewInterface;
use Psr\Http\Message\ResponseFactoryInterface;
use Psr\Http\Message\ResponseInterface;
/**
* Base class for actions.
*
* It's strongly recommended to have your own action base class extending this one
*/
abstract class AbstractAction extends AbstractHandler
{
/**
* @var ResponseFactoryInterface
*/
protected $responseFactory;
/**
* Halt execution and return the given response.
*
* @param ResponseInterface $response The response to be thrown.
* @throws ThrowableResponse Throws a standard exception containing given response.
* @return void
*/
protected static function throwResponse(ResponseInterface $response): void
{
throw ThrowableResponse::of($response);
}
/**
* @param ViewInterface $view The view instance to be used with this action.
* @return ViewInterface
*/
protected function attachView(ViewInterface $view)
{
$view->setResponse($this->responseFactory->createResponse());
return $view;
}
/**
* @return JsonView
*/
protected function json(): JsonView
{
/** @var JsonView $view */
$view = $this->attachView(new JsonView());
return $view;
}
}