1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Bavix\Builder; |
4
|
|
|
|
5
|
|
|
use Bavix\Config\Config; |
6
|
|
|
use Bavix\Context\Cookies; |
7
|
|
|
use Bavix\Context\Session; |
8
|
|
|
use Bavix\Flow\Flow; |
9
|
|
|
use Bavix\Lumper\Bind; |
10
|
|
|
use Bavix\Processors\Factory; |
11
|
|
|
use Bavix\Router\Router; |
12
|
|
|
use Bavix\SDK\Path; |
13
|
|
|
use Psr\Http\Message\ServerRequestInterface; |
14
|
|
|
|
15
|
|
|
class Builder |
16
|
|
|
{ |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @var string |
20
|
|
|
*/ |
21
|
|
|
protected $root; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Builder constructor. |
25
|
|
|
* |
26
|
|
|
* @param string $root |
27
|
|
|
*/ |
28
|
8 |
|
public function __construct(string $root) |
29
|
|
|
{ |
30
|
8 |
|
$this->root = Path::slash($root); |
31
|
8 |
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @return Config |
35
|
|
|
*/ |
36
|
|
|
public function config(): Config |
37
|
|
|
{ |
38
|
7 |
|
return Bind::once(__METHOD__, function () { |
39
|
1 |
|
return new Config($this->root . 'etc'); |
40
|
7 |
|
}); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @return Router |
45
|
|
|
*/ |
46
|
|
|
public function router(): Router |
47
|
|
|
{ |
48
|
1 |
|
return Bind::once(__METHOD__, function () { |
49
|
1 |
|
return new Router($this->config()->get('resolver')); |
50
|
1 |
|
}); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @return ServerRequestInterface |
55
|
|
|
*/ |
56
|
1 |
|
private function _request(): ServerRequestInterface |
57
|
|
|
{ |
58
|
1 |
|
$scheme = filter_input(INPUT_SERVER, 'REQUEST_SCHEME'); |
59
|
1 |
|
$host = filter_input(INPUT_SERVER, 'HTTP_HOST'); |
60
|
1 |
|
$uri = filter_input(INPUT_SERVER, 'REQUEST_URI'); |
61
|
|
|
|
62
|
1 |
|
$uriObject = $this->factory()->uri |
63
|
1 |
|
->createUri($scheme . '://' . $host . $uri); |
64
|
|
|
|
65
|
1 |
|
$request = $this->factory()->request->createServerRequest( |
66
|
1 |
|
filter_input(INPUT_SERVER, 'REQUEST_METHOD') ?? 'GET', |
67
|
1 |
|
$uriObject |
68
|
|
|
); |
69
|
|
|
|
70
|
1 |
|
$query = filter_input_array(INPUT_GET, FILTER_UNSAFE_RAW) ?: []; |
71
|
1 |
|
$data = filter_input_array(INPUT_POST, FILTER_UNSAFE_RAW) ?: []; |
72
|
|
|
|
73
|
|
|
return $request |
74
|
1 |
|
->withQueryParams($query) |
75
|
1 |
|
->withParsedBody($data) |
76
|
1 |
|
->withUploadedFiles($_FILES); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* @return ServerRequestInterface |
81
|
|
|
*/ |
82
|
|
|
public function request(): ServerRequestInterface |
83
|
|
|
{ |
84
|
2 |
|
return Bind::once(__METHOD__, function () { |
85
|
|
|
|
86
|
1 |
|
$factory = $this->factory()->request; |
87
|
|
|
|
88
|
1 |
|
if (method_exists($factory, 'createServerRequestFromGlobals')) |
89
|
|
|
{ |
90
|
|
|
$request = $factory::createServerRequestFromGlobals(); |
91
|
|
|
} |
92
|
|
|
else |
93
|
|
|
{ |
94
|
1 |
|
$request = $this->_request(); |
95
|
|
|
} |
96
|
|
|
|
97
|
1 |
|
if (method_exists($request, 'withRouter')) |
98
|
|
|
{ |
99
|
|
|
return $request->withRouter($this->router()); |
100
|
|
|
} |
101
|
|
|
|
102
|
1 |
|
return $request; |
103
|
2 |
|
}); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* @return Session |
108
|
|
|
*/ |
109
|
|
View Code Duplication |
public function session(): Session |
|
|
|
|
110
|
|
|
{ |
111
|
1 |
|
return Bind::once(__METHOD__, function () { |
112
|
|
|
|
113
|
1 |
|
$content = $this->config()->get('content'); |
114
|
1 |
|
$slice = $content->getSlice('session'); |
115
|
|
|
|
116
|
1 |
|
return new Session($slice->getData('password')); |
117
|
|
|
|
118
|
1 |
|
}); |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* @return Cookies |
123
|
|
|
*/ |
124
|
|
View Code Duplication |
public function cookies(): Cookies |
|
|
|
|
125
|
|
|
{ |
126
|
1 |
|
return Bind::once(__METHOD__, function () { |
127
|
|
|
|
128
|
1 |
|
$content = $this->config()->get('content'); |
129
|
1 |
|
$slice = $content->getSlice('cookies'); |
130
|
|
|
|
131
|
1 |
|
return new Cookies($slice->getData('password')); |
132
|
|
|
|
133
|
1 |
|
}); |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* @return Factory |
138
|
|
|
*/ |
139
|
|
|
public function factory(): Factory |
140
|
|
|
{ |
141
|
1 |
|
return Bind::once(__METHOD__, function () { |
142
|
1 |
|
return new Factory($this->config()->get('factory')); |
143
|
1 |
|
}); |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* @return Flow |
148
|
|
|
*/ |
149
|
|
|
public function flow(): Flow |
150
|
|
|
{ |
151
|
1 |
|
return Bind::once(__METHOD__, function () { |
152
|
1 |
|
return new Flow(null, $this->config()->get('flow')->asArray()); |
153
|
1 |
|
}); |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
} |
157
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.