|
0
|
1 |
<?php |
|
|
2 |
|
|
|
3 |
/* |
|
|
4 |
* This file is part of the Assetic package, an OpenSky project. |
|
|
5 |
* |
|
|
6 |
* (c) 2010-2011 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\Asset; |
|
|
13 |
|
|
|
14 |
use Assetic\Filter\FilterCollection; |
|
|
15 |
use Assetic\Filter\FilterInterface; |
|
|
16 |
|
|
|
17 |
/** |
|
|
18 |
* A base abstract asset. |
|
|
19 |
* |
|
|
20 |
* The methods load() and getLastModified() are left undefined, although a |
|
|
21 |
* reusable doLoad() method is available to child classes. |
|
|
22 |
* |
|
|
23 |
* @author Kris Wallsmith <kris.wallsmith@gmail.com> |
|
|
24 |
*/ |
|
|
25 |
abstract class BaseAsset implements AssetInterface |
|
|
26 |
{ |
|
|
27 |
private $filters; |
|
|
28 |
private $sourceRoot; |
|
|
29 |
private $sourcePath; |
|
|
30 |
private $targetPath; |
|
|
31 |
private $content; |
|
|
32 |
private $loaded; |
|
|
33 |
|
|
|
34 |
/** |
|
|
35 |
* Constructor. |
|
|
36 |
* |
|
|
37 |
* @param array $filters Filters for the asset |
|
|
38 |
*/ |
|
|
39 |
public function __construct($filters = array(), $sourceRoot = null, $sourcePath = null) |
|
|
40 |
{ |
|
|
41 |
$this->filters = new FilterCollection($filters); |
|
|
42 |
$this->sourceRoot = $sourceRoot; |
|
|
43 |
$this->sourcePath = $sourcePath; |
|
|
44 |
$this->loaded = false; |
|
|
45 |
} |
|
|
46 |
|
|
|
47 |
public function __clone() |
|
|
48 |
{ |
|
|
49 |
$this->filters = clone $this->filters; |
|
|
50 |
} |
|
|
51 |
|
|
|
52 |
public function ensureFilter(FilterInterface $filter) |
|
|
53 |
{ |
|
|
54 |
$this->filters->ensure($filter); |
|
|
55 |
} |
|
|
56 |
|
|
|
57 |
public function getFilters() |
|
|
58 |
{ |
|
|
59 |
return $this->filters->all(); |
|
|
60 |
} |
|
|
61 |
|
|
|
62 |
public function clearFilters() |
|
|
63 |
{ |
|
|
64 |
$this->filters->clear(); |
|
|
65 |
} |
|
|
66 |
|
|
|
67 |
/** |
|
|
68 |
* Encapsulates asset loading logic. |
|
|
69 |
* |
|
|
70 |
* @param string $content The asset content |
|
|
71 |
* @param FilterInterface $additionalFilter An additional filter |
|
|
72 |
*/ |
|
|
73 |
protected function doLoad($content, FilterInterface $additionalFilter = null) |
|
|
74 |
{ |
|
|
75 |
$filter = clone $this->filters; |
|
|
76 |
if ($additionalFilter) { |
|
|
77 |
$filter->ensure($additionalFilter); |
|
|
78 |
} |
|
|
79 |
|
|
|
80 |
$asset = clone $this; |
|
|
81 |
$asset->setContent($content); |
|
|
82 |
|
|
|
83 |
$filter->filterLoad($asset); |
|
|
84 |
$this->content = $asset->getContent(); |
|
|
85 |
|
|
|
86 |
$this->loaded = true; |
|
|
87 |
} |
|
|
88 |
|
|
|
89 |
public function dump(FilterInterface $additionalFilter = null) |
|
|
90 |
{ |
|
|
91 |
if (!$this->loaded) { |
|
|
92 |
$this->load(); |
|
|
93 |
} |
|
|
94 |
|
|
|
95 |
$filter = clone $this->filters; |
|
|
96 |
if ($additionalFilter) { |
|
|
97 |
$filter->ensure($additionalFilter); |
|
|
98 |
} |
|
|
99 |
|
|
|
100 |
$asset = clone $this; |
|
|
101 |
$filter->filterDump($asset); |
|
|
102 |
|
|
|
103 |
return $asset->getContent(); |
|
|
104 |
} |
|
|
105 |
|
|
|
106 |
public function getContent() |
|
|
107 |
{ |
|
|
108 |
return $this->content; |
|
|
109 |
} |
|
|
110 |
|
|
|
111 |
public function setContent($content) |
|
|
112 |
{ |
|
|
113 |
$this->content = $content; |
|
|
114 |
} |
|
|
115 |
|
|
|
116 |
public function getSourceRoot() |
|
|
117 |
{ |
|
|
118 |
return $this->sourceRoot; |
|
|
119 |
} |
|
|
120 |
|
|
|
121 |
public function getSourcePath() |
|
|
122 |
{ |
|
|
123 |
return $this->sourcePath; |
|
|
124 |
} |
|
|
125 |
|
|
|
126 |
public function getTargetPath() |
|
|
127 |
{ |
|
|
128 |
return $this->targetPath; |
|
|
129 |
} |
|
|
130 |
|
|
|
131 |
public function setTargetPath($targetPath) |
|
|
132 |
{ |
|
|
133 |
$this->targetPath = $targetPath; |
|
|
134 |
} |
|
|
135 |
} |