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
12namespace Assetic\Filter;
13
14use Assetic\Asset\AssetInterface;
15
16/**
17 * Class CssCacheBustingFilter
18 *
19 * @package Assetic\Filter
20 * @author Maximilian Reichel <info@phramz.com>
21 */
22class CssCacheBustingFilter extends BaseCssFilter
23{
24    private $version;
25    private $format = '%s?%s';
26
27    public function setVersion($version)
28    {
29        $this->version = $version;
30    }
31
32    public function setFormat($versionFormat)
33    {
34        $this->format = $versionFormat;
35    }
36
37    public function filterLoad(AssetInterface $asset)
38    {
39    }
40
41    public function filterDump(AssetInterface $asset)
42    {
43        if (!$this->version) {
44            return;
45        }
46
47        $version = $this->version;
48        $format = $this->format;
49
50        $asset->setContent($this->filterReferences(
51            $asset->getContent(),
52            function ($matches) use ($version, $format) {
53                if (0 === strpos($matches['url'], 'data:')) {
54                    return $matches[0];
55                }
56
57                return str_replace(
58                    $matches['url'],
59                    sprintf($format, $matches['url'], $version),
60                    $matches[0]
61                );
62            }
63        ));
64    }
65}
66