1
|
|
|
<?php |
2
|
|
|
namespace Mezon\Application; |
3
|
|
|
|
4
|
|
|
use Mezon\Transport\RequestParamsInterface; |
5
|
|
|
|
6
|
|
|
/** |
7
|
|
|
* Class Controller |
8
|
|
|
* |
9
|
|
|
* @package Mezon |
10
|
|
|
* @subpackage Controller |
11
|
|
|
* @author Dodonov A.A. |
12
|
|
|
* @version v.1.0 (2020/01/12) |
13
|
|
|
* @copyright Copyright (c) 2020, aeon.org |
14
|
|
|
*/ |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Base class for all views |
18
|
|
|
* |
19
|
|
|
* @deprecated since 2020-06-26 |
20
|
|
|
*/ |
21
|
|
|
class Controller extends ControllerInterface |
|
|
|
|
22
|
|
|
{ |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Router |
26
|
|
|
* |
27
|
|
|
* @var RequestParamsInterface |
28
|
|
|
*/ |
29
|
|
|
private $requestParams = null; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Constructor |
33
|
|
|
* |
34
|
|
|
* @param string $controllerName |
35
|
|
|
* Controller name to be executed |
36
|
|
|
* @param ?RequestParamsInterface $requestParams |
37
|
|
|
* request params fetcher |
38
|
|
|
*/ |
39
|
|
|
public function __construct(string $controllerName = '', ?RequestParamsInterface $requestParams = null) |
40
|
|
|
{ |
41
|
|
|
$this->setControllerName($controllerName); |
42
|
|
|
|
43
|
|
|
$this->requestParams = $requestParams; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Method return $requestParams and thrown exception if it was not set |
48
|
|
|
* |
49
|
|
|
* @return RequestParamsInterface request params fetcher |
50
|
|
|
* @deprecated since 2020-07-06 use getRequestParamsFetcher |
51
|
|
|
* @codeCoverageIgnore |
52
|
|
|
*/ |
53
|
|
|
public function getParamsFetcher(): RequestParamsInterface |
54
|
|
|
{ |
55
|
|
|
return $this->getRequestParamsFetcher(); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Method returns $this->requestParams and creates this object if necessery |
60
|
|
|
* |
61
|
|
|
* @return RequestParamsInterface |
62
|
|
|
*/ |
63
|
|
|
public function getRequestParamsFetcher(): RequestParamsInterface |
64
|
|
|
{ |
65
|
|
|
if ($this->requestParams === null) { |
66
|
|
|
throw (new \Exception('Param fetcher was not setup')); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
return $this->requestParams; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Method runs controller |
74
|
|
|
* |
75
|
|
|
* @param |
76
|
|
|
* string ControllerName |
77
|
|
|
* Controller name to be run |
78
|
|
|
* @return mixed result of the controller |
79
|
|
|
*/ |
80
|
|
|
public function run(string $controllerName = '') |
81
|
|
|
{ |
82
|
|
|
if ($controllerName === '') { |
83
|
|
|
$controllerName = $this->getControllerName(); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
if ($controllerName === '') { |
87
|
|
|
$controllerName = 'Default'; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
if (method_exists($this, 'controller' . $controllerName)) { |
91
|
|
|
return call_user_func([ |
92
|
|
|
$this, |
93
|
|
|
'controller' . $controllerName |
94
|
|
|
]); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
throw (new \Exception('Controller ' . $controllerName . ' was not found')); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* May be these functions should be excluded to base class common with View |
102
|
|
|
*/ |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* Method redirects user to another page |
106
|
|
|
* |
107
|
|
|
* @param string $url |
108
|
|
|
* @codeCoverageIgnore |
109
|
|
|
*/ |
110
|
|
|
public function redirectTo(string $url): void |
111
|
|
|
{ |
112
|
|
|
header("Location: $url"); |
113
|
|
|
exit(0); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* Method builds route data |
118
|
|
|
* |
119
|
|
|
* @param string $route |
120
|
|
|
* route |
121
|
|
|
* @param string $method |
122
|
|
|
* HTTP method |
123
|
|
|
* @param string $function |
124
|
|
|
* controller's function name |
125
|
|
|
* @return array built route data |
126
|
|
|
*/ |
127
|
|
|
public function buildRoute(string $route, string $method, string $function): array |
128
|
|
|
{ |
129
|
|
|
return [ |
130
|
|
|
'route' => $route, |
131
|
|
|
'method' => $method, |
132
|
|
|
'callback' => [ |
133
|
|
|
$this, |
134
|
|
|
$function |
135
|
|
|
] |
136
|
|
|
]; |
137
|
|
|
} |
138
|
|
|
} |
139
|
|
|
|