1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
|
4
|
|
|
namespace Selami\Foundation; |
5
|
|
|
|
6
|
|
|
use Psr\Container\ContainerInterface; |
7
|
|
|
use Selami\Stdlib\Resolver; |
8
|
|
|
use ReflectionClass; |
9
|
|
|
use BadMethodCallException; |
10
|
|
|
|
11
|
|
|
class Controller |
12
|
|
|
{ |
13
|
|
|
const SIMPLE_RESPONSE = 'simpleFactoryResponse'; |
14
|
|
|
const AUTOWIRED_RESPONSE = 'autowiredResponse'; |
15
|
|
|
|
16
|
|
|
private $container; |
17
|
|
|
private $controller; |
18
|
|
|
private $controllerClass; |
19
|
|
|
private $returnType; |
20
|
|
|
private $args; |
21
|
|
|
private $actionOutput; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Controller constructor. |
25
|
|
|
* @param ContainerInterface $container |
26
|
|
|
* @param $controller |
27
|
|
|
* @param int $returnType |
28
|
|
|
* @param array|null $args |
29
|
|
|
*/ |
30
|
|
|
public function __construct( |
31
|
|
|
ContainerInterface $container, |
32
|
|
|
string $controller, int $returnType = Response::HTML, ?array $args = null) |
33
|
|
|
{ |
34
|
|
|
$this->container = $container; |
35
|
|
|
$this->controller = $controller; |
36
|
|
|
$this->returnType = $returnType; |
37
|
|
|
$this->args = $args; |
38
|
|
|
|
39
|
|
|
$this->autowiredResponse(); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
public function getControllerClass() : string |
43
|
|
|
{ |
44
|
|
|
return $this->controllerClass; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
public function getActionOutput() : array |
48
|
|
|
{ |
49
|
|
|
return $this->actionOutput; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
public function getReturnType() : int |
53
|
|
|
{ |
54
|
|
|
return $this->returnType; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
private function autowiredResponse() : void |
58
|
|
|
{ |
59
|
|
|
$this->controllerClass = $this->controller; |
60
|
|
|
if (!class_exists($this->controllerClass)) { |
61
|
|
|
$message = "Controller has not class name as {$this->controllerClass}"; |
62
|
|
|
throw new BadMethodCallException($message); |
63
|
|
|
} |
64
|
|
|
$controllerConstructorArguments = Resolver::getParameterHints($this->controllerClass, '__construct'); |
65
|
|
|
$arguments = []; |
66
|
|
|
foreach ($controllerConstructorArguments as $argumentName =>$argumentType) { |
67
|
|
|
$arguments[] = $this->getArgument($argumentName, $argumentType); |
68
|
|
|
} |
69
|
|
|
$reflectionClass = new ReflectionClass($this->controllerClass); |
70
|
|
|
$controller = $reflectionClass->newInstanceArgs($arguments); |
71
|
|
|
$this->actionOutput = $controller->__invoke(); |
72
|
|
|
if (isset($this->actionOutput['meta']['type']) && $this->actionOutput['meta']['type'] === Dispatcher::REDIRECT) { |
73
|
|
|
$this->returnType = Router::REDIRECT; |
74
|
|
|
} |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
private function getArgument(string $argumentName, string $argumentType) |
78
|
|
|
{ |
79
|
|
|
if ($argumentType === Resolver::ARRAY) { |
80
|
|
|
return $this->{$argumentName}; |
81
|
|
|
} |
82
|
|
|
return $this->container->get($argumentType); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
} |