source: sandbox/newExpressoMail/newExpressoMail/Assetic/Filter/CssImportFilter.php @ 7265

Revision 7265, 3.3 KB checked in by gustavo, 12 years ago (diff)

Ticket #0000 - Criado novo modulo para o desenvolvimento do novo ExpressoMail?

  • Property svn:executable set to *
Line 
1<?php
2
3/*
4 * This file is part of the Assetic package, an OpenSky project.
5 *
6 * (c) 2010-2012 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
12namespace Assetic\Filter;
13
14use Assetic\Asset\AssetInterface;
15use Assetic\Asset\FileAsset;
16use Assetic\Asset\HttpAsset;
17
18/**
19 * Inlines imported stylesheets.
20 *
21 * @author Kris Wallsmith <kris.wallsmith@gmail.com>
22 */
23class CssImportFilter extends BaseCssFilter
24{
25    private $importFilter;
26
27    /**
28     * Constructor.
29     *
30     * @param FilterInterface $importFilter Filter for each imported asset
31     */
32    public function __construct(FilterInterface $importFilter = null)
33    {
34        $this->importFilter = $importFilter ?: new CssRewriteFilter();
35    }
36
37    public function filterLoad(AssetInterface $asset)
38    {
39        $importFilter = $this->importFilter;
40        $sourceRoot = $asset->getSourceRoot();
41        $sourcePath = $asset->getSourcePath();
42
43        $callback = function($matches) use ($importFilter, $sourceRoot, $sourcePath) {
44            if (!$matches['url'] || null === $sourceRoot) {
45                return $matches[0];
46            }
47
48            $importRoot = $sourceRoot;
49
50            if (false !== strpos($matches['url'], '://')) {
51                // absolute
52                list($importScheme, $tmp) = explode('://', $matches['url'], 2);
53                list($importHost, $importPath) = explode('/', $tmp, 2);
54                $importRoot = $importScheme.'://'.$importHost;
55            } elseif (0 === strpos($matches['url'], '//')) {
56                // protocol-relative
57                list($importHost, $importPath) = explode('/', substr($matches['url'], 2), 2);
58                $importHost = '//'.$importHost;
59            } elseif ('/' == $matches['url'][0]) {
60                // root-relative
61                $importPath = substr($matches['url'], 1);
62            } elseif (null !== $sourcePath) {
63                // document-relative
64                $importPath = $matches['url'];
65                if ('.' != $sourceDir = dirname($sourcePath)) {
66                    $importPath = $sourceDir.'/'.$importPath;
67                }
68            } else {
69                return $matches[0];
70            }
71
72            // ignore other imports
73            if ('css' != pathinfo($importPath, PATHINFO_EXTENSION)) {
74                return $matches[0];
75            }
76
77            $importSource = $importRoot.'/'.$importPath;
78            if (false !== strpos($importSource, '://') || 0 === strpos($importSource, '//')) {
79                $import = new HttpAsset($importSource, array($importFilter), true);
80            } elseif (!file_exists($importSource)) {
81                // ignore not found imports
82                return $matches[0];
83            } else {
84                $import = new FileAsset($importSource, array($importFilter), $importRoot, $importPath);
85            }
86
87            $import->setTargetPath($sourcePath);
88
89            return $import->dump();
90        };
91
92        $content = $asset->getContent();
93        $lastHash = md5($content);
94
95        do {
96            $content = $this->filterImports($content, $callback);
97            $hash = md5($content);
98        } while ($lastHash != $hash && $lastHash = $hash);
99
100        $asset->setContent($content);
101    }
102
103    public function filterDump(AssetInterface $asset)
104    {
105    }
106}
Note: See TracBrowser for help on using the repository browser.