diff --git a/core/includes/common.inc b/core/includes/common.inc index ee134ac..1e5f5eb 100644 --- a/core/includes/common.inc +++ b/core/includes/common.inc @@ -7,6 +7,18 @@ use Drupal\Core\Cache\CacheBackendInterface; use Drupal\Core\Database\Database; use Drupal\Core\Template\Attribute; +use Assetic\AssetManager; +use Assetic\AssetWriter; +use Assetic\Asset\AssetCollection; +use Assetic\Asset\FileAsset; +use Assetic\Asset\GlobAsset; +use Assetic\Factory\AssetFactory; +use Assetic\Factory\LazyAssetManager; +use Assetic\Factory\Loader\FunctionCallsFormulaLoader; +use Assetic\Factory\Resource\DirectoryResource; +use Assetic\FilterManager; +use Drupal\Core\Asset\Filter\CssRewriteFilter; +use Assetic\Filter\Yui\CssCompressorFilter; /** * @file * Common functions that many Drupal modules will need to reference. @@ -2721,6 +2733,66 @@ function drupal_get_css($css = NULL, $skip_alter = FALSE) { $styles['#attached']['js'][] = array('type' => 'setting', 'data' => $setting); } + + + // Experiments with Assetic start here. + $am = new AssetManager(); + $fm = new FilterManager(); + // $fm->set('yui', new CssCompressorFilter('/tmp/yuicompressor-2.4.7.jar')); + $fm->set('rewrite', new CssRewriteFilter()); + + $factory = new AssetFactory('sites/default/files/css'); + $factory->setFilterManager($fm); + + // We're going to rebuild the #items array. + $items = array(); + + // @todo Bring in debug value from... where, exactly? + // Sensible to use a debug flag as Symfony does + // (and it's sensible, anyway). + $debug = FALSE; + // $debug = TRUE; + + if (!$debug) { + $groups = drupal_group_css($styles['#items']); + + foreach ($groups as $key => $group) { + $grouped_asset = $group; + unset($grouped_asset['items']); + + $paths = array(); + foreach ($group['items'] as $path) { + $paths[] = DRUPAL_ROOT . '/' . $path['data']; + } + + $asset = $factory->createAsset( + // These are the paths to the CSS files. + $paths, + array( + // These are the filters. + 'rewrite', + // 'yui', + ), + array( + // This is the pattern we want to use for the group filename. + 'output' => 'group-*.css', + ) + ); + + // Add our new grouped asset to the AssetManager. + $am->set('group_' . $key, $asset); + // Add the URL of the new asset to the Drupal style array we created. + $grouped_asset['data'] = 'sites/default/files/css/' . $asset->getTargetPath(); + + if (!file_exists($grouped_asset['data'])) { + $writer = new AssetWriter('sites/default/files/css'); + $writer->writeAsset($asset); + } + $items[$key] = $grouped_asset; + } + $styles['#items'] = $items; + } + return drupal_render($styles); } diff --git a/core/lib/Drupal/Core/Asset/Filter/CssRewriteFilter.php b/core/lib/Drupal/Core/Asset/Filter/CssRewriteFilter.php new file mode 100644 index 0000000..5954ef2 --- /dev/null +++ b/core/lib/Drupal/Core/Asset/Filter/CssRewriteFilter.php @@ -0,0 +1,50 @@ + + */ +class CssRewriteFilter extends BaseCssFilter +{ + public function filterLoad(AssetInterface $asset) + { + } + + public function filterDump(AssetInterface $asset) + { + $sourceBase = $asset->getSourceRoot(); + + $data = ''; + $content = $asset->getContent(); + + // Simplify to a relative URL if the stylesheet URL starts with the + // base URL of the website. + if (substr($sourceBase, 0, strlen(DRUPAL_ROOT)) == DRUPAL_ROOT) { + $sourceBase = substr($sourceBase, strlen(DRUPAL_ROOT)); + } + + _drupal_build_css_path(NULL, $sourceBase . '/'); + // Anchor all paths in the CSS with its base URL, ignoring external and absolute paths. + $data .= preg_replace_callback('/url\(\s*[\'"]?(?![a-z]+:|\/+)([^\'")]+)[\'"]?\s*\)/i', '_drupal_build_css_path', $content); + + // Per the W3C specification at http://www.w3.org/TR/REC-CSS2/cascade.html#at-import, + // @import rules must proceed any other style, so we move those to the top. + $regexp = '/@import[^;]+;/i'; + preg_match_all($regexp, $data, $matches); + $data = preg_replace($regexp, '', $data); + $data = implode('', $matches[0]) . $data; + + $asset->setContent($data); + } +}