1
|
|
|
<?php |
2
|
|
|
namespace Mezon\Application; |
3
|
|
|
|
4
|
|
|
use Mezon\Transport\RequestParamsInterface; |
5
|
|
|
|
6
|
|
|
/** |
7
|
|
|
* Class Presenter |
8
|
|
|
* |
9
|
|
|
* @package Mezon |
10
|
|
|
* @subpackage Presenter |
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 Presenter extends \Mezon\Application\AbstractPresenter |
22
|
|
|
{ |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Router |
26
|
|
|
* |
27
|
|
|
* @var \Mezon\Transport\RequestParams |
28
|
|
|
*/ |
29
|
|
|
private $requestParams = null; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Constructor |
33
|
|
|
* |
34
|
|
|
* @param string $presenterName |
35
|
|
|
* Presenter name to be executed |
36
|
|
|
* @param ?\Mezon\Transport\RequestParams $requestParams |
37
|
|
|
* request params fetcher |
38
|
|
|
*/ |
39
|
|
|
public function __construct(string $presenterName = '', ?\Mezon\Transport\RequestParams $requestParams = null) |
40
|
|
|
{ |
41
|
|
|
$this->setPresenterName($presenterName); |
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
|
|
|
*/ |
51
|
|
|
public function getParamsFetcher(): RequestParamsInterface |
52
|
|
|
{ |
53
|
|
|
if ($this->requestParams === null) { |
54
|
|
|
throw (new \Exception('Param fetcher was not setup')); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
return $this->requestParams; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Method runs controller |
62
|
|
|
* |
63
|
|
|
* @param |
64
|
|
|
* string PresenterName |
65
|
|
|
* Presenter name to be run |
66
|
|
|
* @return mixed result of the controller |
67
|
|
|
*/ |
68
|
|
|
public function run(string $presenterName = '') |
69
|
|
|
{ |
70
|
|
|
if ($presenterName === '') { |
71
|
|
|
$presenterName = $this->getPresenterName(); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
if ($presenterName === '') { |
75
|
|
|
$presenterName = 'Default'; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
if (method_exists($this, 'presenter' . $presenterName)) { |
79
|
|
|
return call_user_func([ |
80
|
|
|
$this, |
81
|
|
|
'presenter' . $presenterName |
82
|
|
|
]); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
throw (new \Exception('Presenter ' . $presenterName . ' was not found')); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* May be these functions should be excluded to base class common with View |
90
|
|
|
*/ |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* Method redirects user to another page |
94
|
|
|
* |
95
|
|
|
* @param string $url |
96
|
|
|
* @codeCoverageIgnore |
97
|
|
|
*/ |
98
|
|
|
public function redirectTo(string $url): void |
99
|
|
|
{ |
100
|
|
|
header("Location: $url"); |
101
|
|
|
exit(0); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* Method builds route data |
106
|
|
|
* |
107
|
|
|
* @param string $route |
108
|
|
|
* route |
109
|
|
|
* @param string $method |
110
|
|
|
* HTTP method |
111
|
|
|
* @param string $function |
112
|
|
|
* controller's function name |
113
|
|
|
* @return array built route data |
114
|
|
|
*/ |
115
|
|
|
public function buildRoute(string $route, string $method, string $function): array |
116
|
|
|
{ |
117
|
|
|
return [ |
118
|
|
|
'route' => $route, |
119
|
|
|
'method' => $method, |
120
|
|
|
'callback' => [ |
121
|
|
|
$this, |
122
|
|
|
$function |
123
|
|
|
] |
124
|
|
|
]; |
125
|
|
|
} |
126
|
|
|
} |
127
|
|
|
|