Create a Caching Compiler
public cachedCompiler ( string $outputDir = '', boolean $debug = false ) : |
||
$outputDir | string | The directory to output cached files to. |
$debug | boolean | Whether or not to enable debugging mode for the compiler. |
Результат |
/** * Apply the asset middleware. * * @param \Psr\Http\Message\ServerRequestInterface $request The request. * @param \Psr\Http\Message\ResponseInterface $response The response. * @param callable $next The callable to invoke the next middleware layer. * @return \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()); }
/** * Generate and save the cached file for a build target. * * @param \MiniAsset\Factory $factory The factory class. * @param \MiniAsset\AssetTarget $build The build target. * @return void */ protected function _buildTarget($factory, $build) { $writer = $factory->writer(); $compiler = $factory->cachedCompiler(); $name = $writer->buildFileName($build); if ($writer->isFresh($build) && !$this->cli->arguments->defined('force')) { $this->verbose('<light_blue>Skip building</light_blue> ' . $name . ' existing file is still fresh.', 'S'); return; } $writer->invalidate($build); $name = $writer->buildFileName($build); try { $contents = $compiler->generate($build); $writer->write($build, $contents); $this->verbose('<green>Saved file</green> for ' . $name, '.'); } catch (Exception $e) { $this->cli->err('<red>Error:</red> ' . $e->getMessage()); } }
/** * Create a Caching Compiler * * @param string $outputDir The directory to output cached files to. * @param bool $debug Whether or not to enable debugging mode for the compiler. * @return \MiniAsset\Output\CachedCompiler */ public function cachedCompiler($outputDir = '', $debug = false) { $outputDir = $outputDir ?: CACHE . 'asset_compress' . DS; $debug = $debug ?: Configure::read('debug'); return parent::cachedCompiler($outputDir, $debug); }
protected function instance() { $factory = new Factory($this->config); return $factory->cachedCompiler(TMP); }