public function respond(Context $context) { $response = $context->getResponse(); if ($response->getBody()->getSize() === 0) { $context->withContentType('text/plain'); $context->write($response->getReasonPhrase()); } $headers = $response->getHeaders(); // remove default content-type // if (is_null($headers['Content-Type']) && isset($this['response.contentType'])) { // $headers['Content-Type'] = $this['response.contentType']; // } if (!headers_sent()) { header(sprintf('HTTP/%s %s %s', $response->getProtocolVersion(), $response->getStatusCode(), $response->getReasonPhrase())); foreach ($response->getHeaders()->normalize() as $name => $value) { header(sprintf('%s: %s', $name, implode(', ', $value ?: [])), false); } } if (!isset(static::$STATUSES_EMPTY[$response->getStatusCode()])) { $body = $response->getBody(); if ($body->isSeekable()) { $body->rewind(); } while (!$body->eof()) { echo $body->read($this['response.chunkSize']); if (connection_status() != CONNECTION_NORMAL) { break; } } } // see koa for the rest }
public function validate(Context $context) { if (empty($this['name'])) { $context->throwError(400); } $this['listen'] = $this['listen'] ?: 80; $this['normalized'] = str_replace('.', '_', $this['name']); }
public function __invoke(Context $context, $next) { $method = $context->getParam('!method'); if (isset($method)) { $context->withMethod($method); } $next($context); }
public function validate(Context $context) { if (empty($context['normalized'])) { $context->throwError(400); } if (empty($this['server'])) { $context->throwError(400); } $this['port'] = $this['port'] ?: 80; $this['line'] = $this['server'] . ':' . $this['port']; }
public function __invoke(Context $context, $next) { if (isset($this->options['allowedMethods'][$context->getMethod()])) { $contentType = $context->getRequest()->getContentType(); if (!isset($this->parsers[$contentType])) { throw new \Exception('Cannot found parser for ' . $contentType); } $parser = $this->parsers[$contentType]; $context = $parser($context); } $next($context); }
public function __invoke(Context $context, callable $next) { if ($this->attributes['nfile.uploadUrl'] === $context->getUri()->getPath()) { $this->routeUpload($context); } else { // initiate repository $repository = $this->getRepository($context); if (isset($context['timezone'])) { $repository->setAttribute('timezone', $context['timezone']); } if (null !== $context['@renderer']) { $context['@renderer']->addTemplatePath(__DIR__ . '/../../templates'); } $context['@norm'] = $this; $next($context); } }
public function __invoke(Context $context, $next = null) { // avoid content negotiator on cli if ($context->getApp()->isCli()) { $next($context); return; } // set response content type by negotiating to context $contentType = $this->negotiate($context); if ($contentType) { $context->withContentType($contentType); } $next($context); // render respect response content type $contentType = $context->getContentType(); if (isset($this['renderers'][$contentType])) { $handler = $this['renderers'][$contentType]->getHandler(); $handler($context); } }
public function upstreamDelete(Context $context) { $body = $context->getParsedBody(); $upstream = MUpstream::get($body); $upstream->remove($context); $this->reload(); return []; }
public function __invoke(Context $context) { $routeInfo = $context['routeInfo']; if (isset($routeInfo)) { switch ($routeInfo[0]) { case Dispatcher::FOUND: $context->withStatus(200); foreach ($routeInfo[2] as $k => $v) { $context[$k] = urldecode($v); } $result = $routeInfo[1]($context); if ($result) { $context->withState($result); } break; case Dispatcher::METHOD_NOT_ALLOWED: $context->throwError(405); break; } } else { $routeBundle = $context['routeBundle']; if (isset($routeBundle)) { $context->shift($routeBundle['uri']); $routeBundle->getHandler()->dispatch($context); $context->unshift($routeBundle['uri']); } } }
public function getCollection(Context $context) { $context->depends('@norm'); if (null === $this->collection) { $this->collection = $context['@norm']->factory($context, $this['collection']); } return $this->collection; }