Completed
Push — master ( b31514...603a9f )
by Changwan
06:16
created

Bootstrap   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 62
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 4

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
dl 0
loc 62
ccs 0
cts 33
cp 0
rs 10
c 0
b 0
f 0
wmc 6
lcom 0
cbo 4

5 Methods

Rating   Name   Duplication   Size   Complexity  
A providers() 0 8 1
A boot() 0 4 1
B execute() 0 26 4
registerConfiguration() 0 1 ?
setRoutes() 0 1 ?
1
<?php
2
namespace Wandu\Foundation\WebApp;
3
4
use Throwable;
5
use Wandu\Config\ConfigServiceProvider;
6
use Wandu\Config\Contracts\Config;
7
use Wandu\DI\ContainerInterface;
8
use Wandu\Foundation\Contracts\Bootstrap as BootstrapContract;
9
use Wandu\Foundation\WebApp\Contracts\HttpErrorHandler;
10
use Wandu\Foundation\WebApp\DefaultHttpErrorHandler;
11
use Wandu\Http\Factory\ServerRequestFactory;
12
use Wandu\Http\HttpServiceProvider;
13
use Wandu\Http\Sender\ResponseSender;
14
use Wandu\Router\Contracts\Routable;
15
use Wandu\Router\Dispatcher;
16
use Wandu\Router\RouterServiceProvider;
17
18
abstract class Bootstrap implements BootstrapContract
19
{
20
    /**
21
     * {@inheritdoc}
22
     */
23
    public function providers(): array
24
    {
25
        return [
26
            new ConfigServiceProvider(),
27
            new HttpServiceProvider(),
28
            new RouterServiceProvider(),
29
        ];
30
    }
31
    
32
    /**
33
     * {@inheritdoc}
34
     */
35
    public function boot(ContainerInterface $app)
36
    {
37
        $this->registerConfiguration($app->get(Config::class));
38
    }
39
40
    /**
41
     * {@inheritdoc}
42
     */
43
    public function execute(ContainerInterface $app): int
44
    {
45
        if (!$app->has(HttpErrorHandler::class)) {
46
            $app->bind(HttpErrorHandler::class, DefaultHttpErrorHandler::class);
47
        }
48
49
        $request = $app->get(ServerRequestFactory::class)->createFromGlobals();
50
        
51
        try {
52
            $dispatcher = $app->get(Dispatcher::class);
53
            $this->setRoutes($routeCollection = $dispatcher->createRouteCollection());
54
            $response = $dispatcher->dispatch($routeCollection->compile(), $request);
55
        } catch (Throwable $exception) {
56
            // output buffer clean
57
            while (ob_get_level() > 0) {
58
                ob_end_clean();
59
            }
60
61
            $errorHandler = $app->get(HttpErrorHandler::class);
62
            $app->get(ResponseSender::class)->sendToGlobal($errorHandler->handle($request, $exception));
63
            return -1;
64
        }
65
66
        $app->get(ResponseSender::class)->sendToGlobal($response);
67
        return 0;
68
    }
69
70
    /**
71
     * @param \Wandu\Config\Contracts\Config $config
72
     */
73
    abstract public function registerConfiguration(Config $config);
74
75
    /**
76
     * @param \Wandu\Router\Contracts\Routable $router
77
     */
78
    abstract public function setRoutes(Routable $router);
79
}
80