Test Failed
Push — master ( cafe13...7a2929 )
by Enrico
02:56
created

Application::process()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 3
dl 0
loc 8
rs 10
c 0
b 0
f 0
ccs 6
cts 6
cp 1
cc 2
nc 2
nop 2
crap 2
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 1
     *
36
     * @param array $values the parameters or objects
37 1
     */
38
    public function __construct(array $values = [])
39 1
    {
40
        parent::__construct($values);
41 1
        
42
        $this['debug'] = false;
43
    }
44
    
45
    /**
46
     * Redefine Registers a service provider.
47
     *
48
     * @param ServiceProviderInterface $provider A ServiceProviderInterface instance
49
     * @param array                    $values   An array of values that customizes the provider
50
     *
51 5
     */
52
    public function register(ServiceProviderInterface $provider, array $values = []) : self
53 5
    {
54
        $this->providers[] = $provider;
55
        
56
        parent::register($provider, $values);
57 5
        
58
        return $this;
59 5
    }
60 1
    
61 1
    
62
    /**
63
     * Boots all service providers.
64
     *
65 5
     * This method is automatically called by handle(), but you can use it
66
     * to boot all service providers when not handling a request.
67
     */
68
    public function boot() : self
69
    {
70
        if ($this->booted) {
71
            return $this;
72
        }
73 4
        
74
        $this->booted = true;
75
        
76 4
        foreach ($this->providers as $provider) {
77
            if ( ($provider instanceof ServiceProviderInterface) && method_exists($provider,'boot') ) {
78
                $provider->boot($this);
79
            }
80 4
        }
81 4
        
82
        return $this;
83 1
    }
84
85 1
    /**
86 3
     * Handles the request and delivers the response.
87 3
     *
88 3
     */
89 1
    public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
90 2
    {
91 1
        // ensure container is booted
92
        if( !$this->booted ){
93 1
            $this->boot();
94 1
        }
95 1
            
96
        return $handler->handle($request);
97
    }
98
99 3
    
100
    /**
101
     * Process the request and delivers the response with error management.
102
     *
103
     */
104
    public function run() : bool
105
    {
106
        // ensure a default for 'uSilex.responseEmitter'
107
        if( !isset($this['uSilex.responseEmitter'])){ 
108
            $this['uSilex.responseEmitter'] = $this->protect(function(){});
109
        }
110
        
111
        try {
112
            $response = $this->process($this['uSilex.request'],$this['uSilex.httpHandler']);
113
           
114
            call_user_func($this['uSilex.responseEmitter'],$response, $this);
115
            
116
            $result = true;
117
        } catch (Exception $e) {
118
            $result = false;
119
            if( isset($this['uSilex.exceptionHandler'])) {
120
                $response = call_user_func($this['uSilex.exceptionHandler'], $e, $this);
121
                call_user_func($this['uSilex.responseEmitter'],$response, $this);
122
            } else {
123
                header('X-PHP-Response-Code: '. $e->getCode(), true, 500);
124
                echo $e->getMessage();
125
            }
126
        }
127
          
128
        return $result;
129
    }
130
    
131
}