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
|
|
|
private $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
|
|
|
public function __construct(Dependency $dependency) |
53
|
|
|
{ |
54
|
|
|
$this->configuration = $dependency->resolve('\Zewa\Config'); |
|
|
|
|
55
|
|
|
$this->event = $dependency->resolve('\Sabre\Event\Emitter', true); |
56
|
|
|
$this->dependency = $dependency; |
57
|
|
|
|
58
|
|
|
/** @var Security security */ |
59
|
|
|
$this->security = $dependency->resolve('\Zewa\Security', true); |
|
|
|
|
60
|
|
|
/** @var Router router */ |
61
|
|
|
$this->router = $dependency->resolve('\Zewa\Router', true); |
62
|
|
|
/** @var Request request */ |
63
|
|
|
$this->request = $dependency->resolve('\Zewa\HTTP\Request', true); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Calls the proper shell for app execution |
68
|
|
|
* |
69
|
|
|
* @access private |
70
|
|
|
*/ |
71
|
|
|
public function initialize() |
72
|
|
|
{ |
73
|
|
|
$request = $this->router->getAction(); |
74
|
|
|
|
75
|
|
|
$isRouteCallback = $this->processRequestParameters($request); |
76
|
|
|
$this->start($isRouteCallback); |
77
|
|
|
|
78
|
|
|
return $this; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* @param $request array |
83
|
|
|
* @param $request[0] string namespace to load |
84
|
|
|
* @param $request[1] string method to call |
85
|
|
|
* @access private |
86
|
|
|
* @return bool |
87
|
|
|
* @throws Exception |
88
|
|
|
*/ |
89
|
|
|
private function processRequestParameters($request) : bool |
90
|
|
|
{ |
91
|
|
|
$params = $this->router->getParameters(); |
92
|
|
|
|
93
|
|
|
if($request !== null) { |
94
|
|
|
if(is_array($request)) { |
95
|
|
|
$callback = false; |
96
|
|
|
$this->request->setRequest($this->dependency->resolve($request[0])); |
97
|
|
|
$this->request->setMethod(($request[1]??[])); |
98
|
|
|
} else { |
99
|
|
|
$callback = true; |
100
|
|
|
$this->request->setRequest($request); |
101
|
|
|
array_unshift($params, $this->dependency); |
102
|
|
|
} |
103
|
|
|
$this->request->setParams($params); |
104
|
|
|
return $callback; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
throw new Exception('Invalid request'); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* @param bool $isRouteCallback |
112
|
|
|
*/ |
113
|
|
|
private function start(bool $isRouteCallback) |
114
|
|
|
{ |
115
|
|
|
$request = $this->request->getRequest(); |
116
|
|
|
$method = $this->request->getMethod(); |
117
|
|
|
$params = $this->request->getParams(); |
118
|
|
|
|
119
|
|
|
if($isRouteCallback === false) { // Routed Callback |
120
|
|
|
$this->output = call_user_func_array( |
121
|
|
|
[&$request, $method], |
122
|
|
|
$params |
123
|
|
|
); |
124
|
|
|
} else { |
125
|
|
|
$this->output = call_user_func_array($request, $params); |
126
|
|
|
} |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* Prepare application return value into a string |
131
|
|
|
* |
132
|
|
|
* @access public |
133
|
|
|
* @return string |
134
|
|
|
*/ |
135
|
|
|
public function __toString() |
136
|
|
|
{ |
137
|
|
|
return $this->output; |
138
|
|
|
} |
139
|
|
|
} |
Let’s assume that you have a directory layout like this:
and let’s assume the following content of
Bar.php
:If both files
OtherDir/Foo.php
andSomeDir/Foo.php
are loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php
However, as
OtherDir/Foo.php
does not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php
, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: