/** * 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()); }
/** * Build all the files declared in the Configuration object. * * @return void */ protected function execute() { if ($this->cli->arguments->defined('bootstrap')) { $this->bootstrapApp(); } $factory = new Factory($this->config()); foreach ($factory->assetCollection() as $target) { $this->_buildTarget($factory, $target); } $this->cli->out('<green>Complete</green>'); return 0; }
protected function turnout() { $config = $this->config(); $factory = new Factory($config); foreach ($factory->assetCollection() as $target) { if (!is_dir($target->outputDir())) { if (!mkdir($target->outputDir(), 0777, true)) { $name = $target->name(); $verbose = '<red>Skip building ' . $name . ' output path could not be created.</red>'; $this->verbose($verbose, '<red>E<red>'); continue; } } $this->_buildTarget($factory, $target); } $this->cli->green('Complete'); return 0; }
/** * Create a single filter * * @param string $name The name of the filter to build. * @param array $config The configuration for the filter. * @return AssetCompress\Filter\AssetFilterInterface */ protected function buildFilter($name, $config) { $className = App::className($name, 'Filter'); if (!class_exists($className)) { $className = App::className('AssetCompress.' . $name, 'Filter'); } $className = $className ?: $name; return parent::buildFilter($className, $config); }
/** * 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()); } }
/** * Get an asset from the collection * * @param string $name The name of the asset you want. * @return null|AssetTarget Either null or the asset target. */ public function get($name) { if (!isset($this->indexed[$name])) { return null; } if (empty($this->indexed[$name])) { $this->indexed[$name] = $this->factory->target($name); } return $this->indexed[$name]; }
protected function execute() { if ($this->cli->arguments->defined('bootstrap')) { $this->bootstrapApp(); } $config = $this->config(); $factory = new Factory($config); $this->verbose('Clearing build timestamps.'); $writer = $factory->writer(); $writer->clearTimestamps(); $this->verbose('Clearing build files:'); $assets = $factory->assetCollection(); if (count($assets) === 0) { $this->cli->err('<red>No build targets defined</red>.'); return 1; } $targets = array_map(function ($target) { return $target->name(); }, iterator_to_array($assets)); $this->_clearPath($config->cachePath('js'), $targets); $this->_clearPath($config->cachePath('css'), $targets); $this->cli->out('<green>Complete</green>'); }
public function testWriter() { $config = AssetConfig::buildFromIniFile($this->integrationFile); $config->theme('Red'); $config->set('js.timestamp', true); $factory = new Factory($config); $writer = $factory->writer(); $expected = ['timestamp' => ['js' => true, 'css' => false], 'path' => TMP, 'theme' => 'Red']; $this->assertEquals($expected, $writer->config()); }
protected function instance() { $factory = new Factory($this->config); return $factory->cachedCompiler(TMP); }