/** * Checks if request is for a compiled asset, otherwise skip any operation * * @param Event $event containing the request and response object * @throws NotFoundException * @return void|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; } // Make sure the request looks like an asset. $targetName = $this->getName($config, $request->url); if (!$targetName) { return; } if (isset($request->query['theme'])) { $config->theme($request->query['theme']); } $factory = new Factory($config); $assets = $factory->assetCollection(); if (!$assets->contains($targetName)) { return; } $build = $assets->get($targetName); try { $compiler = $factory->compiler(); $cacher = $factory->cacher(); if ($cacher->isFresh($build)) { $contents = $cacher->read($build); } else { $contents = $compiler->generate($build); $cacher->write($build, $contents); } } catch (Exception $e) { throw new NotFoundException($e->getMessage()); } $response->type($build->ext()); $response->body($contents); $event->stopPropagation(); return $response; }