Example #1
0
 /**
  * {@inheritdoc}
  */
 public function process()
 {
     $filters = new FilterCollection(array(new CssRewriteFilter()));
     $assets = new AssetCollection();
     $styles = $this->packageStyles($this->packages);
     foreach ($styles as $package => $packageStyles) {
         foreach ($packageStyles as $style => $paths) {
             foreach ($paths as $path) {
                 // The full path to the CSS file.
                 $assetPath = realpath($path);
                 // The root of the CSS file.
                 $sourceRoot = dirname($path);
                 // The style path to the CSS file when external.
                 $sourcePath = $package . '/' . $style;
                 // Where the final CSS will be generated.
                 $targetPath = $this->componentDir;
                 // Build the asset and add it to the collection.
                 $asset = new FileAsset($assetPath, $filters, $sourceRoot, $sourcePath);
                 $asset->setTargetPath($targetPath);
                 $assets->add($asset);
             }
         }
     }
     $css = $assets->dump();
     if (file_put_contents($this->componentDir . '/require.css', $css) === FALSE) {
         $this->io->write('<error>Error writing require.css to destination</error>');
         return false;
     }
 }
 public function onTerminate(CarewEvent $carewEvent)
 {
     foreach ($this->finder->in($carewEvent->getArgument('webDir')) as $file) {
         $asset = new FileAsset($file->getPathname(), array($this->getFilter()));
         file_put_contents($file->getPathname(), $asset->dump());
     }
 }
 /**
  * Adds support for pipeline assets.
  *
  * {@inheritdoc}
  */
 protected function parseInput($input, array $options = array())
 {
     if (is_string($input) && '|' == $input[0]) {
         switch (pathinfo($options['output'], PATHINFO_EXTENSION)) {
             case 'js':
                 $type = 'js';
                 break;
             case 'css':
                 $type = 'css';
                 break;
             default:
                 throw new \RuntimeException('Unsupported pipeline asset type provided: ' . $input);
         }
         $assets = new AssetCollection();
         foreach ($this->locator->locatePipelinedAssets(substr($input, 1), $type) as $formula) {
             $filters = array();
             if ($formula['filter']) {
                 $filters[] = $this->getFilter($formula['filter']);
             }
             $asset = new FileAsset($formula['root'] . '/' . $formula['file'], $filters, $options['root'][0], $formula['file']);
             $asset->setTargetPath($formula['file']);
             $assets->add($asset);
         }
         return $assets;
     }
     return parent::parseInput($input, $options);
 }
Example #4
0
 public function fonts(array $params)
 {
     $font = $params['font'];
     if (strlen($font) < 1) {
         return false;
     }
     $fontfiles = Registry::getInstance()->assets['fonts'];
     if (isset($fontfiles[$font])) {
         $asset = new FileAsset($fontfiles[$font]);
         switch (strtolower(substr($font, strpos($font, ".")))) {
             case ".eot":
                 $type = "application/vnd.ms-fontobject";
                 break;
             case "otf":
                 $type = "font/opentype";
                 break;
             case "ttf":
                 $type = "application/x-font-ttf";
                 break;
             case ".woff":
                 $type = "application/x-font-woff";
                 break;
             default:
                 $type = "application/x-font";
         }
         header('Content-Type: ' . $type);
         echo $asset->dump();
     }
     return true;
 }
 public function testCompassMixin()
 {
     $asset = new FileAsset(__DIR__ . '/fixtures/compass/compass.sass');
     $asset->load();
     $this->filter->filterLoad($asset);
     $this->assertContains('text-decoration', $asset->getContent());
 }
Example #6
0
 public function testCompassExtensionCanBeDisabled()
 {
     $this->setExpectedException("Exception", "Undefined mixin box-shadow: failed at `@include box-shadow(10px " . "10px 8px red);` line: 4");
     $asset = new FileAsset(__DIR__ . '/fixtures/sass/main_compass.scss');
     $asset->load();
     $this->getFilter(false)->filterLoad($asset);
 }
 public function testAssetLastModifiedTimestampIsPrependBeforeFileExtension()
 {
     $asset = new FileAsset(TEST_ASSETS_DIR . '/css/global.css');
     $asset->setTargetPath(TEST_PUBLIC_DIR . '/css/global.css');
     $this->cacheBuster->process($asset);
     $this->assertSame(TEST_PUBLIC_DIR . '/css/global.' . $asset->getLastModified() . '.css', $asset->getTargetPath());
 }
Example #8
0
 public function testRelativeSourceUrlImportImports()
 {
     $asset = new FileAsset(__DIR__ . '/fixtures/jsmin/js.js');
     $asset->load();
     $filter = new JSMinFilter();
     $filter->filterDump($asset);
     $this->assertEquals('var a="abc";;;var bbb="u";', $asset->getContent());
 }
 public function testRelativeSourceUrlImportImports()
 {
     $asset = new FileAsset(__DIR__ . '/fixtures/minifycsscompressor/main.css');
     $asset->load();
     $filter = new MinifyCssCompressorFilter();
     $filter->filterDump($asset);
     $this->assertEquals('body{color:white}body{background:black}', $asset->getContent());
 }
 public function testNonCssImport()
 {
     $asset = new FileAsset(__DIR__ . '/fixtures/cssimport/noncssimport.css', array(), __DIR__ . '/fixtures/cssimport', 'noncssimport.css');
     $asset->load();
     $filter = new CssImportFilter();
     $filter->filterLoad($asset);
     $this->assertEquals(file_get_contents(__DIR__ . '/fixtures/cssimport/noncssimport.css'), $asset->getContent(), '->filterLoad() skips non css');
 }
Example #11
0
 protected function createAssetManager()
 {
     $asset = new FileAsset(__DIR__ . '/../../../tests/test.css');
     $asset->setTargetPath('css/test.css');
     $assetManager = new AssetManager();
     $assetManager->set('test_css', $asset);
     return $assetManager;
 }
 public function testRelativeSourceUrlImportImports()
 {
     $asset = new FileAsset(__DIR__ . '/fixtures/jsmin/js.js');
     $asset->load();
     $filter = new JSqueezeFilter();
     $filter->filterDump($asset);
     $this->assertEquals(";var a='abc',bbb='u';", $asset->getContent());
 }
Example #13
0
 public function testPacker()
 {
     $asset = new FileAsset(__DIR__ . '/fixtures/packer/example.js');
     $asset->load();
     $filter = new PackerFilter();
     $filter->filterDump($asset);
     $this->assertEquals("var exampleFunction=function(arg1,arg2){alert('exampleFunction called!')}", $asset->getContent());
 }
 public function filterLoad(AssetInterface $asset)
 {
     $importFilter = $this->importFilter;
     $sourceRoot = $asset->getSourceRoot();
     $sourcePath = $asset->getSourcePath();
     $callback = function ($matches) use($importFilter, $sourceRoot, $sourcePath) {
         if (!$matches['url']) {
             return $matches[0];
         }
         if (null === $sourceRoot) {
             return $matches[0];
         }
         $importRoot = $sourceRoot;
         if (false !== strpos($matches['url'], '://')) {
             // absolute
             list($importScheme, $tmp) = explode('://', $matches['url'], 2);
             list($importHost, $importPath) = explode('/', $tmp, 2);
             $importRoot = $importScheme . '://' . $importHost;
         } elseif (0 === strpos($matches['url'], '//')) {
             // protocol-relative
             list($importHost, $importPath) = explode('/', substr($matches['url'], 2), 2);
             $importHost = '//' . $importHost;
         } elseif ('/' == $matches['url'][0]) {
             // root-relative
             $importPath = substr($matches['url'], 1);
         } elseif (null !== $sourcePath) {
             // document-relative
             $importPath = $matches['url'];
             if ('.' != ($sourceDir = dirname($sourcePath))) {
                 $importPath = $sourceDir . '/' . $importPath;
             }
         } else {
             return $matches[0];
         }
         // ignore other imports
         if ('css' != pathinfo($importPath, PATHINFO_EXTENSION)) {
             return $matches[0];
         }
         $importSource = $importRoot . '/' . $importPath;
         if (false !== strpos($importSource, '://') || 0 === strpos($importSource, '//')) {
             $import = new HttpAsset($importSource, array($importFilter), true);
         } elseif (!file_exists($importSource)) {
             // ignore not found imports
             return $matches[0];
         } else {
             $import = new FileAsset($importSource, array($importFilter), $importRoot, $importPath);
         }
         $import->setTargetPath($sourcePath);
         return $import->dump();
     };
     $content = $asset->getContent();
     $lastHash = md5($content);
     do {
         $content = $this->filterImports($content, $callback);
         $hash = md5($content);
     } while ($lastHash != $hash && ($lastHash = $hash));
     $asset->setContent($content);
 }
 public function testCompilation()
 {
     $asset = new FileAsset(__DIR__ . '/stubs/coffeescript/script.coffee');
     $asset->load();
     $filter = new CoffeeScriptphpFilter();
     $filter->filterLoad($asset);
     $expected = file_get_contents(__DIR__ . '/stubs/coffeescript/script.js');
     $this->assertEquals($expected, $asset->getContent());
 }
 public function testCssEmbedMhtml()
 {
     $asset = new FileAsset(__DIR__ . '/fixtures/cssembed/test.css');
     $asset->load();
     $this->filter->setMhtml(true);
     $this->filter->setMhtmlRoot('/test');
     $this->filter->filterDump($asset);
     $this->assertContains('url(mhtml:/test/!', $asset->getContent());
 }
 public function testFileAsset()
 {
     $asset = new FileAsset(__DIR__ . '/fixtures/handlebars/template.handlebars');
     $asset->load();
     $this->filter->filterLoad($asset);
     $this->assertNotContains('{{ var }}', $asset->getContent());
     $this->assertContains('Ember.TEMPLATES["template"]', $asset->getContent());
     $this->assertContains('data.buffer.push("<div id=\\"test\\"><h2>");', $asset->getContent());
 }
 public function testMinimizeHandlebars()
 {
     $asset = new FileAsset(__DIR__ . '/fixtures/handlebars/template.handlebars');
     $asset->load();
     $this->filter->setMinimize(true);
     $this->filter->filterLoad($asset);
     $this->assertNotContains('{{ var }}', $asset->getContent());
     $this->assertNotContains("\n", $asset->getContent());
 }
Example #19
0
 public function testRelativeSourceUrlImportImports()
 {
     $asset = new FileAsset(__DIR__ . '/fixtures/cssmin/main.css');
     $asset->load();
     $filter = new CssMinFilter(__DIR__ . '/fixtures/cssmin');
     $filter->setFilter('ImportImports', true);
     $filter->filterDump($asset);
     $this->assertEquals('body{color:white}body{background:black}', $asset->getContent());
 }
 public function testCssEmbedDataUri()
 {
     $data = base64_encode(file_get_contents(__DIR__ . '/fixtures/home.png'));
     $asset = new FileAsset(__DIR__ . '/fixtures/cssembed/test.css');
     $asset->load();
     $filter = new PhpCssEmbedFilter();
     $filter->filterLoad($asset);
     $this->assertContains('url(data:image/png;base64,' . $data, $asset->getContent());
 }
Example #21
0
 public function testCompilingWithImportPath()
 {
     $asset = new FileAsset(__DIR__ . '/stubs/sass/style.sass');
     $asset->load();
     $filter = new SassphpFilter();
     $filter->addImportPath(__DIR__ . '/stubs/sass/import_path');
     $filter->filterLoad($asset);
     $expected = file_get_contents(__DIR__ . '/stubs/sass/style.css');
     $this->assertEquals($expected, $asset->getContent());
 }
Example #22
0
 /**
  * @dataProvider getImages
  */
 public function testFilter($image)
 {
     $asset = new FileAsset($image);
     $asset->load();
     $before = $asset->getContent();
     $this->filter->filterDump($asset);
     $this->assertNotEmpty($asset->getContent(), '->filterLoad() sets content');
     $this->assertNotEquals($before, $asset->getContent(), '->filterLoad() changes the content');
     $this->assertMimeType('image/png', $asset->getContent(), '->filterLoad() creates PNG data');
 }
 public function testFilter()
 {
     $asset = new FileAsset(__DIR__ . '/fixtures/home.jpg');
     $asset->load();
     $before = $asset->getContent();
     $this->filter->filterDump($asset);
     $this->assertNotEmpty($asset->getContent(), '->filterLoad() sets content');
     $this->assertNotEquals($before, $asset->getContent(), '->filterDump() changes the content');
     $this->assertMimeType('image/jpeg', $asset->getContent(), '->filterDump() creates JPEG data');
 }
    public function testCompressImport()
    {
        $expected = <<<EOF
.foo{color:blue}.foo{color:red}
EOF;
        $asset = new FileAsset(__DIR__ . '/fixtures/less/main.less');
        $asset->load();
        $this->filter->addTreeOption('compress', true);
        $this->filter->filterLoad($asset);
        $this->assertEquals($expected, $asset->getContent(), '->filterLoad() sets an include path based on source url');
    }
 /**
  * {@inheritdoc}
  */
 public function process()
 {
     $filters = array(new CssRewriteFilter());
     if ($this->config->has('component-styleFilters')) {
         $customFilters = $this->config->get('component-styleFilters');
         if (isset($customFilters) && is_array($customFilters)) {
             foreach ($customFilters as $filter => $filterParams) {
                 $reflection = new \ReflectionClass($filter);
                 $filters[] = $reflection->newInstanceArgs($filterParams);
             }
         }
     }
     $filterCollection = new FilterCollection($filters);
     $assets = new AssetCollection();
     $styles = $this->packageStyles($this->packages);
     foreach ($styles as $package => $packageStyles) {
         $packageAssets = new AssetCollection();
         $packagePath = $this->componentDir . '/' . $package;
         foreach ($packageStyles as $style => $paths) {
             foreach ($paths as $path) {
                 // The full path to the CSS file.
                 $assetPath = realpath($path);
                 // The root of the CSS file.
                 $sourceRoot = dirname($path);
                 // The style path to the CSS file when external.
                 $sourcePath = $package . '/' . $style;
                 //Replace glob patterns with filenames.
                 $filename = basename($style);
                 if (preg_match('~^\\*(\\.[^\\.]+)$~', $filename, $matches)) {
                     $sourcePath = str_replace($filename, basename($assetPath), $sourcePath);
                 }
                 // Where the final CSS will be generated.
                 $targetPath = $this->componentDir;
                 // Build the asset and add it to the collection.
                 $asset = new FileAsset($assetPath, $filterCollection, $sourceRoot, $sourcePath);
                 $asset->setTargetPath($targetPath);
                 $assets->add($asset);
                 // Add asset to package collection.
                 $sourcePath = preg_replace('{^.*' . preg_quote($package) . '/}', '', $sourcePath);
                 $asset = new FileAsset($assetPath, $filterCollection, $sourceRoot, $sourcePath);
                 $asset->setTargetPath($packagePath);
                 $packageAssets->add($asset);
             }
         }
         if (file_put_contents($packagePath . '/' . $package . '-built.css', $packageAssets->dump()) === FALSE) {
             $this->io->write("<error>Error writing {$package}-built.css to destination</error>");
         }
     }
     if (file_put_contents($this->componentDir . '/require.css', $assets->dump()) === FALSE) {
         $this->io->write('<error>Error writing require.css to destination</error>');
         return false;
     }
     return null;
 }
 public function testAssetWithInputVars()
 {
     $asset = new FileAsset(__DIR__ . '/Fixture/messages.{locale}.js', array(), null, null, array('locale'));
     $asset->setTargetPath('messages.{locale}.js');
     $this->writer->writeAsset($asset);
     $this->assertFileExists($this->dir . '/messages.en.js');
     $this->assertFileExists($this->dir . '/messages.de.js');
     $this->assertFileExists($this->dir . '/messages.fr.js');
     $this->assertEquals('var messages = {"text.greeting": "Hello %name%!"};', file_get_contents($this->dir . '/messages.en.js'));
     $this->assertEquals('var messages = {"text.greeting": "Hallo %name%!"};', file_get_contents($this->dir . '/messages.de.js'));
     $this->assertEquals('var messages = {"text.greet": "All\\u00f4 %name%!"};', file_get_contents($this->dir . '/messages.fr.js'));
 }
Example #27
0
 public function background(array $params)
 {
     $backgrounds = Registry::getInstance()->assets['backgrounds'];
     $dt = new \DateTime();
     $dt->setTime(date("H"), 0, 0);
     srand($dt->getTimestamp());
     $asset = new \Assetic\Asset\FileAsset($backgrounds[rand(0, count($backgrounds) - 1)]);
     header('Content-Type: image/jpeg');
     header("Cache-Control: public, max-age=3600, s-max-age=1800");
     echo $asset->dump();
     return true;
 }
 public function testFilterLoad()
 {
     $asset = new FileAsset(__DIR__ . '/fixtures/sprockets/main.js');
     $asset->load();
     $this->filter->addIncludeDir(__DIR__ . '/fixtures/sprockets/lib1');
     $this->filter->addIncludeDir(__DIR__ . '/fixtures/sprockets/lib2');
     $this->filter->setAssetRoot($this->assetRoot);
     $this->filter->filterLoad($asset);
     $this->assertContains('/* header.js */', $asset->getContent());
     $this->assertContains('/* include.js */', $asset->getContent());
     $this->assertContains('/* footer.js */', $asset->getContent());
     $this->assertFileExists($this->assetRoot . '/images/image.gif');
 }
 /**
  * @return \Assetic\AssetManager
  */
 protected function createAssetManager()
 {
     $manager = new \Assetic\AssetManager();
     $localeInfo = $this->getLocaleInfo();
     foreach ($localeInfo as $lang => $countries) {
         $manager->set('locale_' . $lang, $asset = new FileAsset(dirname(dirname(__DIR__)) . '/Resources/public/images/' . $lang . '.png'));
         $asset->setTargetPath('images/locale/' . $lang . '.png');
         foreach ($countries as $country) {
             $manager->set('locale_' . $lang . '_' . strtolower($country), $asset = new FileAsset(dirname(dirname(__DIR__)) . '/Resources/public/images/' . $lang . '-' . $country . '.png'));
             $asset->setTargetPath('images/locale/' . $lang . '-' . $country . '.png');
         }
     }
     return $manager;
 }
Example #30
0
    public function testImport()
    {
        $asset = new FileAsset(__DIR__ . '/../fixtures/sass/main.scss');
        $asset->load();
        $this->filter->setStyle(ScssFilter::STYLE_COMPACT);
        $this->filter->filterLoad($asset);
        $expected = <<<EOF
.foo { color: blue; }

.foo { color: red; }

EOF;
        $this->assertEquals($expected, $asset->getContent(), '->filterLoad() loads imports');
    }