|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace App\Bootstrap; |
|
4
|
|
|
|
|
5
|
|
|
use Core\Bootstrap\BootstrapInterface; |
|
6
|
|
|
use Core\Application; |
|
7
|
|
|
use Core\ControllerDispatcher\Dispatcher; |
|
8
|
|
|
use Zend\Diactoros\Uri; |
|
9
|
|
|
use Zend\Diactoros\ServerRequestFactory; |
|
10
|
|
|
|
|
11
|
|
|
class DispatcherBootstrap implements BootstrapInterface |
|
12
|
|
|
{ |
|
13
|
|
|
|
|
14
|
|
|
public function boot(Application $app) |
|
15
|
|
|
{ |
|
16
|
|
|
$dispatcher = new Dispatcher(); |
|
17
|
|
|
|
|
18
|
|
|
$dispatcher->setRequest($this->getServerRequest()); |
|
19
|
|
|
$dispatcher->setRouterConfig($this->getRouterConfig()); |
|
20
|
|
|
$dispatcher->setViewEngineConfig($this->getViewEngineConfig()); |
|
21
|
|
|
$dispatcher->setViewResolver($this->getViewResolvers()); |
|
22
|
|
|
|
|
23
|
|
|
$dispatcher->dispatch(); |
|
24
|
|
|
} |
|
25
|
|
|
|
|
26
|
|
|
private function getServerRequest() |
|
27
|
|
|
{ |
|
28
|
|
|
$request = ServerRequestFactory::fromGlobals(); |
|
29
|
|
|
|
|
30
|
|
|
$stripedUri = new Uri( |
|
31
|
|
|
$this->stripUrlPrefix($request->getUri()->getPath()) |
|
32
|
|
|
); |
|
33
|
|
|
|
|
34
|
|
|
return $request->withUri($stripedUri); |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
private function stripUrlPrefix($url) |
|
38
|
|
|
{ |
|
39
|
|
|
$appUrl = $this->getSiteConfig()['url']; |
|
40
|
|
|
|
|
41
|
|
|
if (substr($url, 0, strlen($appUrl)) === $appUrl) { |
|
42
|
|
|
return '/' . substr($url, strlen($appUrl)) ?: '/'; |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
return $url; |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
private function getSiteConfig() |
|
49
|
|
|
{ |
|
50
|
|
|
return require __DIR__ . '/../../config/site.php'; |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
private function getViewEngineConfig() |
|
54
|
|
|
{ |
|
55
|
|
|
return require __DIR__ . '/../../config/viewEngine.php'; |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
private function getViewResolvers() |
|
59
|
|
|
{ |
|
60
|
|
|
return require __DIR__ . '/../../config/viewResolver.php'; |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
private function getRouterConfig() |
|
64
|
|
|
{ |
|
65
|
|
|
return require __DIR__ . '/../../config/router.php'; |
|
66
|
|
|
} |
|
67
|
|
|
} |
|
68
|
|
|
|