1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Class Controller |
4
|
|
|
* |
5
|
|
|
* @package Faulancer\Controller |
6
|
|
|
* @author Florian Knapp <[email protected]> |
7
|
|
|
*/ |
8
|
|
|
namespace Faulancer\Controller; |
9
|
|
|
|
10
|
|
|
use Faulancer\Http\Request; |
11
|
|
|
use Faulancer\Http\Uri; |
12
|
|
|
use Faulancer\Service\AuthenticatorService; |
13
|
|
|
use Faulancer\Service\DbService; |
14
|
|
|
use Faulancer\Service\HttpService; |
15
|
|
|
use Faulancer\Service\SessionManagerService; |
16
|
|
|
use Faulancer\ServiceLocator\ServiceInterface; |
17
|
|
|
use Faulancer\Session\SessionManager; |
18
|
|
|
use Faulancer\View\ViewController; |
19
|
|
|
use Faulancer\ServiceLocator\ServiceLocator; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Class Controller |
23
|
|
|
*/ |
24
|
|
|
abstract class Controller |
25
|
|
|
{ |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Holds the views per controller request |
29
|
|
|
* @var array |
30
|
|
|
*/ |
31
|
|
|
private $viewArray = []; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Controller constructor. |
35
|
|
|
* @param Request $request |
36
|
|
|
*/ |
37
|
|
|
public function __construct(Request $request) |
38
|
|
|
{ |
39
|
|
|
$this->request = $request; |
|
|
|
|
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Returns the service locator |
44
|
|
|
* |
45
|
|
|
* @return ServiceLocator |
46
|
|
|
*/ |
47
|
|
|
public function getServiceLocator() :ServiceLocator |
48
|
|
|
{ |
49
|
|
|
return ServiceLocator::instance(); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Returns the session manager |
54
|
|
|
* |
55
|
|
|
* @return SessionManager |
56
|
|
|
*/ |
57
|
|
|
public function getSessionManager() :SessionManager |
58
|
|
|
{ |
59
|
|
|
return $this->getServiceLocator()->get(SessionManagerService::class); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Returns the view controller |
64
|
|
|
* |
65
|
|
|
* @return ViewController |
66
|
|
|
*/ |
67
|
|
|
public function getView() :ViewController |
68
|
|
|
{ |
69
|
|
|
$calledClass = get_called_class(); |
70
|
|
|
|
71
|
|
|
if (in_array($calledClass, array_keys($this->viewArray))) { |
72
|
|
|
return $this->viewArray[$calledClass]; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
$viewController = new ViewController(); |
76
|
|
|
$this->viewArray[$calledClass] = $viewController; |
77
|
|
|
|
78
|
|
|
return $viewController; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* Returns the orm/entity manager |
83
|
|
|
* |
84
|
|
|
* @return DbService|ServiceInterface |
85
|
|
|
*/ |
86
|
|
|
public function getDb() :DbService |
87
|
|
|
{ |
88
|
|
|
return $this->getServiceLocator()->get(DbService::class); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* Render view with given template |
93
|
|
|
* |
94
|
|
|
* @param string $template |
95
|
|
|
* @param array $variables |
96
|
|
|
* @return string |
97
|
|
|
*/ |
98
|
|
|
public function render(string $template = '', $variables = []) :string |
99
|
|
|
{ |
100
|
|
|
return $this->getView()->setTemplate($template)->setVariables($variables)->render(); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* Set required authentication |
105
|
|
|
* |
106
|
|
|
* @param array $role |
107
|
|
|
* @return bool |
108
|
|
|
*/ |
109
|
|
|
public function requireAuth($role) :bool |
110
|
|
|
{ |
111
|
|
|
/** @var AuthenticatorService $authenticator */ |
112
|
|
|
$authenticator = $this->getServiceLocator()->get(AuthenticatorService::class); |
113
|
|
|
|
114
|
|
|
if ($authenticator->isAuthenticated($role) === false) { |
115
|
|
|
return $authenticator->redirectToAuthentication(); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
return true; |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* @param string $uri |
123
|
|
|
* @return bool |
124
|
|
|
*/ |
125
|
|
|
public function redirect(string $uri) :bool |
126
|
|
|
{ |
127
|
|
|
/** @var HttpService $httpService */ |
128
|
|
|
$httpService = $this->getServiceLocator()->get(HttpService::class); |
129
|
|
|
return $httpService->redirect($uri); |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* @return Request |
134
|
|
|
*/ |
135
|
|
|
public function getRequest() :Request |
136
|
|
|
{ |
137
|
|
|
return $this->request; |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
} |
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: