/** * 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; }
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 testAssetCollectionGlob() { $config = AssetConfig::buildFromIniFile($this->globFile); $factory = new Factory($config); $collection = $factory->assetCollection(); $this->assertCount(1, $collection); $this->assertTrue($collection->contains('all_classes.js')); $asset = $collection->get('all_classes.js'); $files = $asset->files(); $this->assertCount(6, $files, 'Not enough files'); $this->assertEquals(APP . 'js/classes/base_class.js', $files[0]->path()); $this->assertEquals(APP . 'js/classes/base_class_two.js', $files[1]->path()); $this->assertEquals(APP . 'js/classes/double_inclusion.js', $files[2]->path()); $this->assertEquals(APP . 'js/classes/nested_class.js', $files[3]->path()); $this->assertEquals(APP . 'js/classes/slideshow.js', $files[4]->path()); $this->assertEquals(APP . 'js/classes/template.js', $files[5]->path()); }
public function testAssetCollectionGlob() { $config = AssetConfig::buildFromIniFile($this->globFile); $factory = new Factory($config); $collection = $factory->assetCollection(); $this->assertCount(1, $collection); $this->assertTrue($collection->contains('all_classes.js')); $asset = $collection->get('all_classes.js'); $this->assertEquals([APP . 'js/', APP . 'js/classes/', APP . 'js/secondary/'], $asset->paths(), 'Should have expanded paths'); $files = $asset->files(); $this->assertCount(6, $files, 'Not enough files'); $expectedPaths = [APP . 'js/classes/base_class.js', APP . 'js/classes/base_class_two.js', APP . 'js/classes/double_inclusion.js', APP . 'js/classes/nested_class.js', APP . 'js/classes/slideshow.js', APP . 'js/classes/template.js']; foreach ($expectedPaths as $i => $expected) { $this->assertEquals($expected, $files[$i]->path()); } }