class CssImportFilter

Inlines imported stylesheets.

@author Kris Wallsmith <kris.wallsmith@gmail.com>

Hierarchy

Expanded class hierarchy of CssImportFilter

File

drupal/core/vendor/kriswallsmith/assetic/src/Assetic/Filter/CssImportFilter.php, line 24

Namespace

Assetic\Filter
View source
class CssImportFilter extends BaseCssFilter implements DependencyExtractorInterface {
  private $importFilter;

  /**
   * Constructor.
   *
   * @param FilterInterface $importFilter Filter for each imported asset
   */
  public function __construct(FilterInterface $importFilter = null) {
    $this->importFilter = $importFilter ?: new CssRewriteFilter();
  }
  public function filterLoad(AssetInterface $asset) {
    $importFilter = $this->importFilter;
    $sourceRoot = $asset
      ->getSourceRoot();
    $sourcePath = $asset
      ->getSourcePath();
    $callback = function ($matches) use ($importFilter, $sourceRoot, $sourcePath) {
      if (!$matches['url'] || 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 filterDump(AssetInterface $asset) {
  }
  public function getChildren(AssetFactory $factory, $content, $loadPath = null) {

    // todo
    return array();
  }

}

Members

Namesort descending Modifiers Type Description Overrides
BaseCssFilter::filterIEFilters protected function
BaseCssFilter::filterImports protected function
BaseCssFilter::filterReferences protected function
BaseCssFilter::filterUrls protected function
CssImportFilter::$importFilter private property
CssImportFilter::filterDump public function Filters an asset just before it's dumped. Overrides FilterInterface::filterDump
CssImportFilter::filterLoad public function Filters an asset after it has been loaded. Overrides FilterInterface::filterLoad
CssImportFilter::getChildren public function Returns child assets. Overrides DependencyExtractorInterface::getChildren
CssImportFilter::__construct public function Constructor.