1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
|
4
|
|
|
namespace Selami; |
5
|
|
|
|
6
|
|
|
use Psr\Container\ContainerInterface; |
7
|
|
|
use Selami\Router\Router; |
8
|
|
|
use Selami\Stdlib\Resolver; |
9
|
|
|
use ReflectionClass; |
10
|
|
|
|
11
|
|
|
class ApplicationController |
12
|
|
|
{ |
13
|
|
|
private $container; |
14
|
|
|
private $controller; |
15
|
|
|
private $controllerClass; |
16
|
|
|
private $returnType; |
17
|
|
|
private $uriParameters; |
18
|
|
|
private $actionOutput; |
19
|
|
|
|
20
|
|
|
public function __construct( |
21
|
|
|
ContainerInterface $container, |
22
|
|
|
string $controller, |
23
|
|
|
int $returnType = Router::HTML, |
24
|
|
|
?array $uriParameters = [] |
25
|
|
|
) { |
26
|
|
|
$this->container = $container; |
27
|
|
|
$this->controller = $controller; |
28
|
|
|
$this->returnType = $returnType; |
29
|
|
|
$this->uriParameters = $uriParameters; |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
public function getControllerClass() : string |
33
|
|
|
{ |
34
|
|
|
return $this->controllerClass; |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
public function getActionOutput() : array |
38
|
|
|
{ |
39
|
|
|
return $this->actionOutput; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
public function getReturnType() : int |
43
|
|
|
{ |
44
|
|
|
return $this->returnType; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
public function getUriParameters() : array |
48
|
|
|
{ |
49
|
|
|
return $this->uriParameters; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
public function getControllerResponse() : ControllerResponse |
53
|
|
|
{ |
54
|
|
|
$this->controllerClass = $this->controller; |
55
|
|
|
$controllerConstructorArguments = Resolver::getParameterHints($this->controllerClass, '__construct'); |
56
|
|
|
$arguments = []; |
57
|
|
|
foreach ($controllerConstructorArguments as $argumentName => $argumentType) { |
58
|
|
|
$arguments[] = $this->getArgument($argumentName, $argumentType); |
59
|
|
|
} |
60
|
|
|
$controllerClass = new ReflectionClass($this->controllerClass); |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* @var $controllerObject \Selami\Interfaces\Controller |
64
|
|
|
*/ |
65
|
|
|
$controllerObject = $controllerClass->newInstanceArgs($arguments); |
66
|
|
|
return $controllerObject(); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
private function getArgument(string $argumentName, string $argumentType) |
70
|
|
|
{ |
71
|
|
|
if ($argumentType === Resolver::ARRAY) { |
72
|
|
|
return $this->container->has($argumentName) ? |
73
|
|
|
$this->container->get($argumentName) : $this->{'get'.ucfirst($argumentName)}(); |
74
|
|
|
} |
75
|
|
|
return $this->container->get($argumentType); |
76
|
|
|
} |
77
|
|
|
} |
78
|
|
|
|