/** * Execute the middleware. * * @param ServerRequestInterface $request * @param ResponseInterface $response * @param callable $next * * @return ResponseInterface */ public function __invoke(ServerRequestInterface $request, ResponseInterface $response, callable $next) { if ($this->autodetect) { $this->basePath(Utils\Helpers::joinPath(self::detectBasePath($request), $this->basePath)); } $uri = $request->getUri(); $path = $this->getPath($uri->getPath()); $request = $request->withUri($uri->withPath($path)); $request = Middleware::setAttribute($request, self::KEY, $this->basePath); return $next($request, $response); }
/** * Returns the filename of the response file. * * @param RequestInterface $request * @param string $indexExt * * @return string */ private function getFilename(RequestInterface $request, $indexExt = 'html') { $path = $this->getPath($request->getUri()->getPath()); $parts = pathinfo($path); $path = isset($parts['dirname']) ? $parts['dirname'] : ''; $filename = isset($parts['basename']) ? $parts['basename'] : ''; //if it's a directory, append the index file if (empty($parts['extension'])) { $filename .= "/index.{$indexExt}"; } return Helpers::joinPath($this->directory, $path, $filename); }
/** * Returns the filename of the response file. * * @param RequestInterface $request * * @return string */ private function getFilename(RequestInterface $request) { $path = $this->getPath($request->getUri()->getPath()); $parts = pathinfo($path); $path = isset($parts['dirname']) ? $parts['dirname'] : ''; $filename = isset($parts['basename']) ? $parts['basename'] : ''; //if it's a directory, append "/index.html" if (empty($parts['extension'])) { $filename .= '/index.html'; } return Helpers::joinPath($this->storage, $path, $filename); }
/** * Execute the middleware. * * @param ServerRequestInterface $request * @param ResponseInterface $response * @param callable $next * * @return ResponseInterface */ public function __invoke(ServerRequestInterface $request, ResponseInterface $response, callable $next) { if (!Middleware::hasAttribute($request, FormatNegotiator::KEY)) { throw new RuntimeException('This middleware needs FormatNegotiator executed before'); } $renderer = $this->debugBar->getJavascriptRenderer(); //Is an asset? $path = $request->getUri()->getPath(); $renderPath = $renderer->getBaseUrl(); if (strpos($path, $renderPath) === 0) { $file = $renderer->getBasePath() . substr($path, strlen($renderPath)); if (file_exists($file)) { $body = Middleware::createStream(); $body->write(file_get_contents($file)); return $response->withBody($body); } } $response = $next($request, $response); //Fix the render baseUrl $renderPath = Utils\Helpers::joinPath(BasePath::getBasePath($request), $renderer->getBaseUrl()); $renderer->setBaseUrl($renderPath); $ajax = Utils\Helpers::isAjax($request); //Redirection response if (Utils\Helpers::isRedirect($response)) { if ($this->debugBar->isDataPersisted() || session_status() === PHP_SESSION_ACTIVE) { $this->debugBar->stackData(); } //Html response } elseif (FormatNegotiator::getFormat($request) === 'html') { if (!$ajax) { $response = $this->inject($response, $renderer->renderHead(), 'head'); } $response = $this->inject($response, $renderer->render(!$ajax), 'body'); //Ajax response } elseif ($ajax && $this->captureAjax) { $headers = $this->debugBar->getDataAsHeaders(); foreach ($headers as $name => $value) { $response = $response->withHeader($name, $value); } } return $response; }
/** * Parses the path and return the file and transform values. * For example, the path "/images/small.avatar.jpg" returns: * ["/images/avatar.jpg", "resizeCrop,50,50"]. * * @param string $path * * @return null|array [file, transform] */ private function parsePath($path) { $info = pathinfo($path); try { $pieces = explode('.', $info['filename'], 2); } catch (Exception $e) { return; } if (count($pieces) === 2) { list($transform, $file) = $pieces; //Check if the size is valid if (!isset($this->sizes[$transform])) { return; } return [Utils\Helpers::joinPath($info['dirname'], "{$file}." . $info['extension']), $this->sizes[$transform]]; } }
/** * Execute the middleware. * * @param ServerRequestInterface $request * @param ResponseInterface $response * @param callable $next * * @return ResponseInterface */ public function __invoke(ServerRequestInterface $request, ResponseInterface $response, callable $next) { $language = null; //Use path if ($this->usePath) { $uri = $request->getUri(); $path = ltrim($this->getPath($uri->getPath()), '/'); $dirs = explode('/', $path, 2); $first = array_shift($dirs); if (!empty($first) && in_array($first, $this->languages, true)) { $language = $first; //remove the language in the path $request = $request->withUri($uri->withPath('/' . array_shift($dirs))); } } //Use http headers if ($language === null) { $language = $this->negotiateHeader($request->getHeaderLine('Accept-Language'), new Negotiator(), $this->languages); if (empty($language)) { $language = isset($this->languages[0]) ? $this->languages[0] : null; } //Redirect to a path with the language if ($this->redirectStatus && $this->usePath) { $path = Utils\Helpers::joinPath($this->basePath, $language, $this->getPath($uri->getPath())); return self::getRedirectResponse($this->redirectStatus, $uri->withPath($path), $response); } } $request = Middleware::setAttribute($request, self::KEY, $language); if ($language !== null) { $response = $response->withHeader('Content-Language', $language); } return $next($request, $response); }
/** * Parses the path and return the file and transform values. * For example, the path "/images/small.avatar.jpg" returns: * ["/images/avatar.jpg", "resizeCrop,50,50"]. * * @param string $path * * @return false|array [file, transform] */ private function parsePath($path) { $info = pathinfo($path); $file = $info['basename']; $path = $info['dirname']; foreach ($this->sizes as $pattern => $transform) { if (strpos($pattern, '/') === false) { $patternFile = $pattern; $patternPath = ''; } else { $patternFile = pathinfo($pattern, PATHINFO_BASENAME); $patternPath = pathinfo($pattern, PATHINFO_DIRNAME); } if (substr($file, 0, strlen($patternFile)) === $patternFile && ($patternPath === '' || substr($path, -strlen($patternPath)) === $patternPath)) { return [Utils\Helpers::joinPath($path, substr($file, strlen($patternFile))), $transform]; } } return false; }
/** * Parses the path and return the file and transform values. * For example, the path "/images/small.avatar.jpg" returns: * ["/images/avatar.jpg", "resizeCrop,50,50"]. * * @param string $path * * @return false|array [file, transform] */ private function parsePath($path) { $info = pathinfo($path); $basename = $info['basename']; $dirname = $info['dirname']; foreach ($this->sizes as $prefix => $paths) { if (strpos($basename, $prefix) !== 0) { continue; } foreach ($paths as $path => $transform) { $needle = $path === '' ? '' : substr($dirname, -strlen($path)); if ($path === $needle) { return [Utils\Helpers::joinPath($dirname, substr($basename, strlen($prefix))), $transform]; } } } return false; }