Passed
Push — master ( e4676b...9df15b )
by Enrico
02:10
created

Application::getProviders()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php 
2
namespace uSILEX;
3
4
use Pimple\Container;
5
use Pimple\ServiceProviderInterface;
6
use Psr\Http\Server\RequestHandlerInterface;
7
use Psr\Http\Message\ResponseInterface;
8
use Psr\Http\Message\ServerRequestInterface;
9
use Psr\Http\Server\MiddlewareInterface;
10
use TypeError;
11
12
class Application extends Container implements RequestHandlerInterface
13
{    
14
    const VERSION = '1.0.0';
15
    
16
    protected $providers = [];
17
    protected $middlewares = [];
18
    protected $onResponseListeners = [];
19
    protected $booted = false;
20
21
    /**
22
     * Instantiate a new Application.
23
     *
24
     * Objects and parameters can be passed as argument to the constructor.
25
     *
26
     */
27 5
    public function __construct()
28
    {
29 5
        parent::__construct();
30 5
        $this['debug'] = false;
31 5
        $this['version'] = static::VERSION;
32 5
    }
33
    
34
    
35 2
    public function getMiddlewares() : array
36
    {
37 2
        return $this->middlewares;
38
    }
39
    
40
    
41
    public function resetMiddlewaresQueue()  : Application
42
    {
43
        reset( $this->middlewares);
44
        
45
        return $this;
46
    }
47
    
48
    
49 3
    public function registerAsMiddleware(string $servicename) : Application
50
    {
51 3
        $this->middlewares[] = $servicename;
52
        
53 3
        return $this;
54
    }
55
    
56
    
57 2
    public function onResponse(string $serviceName)
58
    {
59 2
        $this->onResponseListeners[] = $serviceName;
60
        
61 2
        return $this;
62
    }
63
    
64
    
65 1
    public function handle(ServerRequestInterface $request) : ResponseInterface
66
    {
67 1
        if (!$this->booted) {
68 1
            $this->boot();
69
        }
70
        
71 1
        $middlewareServiceName = current($this->middlewares);
72 1
        $middleware = $this[$middlewareServiceName];
73 1
        next($this->middlewares);      
74
        
75 1
        if( $middleware instanceof MiddlewareInterface ) {
76 1
            $result = $middleware->process($request, $this);
77
        } elseif ( $middleware instanceof ResponseInterface) {
78
            $result = $middleware;
79
        } else {
80
            throw new TypeError;
81
        }
82
        
83 1
        return $result;
84
    }
85
    
86
    
87 1
    public function getProviders()
88
    {
89 1
        return $this->providers;
90
    }
91
    
92
    
93
    /**
94
     * Registers a service provider.
95
     *
96
     * @param ServiceProviderInterface $provider A ServiceProviderInterface instance
97
     * @param array                    $values   An array of values that customizes the provider
98
     *
99
     * @return Application
100
     */
101 2
    public function register(ServiceProviderInterface $provider, array $values = [])
102
    {
103 2
        $this->providers[] = $provider;
104
        
105 2
        parent::register($provider, $values);
106
        
107 2
        return $this;
108
    }
109
    
110
    
111
    /**
112
     * Boots all service providers.
113
     *
114
     * This method is automatically called by handle(), but you can use it
115
     * to boot all service providers when not handling a request.
116
     */
117 2
    public function boot()
118
    {
119 2
        if ($this->booted) {
120
            return;
121
        }
122
        
123 2
        $this->booted = true;
124
        
125 2
        foreach ($this->providers as $provider) {
126 1
            if ($provider instanceof BootableProviderInterface) {
127 1
                $provider->boot($this);
128
            }
129
        }
130 2
    }
131
    
132
    
133
    /**
134
     * Handles the request and delivers the response.
135
     *
136
     */
137 2
    public function run()
138
    {
139
        
140 2
        assert( isset($this['request']) );
141 2
        assert( !empty($this->getMiddlewares()) );
142
        
143
        
144
        // define $this['response'] just for testing purposes
145 2
        if (!isset($this['response'])) {
146
            $this->resetMiddlewaresQueue();
147
            $this['response'] = $this->handle($this['request']);
148
        }
149
        
150
        // call onResponse hook
151 2
        foreach( $this->onResponseListeners as $serviceName) {
152 2
            $this['response'] = $this[$serviceName];
153
        }
154
        
155
        
156 2
        if( isset($this['responseEmitter']) && is_callable($this['responseEmitter'])) {
157
            call_user_func($this['responseEmitter'],$this['response']);
158
        }
159
        
160 2
    }
161
    
162
}