1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
namespace Zewa; |
4
|
|
|
|
5
|
|
|
use Sabre\Event\Emitter; |
6
|
|
|
use Zewa\Exception\Exception; |
7
|
|
|
use Zewa\HTTP\Request; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* This class is the starting point for application |
11
|
|
|
* |
12
|
|
|
* @author Zechariah Walden<zech @ zewadesign.com> |
13
|
|
|
*/ |
14
|
|
|
class App |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* Return value from application |
18
|
|
|
* |
19
|
|
|
* @var string |
20
|
|
|
*/ |
21
|
|
|
public $output = ''; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @var Dependency $dependency |
25
|
|
|
*/ |
26
|
|
|
private $dependency; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @var Emitter |
30
|
|
|
*/ |
31
|
|
|
private $event; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @var Router |
35
|
|
|
*/ |
36
|
|
|
private $router; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @var Request |
40
|
|
|
*/ |
41
|
|
|
private $request; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Application bootstrap process |
45
|
|
|
* |
46
|
|
|
* The application registers the configuration in the app/config/core.php |
47
|
|
|
* and then processes, and makes available the configured resources |
48
|
|
|
* |
49
|
|
|
* App constructor. |
50
|
|
|
* @param Dependency $dependency |
51
|
|
|
*/ |
52
|
5 |
|
public function __construct(Dependency $dependency) |
53
|
|
|
{ |
54
|
5 |
|
$this->configuration = $dependency->resolve('\Zewa\Config'); |
|
|
|
|
55
|
5 |
|
$this->event = $dependency->resolve('\Sabre\Event\Emitter', true); |
56
|
5 |
|
$this->dependency = $dependency; |
57
|
|
|
|
58
|
|
|
/** @var Security security */ |
59
|
5 |
|
$this->security = $dependency->resolve('\Zewa\Security', true); |
|
|
|
|
60
|
|
|
/** @var Router router */ |
61
|
5 |
|
$this->router = $dependency->resolve('\Zewa\Router', true); |
62
|
|
|
/** @var Request request */ |
63
|
5 |
|
$this->request = $dependency->resolve('\Zewa\HTTP\Request', true); |
64
|
5 |
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Calls the proper shell for app execution |
68
|
|
|
* |
69
|
|
|
* @access private |
70
|
|
|
*/ |
71
|
5 |
|
public function initialize() |
72
|
|
|
{ |
73
|
5 |
|
$request = $this->router->getAction(); |
74
|
|
|
|
75
|
5 |
|
$isRouteCallback = $this->processRequestParameters($request); |
76
|
|
|
|
77
|
4 |
|
$this->start($isRouteCallback); |
78
|
|
|
|
79
|
4 |
|
return $this; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* @param $request array |
84
|
|
|
* @param $request[0] string namespace to load |
85
|
|
|
* @param $request[1] string method to call |
86
|
|
|
* @access private |
87
|
|
|
* @return bool |
88
|
|
|
* @throws Exception |
89
|
|
|
*/ |
90
|
5 |
|
private function processRequestParameters($request) : bool |
91
|
|
|
{ |
92
|
5 |
|
$params = $this->router->getParameters(); |
93
|
|
|
|
94
|
5 |
|
if ($request !== null) { |
95
|
4 |
|
if (is_array($request)) { |
96
|
2 |
|
$callback = false; |
97
|
2 |
|
$this->request->setRequest($this->dependency->resolve($request[0])); |
98
|
2 |
|
$this->request->setMethod(($request[1]??[])); |
99
|
|
|
} else { |
100
|
2 |
|
$callback = true; |
101
|
2 |
|
$this->request->setRequest($request); |
102
|
2 |
|
array_unshift($params, $this->dependency); |
103
|
|
|
} |
104
|
4 |
|
$this->request->setParams($params); |
105
|
4 |
|
return $callback; |
106
|
|
|
} |
107
|
|
|
|
108
|
1 |
|
throw new Exception('Invalid request'); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* @param bool $isRouteCallback |
113
|
|
|
*/ |
114
|
4 |
|
private function start(bool $isRouteCallback) |
115
|
|
|
{ |
116
|
4 |
|
$request = $this->request->getRequest(); |
117
|
4 |
|
$method = $this->request->getMethod(); |
118
|
4 |
|
$params = $this->request->getParams(); |
119
|
|
|
|
120
|
4 |
|
if ($isRouteCallback === false) { // Routed Callback |
121
|
2 |
|
$this->output = call_user_func_array( |
122
|
2 |
|
[&$request, $method], |
123
|
|
|
$params |
124
|
|
|
); |
125
|
|
|
} else { |
126
|
2 |
|
$this->output = call_user_func_array($request, $params); |
127
|
|
|
} |
128
|
4 |
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* Prepare application return value into a string |
132
|
|
|
* |
133
|
|
|
* @access public |
134
|
|
|
* @return string |
135
|
|
|
*/ |
136
|
1 |
|
public function __toString() |
137
|
|
|
{ |
138
|
1 |
|
return $this->output; |
139
|
|
|
} |
140
|
|
|
} |
141
|
|
|
|
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: