ILIAS  release_5-2 Revision v5.2.25-18-g3f80b828510
BaseAsset.php
Go to the documentation of this file.
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\Asset;
13
16
25abstract class BaseAsset implements AssetInterface
26{
27 private $filters;
28 private $sourceRoot;
29 private $sourcePath;
30 private $sourceDir;
31 private $targetPath;
32 private $content;
33 private $loaded;
34 private $vars;
35 private $values;
36
45 public function __construct($filters = array(), $sourceRoot = null, $sourcePath = null, array $vars = array())
46 {
47 $this->filters = new FilterCollection($filters);
48 $this->sourceRoot = $sourceRoot;
49 $this->sourcePath = $sourcePath;
50 if ($sourcePath && $sourceRoot) {
51 $this->sourceDir = dirname("$sourceRoot/$sourcePath");
52 }
53 $this->vars = $vars;
54 $this->values = array();
55 $this->loaded = false;
56 }
57
58 public function __clone()
59 {
60 $this->filters = clone $this->filters;
61 }
62
63 public function ensureFilter(FilterInterface $filter)
64 {
65 $this->filters->ensure($filter);
66 }
67
68 public function getFilters()
69 {
70 return $this->filters->all();
71 }
72
73 public function clearFilters()
74 {
75 $this->filters->clear();
76 }
77
84 protected function doLoad($content, FilterInterface $additionalFilter = null)
85 {
86 $filter = clone $this->filters;
87 if ($additionalFilter) {
88 $filter->ensure($additionalFilter);
89 }
90
91 $asset = clone $this;
92 $asset->setContent($content);
93
94 $filter->filterLoad($asset);
95 $this->content = $asset->getContent();
96
97 $this->loaded = true;
98 }
99
100 public function dump(FilterInterface $additionalFilter = null)
101 {
102 if (!$this->loaded) {
103 $this->load();
104 }
105
106 $filter = clone $this->filters;
107 if ($additionalFilter) {
108 $filter->ensure($additionalFilter);
109 }
110
111 $asset = clone $this;
112 $filter->filterDump($asset);
113
114 return $asset->getContent();
115 }
116
117 public function getContent()
118 {
119 return $this->content;
120 }
121
122 public function setContent($content)
123 {
124 $this->content = $content;
125 }
126
127 public function getSourceRoot()
128 {
129 return $this->sourceRoot;
130 }
131
132 public function getSourcePath()
133 {
134 return $this->sourcePath;
135 }
136
137 public function getSourceDirectory()
138 {
139 return $this->sourceDir;
140 }
141
142 public function getTargetPath()
143 {
144 return $this->targetPath;
145 }
146
147 public function setTargetPath($targetPath)
148 {
149 if ($this->vars) {
150 foreach ($this->vars as $var) {
151 if (false === strpos($targetPath, $var)) {
152 throw new \RuntimeException(sprintf('The asset target path "%s" must contain the variable "{%s}".', $targetPath, $var));
153 }
154 }
155 }
156
157 $this->targetPath = $targetPath;
158 }
159
160 public function getVars()
161 {
162 return $this->vars;
163 }
164
165 public function setValues(array $values)
166 {
167 foreach ($values as $var => $v) {
168 if (!in_array($var, $this->vars, true)) {
169 throw new \InvalidArgumentException(sprintf('The asset with source path "%s" has no variable named "%s".', $this->sourcePath, $var));
170 }
171 }
172
173 $this->values = $values;
174 $this->loaded = false;
175 }
176
177 public function getValues()
178 {
179 return $this->values;
180 }
181}
sprintf('%.4f', $callTime)
A base abstract asset.
Definition: BaseAsset.php:26
getSourceDirectory()
Returns the asset's source directory.
Definition: BaseAsset.php:137
setValues(array $values)
Sets the values for the asset's variables.
Definition: BaseAsset.php:165
getFilters()
Returns an array of filters currently applied.
Definition: BaseAsset.php:68
ensureFilter(FilterInterface $filter)
Ensures the current asset includes the supplied filter.
Definition: BaseAsset.php:63
doLoad($content, FilterInterface $additionalFilter=null)
Encapsulates asset loading logic.
Definition: BaseAsset.php:84
setContent($content)
Sets the content of the current asset.
Definition: BaseAsset.php:122
setTargetPath($targetPath)
Sets the URL for the current asset.
Definition: BaseAsset.php:147
dump(FilterInterface $additionalFilter=null)
Applies dump filters and returns the asset as a string.
Definition: BaseAsset.php:100
getContent()
Returns the loaded content of the current asset.
Definition: BaseAsset.php:117
getSourcePath()
Returns the relative path for the source asset.
Definition: BaseAsset.php:132
__construct($filters=array(), $sourceRoot=null, $sourcePath=null, array $vars=array())
Constructor.
Definition: BaseAsset.php:45
getSourceRoot()
Returns an absolute path or URL to the source asset's root directory.
Definition: BaseAsset.php:127
getTargetPath()
Returns the URL for the current asset.
Definition: BaseAsset.php:142
clearFilters()
Clears all filters from the current asset.
Definition: BaseAsset.php:73
getValues()
Returns the current values for this asset.
Definition: BaseAsset.php:177
getVars()
Returns an array of variable names for this asset.
Definition: BaseAsset.php:160
A collection of filters.
An exception for terminatinating execution or to throw for unit testing.
An asset has a mutable URL and content and can be loaded and dumped.
load(FilterInterface $additionalFilter=null)
Loads the asset into memory and applies load filters.
A filter manipulates an asset at load and dump.