1: <?php
2:
3: namespace PHPixie\Framework;
4:
5: use PHPixie\Processors\Processor;
6: use PHPixie\Slice\Data;
7: use Psr\Http\Message\ResponseInterface;
8: use Psr\Http\Message\ServerRequestInterface;
9:
10: 11: 12: 13: 14:
15: class HTTP
16: {
17: 18: 19:
20: protected $builder;
21:
22: 23: 24:
25: protected $configData;
26:
27: 28: 29:
30: protected $instances = array();
31:
32: 33: 34: 35:
36: public function __construct($builder)
37: {
38: $this->builder = $builder;
39: }
40:
41: 42: 43: 44: 45: 46: 47:
48: public function processor()
49: {
50: return $this->instance('processor');
51: }
52:
53: 54: 55: 56:
57: public function routeTranslator()
58: {
59: return $this->instance('routeTranslator');
60: }
61:
62: 63: 64: 65:
66: protected function instance($name)
67: {
68: if(!array_key_exists($name, $this->instances)) {
69: $method = 'build'.ucfirst($name);
70: $this->instances[$name] = $this->$method();
71: }
72:
73: return $this->instances[$name];
74: }
75:
76: 77: 78:
79: protected function buildRouteTranslator()
80: {
81: $route = $this->builder->components()->route();
82: $httpConfig = $this->builder->configuration()->httpConfig();
83:
84: return $route->translator(
85: $this->builder->configuration()->httpRouteResolver(),
86: $httpConfig->slice('translator'),
87: $this->builder->context()
88: );
89: }
90:
91: 92: 93: 94: 95:
96: public function processSapiRequest()
97: {
98: $http = $this->builder->components()->http();
99: $serverRequest = $http->sapiServerRequest();
100:
101: $response = $this->processor()->process($serverRequest);
102:
103: $http->output(
104: $response,
105: $this->builder->context()->httpContext()
106: );
107: }
108:
109: 110: 111: 112: 113:
114: public function processServerRequest($serverRequest)
115: {
116: $response = $this->processor()->process($serverRequest);
117:
118: return $response->asResponseMessage(
119: $this->builder->context()->httpContext()
120: );
121: }
122:
123: 124: 125: 126:
127: protected function buildProcessor()
128: {
129: $processors = $this->builder->components()->processors();
130:
131: return $processors->catchException(
132: $processors->chain(array(
133: $this->requestProcessor(),
134: $this->contextProcessor(),
135: $processors->checkIsProcessable(
136: $this->builder->configuration()->httpProcessor(),
137: $this->dispatchProcessor(),
138: $this->notFoundProcessor()
139: )
140: )),
141: $this->exceptionProcessor()
142: );
143: }
144:
145: 146: 147: 148: 149:
150: protected function requestProcessor()
151: {
152: $components = $this->builder->components();
153:
154: $processors = $components->processors();
155: $httpProcessors = $components->httpProcessors();
156:
157: return $processors->chain(array(
158: $httpProcessors->parseBody(),
159: $this->parseRouteProcessor(),
160: $httpProcessors->buildRequest()
161: ));
162: }
163:
164: 165: 166: 167: 168:
169: protected function contextProcessor()
170: {
171: $components = $this->builder->components();
172:
173: $processors = $components->processors();
174: $httpProcessors = $components->httpProcessors();
175: $authProcessors = $components->authProcessors();
176:
177: $context = $this->builder->context();
178:
179: return $processors->chain(array(
180: $httpProcessors->updateContext($context),
181: $authProcessors->updateContext($context),
182: ));
183: }
184:
185: 186: 187: 188: 189:
190: protected function parseRouteProcessor()
191: {
192: $frameworkProcessors = $this->builder->processors();
193:
194: $translator = $this->routeTranslator();
195: return $frameworkProcessors->httpParseRoute($translator);
196: }
197:
198: 199: 200: 201: 202: 203: 204: 205: 206: 207:
208: protected function dispatchProcessor()
209: {
210: $processors = $this->builder->components()->processors();
211: $frameworkProcessors = $this->builder->processors();
212:
213: return $processors->chain(array(
214: $this->builder->configuration()->httpProcessor(),
215: $frameworkProcessors->httpNormalizeResponse(),
216: ));
217: }
218:
219: 220: 221: 222:
223: protected function exceptionProcessor()
224: {
225: $frameworkProcessors = $this->builder->processors();
226: $httpConfig = $this->builder->configuration()->httpConfig();
227:
228: return $frameworkProcessors->httpExceptionResponse(
229: $httpConfig->slice('exceptionResponse')
230: );
231: }
232:
233: 234: 235: 236:
237: protected function notFoundProcessor()
238: {
239: $frameworkProcessors = $this->builder->processors();
240: $httpConfig = $this->builder->configuration()->httpConfig();
241:
242: return $frameworkProcessors->httpNotFoundResponse(
243: $httpConfig->slice('notFoundResponse')
244: );
245: }
246: }