[7265] | 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\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 | private $vars; |
---|
| 34 | private $values; |
---|
| 35 | |
---|
| 36 | /** |
---|
| 37 | * Constructor. |
---|
| 38 | * |
---|
| 39 | * @param array $filters Filters for the asset |
---|
| 40 | * @param string $sourceRoot The root directory |
---|
| 41 | * @param string $sourcePath The asset path |
---|
| 42 | * @param array $vars |
---|
| 43 | */ |
---|
| 44 | public function __construct($filters = array(), $sourceRoot = null, $sourcePath = null, array $vars = array()) |
---|
| 45 | { |
---|
| 46 | $this->filters = new FilterCollection($filters); |
---|
| 47 | $this->sourceRoot = $sourceRoot; |
---|
| 48 | $this->sourcePath = $sourcePath; |
---|
| 49 | $this->vars = $vars; |
---|
| 50 | $this->values = array(); |
---|
| 51 | $this->loaded = false; |
---|
| 52 | } |
---|
| 53 | |
---|
| 54 | public function __clone() |
---|
| 55 | { |
---|
| 56 | $this->filters = clone $this->filters; |
---|
| 57 | } |
---|
| 58 | |
---|
| 59 | public function ensureFilter(FilterInterface $filter) |
---|
| 60 | { |
---|
| 61 | $this->filters->ensure($filter); |
---|
| 62 | } |
---|
| 63 | |
---|
| 64 | public function getFilters() |
---|
| 65 | { |
---|
| 66 | return $this->filters->all(); |
---|
| 67 | } |
---|
| 68 | |
---|
| 69 | public function clearFilters() |
---|
| 70 | { |
---|
| 71 | $this->filters->clear(); |
---|
| 72 | } |
---|
| 73 | |
---|
| 74 | /** |
---|
| 75 | * Encapsulates asset loading logic. |
---|
| 76 | * |
---|
| 77 | * @param string $content The asset content |
---|
| 78 | * @param FilterInterface $additionalFilter An additional filter |
---|
| 79 | */ |
---|
| 80 | protected function doLoad($content, FilterInterface $additionalFilter = null) |
---|
| 81 | { |
---|
| 82 | $filter = clone $this->filters; |
---|
| 83 | if ($additionalFilter) { |
---|
| 84 | $filter->ensure($additionalFilter); |
---|
| 85 | } |
---|
| 86 | |
---|
| 87 | $asset = clone $this; |
---|
| 88 | $asset->setContent($content); |
---|
| 89 | |
---|
| 90 | $filter->filterLoad($asset); |
---|
| 91 | $this->content = $asset->getContent(); |
---|
| 92 | |
---|
| 93 | $this->loaded = true; |
---|
| 94 | } |
---|
| 95 | |
---|
| 96 | public function dump(FilterInterface $additionalFilter = null) |
---|
| 97 | { |
---|
| 98 | if (!$this->loaded) { |
---|
| 99 | $this->load(); |
---|
| 100 | } |
---|
| 101 | |
---|
| 102 | $filter = clone $this->filters; |
---|
| 103 | if ($additionalFilter) { |
---|
| 104 | $filter->ensure($additionalFilter); |
---|
| 105 | } |
---|
| 106 | |
---|
| 107 | $asset = clone $this; |
---|
| 108 | $filter->filterDump($asset); |
---|
| 109 | |
---|
| 110 | return $asset->getContent(); |
---|
| 111 | } |
---|
| 112 | |
---|
| 113 | public function getContent() |
---|
| 114 | { |
---|
| 115 | return $this->content; |
---|
| 116 | } |
---|
| 117 | |
---|
| 118 | public function setContent($content) |
---|
| 119 | { |
---|
| 120 | $this->content = $content; |
---|
| 121 | } |
---|
| 122 | |
---|
| 123 | public function getSourceRoot() |
---|
| 124 | { |
---|
| 125 | return $this->sourceRoot; |
---|
| 126 | } |
---|
| 127 | |
---|
| 128 | public function getSourcePath() |
---|
| 129 | { |
---|
| 130 | return $this->sourcePath; |
---|
| 131 | } |
---|
| 132 | |
---|
| 133 | public function getTargetPath() |
---|
| 134 | { |
---|
| 135 | return $this->targetPath; |
---|
| 136 | } |
---|
| 137 | |
---|
| 138 | public function setTargetPath($targetPath) |
---|
| 139 | { |
---|
| 140 | if ($this->vars) { |
---|
| 141 | foreach ($this->vars as $var) { |
---|
| 142 | if (false === strpos($targetPath, $var)) { |
---|
| 143 | throw new \RuntimeException(sprintf('The asset target path "%s" must contain the variable "{%s}".', $targetPath, $var)); |
---|
| 144 | } |
---|
| 145 | } |
---|
| 146 | } |
---|
| 147 | |
---|
| 148 | $this->targetPath = $targetPath; |
---|
| 149 | } |
---|
| 150 | |
---|
| 151 | public function getVars() |
---|
| 152 | { |
---|
| 153 | return $this->vars; |
---|
| 154 | } |
---|
| 155 | |
---|
| 156 | public function setValues(array $values) |
---|
| 157 | { |
---|
| 158 | foreach ($values as $var => $v) { |
---|
| 159 | if (!in_array($var, $this->vars, true)) { |
---|
| 160 | throw new \InvalidArgumentException(sprintf('The asset with source path "%s" has no variable named "%s".', $this->sourcePath, $var)); |
---|
| 161 | } |
---|
| 162 | } |
---|
| 163 | |
---|
| 164 | $this->values = $values; |
---|
| 165 | $this->loaded = false; |
---|
| 166 | } |
---|
| 167 | |
---|
| 168 | public function getValues() |
---|
| 169 | { |
---|
| 170 | return $this->values; |
---|
| 171 | } |
---|
| 172 | } |
---|