public function __invoke($request, $response, $next)
{
$config = $this->config;
$production = !Configure::read('debug');
if ($production && !$config->general('alwaysEnableController')) {
return $next($request, $response);
}
// Make sure the request looks like an asset.
$targetName = $this->getName($config, $request->getUri()->getPath());
if (!$targetName) {
return $next($request, $response);
}
$queryParams = $request->getQueryParams();
if (isset($queryParams['theme'])) {
$config->theme($queryParams['theme']);
}
$factory = new Factory($config);
$assets = $factory->assetCollection();
if (!$assets->contains($targetName)) {
return $next($request, $response);
}
$build = $assets->get($targetName);
try {
$compiler = $factory->cachedCompiler();
$contents = $compiler->generate($build);
} catch (Exception $e) {
throw new NotFoundException($e->getMessage());
}
return $this->respond($response, $contents, $build);
}