Passed
Push — master ( 22a57b...53ccca )
by Enrico
02:43
created

Application::boot()   A

Complexity

Conditions 5
Paths 4

Size

Total Lines 15
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 5

Importance

Changes 0
Metric Value
eloc 7
dl 0
loc 15
ccs 8
cts 8
cp 1
rs 9.6111
c 0
b 0
f 0
cc 5
nc 4
nop 0
crap 5
1
<?php 
2
3
/*
4
 * This file is part of the uSilex framework.
5
 *
6
 * (c) Enrico Fagnoni <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace uSilex;
13
14
use Pimple\Container;
15
use Pimple\ServiceProviderInterface;
16
use Psr\Http\Message\ResponseInterface;
17
use Psr\Http\Message\ServerRequestInterface;
18
use Psr\Http\Server\RequestHandlerInterface;
19
use Psr\Http\Server\MiddlewareInterface;
20
use Exception;
21
22
/**
23
 * The uSilex framework class.
24
*/
25
class Application extends Container implements MiddlewareInterface
26
{
27
    
28
    protected $providers = [];
29
    protected $booted = false;
30
31
    /**
32
     * Instantiate a new Application.
33
     *
34
     * Objects and parameters can be passed as argument to the constructor.
35
     *
36
     * @param array $values the parameters or objects
37
     */
38 10
    public function __construct(array $values = [])
39
    {
40 10
        parent::__construct($values);
41
        
42 10
        $this['debug'] = false;
43 10
        $this['handler.queue'] = [];
44 10
    }
45
    
46
    /**
47
     * Redefine Registers a service provider.
48
     *
49
     * @param ServiceProviderInterface $provider A ServiceProviderInterface instance
50
     * @param array                    $values   An array of values that customizes the provider
51
     *
52
     */
53 6
    public function register(ServiceProviderInterface $provider, array $values = []) : self
54
    {
55 6
        $this->providers[] = $provider;
56
        
57 6
        parent::register($provider, $values);
58
        
59 6
        return $this;
60
    }
61
    
62
    
63
    /**
64
     * Boots all service providers.
65
     *
66
     * This method is automatically called by handle(), but you can use it
67
     * to boot all service providers when not handling a request.
68
     */
69 1
    public function boot() : self
70
    {
71 1
        if ($this->booted) {
72 1
            return $this;
73
        }
74
        
75 1
        $this->booted = true;
76
        
77 1
        foreach ($this->providers as $provider) {
78 1
            if (($provider instanceof ServiceProviderInterface) && method_exists($provider, 'boot')) {
79 1
                $provider->boot($this);
80
            }
81
        }
82
        
83 1
        return $this;
84
    }
85
86
    
87
    /**
88
     * Handles the request and delivers the response.
89
     *
90
     */
91 2
    public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
92
    {            
93 2
        return $handler->handle($request);
94
    }
95
96
    
97
    /**
98
     * Process the request and delivers the response with error management.
99
     *
100
     */
101 3
    public function run() : bool
102
    {
103
        // ensure a default for 'uSilex.responseEmitter'
104 3
        if (!isset($this['uSilex.responseEmitter'])) { 
105
            $this['uSilex.responseEmitter'] = $this->protect(function() {});
106
        }
107
        
108
        try {
109 3
            $response = $this->process($this['uSilex.request'], $this['uSilex.httpHandler']);
110
           
111 1
            call_user_func($this['uSilex.responseEmitter'], $response, $this);
112
            
113 1
            $result = true;
114 2
        } catch (Exception $e) {
115 2
            $result = false;
116 2
            if (isset($this['uSilex.exceptionHandler'])) {
117 1
                $response = call_user_func($this['uSilex.exceptionHandler'], $e, $this);
118 1
                call_user_func($this['uSilex.responseEmitter'], $response, $this);
119
            } else {
120 1
                header('X-PHP-Response-Code: '.$e->getCode(), true, 500);
121 1
                echo $e->getMessage();
122
            }
123
        }
124
          
125 3
        return $result;
126
    }
127
    
128
}