/** * Set container for response content * * @author Krzysztof Kalkhoff * * @param ResponseInterface $obj * @return Response */ public function setObject(ResponseInterface $obj) { $this->object = $obj; $this->setContent($obj->getContent()); $this->setContentType($obj->getContentType()); return $this; }
public function parse(RequestInterface $request, ResponseInterface $response, MapInterface $attributes) : MapInterface { $start = $this->clock->now(); if (!$this->isHtml($attributes)) { return $attributes; } $document = $this->reader->read($response->body()); try { $meta = (new Elements('meta'))((new Head())($document))->filter(function (ElementInterface $meta) : bool { return $meta->attributes()->contains('name') && $meta->attributes()->contains('content'); })->filter(function (ElementInterface $meta) : bool { $name = $meta->attributes()->get('name')->value(); return (string) (new Str($name))->toLower() === 'theme-color'; }); } catch (ElementNotFoundException $e) { return $attributes; } if ($meta->size() !== 1) { return $attributes; } try { $colour = Colour::fromString($meta->current()->attributes()->get('content')->value()); } catch (ExceptionInterface $e) { return $attributes; } return $attributes->put(self::key(), new Attribute(self::key(), $colour, $this->clock->now()->elapsedSince($start)->milliseconds())); }
public function parse(RequestInterface $request, ResponseInterface $response, MapInterface $attributes) : MapInterface { $start = $this->clock->now(); if (!$this->isHtml($attributes)) { return $attributes; } $document = $this->reader->read($response->body()); try { $metas = (new Elements('meta'))((new Head())($document)); } catch (ElementNotFoundException $e) { return $attributes; } $meta = $metas->filter(function (ElementInterface $meta) : bool { return $meta->attributes()->contains('name') && $meta->attributes()->contains('content'); })->filter(function (ElementInterface $meta) : bool { $name = $meta->attributes()->get('name')->value(); return (string) (new Str($name))->toLower() === 'description'; }); if ($meta->size() !== 1) { return $attributes; } $description = $meta->current()->attributes()->get('content')->value(); $description = (new Str($description))->trim()->pregReplace('/\\t/m', ' ')->pregReplace('/ {2,}/m', ' '); if ($description->length() > 150) { $description = $description->substring(0, 150)->append('...'); } return $attributes->put(self::key(), new Attribute(self::key(), (string) $description, $this->clock->now()->elapsedSince($start)->milliseconds())); }
public function parse(RequestInterface $request, ResponseInterface $response, MapInterface $attributes) : MapInterface { $start = $this->clock->now(); if (!$this->isHtml($attributes)) { return $attributes; } $document = $this->reader->read($response->body()); try { $links = (new Elements('link'))((new Head())($document)); } catch (ElementNotFoundException $e) { return $attributes; } $links = $links->filter(function (NodeInterface $link) : bool { return $link instanceof Link; })->filter(function (Link $link) : bool { return $link->relationship() === 'alternate' && $link->attributes()->contains('hreflang'); }); if ($links->size() === 0) { return $attributes; } $alternates = $links->reduce(new Map(UrlInterface::class, 'string'), function (Map $links, Link $link) : Map { return $links->put($link->href(), $link->attributes()->get('hreflang')->value()); })->groupBy(function (UrlInterface $url, string $language) { return $language; })->map(function (string $language, SequenceInterface $links) use($request, $attributes) : SequenceInterface { return $links->map(function (Pair $link) use($request, $attributes) : UrlInterface { return $this->resolver->resolve($request, $attributes, $link->key()); }); })->reduce(new Map('string', AttributeInterface::class), function (Map $languages, string $language, SequenceInterface $links) use($start) : Map { return $languages->put($language, new Alternate($language, $links->reduce(new Set(UrlInterface::class), function (Set $links, UrlInterface $link) : Set { return $links->add($link); }), $this->clock->now()->elapsedSince($start)->milliseconds())); }); return $attributes->put(self::key(), new Alternates($alternates)); }
public function parse(RequestInterface $request, ResponseInterface $response, MapInterface $attributes) : MapInterface { $start = $this->clock->now(); if (!$this->isHtml($attributes)) { return $attributes; } $document = $this->reader->read($response->body()); try { $metas = (new Elements('meta'))((new Head())($document)); } catch (ElementNotFoundException $e) { return $attributes; } $meta = $metas->filter(function (ElementInterface $meta) : bool { return $meta->attributes()->contains('name') && $meta->attributes()->get('name')->value() === 'apple-itunes-app' && $meta->attributes()->contains('content'); }); if ($meta->size() !== 1) { return $attributes; } $content = $meta->current()->attributes()->get('content')->value(); $content = new Str($content); if (!$content->match(self::PATTERN)) { return $attributes; } $matches = $content->getMatches(self::PATTERN); return $attributes->put(self::key(), new Attribute(self::key(), (string) $matches['uri'], $this->clock->now()->elapsedSince($start)->milliseconds())); }
/** * index * * @param ServerRequestInterface $request * @param ResponseInterface $response * @param callable $next * @return ResponseInterface */ public function indexAction(ServerRequestInterface $request, ResponseInterface $response, callable $next) { $name = $request->getAttribute("name", "Unknown"); $template = new Template("layout.phtml"); $response->getBody()->write($template->render(["name" => $name])); return $next($request, $response); }
public function parse(RequestInterface $request, ResponseInterface $response, MapInterface $attributes) : MapInterface { $start = $this->clock->now(); if (!$response->headers()->has('Content-Type') || !$response->headers()->get('Content-Type') instanceof ContentType) { return $attributes; } return $attributes->put(self::key(), new Attribute(self::key(), MediaType::fromString((string) $response->headers()->get('Content-Type')->values()->current()), $this->clock->now()->elapsedSince($start)->milliseconds())); }
public function parse(RequestInterface $request, ResponseInterface $response, MapInterface $attributes) : MapInterface { $start = $this->clock->now(); if (!$response->headers()->has('Content-Type') || !($header = $response->headers()->get('Content-Type')) instanceof ContentType || !$header->values()->current()->parameters()->contains('charset')) { return $attributes; } return $attributes->put(self::key(), new Attribute(self::key(), $header->values()->current()->parameters()->get('charset')->value(), $this->clock->now()->elapsedSince($start)->milliseconds())); }
public function parse(RequestInterface $request, ResponseInterface $response, MapInterface $attributes) : MapInterface { $start = $this->clock->now(); if (!$this->isImage($attributes) || !$response->body()->knowsSize()) { return $attributes; } return $attributes->put(self::key(), new Attribute(self::key(), $response->body()->size(), $this->clock->now()->elapsedSince($start)->milliseconds())); }
public function parse(RequestInterface $request, ResponseInterface $response, MapInterface $attributes) : MapInterface { $start = $this->clock->now(); if (!$this->isImage($attributes)) { return $attributes; } $infos = getimagesizefromstring((string) $response->body()); $time = $this->clock->now()->elapsedSince($start)->milliseconds(); return $attributes->put(self::key(), new Attributes(self::key(), (new Map('string', AttributeInterface::class))->put('width', new Attribute('width', $infos[0], $time))->put('height', new Attribute('height', $infos[1], $time)))); }
public function make(string $name, UrlInterface $url, ResponseInterface $response) : HttpResource { $headers = $response->headers(); if ($response->statusCode()->value() !== StatusCode::codes()->get('OK') || !$headers->has('Content-Type') || (string) $headers->get('Content-Type')->values()->current() !== 'application/json') { throw new InvalidArgumentException(); } $data = json_decode((string) $response->body(), true); $data['url'] = (string) $url; return $this->denormalizer->denormalize($data, HttpResource::class, null, ['name' => $name]); }
public function parse(RequestInterface $request, ResponseInterface $response, MapInterface $attributes) : MapInterface { $start = $this->clock->now(); if (!$response->headers()->has('Content-Language') || !$response->headers()->get('Content-Language') instanceof ContentLanguage) { return $attributes; } return $attributes->put(self::key(), new Attribute(self::key(), $response->headers()->get('Content-Language')->values()->reduce(new Set('string'), function (Set $carry, ContentLanguageValue $language) : Set { return $carry->add((string) $language); }), $this->clock->now()->elapsedSince($start)->milliseconds())); }
public function parse(RequestInterface $request, ResponseInterface $response, MapInterface $attributes) : MapInterface { $start = $this->clock->now(); if (!$response->headers()->has('Cache-Control')) { return $attributes; } $directives = $response->headers()->get('Cache-Control')->values()->filter(function (HeaderValueInterface $value) : bool { return $value instanceof SharedMaxAge; }); if ($directives->size() !== 1) { return $attributes; } return $attributes->put(self::key(), new Attribute(self::key(), $this->clock->now()->goForward(new Second($directives->current()->age())), $this->clock->now()->elapsedSince($start)->milliseconds())); }
public function parse(RequestInterface $request, ResponseInterface $response, MapInterface $attributes) : MapInterface { $start = $this->clock->now(); if (!$response->headers()->has('Link') || !$response->headers()->get('Link') instanceof Link) { return $attributes; } $links = $response->headers()->get('Link')->values()->filter(function (LinkValue $value) : bool { return $value->relationship() === 'canonical'; }); if ($links->size() !== 1) { return $attributes; } return $attributes->put(self::key(), new Attribute(self::key(), $this->resolver->resolve($request, $attributes, $links->current()->url()), $this->clock->now()->elapsedSince($start)->milliseconds())); }
public function __construct(ResponseInterface &$response, $code = 0, $message = null, $previous = null) { if ($code === 0) { try { $status = $response->getStatus(); } catch (\Exception $e) { throw new \LogicException(\get_class($this) . ' thrown with no status code and it was not set in the Response instance', 0, $e); } } else { $response->setStatus($code); } $this->response = $response; $status = $response->getStatus(); $this->code = $status['code']; $this->message = $status['message']; }
public function parse(RequestInterface $request, ResponseInterface $response, MapInterface $attributes) : MapInterface { $start = $this->clock->now(); if (!$this->isHtml($attributes)) { return $attributes; } $document = $this->reader->read($response->body()); $title = $this->getH1($document); if (empty($title)) { $title = $this->getTitle($document); if (empty($title)) { return $attributes; } } return $attributes->put(self::key(), new Attribute(self::key(), $title, $this->clock->now()->elapsedSince($start)->milliseconds())); }
public function parse(RequestInterface $request, ResponseInterface $response, MapInterface $attributes) : MapInterface { $start = $this->clock->now(); if (!$this->isHtml($attributes)) { return $attributes; } $document = $this->reader->read($response->body()); try { $articles = (new Elements('article'))((new Body())($document)); } catch (ElementNotFoundException $e) { return $attributes; } if ($articles->size() <= 1) { return $attributes; } return $attributes->put(self::key(), new Attribute(self::key(), true, $this->clock->now()->elapsedSince($start)->milliseconds())); }
public function parse(RequestInterface $request, ResponseInterface $response, MapInterface $attributes) : MapInterface { $start = $this->clock->now(); if (!$this->isHtml($attributes)) { return $attributes; } $document = $this->reader->read($response->body()); try { $base = (new Element('base'))((new Head())($document)); } catch (ElementNotFoundException $e) { return $attributes; } if (!$base instanceof Base) { return $attributes; } return $attributes->put(self::key(), new Attribute(self::key(), $base->href(), $this->clock->now()->elapsedSince($start)->milliseconds())); }
/** * Sends the HTTP response back to a HTTP client. * * This calls php's header() function and streams the body to php://output. * * @param ResponseInterface $response * @return void */ static function sendResponse(ResponseInterface $response) { header('HTTP/' . $response->getHttpVersion() . ' ' . $response->getStatus() . ' ' . $response->getStatusText()); foreach ($response->getHeaders() as $key => $value) { foreach ($value as $k => $v) { if ($k === 0) { header($key . ': ' . $v); } else { header($key . ': ' . $v, false); } } } $body = $response->getBody(); if (is_null($body)) { return; } $contentLength = $response->getHeader('Content-Length'); if ($contentLength !== null) { $output = fopen('php://output', 'wb'); if (is_resource($body) && get_resource_type($body) == 'stream') { stream_copy_to_stream($body, $output, $contentLength); } else { fwrite($output, $body, $contentLength); } } else { file_put_contents('php://output', $body); } if (is_resource($body)) { fclose($body); } }
public function render(ResponseInterface $response, $template, array $data = []) { if (isset($data['template'])) { throw new \InvalidArgumentException("Duplicate template key found"); } if (!is_file($this->templatePath . $template)) { throw new \RuntimeException("View cannot render `{$template}` because the template does not exist"); } $render = function ($template, $data) { extract($data); include $template; }; ob_start(); $render($this->templatePath . $template, $data); $output = ob_get_clean(); $response->getBody()->write($output); return $response; }
public function render(ResponseInterface $response, EventInterface $e = null) { $values = $response->getValues(); $globals = array(); if (isset($values['content'])) { $globals = $values['content']; } $view = $this->getView(); $views = array(); foreach ($values as $context => $vars) { $view->clearVars(); $view->assign($globals + $vars); $script = $context . '.phtml'; $views[$context] = $view->render($script); } $layout = $this->getLayout(); $layout->assign($views); return $layout->render(); }
public function parse(RequestInterface $request, ResponseInterface $response, MapInterface $attributes) : MapInterface { $start = $this->clock->now(); if (!$this->isHtml($attributes)) { return $attributes; } $document = $this->reader->read($response->body()); try { $citations = (new Elements('cite'))((new Body())($document)); } catch (ElementNotFoundException $e) { return $attributes; } $citations = $citations->reduce(new Set('string'), function (Set $citations, NodeInterface $cite) : Set { return $citations->add(trim((new Text())($cite))); }); if ($citations->size() === 0) { return $attributes; } return $attributes->put(self::key(), new Attribute(self::key(), $citations, $this->clock->now()->elapsedSince($start)->milliseconds())); }
public function parse(RequestInterface $request, ResponseInterface $response, MapInterface $attributes) : MapInterface { $start = $this->clock->now(); if (!$this->isHtml($attributes)) { return $attributes; } $document = $this->reader->read($response->body()); try { $metas = (new Elements('meta'))((new Head())($document)); } catch (ElementNotFoundException $e) { return $attributes; } $meta = $metas->filter(function (ElementInterface $meta) : bool { return $meta->attributes()->contains('charset'); }); if ($meta->size() !== 1) { return $attributes; } return $attributes->put(self::key(), new Attribute(self::key(), $meta->current()->attributes()->get('charset')->value(), $this->clock->now()->elapsedSince($start)->milliseconds())); }
public function parse(RequestInterface $request, ResponseInterface $response, MapInterface $attributes) : MapInterface { $start = $this->clock->now(); $languages = null; if (!$this->isHtml($attributes)) { return $attributes; } $document = $this->reader->read($response->body()); try { $html = (new Element('html'))($document); if ($html->attributes()->contains('lang')) { $languages = $html->attributes()->get('lang'); } } catch (ElementNotFoundException $e) { //pass } if (!$languages instanceof AttributeInterface) { try { $metas = (new Elements('meta'))((new Head())($document))->filter(function (ElementInterface $element) : bool { return $element->attributes()->contains('http-equiv') && $element->attributes()->contains('content'); })->filter(function (ElementInterface $meta) : bool { $header = $meta->attributes()->get('http-equiv')->value(); $header = new Str($header); return (string) $header->toLower() === 'content-language'; }); if ($metas->size() === 1) { $languages = $metas->current()->attributes()->get('content'); } } catch (ElementNotFoundException $e) { //pass } } if (!$languages instanceof AttributeInterface) { return $attributes; } $languages = $this->parseAttribute($languages); if ($languages->size() === 0) { return $attributes; } return $attributes->put(self::key(), new Attribute(self::key(), $languages, $this->clock->now()->elapsedSince($start)->milliseconds())); }
public function parse(RequestInterface $request, ResponseInterface $response, MapInterface $attributes) : MapInterface { $start = $this->clock->now(); if (!$this->isHtml($attributes)) { return $attributes; } $document = $this->reader->read($response->body()); try { $links = (new Elements('link'))((new Head())($document))->filter(function (NodeInterface $link) : bool { return $link instanceof Link; })->filter(function (Link $link) : bool { return $link->relationship() === 'alternate' && $link->attributes()->contains('type') && $link->attributes()->get('type')->value() === 'application/rss+xml'; }); } catch (ElementNotFoundException $e) { return $attributes; } if ($links->size() !== 1) { return $attributes; } return $attributes->put(self::key(), new Attribute(self::key(), $this->resolver->resolve($request, $attributes, $links->current()->href()), $this->clock->now()->elapsedSince($start)->milliseconds())); }
public function parse(RequestInterface $request, ResponseInterface $response, MapInterface $attributes) : MapInterface { $start = $this->clock->now(); if (!$this->isHtml($attributes)) { return $attributes; } $document = $this->reader->read($response->body()); try { $anchors = (new Elements('a'))((new Body())($document)); } catch (ElementNotFoundException $e) { return $attributes; } $anchors = $anchors->filter(function (NodeInterface $node) : bool { return $node instanceof A; })->filter(function (A $anchor) : bool { return (new Str((string) $anchor->href()))->match('~^#~'); })->reduce(new Set('string'), function (Set $anchors, A $anchor) : Set { return $anchors->add(substr((string) $anchor->href(), 1)); }); return $attributes->put(self::key(), new Attribute(self::key(), $anchors, $this->clock->now()->elapsedSince($start)->milliseconds())); }
public function parse(RequestInterface $request, ResponseInterface $response, MapInterface $attributes) : MapInterface { $start = $this->clock->now(); if (!$this->isHtml($attributes)) { return $attributes; } $document = $this->reader->read($response->body()); $document = (new RemoveNodes($this->toIgnore))($document); try { $body = (new Body())($document); } catch (ElementNotFoundException $e) { return $attributes; } foreach (['article', 'document', 'main'] as $role) { $elements = (new Role($role))($body); if ($elements->size() === 1) { break; } } if ($elements->size() === 1) { $node = $elements->current(); } else { foreach (['main', 'article'] as $tag) { $elements = (new Elements($tag))($body); if ($elements->size() === 1) { break; } } if ($elements->size() === 1) { $node = $elements->current(); } else { $node = (new FindContentNode())((new Map('int', NodeInterface::class))->put(0, $body)); } } $text = trim((new Text())($node)); if (empty($text)) { return $attributes; } return $attributes->put(self::key(), new Attribute(self::key(), $text, $this->clock->now()->elapsedSince($start)->milliseconds())); }
public function parse(RequestInterface $request, ResponseInterface $response, MapInterface $attributes) : MapInterface { $start = $this->clock->now(); if (!$this->isHtml($attributes)) { return $attributes; } $document = $this->reader->read($response->body()); $links = new Set(UrlInterface::class); try { $links = (new Elements('link'))((new Head())($document))->filter(function (NodeInterface $link) : bool { return $link instanceof Link; })->filter(function (Link $link) : bool { return in_array($link->relationship(), ['first', 'next', 'previous', 'last'], true); })->reduce($links, function (Set $links, Link $link) : Set { return $links->add($link->href()); }); } catch (ElementNotFoundException $e) { //pass } try { $links = (new Elements('a'))((new Body())($document))->filter(function (NodeInterface $a) : bool { return $a instanceof A; })->filter(function (A $a) : bool { return substr((string) $a, 0, 1) !== '#'; })->reduce($links, function (Set $links, A $a) : Set { return $links->add($a->href()); }); } catch (ElementNotFoundException $e) { //pass } $links = $links->map(function (UrlInterface $link) use($request, $attributes) : UrlInterface { return $this->resolver->resolve($request, $attributes, $link); }); $links = (new RemoveDuplicatedUrls())($links); if ($links->size() === 0) { return $attributes; } return $attributes->put(self::key(), new Attribute(self::key(), $links, $this->clock->now()->elapsedSince($start)->milliseconds())); }
public function parse(RequestInterface $request, ResponseInterface $response, MapInterface $attributes) : MapInterface { $start = $this->clock->now(); if (!$this->isHtml($attributes)) { return $attributes; } $document = $this->reader->read($response->body()); try { $links = (new Elements('link'))((new Head())($document)); } catch (ElementNotFoundException $e) { return $attributes; } $link = $links->filter(function (NodeInterface $link) : bool { return $link instanceof Link; })->filter(function (Link $link) : bool { return (string) $link->href()->scheme() === 'android-app'; }); if ($link->size() !== 1) { return $attributes; } return $attributes->put(self::key(), new Attribute(self::key(), $link->current()->href(), $this->clock->now()->elapsedSince($start)->milliseconds())); }
/** * Sends the HTTP response back to a HTTP client. * * This calls php's header() function and streams the body to php://output. * * @param ResponseInterface $response * @return void */ static function sendResponse(ResponseInterface $response) { header('HTTP/' . $response->getHttpVersion() . ' ' . $response->getStatus() . ' ' . $response->getStatusText()); foreach ($response->getHeaders() as $key => $value) { header($key . ': ' . $value); } file_put_contents('php://output', $response->getBody()); }