1
|
|
|
<?php namespace Comodojo\Dispatcher; |
2
|
|
|
|
3
|
|
|
use \Monolog\Logger; |
4
|
|
|
use \Comodojo\Dispatcher\Components\Configuration; |
5
|
|
|
use \Comodojo\Dispatcher\Request\Model as Request; |
6
|
|
|
use \Comodojo\Dispatcher\Router\Collector as RouteCollector; |
7
|
|
|
use \Comodojo\Dispatcher\Response\Model as Response; |
8
|
|
|
use \Comodojo\Dispatcher\Extra\Model as Extra; |
9
|
|
|
use \Comodojo\Dispatcher\Components\Timestamp as TimestampTrait; |
10
|
|
|
use \Comodojo\Dispatcher\Output\Processor; |
11
|
|
|
use \Comodojo\Dispatcher\Events\DispatcherEvent; |
12
|
|
|
use \Comodojo\Dispatcher\Events\ServiceEvent; |
13
|
|
|
use \Comodojo\Dispatcher\Router\RoutingTableInterface; |
14
|
|
|
use \Comodojo\Dispatcher\Log\DispatcherLogger; |
15
|
|
|
use \Comodojo\Dispatcher\Cache\DispatcherCache; |
16
|
|
|
use \Comodojo\Cache\CacheManager; |
17
|
|
|
use \League\Event\Emitter; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* |
21
|
|
|
* @package Comodojo dispatcher |
22
|
|
|
* @author Marco Giovinazzi <[email protected]> |
23
|
|
|
* @license GPL-3.0+ |
24
|
|
|
* |
25
|
|
|
* LICENSE: |
26
|
|
|
* |
27
|
|
|
* This program is free software: you can redistribute it and/or modify |
28
|
|
|
* it under the terms of the GNU Affero General Public License as |
29
|
|
|
* published by the Free Software Foundation, either version 3 of the |
30
|
|
|
* License, or (at your option) any later version. |
31
|
|
|
* |
32
|
|
|
* This program is distributed in the hope that it will be useful, |
33
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
34
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
35
|
|
|
* GNU Affero General Public License for more details. |
36
|
|
|
* |
37
|
|
|
* You should have received a copy of the GNU Affero General Public License |
38
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
39
|
|
|
*/ |
40
|
|
|
|
41
|
|
|
class Dispatcher { |
42
|
|
|
|
43
|
|
|
use TimestampTrait; |
44
|
|
|
|
45
|
|
|
private $configuration; |
46
|
|
|
|
47
|
|
|
private $request; |
48
|
|
|
|
49
|
|
|
private $router; |
50
|
|
|
|
51
|
|
|
private $response; |
52
|
|
|
|
53
|
|
|
private $extra; |
54
|
|
|
|
55
|
|
|
private $logger; |
56
|
|
|
|
57
|
|
|
private $cache; |
58
|
|
|
|
59
|
|
|
private $events; |
60
|
|
|
|
61
|
|
|
public function __construct( |
62
|
|
|
$configuration = array(), |
63
|
|
|
Emitter $events = null, |
64
|
|
|
CacheManager $cache = null, |
65
|
|
|
Logger $logger = null |
66
|
|
|
) { |
67
|
|
|
|
68
|
|
|
ob_start(); |
69
|
|
|
|
70
|
|
|
$this->setTimestamp(); |
71
|
|
|
|
72
|
|
|
$this->configuration = new Configuration($configuration); |
73
|
|
|
|
74
|
|
|
$this->events = is_null($events) ? new Emitter() : $emitter; |
|
|
|
|
75
|
|
|
|
76
|
|
|
$this->logger = is_null($logger) ? DispatcherLogger::create($this->configuration) : $logger; |
|
|
|
|
77
|
|
|
|
78
|
|
|
$this->cache = is_null($cache) ? DispatcherCache::create($this->configuration, $this->logger) : $cache; |
|
|
|
|
79
|
|
|
|
80
|
|
|
$this->request = new Request($this->configuration, $this->logger); |
81
|
|
|
|
82
|
|
|
$this->router = new RouteCollector($this->configuration, $this->logger, $this->cache); |
83
|
|
|
|
84
|
|
|
$this->response = new Response($this->configuration, $this->logger); |
85
|
|
|
|
86
|
|
|
$this->extra = new Extra($this->logger); |
|
|
|
|
87
|
|
|
|
88
|
|
|
$this->router->load( $routing_table ); |
|
|
|
|
89
|
|
|
|
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
public function configuration() { |
93
|
|
|
|
94
|
|
|
return $this->configuration; |
95
|
|
|
|
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
public function events() { |
99
|
|
|
|
100
|
|
|
return $this->events; |
101
|
|
|
|
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
public function cache() { |
105
|
|
|
|
106
|
|
|
return $this->cache; |
107
|
|
|
|
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
public function request() { |
111
|
|
|
|
112
|
|
|
return $this->request; |
113
|
|
|
|
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
public function router() { |
117
|
|
|
|
118
|
|
|
return $this->router; |
119
|
|
|
|
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
public function response() { |
123
|
|
|
|
124
|
|
|
return $this->response; |
125
|
|
|
|
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
public function extra() { |
129
|
|
|
|
130
|
|
|
return $this->extra; |
131
|
|
|
|
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
public function dispatch() { |
135
|
|
|
|
136
|
|
|
$this->events->emit( new DispatcherEvent($this) ); |
137
|
|
|
|
138
|
|
|
if ( $this->configuration()->get('dispatcher-enabled') === false ) { |
139
|
|
|
|
140
|
|
|
$status = $this->configuration()->get('dispatcher-disabled-status'); |
141
|
|
|
|
142
|
|
|
$content = $this->configuration()->get('dispatcher-disabled-message'); |
143
|
|
|
|
144
|
|
|
$this->response()->status()->set($status); |
145
|
|
|
|
146
|
|
|
$this->response()->content()->set($content); |
147
|
|
|
|
148
|
|
|
} else { |
149
|
|
|
|
150
|
|
|
$this->events->emit( $this->emitServiceSpecializedEvents('dispatcher.request') ); |
151
|
|
|
|
152
|
|
|
$this->events->emit( $this->emitServiceSpecializedEvents('dispatcher.request.'.$this->request->method()->get()) ); |
153
|
|
|
|
154
|
|
|
$this->events->emit( $this->emitServiceSpecializedEvents('dispatcher.request.#') ); |
155
|
|
|
|
156
|
|
|
$this->router->route($this->request); |
157
|
|
|
|
158
|
|
|
$this->events->emit( $this->emitServiceSpecializedEvents('dispatcher.route') ); |
159
|
|
|
|
160
|
|
|
$this->events->emit( $this->emitServiceSpecializedEvents('dispatcher.route.'.$this->router->getType()) ); |
161
|
|
|
|
162
|
|
|
$this->events->emit( $this->emitServiceSpecializedEvents('dispatcher.route.'.$this->router->getService()) ); |
163
|
|
|
|
164
|
|
|
$this->events->emit( $this->emitServiceSpecializedEvents('dispatcher.route.#') ); |
165
|
|
|
|
166
|
|
|
// translate route to service |
167
|
|
|
|
168
|
|
|
$this->router->compose($this->response); |
169
|
|
|
|
170
|
|
|
$this->events->emit( $this->emitServiceSpecializedEvents('dispatcher.response') ); |
171
|
|
|
|
172
|
|
|
$this->events->emit( $this->emitServiceSpecializedEvents('dispatcher.response.'.$this->response->status()->get()) ); |
173
|
|
|
|
174
|
|
|
$this->events->emit( $this->emitServiceSpecializedEvents('dispatcher.response.#') ); |
175
|
|
|
|
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
$return = Processor::parse($this->configuration, $this->logger, $this->response); |
179
|
|
|
|
180
|
|
|
ob_end_clean(); |
181
|
|
|
|
182
|
|
|
return $return; |
183
|
|
|
|
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
private function emitServiceSpecializedEvents($name) { |
187
|
|
|
|
188
|
|
|
return new ServiceEvent( |
189
|
|
|
$name, |
190
|
|
|
$this->logger, |
191
|
|
|
$this->request, |
192
|
|
|
$this->router, |
193
|
|
|
$this->response, |
194
|
|
|
$this->extra |
195
|
|
|
); |
196
|
|
|
|
197
|
|
|
} |
198
|
|
|
|
199
|
|
|
} |
200
|
|
|
|
This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.