MiniAsset\Middleware\AssetMiddleware::__invoke PHP Метод

__invoke() публичный Метод

Apply the asset middleware.
public __invoke ( Psr\Http\Message\ServerRequestInterface $request, Psr\Http\Message\ResponseInterface $response, callable $next ) : Psr\Http\Message\ResponseInterface
$request Psr\Http\Message\ServerRequestInterface The request.
$response Psr\Http\Message\ResponseInterface The response.
$next callable The callable to invoke the next middleware layer.
Результат Psr\Http\Message\ResponseInterface A response.
    public function __invoke($request, $response, $next)
    {
        $path = $request->getUri()->getPath();
        if (strpos($path, $this->urlPrefix) !== 0) {
            // Not an asset request.
            return $next($request, $response);
        }
        $factory = new Factory($this->config);
        $assets = $factory->assetCollection();
        $targetName = substr($path, strlen($this->urlPrefix));
        if (!$assets->contains($targetName)) {
            // Unknown build.
            return $next($request, $response);
        }
        try {
            $build = $assets->get($targetName);
            $compiler = $factory->cachedCompiler($this->outputDir);
            $contents = $compiler->generate($build);
        } catch (Exception $e) {
            // Could not build the asset.
            $response->getBody()->write($e->getMessage());
            return $response->withStatus(400)->withHeader('Content-Type', 'text/plain');
        }
        return $this->respond($response, $contents, $build->ext());
    }