1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Equip; |
4
|
|
|
|
5
|
|
|
use Auryn\Injector; |
6
|
|
|
use Equip\Configuration\ConfigurationSet; |
7
|
|
|
use Equip\Directory; |
8
|
|
|
use Equip\Middleware\MiddlewareSet; |
9
|
|
|
use Relay\Relay; |
10
|
|
|
|
11
|
|
|
class Application |
12
|
|
|
{ |
13
|
|
|
/** |
14
|
|
|
* Create a new application |
15
|
|
|
* |
16
|
|
|
* @param Injector $injector |
17
|
|
|
* @param ConfigurationSet $configuration |
18
|
|
|
* @param MiddlewareSet $middleware |
19
|
|
|
* |
20
|
|
|
* @return static |
21
|
|
|
*/ |
22
|
2 |
|
public static function build( |
23
|
|
|
Injector $injector = null, |
24
|
|
|
ConfigurationSet $configuration = null, |
25
|
|
|
MiddlewareSet $middleware = null |
26
|
|
|
) { |
27
|
2 |
|
return new static($injector, $configuration, $middleware); |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @var Injector |
32
|
|
|
*/ |
33
|
|
|
private $injector; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @var ConfigurationSet |
37
|
|
|
*/ |
38
|
|
|
private $configuration; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @var MiddlewareSet |
42
|
|
|
*/ |
43
|
|
|
private $middleware; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @var callable|string |
47
|
|
|
*/ |
48
|
|
|
private $routing; |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @param Injector $injector |
52
|
|
|
* @param ConfigurationSet $configuration |
53
|
|
|
* @param MiddlewareSet $middleware |
54
|
|
|
*/ |
55
|
6 |
|
public function __construct( |
56
|
|
|
Injector $injector = null, |
57
|
|
|
ConfigurationSet $configuration = null, |
58
|
|
|
MiddlewareSet $middleware = null |
59
|
|
|
) { |
60
|
6 |
|
$this->injector = $injector ?: new Injector; |
61
|
6 |
|
$this->configuration = $configuration ?: new ConfigurationSet; |
62
|
6 |
|
$this->middleware = $middleware ?: new MiddlewareSet; |
63
|
6 |
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Change configuration values |
67
|
|
|
* |
68
|
|
|
* @param array $configuration |
69
|
|
|
* |
70
|
|
|
* @return self |
71
|
|
|
*/ |
72
|
2 |
|
public function setConfiguration(array $configuration) |
73
|
|
|
{ |
74
|
2 |
|
$this->configuration = $this->configuration->withValues($configuration); |
75
|
2 |
|
return $this; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Change middleware |
80
|
|
|
* |
81
|
|
|
* @param array $middleware |
82
|
|
|
* |
83
|
|
|
* @return self |
84
|
|
|
*/ |
85
|
1 |
|
public function setMiddleware(array $middleware) |
86
|
|
|
{ |
87
|
1 |
|
$this->middleware = $this->middleware->withValues($middleware); |
88
|
1 |
|
return $this; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* Change routing |
93
|
|
|
* |
94
|
|
|
* @param callable|string $routing |
95
|
|
|
* |
96
|
|
|
* @return self |
97
|
|
|
*/ |
98
|
2 |
|
public function setRouting($routing) |
99
|
|
|
{ |
100
|
2 |
|
$this->routing = $routing; |
101
|
2 |
|
return $this; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* Run the application |
106
|
|
|
* |
107
|
|
|
* @param string $runner |
108
|
|
|
* |
109
|
|
|
* @return \Psr\Http\Message\ResponseInterface |
110
|
|
|
*/ |
111
|
1 |
|
public function run($runner = Relay::class) |
112
|
|
|
{ |
113
|
1 |
|
$this->configuration->apply($this->injector); |
114
|
|
|
|
115
|
1 |
|
return $this->injector |
116
|
1 |
|
->share($this->middleware) |
117
|
1 |
|
->prepare(Directory::class, $this->routing) |
118
|
1 |
|
->execute($runner); |
119
|
|
|
} |
120
|
|
|
} |
121
|
|
|
|