|
1
|
|
|
<?php |
|
2
|
|
|
declare(strict_types=1); |
|
3
|
|
|
namespace Zewa; |
|
4
|
|
|
|
|
5
|
|
|
use Sabre\Event\Emitter; |
|
6
|
|
|
|
|
7
|
|
|
/** |
|
8
|
|
|
* This class is the starting point for application |
|
9
|
|
|
* |
|
10
|
|
|
* @author Zechariah Walden<zech @ zewadesign.com> |
|
11
|
|
|
*/ |
|
12
|
|
|
class App |
|
13
|
|
|
{ |
|
14
|
|
|
/** |
|
15
|
|
|
* Return value from application |
|
16
|
|
|
* |
|
17
|
|
|
* @var string |
|
18
|
|
|
*/ |
|
19
|
|
|
private $output = false; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* Namespaced controller path |
|
23
|
|
|
* |
|
24
|
|
|
* @var string |
|
25
|
|
|
*/ |
|
26
|
|
|
private $class; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* Instantiated class object |
|
30
|
|
|
* |
|
31
|
|
|
* @var Controller |
|
32
|
|
|
*/ |
|
33
|
|
|
private $instantiatedClass; |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* Module being accessed |
|
37
|
|
|
* |
|
38
|
|
|
* @var string |
|
39
|
|
|
*/ |
|
40
|
|
|
private $module; |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* Controller being accessed |
|
44
|
|
|
* |
|
45
|
|
|
* @var string |
|
46
|
|
|
*/ |
|
47
|
|
|
private $controller; |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* Method being accessed |
|
51
|
|
|
* |
|
52
|
|
|
* @var string |
|
53
|
|
|
*/ |
|
54
|
|
|
private $method; |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* Params being passed |
|
58
|
|
|
* |
|
59
|
|
|
* @var array |
|
60
|
|
|
*/ |
|
61
|
|
|
private $params; |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* @var DIContainer $container |
|
65
|
|
|
*/ |
|
66
|
|
|
private $container; |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* @var Emitter |
|
70
|
|
|
*/ |
|
71
|
|
|
private $event; |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* Application bootstrap process |
|
75
|
|
|
* |
|
76
|
|
|
* The application registers the configuration in the app/config/core.php |
|
77
|
|
|
* and then processes, and makes available the configured resources |
|
78
|
|
|
* |
|
79
|
|
|
* App constructor. |
|
80
|
|
|
* @param DIContainer $container |
|
81
|
|
|
*/ |
|
82
|
28 |
|
public function __construct(DIContainer $container) |
|
83
|
|
|
{ |
|
84
|
28 |
|
$this->configuration = $container->resolve('\Zewa\Config'); |
|
|
|
|
|
|
85
|
28 |
|
$this->event = $container->resolve('\Sabre\Event\Emitter', true); |
|
86
|
28 |
|
$this->container = $container; |
|
87
|
|
|
|
|
88
|
28 |
|
$this->router = $container->resolve('\Zewa\Router', true); |
|
|
|
|
|
|
89
|
18 |
|
$this->request = $container->resolve('\Zewa\Request', true); |
|
|
|
|
|
|
90
|
18 |
|
$this->view = $container->resolve('\Zewa\View'); |
|
|
|
|
|
|
91
|
|
|
|
|
92
|
18 |
|
$this->prepare(); |
|
93
|
18 |
|
} |
|
94
|
|
|
|
|
95
|
|
|
/** |
|
96
|
|
|
* Calls the proper shell for app execution |
|
97
|
|
|
* |
|
98
|
|
|
* @access private |
|
99
|
|
|
*/ |
|
100
|
|
|
public function initialize() |
|
101
|
|
|
{ |
|
102
|
|
|
$this->start(); |
|
103
|
|
|
return $this; |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
/** |
|
107
|
|
|
* App preparation cycle |
|
108
|
|
|
*/ |
|
109
|
18 |
|
private function prepare() |
|
110
|
|
|
{ |
|
111
|
18 |
|
$routerConfig = $this->configuration->get('Routing'); |
|
112
|
|
|
|
|
113
|
18 |
|
$this->module = ucfirst($routerConfig->module); |
|
114
|
18 |
|
$this->controller = ucfirst($routerConfig->controller); |
|
115
|
18 |
|
$this->method = $routerConfig->method; |
|
116
|
18 |
|
$this->params = $routerConfig->params; |
|
117
|
18 |
|
$this->class = 'Zewa\\App\\Modules\\' . $this->module . '\\Controllers\\' . ucfirst($this->controller); |
|
118
|
18 |
|
} |
|
119
|
|
|
|
|
120
|
|
|
// public function setContainer(Container $container) |
|
|
|
|
|
|
121
|
|
|
// { |
|
122
|
|
|
// $this->container = $container; |
|
123
|
|
|
// } |
|
124
|
|
|
|
|
125
|
|
|
/** |
|
126
|
|
|
* Verifies the provided application request is a valid request |
|
127
|
|
|
* |
|
128
|
|
|
* @access private |
|
129
|
|
|
*/ |
|
130
|
|
|
private function validateRequest() |
|
131
|
|
|
{ |
|
132
|
|
|
//catch exception and handle |
|
133
|
|
|
try { |
|
134
|
|
|
$class = new \ReflectionClass($this->class); |
|
135
|
|
|
$class->getMethod($this->method); |
|
136
|
|
|
} catch (\ReflectionException $e) { |
|
137
|
|
|
$view = $this->container->resolve('\Zewa\View'); |
|
138
|
|
|
$this->output = $view->render404(['Invalid method requests']); //Router::show404( |
|
139
|
|
|
return false; |
|
140
|
|
|
} |
|
141
|
|
|
|
|
142
|
|
|
return true; |
|
143
|
|
|
} |
|
144
|
|
|
|
|
145
|
|
|
/** |
|
146
|
|
|
* Processes the application request |
|
147
|
|
|
* |
|
148
|
|
|
* @access private |
|
149
|
|
|
*/ |
|
150
|
|
|
private function start() |
|
151
|
|
|
{ |
|
152
|
|
|
if ($this->validateRequest() === false) { |
|
153
|
|
|
return false; |
|
154
|
|
|
} |
|
155
|
|
|
|
|
156
|
|
|
$this->instantiatedClass = $this->container->resolve($this->class); |
|
157
|
|
|
|
|
158
|
|
|
$this->instantiatedClass->setConfig($this->configuration); |
|
159
|
|
|
$this->instantiatedClass->setEvent($this->event); |
|
160
|
|
|
$this->instantiatedClass->setRouter($this->router); |
|
161
|
|
|
$this->instantiatedClass->setRequest($this->request); |
|
162
|
|
|
$this->instantiatedClass->setContainer($this->container); |
|
163
|
|
|
$this->instantiatedClass->setView($this->view); |
|
164
|
|
|
|
|
165
|
|
|
$this->output = call_user_func_array( |
|
166
|
|
|
[&$this->instantiatedClass, $this->method], |
|
167
|
|
|
$this->params |
|
168
|
|
|
); |
|
169
|
|
|
} |
|
170
|
|
|
|
|
171
|
|
|
/** |
|
172
|
|
|
* Prepare application return value into a string |
|
173
|
|
|
* |
|
174
|
|
|
* @access public |
|
175
|
|
|
* @return string |
|
176
|
|
|
*/ |
|
177
|
|
|
public function __toString() |
|
178
|
|
|
{ |
|
179
|
|
|
if (!$this->output) { |
|
180
|
|
|
$this->output = ''; |
|
181
|
|
|
} |
|
182
|
|
|
|
|
183
|
|
|
return $this->output; |
|
184
|
|
|
} |
|
185
|
|
|
} |
|
186
|
|
|
|
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: