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 | |
---|
12 | namespace Assetic\Factory\Worker; |
---|
13 | |
---|
14 | use Assetic\Asset\AssetInterface; |
---|
15 | use Assetic\Filter\FilterInterface; |
---|
16 | |
---|
17 | /** |
---|
18 | * Applies a filter to an asset based on a source and/or target path match. |
---|
19 | * |
---|
20 | * @author Kris Wallsmith <kris.wallsmith@gmail.com> |
---|
21 | * @todo A better asset-matcher mechanism |
---|
22 | */ |
---|
23 | class EnsureFilterWorker implements WorkerInterface |
---|
24 | { |
---|
25 | const CHECK_SOURCE = 1; |
---|
26 | const CHECK_TARGET = 2; |
---|
27 | |
---|
28 | private $pattern; |
---|
29 | private $filter; |
---|
30 | private $flags; |
---|
31 | |
---|
32 | /** |
---|
33 | * Constructor. |
---|
34 | * |
---|
35 | * @param string $pattern A regex for checking the asset's target URL |
---|
36 | * @param FilterInterface $filter A filter to apply if the regex matches |
---|
37 | * @param integer $flags Flags for what to check |
---|
38 | */ |
---|
39 | public function __construct($pattern, FilterInterface $filter, $flags = null) |
---|
40 | { |
---|
41 | if (null === $flags) { |
---|
42 | $flags = self::CHECK_SOURCE | self::CHECK_TARGET; |
---|
43 | } |
---|
44 | |
---|
45 | $this->pattern = $pattern; |
---|
46 | $this->filter = $filter; |
---|
47 | $this->flags = $flags; |
---|
48 | } |
---|
49 | |
---|
50 | public function process(AssetInterface $asset) |
---|
51 | { |
---|
52 | if ( |
---|
53 | (self::CHECK_SOURCE === (self::CHECK_SOURCE & $this->flags) && preg_match($this->pattern, $asset->getSourcePath())) |
---|
54 | || |
---|
55 | (self::CHECK_TARGET === (self::CHECK_TARGET & $this->flags) && preg_match($this->pattern, $asset->getTargetPath())) |
---|
56 | ) { |
---|
57 | $asset->ensureFilter($this->filter); |
---|
58 | } |
---|
59 | } |
---|
60 | } |
---|