Implement caching for feature collections (#30) and maps (#4).

This commit is contained in:
David Molineus
2016-10-06 15:47:33 +02:00
parent 2e5139a756
commit 8c24f5e735
16 changed files with 398 additions and 75 deletions

View File

@@ -35,6 +35,17 @@ class ContaoAssets implements Assets
*/
private $assetsManager;
/**
* Cached assets.
*
* @var array
*/
private $cache = [
'stylesheets' => [],
'javascripts' => [],
'map' => []
];
/**
* ContaoAssets constructor.
*
@@ -52,6 +63,8 @@ class ContaoAssets implements Assets
*/
public function addJavascript($script, $type = self::TYPE_SOURCE)
{
$this->cache['javascripts'][] = [$script, $type];
switch ($type) {
case static::TYPE_SOURCE:
$GLOBALS['TL_HEAD'][] = sprintf('<script>%s</script>', $script);
@@ -70,6 +83,8 @@ class ContaoAssets implements Assets
*/
public function addStylesheet($stylesheet, $type = self::TYPE_FILE)
{
$this->cache['stylesheets'][] = [$stylesheet, $type];
switch ($type) {
case static::TYPE_SOURCE:
$GLOBALS['TL_HEAD'][] = sprintf('<style>%s</style>', $stylesheet);
@@ -94,8 +109,39 @@ class ContaoAssets implements Assets
*/
public function setMap($map)
{
$this->map = $map;
$this->cache['map'] = $map;
$this->map = $map;
return $this;
}
/**
* Export to array.
*
* @return array
*/
public function toArray()
{
return $this->cache;
}
/**
* From array.
*
* @param array $cache Cache.
*
* @return void
*/
public function fromArray(array $cache)
{
foreach ($cache['javascripts'] as $javascript) {
$this->addJavascript($javascript[0], $javascript[1]);
}
foreach ($cache['stylesheets'] as $stylesheet) {
$this->addStylesheet($stylesheet[0], $stylesheet[1]);
}
$this->map = $cache['map'];
}
}