ILIAS  release_5-2 Revision v5.2.25-18-g3f80b82851
CssImportFilter.php
Go to the documentation of this file.
1 <?php
2 
3 /*
4  * This file is part of the Assetic package, an OpenSky project.
5  *
6  * (c) 2010-2014 OpenSky Project Inc
7  *
8  * For the full copyright and license information, please view the LICENSE
9  * file that was distributed with this source code.
10  */
11 
12 namespace Assetic\Filter;
13 
18 
25 {
26  private $importFilter;
27 
33  public function __construct(FilterInterface $importFilter = null)
34  {
35  $this->importFilter = $importFilter ?: new CssRewriteFilter();
36  }
37 
38  public function filterLoad(AssetInterface $asset)
39  {
41  $sourceRoot = $asset->getSourceRoot();
42  $sourcePath = $asset->getSourcePath();
43 
44  $callback = function ($matches) use ($importFilter, $sourceRoot, $sourcePath) {
45  if (!$matches['url'] || null === $sourceRoot) {
46  return $matches[0];
47  }
48 
49  $importRoot = $sourceRoot;
50 
51  if (false !== strpos($matches['url'], '://')) {
52  // absolute
53  list($importScheme, $tmp) = explode('://', $matches['url'], 2);
54  list($importHost, $importPath) = explode('/', $tmp, 2);
55  $importRoot = $importScheme.'://'.$importHost;
56  } elseif (0 === strpos($matches['url'], '//')) {
57  // protocol-relative
58  list($importHost, $importPath) = explode('/', substr($matches['url'], 2), 2);
59  $importRoot = '//'.$importHost;
60  } elseif ('/' == $matches['url'][0]) {
61  // root-relative
62  $importPath = substr($matches['url'], 1);
63  } elseif (null !== $sourcePath) {
64  // document-relative
65  $importPath = $matches['url'];
66  if ('.' != $sourceDir = dirname($sourcePath)) {
67  $importPath = $sourceDir.'/'.$importPath;
68  }
69  } else {
70  return $matches[0];
71  }
72 
73  $importSource = $importRoot.'/'.$importPath;
74  if (false !== strpos($importSource, '://') || 0 === strpos($importSource, '//')) {
75  $import = new HttpAsset($importSource, array($importFilter), true);
76  } elseif ('css' != pathinfo($importPath, PATHINFO_EXTENSION) || !file_exists($importSource)) {
77  // ignore non-css and non-existant imports
78  return $matches[0];
79  } else {
80  $import = new FileAsset($importSource, array($importFilter), $importRoot, $importPath);
81  }
82 
83  $import->setTargetPath($sourcePath);
84 
85  return $import->dump();
86  };
87 
88  $content = $asset->getContent();
89  $lastHash = md5($content);
90 
91  do {
92  $content = $this->filterImports($content, $callback);
93  $hash = md5($content);
94  } while ($lastHash != $hash && $lastHash = $hash);
95 
96  $asset->setContent($content);
97  }
98 
99  public function filterDump(AssetInterface $asset)
100  {
101  }
102 
103  public function getChildren(AssetFactory $factory, $content, $loadPath = null)
104  {
105  // todo
106  return array();
107  }
108 }
filterImports($content, $callback, $limit=-1, &$count=0, $includeUrl=true)
getSourceRoot()
Returns an absolute path or URL to the source asset&#39;s root directory.
filterDump(AssetInterface $asset)
Filters an asset just before it&#39;s dumped.
filterLoad(AssetInterface $asset)
Filters an asset after it has been loaded.
getSourcePath()
Returns the relative path for the source asset.
getChildren(AssetFactory $factory, $content, $loadPath=null)
Returns child assets.
getContent()
Returns the loaded content of the current asset.
Fixes relative CSS urls.
Represents an asset loaded via an HTTP request.
Definition: HttpAsset.php:22
Represents an asset loaded from a file.
Definition: FileAsset.php:22
A filter manipulates an asset at load and dump.
__construct(FilterInterface $importFilter=null)
Constructor.
An abstract filter for dealing with CSS.
An asset has a mutable URL and content and can be loaded and dumped.
Create styles array
The data for the language used.
The asset factory creates asset objects.
Inlines imported stylesheets.
A filter that knows how to extract dependencies.
setContent($content)
Sets the content of the current asset.