Files
contao-leaflet-maps/src/ContaoAssets.php

149 lines
3.1 KiB
PHP
Raw Normal View History

2014-12-29 12:17:40 +01:00
<?php
/**
2017-10-05 15:45:43 +02:00
* Leaflet maps for Contao CMS.
*
2016-10-11 10:40:15 +02:00
* @package contao-leaflet-maps
2014-12-29 12:17:40 +01:00
* @author David Molineus <david.molineus@netzmacht.de>
2017-10-05 15:45:43 +02:00
* @copyright 2016-2017 netzmacht David Molineus. All rights reserved.
* @license LGPL-3.0 https://github.com/netzmacht/contao-leaflet-maps/blob/master/LICENSE
2014-12-29 12:17:40 +01:00
* @filesource
*/
namespace Netzmacht\Contao\Leaflet;
2016-10-05 16:11:59 +02:00
use Netzmacht\Contao\Toolkit\View\Assets\AssetsManager;
2014-12-29 12:17:40 +01:00
use Netzmacht\LeafletPHP\Assets;
/**
2015-01-12 19:03:29 +01:00
* Class ContaoAssets handles leaflet assets and integrate them into the Contao assets registry (Superglobals).
*
2014-12-29 12:17:40 +01:00
* @package Netzmacht\Contao\Leaflet
*/
class ContaoAssets implements Assets
{
/**
2015-01-12 19:03:29 +01:00
* The map javascript.
*
* @var string
2014-12-29 12:17:40 +01:00
*/
private $map;
2016-10-05 16:11:59 +02:00
/**
* Assets manager.
*
* @var AssetsManager
*/
private $assetsManager;
/**
* Cached assets.
*
* @var array
*/
private $cache = [
'stylesheets' => [],
'javascripts' => [],
2016-10-06 15:49:15 +02:00
'map' => []
];
2016-10-05 16:11:59 +02:00
/**
* ContaoAssets constructor.
*
* @param AssetsManager $assetsManager Contao assets manager.
*/
public function __construct(AssetsManager $assetsManager)
{
$this->assetsManager = $assetsManager;
}
2014-12-29 12:17:40 +01:00
/**
* {@inheritdoc}
2015-01-12 19:03:29 +01:00
*
* @SuppressWarnings(PHPMD.Superglobals)
2014-12-29 12:17:40 +01:00
*/
public function addJavascript($script, $type = self::TYPE_SOURCE)
{
$this->cache['javascripts'][] = [$script, $type];
2014-12-29 12:17:40 +01:00
switch ($type) {
case static::TYPE_SOURCE:
$GLOBALS['TL_HEAD'][] = sprintf('<script>%s</script>', $script);
break;
case static::TYPE_FILE:
default:
2016-10-05 16:11:59 +02:00
$this->assetsManager->addJavascript($script);
2014-12-29 12:17:40 +01:00
}
}
/**
* {@inheritdoc}
2015-01-12 19:03:29 +01:00
*
* @SuppressWarnings(PHPMD.Superglobals)
2014-12-29 12:17:40 +01:00
*/
public function addStylesheet($stylesheet, $type = self::TYPE_FILE)
{
$this->cache['stylesheets'][] = [$stylesheet, $type];
2014-12-29 12:17:40 +01:00
switch ($type) {
case static::TYPE_SOURCE:
$GLOBALS['TL_HEAD'][] = sprintf('<style>%s</style>', $stylesheet);
break;
case static::TYPE_FILE:
default:
2016-10-05 16:11:59 +02:00
$this->assetsManager->addStylesheet($stylesheet);
2014-12-29 12:17:40 +01:00
}
}
/**
* {@inheritdoc}
*/
public function getMap()
{
return $this->map;
}
/**
* {@inheritdoc}
*/
public function setMap($map)
{
$this->cache['map'] = $map;
$this->map = $map;
2014-12-29 12:17:40 +01:00
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'];
}
2014-12-29 12:17:40 +01:00
}