cachedCompiler() public method

Create a Caching Compiler
public cachedCompiler ( string $outputDir = '', boolean $debug = false ) : CachedCompiler
$outputDir string The directory to output cached files to.
$debug boolean Whether or not to enable debugging mode for the compiler.
return MiniAsset\Output\CachedCompiler
Ejemplo n.º 1
0
 /**
  * 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());
 }
Ejemplo n.º 2
0
 /**
  * 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());
     }
 }
Ejemplo n.º 3
0
 /**
  * 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);
 }
Ejemplo n.º 4
0
 protected function instance()
 {
     $factory = new Factory($this->config);
     return $factory->cachedCompiler(TMP);
 }