/** * @dataProvider getFilters */ public function testImport($filter1, $filter2) { $asset = new FileAsset(__DIR__ . '/fixtures/cssimport/main.css', array(), __DIR__ . '/fixtures/cssimport', 'main.css'); $asset->setTargetPath('foo/bar.css'); $asset->ensureFilter($filter1); $asset->ensureFilter($filter2); $expected = <<<CSS /* main.css */ /* import.css */ body { color: red; } /* more/evenmore/deep1.css */ /* more/evenmore/deep2.css */ body { background: url(../more/evenmore/bg.gif); } body { color: black; } CSS; $this->assertEquals($expected, $asset->dump(), '->filterLoad() inlines CSS imports'); }
/** * @param $path String target path * @return \Assetic\Filter\FileAsset */ public function asset($opts = array()) { $asset = new FileAsset($this->real_path); $ext = strtolower(substr($this->real_path, strrpos($this->real_path, '.'))); if (in_array($ext, array('.css', '.scss'))) { //il faut rajouter la méthode asset-url $scss = new ScssphpFilter(); if (isset($opts['compass']) && $opts['compass']) { $scss->enableCompass(true); } $scss->registerFunction('aphet_url', function ($args, $scss) { if ($args[0][0] === 'string') { $url = is_array($args[0][2][0]) ? $args[0][2][0][2][0] : $args[0][2][0]; } else { throw new \Exception('je ne sais pas quoi faire là'); } if (strpos($url, '?') !== false) { list($url, $query) = explode('?', $url); } else { $query = null; } if (strpos($url, '#') !== false) { list($url, $hash) = explode('#', $url); } else { $hash = null; } return 'url(' . aphet_url($url) . ($query ? "?{$query}" : '') . ($hash ? "?{$hash}" : '') . ')'; }); $asset->ensureFilter($scss); } elseif ($ext === '.js') { $filter = new \Assetic\Filter\CallablesFilter(function ($asset) { $asset->setContent(preg_replace_callback('/aphet_url\\((.*)\\)/', function ($match) { return '\'' . aphet_url(json_decode($match[1])) . '\''; }, $asset->getContent())); }); $asset->ensureFilter($filter); } return $asset; }
public function cssPackAction() { $collection = new AssetCollection(); foreach ($this->container->getParameter('cms.cms_resources.css_pack') as $asset) { $assetPath = $this->container->getApplication()->getWebRoot() . DIRECTORY_SEPARATOR . $asset; $assetObject = new FileAsset($assetPath, array(), $this->container->getApplication()->getWebRoot()); $assetObject->setTargetPath('/_cms_internal/'); if (substr($asset, strrpos($asset, '.')) == '.less') { $assetObject->ensureFilter(new LessphpFilter()); } $collection->add($assetObject); } $content = $this->container->getCache()->fetch('cms_assets', 'css_pack', function () use($collection) { return $collection->dump(new CssRewriteFilter()); }, $collection->getLastModified()); return new Response($content, 200, array('Content-Type' => 'text/css')); }
/** * @param RequestResponseEvent $event */ public function listen(RequestResponseEvent $event) { $path = $event->getRequest()->getPathInfo(); $parts = pathinfo($path); $parts = array_merge(array('dirname' => '', 'basename' => '', 'extension' => '', 'filename'), $parts); switch (strtolower($parts['extension'])) { case 'css': //possible it's not yet compiled less file? $lessFile = $this->container->getApplication()->getWebRoot() . $path . '.less'; if (is_file($lessFile)) { $asset = new FileAsset($lessFile); $asset->ensureFilter(new LessphpFilter()); $content = $this->container->getCache()->fetch('assets_404', $path, function () use($asset) { return $asset->dump(); }, $asset->getLastModified(), 0, true); $event->setResponse(new Response($content, 200, array('Content-Type' => 'text/css'))); $event->stopPropagation(); return; } break; } }
/** * Call * * This method will check the HTTP request to see if it is a CSS file. If * so, it will attempt to find a corresponding LESS file. If one is found, * it will compile the file to CSS and serve it, optionally saving the CSS * to a filesystem cache. The request will end at this point. * * If the request is not for a CSS file, or if the corresponding LESS file * is not found, this middleware will pass the request on. */ public function call() { $app = $this->app; // PHP 5.3 closures do not have access to $this, so we must proxy it in. // However, the proxy only has access to public fields. $self = $this; $app->hook('slim.before', function () use($app, $self) { $path = $app->request()->getPathInfo(); // Run filter only for requests for CSS files if (preg_match('/\\.css$/', $path)) { $path = preg_replace('/\\.css$/', '.less', $path); // Get absolute physical path on filesystem for LESS file $abs = str_replace('\\', '/', getcwd()) . $self->options['src'] . '/' . $path; $abs = str_replace('//', '/', $abs); $self->debug("Looking for LESS file: {$abs}"); $abs = realpath($abs); if ($abs === false) { // If LESS file is not found, just return $self->debug("LESS file not found: {$abs}"); return; } $self->debug("LESS file found: {$abs}"); // Prepare Assetic $lessFilter = new LessphpFilter(); $importFilter = new CssImportFilter(); $css = new FileAsset($abs, array($lessFilter, $importFilter)); if ($self->options['minify'] === true) { // Minify, if desired $self->debug("Minifying LESS file: {$abs}"); $css->ensureFilter(new CssMinFilter()); } if ($self->options['cache'] === true) { // Cache results, if desired $self->debug("Caching LESS file: {$self->options['cache.dir']}"); $cache = new FilesystemCache($self->options['cache.dir']); $css = new AssetCache($css, $cache); } // Render results and exit $res = $app->response(); $res['Content-Type'] = 'text/css'; $app->halt(200, $css->dump()); } }); $this->next->call(); }
/** * When first part of a module name is not specified in paths and not equals baseUrl, we can't find module * so this module exclusions will not be excluded * * @covers Hearsay\RequireJSBundle\Assetic\Filter\RJsFilter::filterDump * @covers Hearsay\RequireJSBundle\Assetic\Filter\RJsFilter::addModule * @covers Hearsay\RequireJSBundle\Assetic\Filter\RJsFilter::addOption */ public function testNotFindModuleNameAmongPaths() { $this->filter->addOption('preserveLicenseComments', false); // registering optimizer modules config $this->filter->addModule("module/file3", array("include" => array("module/file", "module/file2"))); $this->filter->addModule("module2/file4", array("exclude" => array("module/file3"))); $asset = new FileAsset(__DIR__ . '/modules/module2/file4.js'); $asset->ensureFilter($this->filter); // expecting result contains only content of file4 although file4 depends on file3 and file3 depends on file 2 and file $this->assertRegExp('/^define\\("module\\/file2",\\{js:"got it twice"\\}\\),define\\("module\\/file",\\{js:"got it"\\}\\),' . 'require\\(\\["module\\/file2","module\\/file"\\],function\\(e,t\\)\\{return console.log\\(e,t\\)\\}\\),' . 'define\\("module\\/file3",function\\(\\)\\{\\}\\),require\\(\\["module\\/file3"\\],function\\(e\\)\\{return console.log\\(e\\)\\}\\),' . 'define\\(".{32}",function\\(\\)\\{\\}\\);$/', $asset->dump(), 'Defined exclusions for modules/module2/file4.js excluded incorrectly'); }