ILIAS  release_5-2 Revision v5.2.25-18-g3f80b828510
Assetic\Factory\AssetFactory Class Reference

The asset factory creates asset objects. More...

+ Collaboration diagram for Assetic\Factory\AssetFactory:

Public Member Functions

 __construct ($root, $debug=false)
 Constructor. More...
 
 setDebug ($debug)
 Sets debug mode for the current factory. More...
 
 isDebug ()
 Checks if the factory is in debug mode. More...
 
 setDefaultOutput ($output)
 Sets the default output string. More...
 
 addWorker (WorkerInterface $worker)
 Adds a factory worker. More...
 
 getAssetManager ()
 Returns the current asset manager. More...
 
 setAssetManager (AssetManager $am)
 Sets the asset manager to use when creating asset references. More...
 
 getFilterManager ()
 Returns the current filter manager. More...
 
 setFilterManager (FilterManager $fm)
 Sets the filter manager to use when adding filters. More...
 
 createAsset ($inputs=array(), $filters=array(), array $options=array())
 Creates a new asset. More...
 
 generateAssetName ($inputs, $filters, $options=array())
 
 getLastModified (AssetInterface $asset)
 

Protected Member Functions

 parseInput ($input, array $options=array())
 Parses an input string string into an asset. More...
 
 createAssetCollection (array $assets=array(), array $options=array())
 
 createAssetReference ($name)
 
 createHttpAsset ($sourceUrl, $vars)
 
 createGlobAsset ($glob, $root=null, $vars)
 
 createFileAsset ($source, $root=null, $path=null, $vars)
 
 getFilter ($name)
 

Private Member Functions

 applyWorkers (AssetCollectionInterface $asset)
 Filters an asset collection through the factory workers. More...
 

Static Private Member Functions

static isAbsolutePath ($path)
 
static findRootDir ($path, array $roots)
 Loops through the root directories and returns the first match. More...
 

Private Attributes

 $root
 
 $debug
 
 $output
 
 $workers
 
 $am
 
 $fm
 

Detailed Description

The asset factory creates asset objects.

Author
Kris Wallsmith kris..nosp@m.wall.nosp@m.smith.nosp@m.@gma.nosp@m.il.co.nosp@m.m

Definition at line 31 of file AssetFactory.php.

Constructor & Destructor Documentation

◆ __construct()

Assetic\Factory\AssetFactory::__construct (   $root,
  $debug = false 
)

Constructor.

Parameters
string$rootThe default root directory
Boolean$debugFilters prefixed with a "?" will be omitted in debug mode

Definition at line 46 of file AssetFactory.php.

47 {
48 $this->root = rtrim($root, '/');
49 $this->debug = $debug;
50 $this->output = 'assetic/*';
51 $this->workers = array();
52 }

References Assetic\Factory\AssetFactory\$debug, and Assetic\Factory\AssetFactory\$root.

Member Function Documentation

◆ addWorker()

Assetic\Factory\AssetFactory::addWorker ( WorkerInterface  $worker)

Adds a factory worker.

Parameters
WorkerInterface$workerA worker

Definition at line 89 of file AssetFactory.php.

90 {
91 $this->workers[] = $worker;
92 }

◆ applyWorkers()

Assetic\Factory\AssetFactory::applyWorkers ( AssetCollectionInterface  $asset)
private

Filters an asset collection through the factory workers.

Each leaf asset will be processed first, followed by the asset collection itself.

Parameters
AssetCollectionInterface$assetAn asset collection
Returns
AssetCollectionInterface

Definition at line 380 of file AssetFactory.php.

381 {
382 foreach ($asset as $leaf) {
383 foreach ($this->workers as $worker) {
384 $retval = $worker->process($leaf, $this);
385
386 if ($retval instanceof AssetInterface && $leaf !== $retval) {
387 $asset->replaceLeaf($leaf, $retval);
388 }
389 }
390 }
391
392 foreach ($this->workers as $worker) {
393 $retval = $worker->process($asset, $this);
394
395 if ($retval instanceof AssetInterface) {
396 $asset = $retval;
397 }
398 }
399
400 return $asset instanceof AssetCollectionInterface ? $asset : $this->createAssetCollection(array($asset));
401 }
createAssetCollection(array $assets=array(), array $options=array())
replaceLeaf(AssetInterface $needle, AssetInterface $replacement, $graceful=false)
Replaces an existing leaf with a new one.
An asset has a mutable URL and content and can be loaded and dumped.

References Assetic\Factory\AssetFactory\createAssetCollection(), and Assetic\Asset\AssetCollectionInterface\replaceLeaf().

Referenced by Assetic\Factory\AssetFactory\createAsset().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ createAsset()

Assetic\Factory\AssetFactory::createAsset (   $inputs = array(),
  $filters = array(),
array  $options = array() 
)

Creates a new asset.

Prefixing a filter name with a question mark will cause it to be omitted when the factory is in debug mode.

Available options:

  • output: An output string
  • name: An asset name for interpolation in output patterns
  • debug: Forces debug mode on or off for this asset
  • root: An array or string of more root directories
Parameters
array | string$inputsAn array of input strings
array | string$filtersAn array of filter names
array$optionsAn array of options
Returns
AssetCollection An asset collection

Definition at line 153 of file AssetFactory.php.

154 {
155 if (!is_array($inputs)) {
156 $inputs = array($inputs);
157 }
158
159 if (!is_array($filters)) {
160 $filters = array($filters);
161 }
162
163 if (!isset($options['output'])) {
164 $options['output'] = $this->output;
165 }
166
167 if (!isset($options['vars'])) {
168 $options['vars'] = array();
169 }
170
171 if (!isset($options['debug'])) {
172 $options['debug'] = $this->debug;
173 }
174
175 if (!isset($options['root'])) {
176 $options['root'] = array($this->root);
177 } else {
178 if (!is_array($options['root'])) {
179 $options['root'] = array($options['root']);
180 }
181
182 $options['root'][] = $this->root;
183 }
184
185 if (!isset($options['name'])) {
186 $options['name'] = $this->generateAssetName($inputs, $filters, $options);
187 }
188
189 $asset = $this->createAssetCollection(array(), $options);
190 $extensions = array();
191
192 // inner assets
193 foreach ($inputs as $input) {
194 if (is_array($input)) {
195 // nested formula
196 $asset->add(call_user_func_array(array($this, 'createAsset'), $input));
197 } else {
198 $asset->add($this->parseInput($input, $options));
199 $extensions[pathinfo($input, PATHINFO_EXTENSION)] = true;
200 }
201 }
202
203 // filters
204 foreach ($filters as $filter) {
205 if ('?' != $filter[0]) {
206 $asset->ensureFilter($this->getFilter($filter));
207 } elseif (!$options['debug']) {
208 $asset->ensureFilter($this->getFilter(substr($filter, 1)));
209 }
210 }
211
212 // append variables
213 if (!empty($options['vars'])) {
214 $toAdd = array();
215 foreach ($options['vars'] as $var) {
216 if (false !== strpos($options['output'], '{'.$var.'}')) {
217 continue;
218 }
219
220 $toAdd[] = '{'.$var.'}';
221 }
222
223 if ($toAdd) {
224 $options['output'] = str_replace('*', '*.'.implode('.', $toAdd), $options['output']);
225 }
226 }
227
228 // append consensus extension if missing
229 if (1 == count($extensions) && !pathinfo($options['output'], PATHINFO_EXTENSION) && $extension = key($extensions)) {
230 $options['output'] .= '.'.$extension;
231 }
232
233 // output --> target url
234 $asset->setTargetPath(str_replace('*', $options['name'], $options['output']));
235
236 // apply workers and return
237 return $this->applyWorkers($asset);
238 }
applyWorkers(AssetCollectionInterface $asset)
Filters an asset collection through the factory workers.
generateAssetName($inputs, $filters, $options=array())
parseInput($input, array $options=array())
Parses an input string string into an asset.
if(!is_array($argv)) $options

References Assetic\Factory\AssetFactory\$debug, $options, Assetic\Factory\AssetFactory\$output, Assetic\Factory\AssetFactory\$root, Assetic\Factory\AssetFactory\applyWorkers(), Assetic\Factory\AssetFactory\createAssetCollection(), Assetic\Factory\AssetFactory\generateAssetName(), Assetic\Factory\AssetFactory\getFilter(), and Assetic\Factory\AssetFactory\parseInput().

Referenced by Assetic\Filter\LessFilter\getChildren(), Assetic\Filter\LessphpFilter\getChildren(), and Assetic\Filter\ScssphpFilter\getChildren().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ createAssetCollection()

Assetic\Factory\AssetFactory::createAssetCollection ( array  $assets = array(),
array  $options = array() 
)
protected

Definition at line 332 of file AssetFactory.php.

333 {
334 return new AssetCollection($assets, array(), null, isset($options['vars']) ? $options['vars'] : array());
335 }

References $options.

Referenced by Assetic\Factory\AssetFactory\applyWorkers(), and Assetic\Factory\AssetFactory\createAsset().

+ Here is the caller graph for this function:

◆ createAssetReference()

Assetic\Factory\AssetFactory::createAssetReference (   $name)
protected

Definition at line 337 of file AssetFactory.php.

338 {
339 if (!$this->am) {
340 throw new \LogicException('There is no asset manager.');
341 }
342
343 return new AssetReference($this->am, $name);
344 }

Referenced by Assetic\Factory\AssetFactory\parseInput().

+ Here is the caller graph for this function:

◆ createFileAsset()

Assetic\Factory\AssetFactory::createFileAsset (   $source,
  $root = null,
  $path = null,
  $vars 
)
protected

Definition at line 356 of file AssetFactory.php.

357 {
358 return new FileAsset($source, array(), $root, $path, $vars);
359 }
$path
Definition: aliased.php:25

References $path, and Assetic\Factory\AssetFactory\$root.

Referenced by Assetic\Factory\AssetFactory\parseInput().

+ Here is the caller graph for this function:

◆ createGlobAsset()

Assetic\Factory\AssetFactory::createGlobAsset (   $glob,
  $root = null,
  $vars 
)
protected

Definition at line 351 of file AssetFactory.php.

352 {
353 return new GlobAsset($glob, array(), $root, $vars);
354 }

References Assetic\Factory\AssetFactory\$root.

Referenced by Assetic\Factory\AssetFactory\parseInput().

+ Here is the caller graph for this function:

◆ createHttpAsset()

Assetic\Factory\AssetFactory::createHttpAsset (   $sourceUrl,
  $vars 
)
protected

Definition at line 346 of file AssetFactory.php.

347 {
348 return new HttpAsset($sourceUrl, array(), false, $vars);
349 }

Referenced by Assetic\Factory\AssetFactory\parseInput().

+ Here is the caller graph for this function:

◆ findRootDir()

static Assetic\Factory\AssetFactory::findRootDir (   $path,
array  $roots 
)
staticprivate

Loops through the root directories and returns the first match.

Parameters
string$pathAn absolute path
array$rootsAn array of root directories
Returns
string|null The matching root directory, if found

Definition at line 416 of file AssetFactory.php.

417 {
418 foreach ($roots as $root) {
419 if (0 === strpos($path, $root)) {
420 return $root;
421 }
422 }
423 }

References $path, and Assetic\Factory\AssetFactory\$root.

◆ generateAssetName()

Assetic\Factory\AssetFactory::generateAssetName (   $inputs,
  $filters,
  $options = array() 
)

Definition at line 240 of file AssetFactory.php.

241 {
242 foreach (array_diff(array_keys($options), array('output', 'debug', 'root')) as $key) {
243 unset($options[$key]);
244 }
245
246 ksort($options);
247
248 return substr(sha1(serialize($inputs).serialize($filters).serialize($options)), 0, 7);
249 }

References $options.

Referenced by Assetic\Factory\AssetFactory\createAsset().

+ Here is the caller graph for this function:

◆ getAssetManager()

Assetic\Factory\AssetFactory::getAssetManager ( )

Returns the current asset manager.

Returns
AssetManager|null The asset manager

Definition at line 99 of file AssetFactory.php.

100 {
101 return $this->am;
102 }

References Assetic\Factory\AssetFactory\$am.

◆ getFilter()

Assetic\Factory\AssetFactory::getFilter (   $name)
protected

Definition at line 361 of file AssetFactory.php.

362 {
363 if (!$this->fm) {
364 throw new \LogicException('There is no filter manager.');
365 }
366
367 return $this->fm->get($name);
368 }

Referenced by Assetic\Factory\AssetFactory\createAsset().

+ Here is the caller graph for this function:

◆ getFilterManager()

Assetic\Factory\AssetFactory::getFilterManager ( )

Returns the current filter manager.

Returns
FilterManager|null The filter manager

Definition at line 119 of file AssetFactory.php.

120 {
121 return $this->fm;
122 }

References Assetic\Factory\AssetFactory\$fm.

◆ getLastModified()

Assetic\Factory\AssetFactory::getLastModified ( AssetInterface  $asset)

Definition at line 251 of file AssetFactory.php.

252 {
253 $mtime = 0;
254 foreach ($asset instanceof AssetCollectionInterface ? $asset : array($asset) as $leaf) {
255 $mtime = max($mtime, $leaf->getLastModified());
256
257 if (!$filters = $leaf->getFilters()) {
258 continue;
259 }
260
261 $prevFilters = array();
262 foreach ($filters as $filter) {
263 $prevFilters[] = $filter;
264
265 if (!$filter instanceof DependencyExtractorInterface) {
266 continue;
267 }
268
269 // extract children from leaf after running all preceeding filters
270 $clone = clone $leaf;
271 $clone->clearFilters();
272 foreach (array_slice($prevFilters, 0, -1) as $prevFilter) {
273 $clone->ensureFilter($prevFilter);
274 }
275 $clone->load();
276
277 foreach ($filter->getChildren($this, $clone->getContent(), $clone->getSourceDirectory()) as $child) {
278 $mtime = max($mtime, $this->getLastModified($child));
279 }
280 }
281 }
282
283 return $mtime;
284 }
getLastModified(AssetInterface $asset)

References Assetic\Factory\AssetFactory\getLastModified().

Referenced by Assetic\Factory\Worker\CacheBustingWorker\getHash(), and Assetic\Factory\AssetFactory\getLastModified().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ isAbsolutePath()

static Assetic\Factory\AssetFactory::isAbsolutePath (   $path)
staticprivate

Definition at line 403 of file AssetFactory.php.

404 {
405 return '/' == $path[0] || '\\' == $path[0] || (3 < strlen($path) && ctype_alpha($path[0]) && $path[1] == ':' && ('\\' == $path[2] || '/' == $path[2]));
406 }

References $path.

◆ isDebug()

Assetic\Factory\AssetFactory::isDebug ( )

Checks if the factory is in debug mode.

Returns
Boolean Debug mode

Definition at line 69 of file AssetFactory.php.

70 {
71 return $this->debug;
72 }

References Assetic\Factory\AssetFactory\$debug.

◆ parseInput()

Assetic\Factory\AssetFactory::parseInput (   $input,
array  $options = array() 
)
protected

Parses an input string string into an asset.

The input string can be one of the following:

  • A reference: If the string starts with an "at" sign it will be interpreted as a reference to an asset in the asset manager
  • An absolute URL: If the string contains "://" or starts with "//" it will be interpreted as an HTTP asset
  • A glob: If the string contains a "*" it will be interpreted as a glob
  • A path: Otherwise the string is interpreted as a filesystem path

Both globs and paths will be absolutized using the current root directory.

Parameters
string$inputAn input string
array$optionsAn array of options
Returns
AssetInterface An asset

Definition at line 303 of file AssetFactory.php.

304 {
305 if ('@' == $input[0]) {
306 return $this->createAssetReference(substr($input, 1));
307 }
308
309 if (false !== strpos($input, '://') || 0 === strpos($input, '//')) {
310 return $this->createHttpAsset($input, $options['vars']);
311 }
312
313 if (self::isAbsolutePath($input)) {
314 if ($root = self::findRootDir($input, $options['root'])) {
315 $path = ltrim(substr($input, strlen($root)), '/');
316 } else {
317 $path = null;
318 }
319 } else {
321 $path = $input;
322 $input = $this->root.'/'.$path;
323 }
324
325 if (false !== strpos($input, '*')) {
326 return $this->createGlobAsset($input, $root, $options['vars']);
327 }
328
329 return $this->createFileAsset($input, $root, $path, $options['vars']);
330 }
createGlobAsset($glob, $root=null, $vars)
createFileAsset($source, $root=null, $path=null, $vars)
createHttpAsset($sourceUrl, $vars)

References $options, $path, Assetic\Factory\AssetFactory\$root, Assetic\Factory\AssetFactory\createAssetReference(), Assetic\Factory\AssetFactory\createFileAsset(), Assetic\Factory\AssetFactory\createGlobAsset(), and Assetic\Factory\AssetFactory\createHttpAsset().

Referenced by Assetic\Factory\AssetFactory\createAsset().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ setAssetManager()

Assetic\Factory\AssetFactory::setAssetManager ( AssetManager  $am)

Sets the asset manager to use when creating asset references.

Parameters
AssetManager$amThe asset manager

Definition at line 109 of file AssetFactory.php.

110 {
111 $this->am = $am;
112 }

References Assetic\Factory\AssetFactory\$am.

◆ setDebug()

Assetic\Factory\AssetFactory::setDebug (   $debug)

Sets debug mode for the current factory.

Parameters
Boolean$debugDebug mode

Definition at line 59 of file AssetFactory.php.

60 {
61 $this->debug = $debug;
62 }

References Assetic\Factory\AssetFactory\$debug.

◆ setDefaultOutput()

Assetic\Factory\AssetFactory::setDefaultOutput (   $output)

Sets the default output string.

Parameters
string$outputThe default output string

Definition at line 79 of file AssetFactory.php.

80 {
81 $this->output = $output;
82 }

References Assetic\Factory\AssetFactory\$output.

◆ setFilterManager()

Assetic\Factory\AssetFactory::setFilterManager ( FilterManager  $fm)

Sets the filter manager to use when adding filters.

Parameters
FilterManager$fmThe filter manager

Definition at line 129 of file AssetFactory.php.

130 {
131 $this->fm = $fm;
132 }

References Assetic\Factory\AssetFactory\$fm.

Field Documentation

◆ $am

Assetic\Factory\AssetFactory::$am
private

◆ $debug

◆ $fm

Assetic\Factory\AssetFactory::$fm
private

◆ $output

Assetic\Factory\AssetFactory::$output
private

◆ $root

◆ $workers

Assetic\Factory\AssetFactory::$workers
private

Definition at line 36 of file AssetFactory.php.


The documentation for this class was generated from the following file: