ILIAS  release_5-2 Revision v5.2.25-18-g3f80b828510
Assetic\Asset\AssetCache Class Reference

Caches an asset to avoid the cost of loading and dumping. More...

+ Inheritance diagram for Assetic\Asset\AssetCache:
+ Collaboration diagram for Assetic\Asset\AssetCache:

Public Member Functions

 __construct (AssetInterface $asset, CacheInterface $cache)
 
 ensureFilter (FilterInterface $filter)
 Ensures the current asset includes the supplied filter. More...
 
 getFilters ()
 Returns an array of filters currently applied. More...
 
 clearFilters ()
 Clears all filters from the current asset. More...
 
 load (FilterInterface $additionalFilter=null)
 Loads the asset into memory and applies load filters. More...
 
 dump (FilterInterface $additionalFilter=null)
 Applies dump filters and returns the asset as a string. More...
 
 getContent ()
 Returns the loaded content of the current asset. More...
 
 setContent ($content)
 Sets the content of the current asset. More...
 
 getSourceRoot ()
 Returns an absolute path or URL to the source asset's root directory. More...
 
 getSourcePath ()
 Returns the relative path for the source asset. More...
 
 getSourceDirectory ()
 Returns the asset's source directory. More...
 
 getTargetPath ()
 Returns the URL for the current asset. More...
 
 setTargetPath ($targetPath)
 Sets the URL for the current asset. More...
 
 getLastModified ()
 Returns the time the current asset was last modified. More...
 
 getVars ()
 Returns an array of variable names for this asset. More...
 
 setValues (array $values)
 Sets the values for the asset's variables. More...
 
 getValues ()
 Returns the current values for this asset. More...
 

Static Private Member Functions

static getCacheKey (AssetInterface $asset, FilterInterface $additionalFilter=null, $salt='')
 Returns a cache key for the current asset. More...
 

Private Attributes

 $asset
 
 $cache
 

Detailed Description

Caches an asset to avoid the cost of loading and dumping.

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

Definition at line 23 of file AssetCache.php.

Constructor & Destructor Documentation

◆ __construct()

Assetic\Asset\AssetCache::__construct ( AssetInterface  $asset,
CacheInterface  $cache 
)

Definition at line 28 of file AssetCache.php.

References Assetic\Asset\AssetCache\$asset, and Assetic\Asset\AssetCache\$cache.

29  {
30  $this->asset = $asset;
31  $this->cache = $cache;
32  }

Member Function Documentation

◆ clearFilters()

Assetic\Asset\AssetCache::clearFilters ( )

Clears all filters from the current asset.

Implements Assetic\Asset\AssetInterface.

Definition at line 44 of file AssetCache.php.

45  {
46  $this->asset->clearFilters();
47  }

◆ dump()

Assetic\Asset\AssetCache::dump ( FilterInterface  $additionalFilter = null)

Applies dump filters and returns the asset as a string.

You may provide an additional filter to apply during dump.

Dumping an asset should not change its state.

If the current asset has not been loaded yet, it should be automatically loaded at this time.

Parameters
FilterInterface$additionalFilterAn additional filter
Returns
string The filtered content of the current asset

Implements Assetic\Asset\AssetInterface.

Definition at line 62 of file AssetCache.php.

63  {
64  $cacheKey = self::getCacheKey($this->asset, $additionalFilter, 'dump');
65  if ($this->cache->has($cacheKey)) {
66  return $this->cache->get($cacheKey);
67  }
68 
69  $content = $this->asset->dump($additionalFilter);
70  $this->cache->set($cacheKey, $content);
71 
72  return $content;
73  }

◆ ensureFilter()

Assetic\Asset\AssetCache::ensureFilter ( FilterInterface  $filter)

Ensures the current asset includes the supplied filter.

Parameters
FilterInterface$filterA filter

Implements Assetic\Asset\AssetInterface.

Definition at line 34 of file AssetCache.php.

35  {
36  $this->asset->ensureFilter($filter);
37  }

◆ getCacheKey()

static Assetic\Asset\AssetCache::getCacheKey ( AssetInterface  $asset,
FilterInterface  $additionalFilter = null,
  $salt = '' 
)
staticprivate

Returns a cache key for the current asset.

The key is composed of everything but an asset's content:

  • source root
  • source path
  • target url
  • last modified
  • filters
Parameters
AssetInterface$assetThe asset
FilterInterface$additionalFilterAny additional filter being applied
string$saltSalt for the key
Returns
string A key for identifying the current asset

Definition at line 147 of file AssetCache.php.

References Assetic\Asset\AssetCache\$asset, Assetic\Asset\AssetInterface\getFilters(), Assetic\Asset\AssetInterface\getLastModified(), Assetic\Asset\AssetInterface\getSourcePath(), Assetic\Asset\AssetInterface\getSourceRoot(), Assetic\Asset\AssetInterface\getTargetPath(), and Assetic\Asset\AssetInterface\getValues().

148  {
149  if ($additionalFilter) {
150  $asset = clone $asset;
151  $asset->ensureFilter($additionalFilter);
152  }
153 
154  $cacheKey = $asset->getSourceRoot();
155  $cacheKey .= $asset->getSourcePath();
156  $cacheKey .= $asset->getTargetPath();
157  $cacheKey .= $asset->getLastModified();
158 
159  foreach ($asset->getFilters() as $filter) {
160  if ($filter instanceof HashableInterface) {
161  $cacheKey .= $filter->hash();
162  } else {
163  $cacheKey .= serialize($filter);
164  }
165  }
166 
167  if ($values = $asset->getValues()) {
168  asort($values);
169  $cacheKey .= serialize($values);
170  }
171 
172  return md5($cacheKey.$salt);
173  }
+ Here is the call graph for this function:

◆ getContent()

Assetic\Asset\AssetCache::getContent ( )

Returns the loaded content of the current asset.

Returns
string The content

Implements Assetic\Asset\AssetInterface.

Definition at line 75 of file AssetCache.php.

76  {
77  return $this->asset->getContent();
78  }

◆ getFilters()

Assetic\Asset\AssetCache::getFilters ( )

Returns an array of filters currently applied.

Returns
array An array of filters

Implements Assetic\Asset\AssetInterface.

Definition at line 39 of file AssetCache.php.

40  {
41  return $this->asset->getFilters();
42  }

◆ getLastModified()

Assetic\Asset\AssetCache::getLastModified ( )

Returns the time the current asset was last modified.

Returns
integer|null A UNIX timestamp

Implements Assetic\Asset\AssetInterface.

Definition at line 110 of file AssetCache.php.

111  {
112  return $this->asset->getLastModified();
113  }

◆ getSourceDirectory()

Assetic\Asset\AssetCache::getSourceDirectory ( )

Returns the asset's source directory.

The source directory is the directory the asset was located in and can be used to resolve references relative to an asset.

Returns
string|null The asset's source directory

Implements Assetic\Asset\AssetInterface.

Definition at line 95 of file AssetCache.php.

96  {
97  return $this->asset->getSourceDirectory();
98  }

◆ getSourcePath()

Assetic\Asset\AssetCache::getSourcePath ( )

Returns the relative path for the source asset.

This value can be combined with the asset's source root (if both are non-null) to get something compatible with file_get_contents().

For example:

  • 'js/main.js'
  • 'main.js'
  • null
Returns
string|null The source asset path

Implements Assetic\Asset\AssetInterface.

Definition at line 90 of file AssetCache.php.

91  {
92  return $this->asset->getSourcePath();
93  }

◆ getSourceRoot()

Assetic\Asset\AssetCache::getSourceRoot ( )

Returns an absolute path or URL to the source asset's root directory.

This value should be an absolute path to a directory in the filesystem, an absolute URL with no path, or null.

For example:

Returns
string|null The asset's root

Implements Assetic\Asset\AssetInterface.

Definition at line 85 of file AssetCache.php.

86  {
87  return $this->asset->getSourceRoot();
88  }

◆ getTargetPath()

Assetic\Asset\AssetCache::getTargetPath ( )

Returns the URL for the current asset.

Returns
string|null A web URL where the asset will be dumped

Implements Assetic\Asset\AssetInterface.

Definition at line 100 of file AssetCache.php.

101  {
102  return $this->asset->getTargetPath();
103  }

◆ getValues()

Assetic\Asset\AssetCache::getValues ( )

Returns the current values for this asset.

Returns
array an array of strings

Implements Assetic\Asset\AssetInterface.

Definition at line 125 of file AssetCache.php.

126  {
127  return $this->asset->getValues();
128  }

◆ getVars()

Assetic\Asset\AssetCache::getVars ( )

Returns an array of variable names for this asset.

Returns
array

Implements Assetic\Asset\AssetInterface.

Definition at line 115 of file AssetCache.php.

116  {
117  return $this->asset->getVars();
118  }

◆ load()

Assetic\Asset\AssetCache::load ( FilterInterface  $additionalFilter = null)

Loads the asset into memory and applies load filters.

You may provide an additional filter to apply during load.

Parameters
FilterInterface$additionalFilterAn additional filter

Implements Assetic\Asset\AssetInterface.

Definition at line 49 of file AssetCache.php.

50  {
51  $cacheKey = self::getCacheKey($this->asset, $additionalFilter, 'load');
52  if ($this->cache->has($cacheKey)) {
53  $this->asset->setContent($this->cache->get($cacheKey));
54 
55  return;
56  }
57 
58  $this->asset->load($additionalFilter);
59  $this->cache->set($cacheKey, $this->asset->getContent());
60  }

◆ setContent()

Assetic\Asset\AssetCache::setContent (   $content)

Sets the content of the current asset.

Filters can use this method to change the content of the asset.

Parameters
string$contentThe asset content

Implements Assetic\Asset\AssetInterface.

Definition at line 80 of file AssetCache.php.

81  {
82  $this->asset->setContent($content);
83  }

◆ setTargetPath()

Assetic\Asset\AssetCache::setTargetPath (   $targetPath)

Sets the URL for the current asset.

Parameters
string$targetPathA web URL where the asset will be dumped

Implements Assetic\Asset\AssetInterface.

Definition at line 105 of file AssetCache.php.

106  {
107  $this->asset->setTargetPath($targetPath);
108  }

◆ setValues()

Assetic\Asset\AssetCache::setValues ( array  $values)

Sets the values for the asset's variables.

Parameters
array$values

Implements Assetic\Asset\AssetInterface.

Definition at line 120 of file AssetCache.php.

121  {
122  $this->asset->setValues($values);
123  }

Field Documentation

◆ $asset

Assetic\Asset\AssetCache::$asset
private

◆ $cache

Assetic\Asset\AssetCache::$cache
private

Definition at line 26 of file AssetCache.php.

Referenced by Assetic\Asset\AssetCache\__construct().


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