/** * Checks if request is for a compiled asset, otherwise skip any operation * * @param Event $event containing the request and response object * @throws \Cake\Network\Exception\NotFoundException * @return \Cake\Network\Response|null Response if the client is requesting a recognized asset, null otherwise */ public function beforeDispatch(Event $event) { $request = $event->data['request']; $response = $event->data['response']; $config = $this->_getConfig(); $production = !Configure::read('debug'); if ($production && !$config->general('alwaysEnableController')) { return null; } // Make sure the request looks like an asset. $targetName = $this->getName($config, $request->url); if (!$targetName) { return null; } if (isset($request->query['theme'])) { $config->theme($request->query['theme']); } $factory = new Factory($config); $assets = $factory->assetCollection(); if (!$assets->contains($targetName)) { return null; } $build = $assets->get($targetName); try { $compiler = $factory->cachedCompiler(); $contents = $compiler->generate($build); } catch (Exception $e) { throw new NotFoundException($e->getMessage()); } $response->type($build->ext()); $response->body($contents); $event->stopPropagation(); return $response; }
/** * Get an asset or delegate to the next middleware * * @param \Psr\Http\Message\ServerRequestInterface $request The request. * @param \Psr\Http\Message\ResponseInterface $response The response. * @param callable $next Callback to invoke the next middleware. * @return \Psr\Http\Message\ResponseInterface A response */ 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); }
/** * clear the builds for a specific extension. * * @return void */ protected function _clearBuilds() { $themes = (array) $this->config->general('themes'); if ($themes) { $this->config->theme($themes[0]); } $assets = $this->factory->assetCollection(); if (count($assets) === 0) { $this->err('No build targets defined, skipping'); return; } $targets = array_map(function ($target) { return $target->name(); }, iterator_to_array($assets)); $this->_clearPath(CACHE . 'asset_compress' . DS, $themes, $targets); $this->_clearPath($this->config->cachePath('js'), $themes, $targets); $this->_clearPath($this->config->cachePath('css'), $themes, $targets); }