public function testFilterDump() { $expected = 'body{baz:foo;foo:new;too:me}div.other,div.test{border:1px solid black}'; $inputCss = 'body { foo:bar; too: me;} body { baz:foo; } body { foo: new } div.test { border: 1px solid black; } div.other { border: 1px solid black} '; $asset = new StringAsset($inputCss); $asset->ensureFilter($this->filter); $result = $asset->dump(); $this->assertEquals($expected, $result); }
public function processFile($path, $config = []) { $rel = b::param('rel', false, $config); $url = b::param('url', false, $config); $useGlobalFilters = b::param('useGlobalFilters', true, $config); // get the file $file = $this->getFile($path); $root = pathinfo($path)['dirname']; $ext = pathinfo($path)['extension']; // get it's tree $content = $file->getContent(); if (empty($content)) { return $content; } $inc = []; // parse the string $found = $this->parseString($content, $root); if (b::param('filterOnly', false, $config) === 'true') { $found = ['filter' => $found['filter']]; } $tree = $this->getCombinedTree($found, $ext); $reduce = function ($items, $reduce) { $resp = []; foreach ($items as $key => $files) { $resp[] = $key; $resp += $reduce($files, $reduce); } return $resp; }; // loop through each file and append foreach (array_unique($reduce($tree, $reduce)) as $f) { if ($f === $path) { continue; } $inc[] = $this->processFile($f); } $source = false; $sourcePath = false; $targetPath = false; if ($url) { $parts = parse_url($url); $source = "{$parts['scheme']}://{$parts['host']}"; $targetPath = trim($parts['path'], '/'); $sourcePath = $targetPath . '/' . trim($rel, '/'); } $inc[] = $content; $a = new StringAsset(implode("\n", $inc), [], $source, $sourcePath); if ($targetPath) { $a->setTargetPath($targetPath); } // use filters if ($useGlobalFilters !== false) { if (array_key_exists($ext, $this->_filters)) { foreach ($this->_filters[$ext] as $filter) { if ($filter[1] === false and b::env() === 'dev') { continue; } $a->ensureFilter(new $filter[0]()); } } foreach ($this->_filters['*'] as $filter) { if ($filter[1] === false and b::env() === 'dev') { continue; } $a->ensureFilter(new $filter[0]()); } } $a->ensureFilter(new CssRewriteFilter()); return trim($a->dump()); }
/** * Gets the string asset * * @param string $content * @return StringAsset */ private function getStringAsset($content) { $stringAsset = new StringAsset($content); $stringAsset->ensureFilter($this->filter); return $stringAsset; }