diff --git a/module/config/config.php b/module/config/config.php
index c9b58f3..d2acc49 100644
--- a/module/config/config.php
+++ b/module/config/config.php
@@ -100,7 +100,7 @@ $GLOBALS['LEAFLET_MAPPERS'][] = 'Netzmacht\Contao\Leaflet\Mapper\Layer\VectorsLa
$GLOBALS['LEAFLET_MAPPERS'][] = 'Netzmacht\Contao\Leaflet\Mapper\Layer\ReferenceLayerMapper';
$GLOBALS['LEAFLET_MAPPERS'][] = function () {
return new \Netzmacht\Contao\Leaflet\Mapper\Layer\MarkerClusterLayerMapper(
- $GLOBALS['container'][\Netzmacht\Contao\Toolkit\DependencyInjection\Services::ASSETS_MANAGER]
+ $GLOBALS['container'][\Netzmacht\Contao\Leaflet\DependencyInjection\LeafletServices::MAP_ASSETS]
);
};
diff --git a/module/config/services.php b/module/config/services.php
index 48ceddf..b3e0dc5 100644
--- a/module/config/services.php
+++ b/module/config/services.php
@@ -9,6 +9,8 @@
*
*/
+use Doctrine\Common\Cache\ArrayCache;
+use Doctrine\Common\Cache\FilesystemCache;
use Interop\Container\ContainerInterface;
use Netzmacht\Contao\Leaflet\Alias\DefaultAliasFilter;
use Netzmacht\Contao\Leaflet\Boot;
@@ -16,6 +18,7 @@ use Netzmacht\Contao\Leaflet\ContaoAssets;
use Netzmacht\Contao\Leaflet\Dca\ControlCallbacks;
use Netzmacht\Contao\Leaflet\Dca\FrontendIntegration;
use Netzmacht\Contao\Leaflet\Dca\LayerCallbacks;
+use Netzmacht\Contao\Leaflet\Dca\LeafletCallbacks;
use Netzmacht\Contao\Leaflet\Dca\MapCallbacks;
use Netzmacht\Contao\Leaflet\Dca\Validator;
use Netzmacht\Contao\Leaflet\Dca\VectorCallbacks;
@@ -56,6 +59,7 @@ $container[LeafletServices::MAP_PROVIDER] = $container->share(function ($contain
$container[Services::EVENT_DISPATCHER],
$container[Services::INPUT],
$container[LeafletServices::MAP_ASSETS],
+ $container[LeafletServices::CACHE],
$GLOBALS['LEAFLET_FILTERS'],
\Config::get('debugMode') || \Config::get('displayErrors')
);
@@ -77,7 +81,7 @@ $container[LeafletServices::BOOT] = $container->share(function ($container) {
$container['leaflet.boot.subscriber'] = $container->share(function ($container) {
return new BootSubscriber(
- $container[Services::ASSETS_MANAGER],
+ $container[LeafletServices::MAP_ASSETS],
$GLOBALS['LEAFLET_MAPPERS'],
$GLOBALS['LEAFLET_ENCODERS'],
$GLOBALS['LEAFLET_LIBRARIES']
@@ -144,6 +148,21 @@ $container[LeafletServices::FRONTEND_VALUE_FILTER] = $container->share(function(
return new ValueFilter($container[Services::INSERT_TAG_REPLACER]);
});
+/**
+ * Internal used leaflet cache.
+ *
+ * @var Cache
+ */
+$container[LeafletServices::CACHE] = $container->share(
+ function ($container) {
+ if ($container[Services::PRODUCTION_MODE]) {
+ return new FilesystemCache(TL_ROOT . '/system/cache/leaflet');
+ } else {
+ return new ArrayCache();
+ }
+ }
+);
+
/**
* Leaflet alias generator.
*
@@ -266,6 +285,19 @@ $container['leaflet.dca.frontend-integration'] = $container->share(
}
);
+/**
+ * Common callback helpers.
+ *
+ * @return LeafletCallbacks
+ */
+$container['leaflet.dca.common'] = $container->share(
+ function ($container) {
+ return new LeafletCallbacks(
+ $container[Services::FILE_SYSTEM]
+ );
+ }
+);
+
/**
* Validator helper class.
*
diff --git a/module/dca/tl_leaflet_control.php b/module/dca/tl_leaflet_control.php
index 9ee11ba..6267f7d 100644
--- a/module/dca/tl_leaflet_control.php
+++ b/module/dca/tl_leaflet_control.php
@@ -23,7 +23,10 @@ $GLOBALS['TL_DCA']['tl_leaflet_control'] = array
'id' => 'primary',
'pid' => 'index',
)
- )
+ ),
+ 'onsubmit_callback' => [
+ \Netzmacht\Contao\Leaflet\Dca\LeafletCallbacks::callback('clearCache'),
+ ],
),
// List
@@ -211,7 +214,10 @@ $GLOBALS['TL_DCA']['tl_leaflet_control'] = array
'inputType' => 'checkbox',
'filter' => true,
'eval' => array('tl_class' => 'w50'),
- 'sql' => "char(1) NOT NULL default ''"
+ 'sql' => "char(1) NOT NULL default ''",
+ 'save_callback' => [
+ \Netzmacht\Contao\Leaflet\Dca\LeafletCallbacks::callback('clearCache'),
+ ],
),
'zoomInText' => array
(
diff --git a/module/dca/tl_leaflet_icon.php b/module/dca/tl_leaflet_icon.php
index 1b4ca7d..0f9af6b 100644
--- a/module/dca/tl_leaflet_icon.php
+++ b/module/dca/tl_leaflet_icon.php
@@ -21,7 +21,10 @@ $GLOBALS['TL_DCA']['tl_leaflet_icon'] = array
'id' => 'primary',
'alias' => 'unique',
)
- )
+ ),
+ 'onsubmit_callback' => [
+ \Netzmacht\Contao\Leaflet\Dca\LeafletCallbacks::callback('clearCache'),
+ ],
),
'list' => array
@@ -207,7 +210,10 @@ $GLOBALS['TL_DCA']['tl_leaflet_icon'] = array
'exclude' => true,
'inputType' => 'checkbox',
'eval' => array('tl_class' => 'w50'),
- 'sql' => "char(1) NOT NULL default ''"
+ 'sql' => "char(1) NOT NULL default ''",
+ 'save_callback' => [
+ \Netzmacht\Contao\Leaflet\Dca\LeafletCallbacks::callback('clearCache'),
+ ],
),
'iconImage' => array
(
diff --git a/module/dca/tl_leaflet_layer.php b/module/dca/tl_leaflet_layer.php
index 58c0ef5..7663298 100644
--- a/module/dca/tl_leaflet_layer.php
+++ b/module/dca/tl_leaflet_layer.php
@@ -31,7 +31,10 @@ $GLOBALS['TL_DCA']['tl_leaflet_layer'] = array
function() {
\Controller::loadLanguageFile('leaflet');
}
- )
+ ),
+ 'onsubmit_callback' => [
+ \Netzmacht\Contao\Leaflet\Dca\LeafletCallbacks::callback('clearCache'),
+ ],
),
'list' => array
(
@@ -150,7 +153,7 @@ $GLOBALS['TL_DCA']['tl_leaflet_layer'] = array
'default' => array(
'title' => array('title', 'alias', 'type'),
'config' => array(),
- 'expert' => array(':hide'),
+ 'expert' => array(':hide', 'cache'),
'active' => array('active'),
),
'markers extends default' => array(
@@ -224,7 +227,8 @@ $GLOBALS['TL_DCA']['tl_leaflet_layer'] = array
),
'metasubpalettes' => array(
- 'spiderfyOnMaxZoom' => array('spiderfyDistanceMultiplier')
+ 'spiderfyOnMaxZoom' => array('spiderfyDistanceMultiplier'),
+ 'cache' => array('cacheLifeTime')
),
'fields' => array
@@ -297,7 +301,10 @@ $GLOBALS['TL_DCA']['tl_leaflet_layer'] = array
'inputType' => 'checkbox',
'filter' => true,
'eval' => array('tl_class' => 'w50'),
- 'sql' => "char(1) NOT NULL default ''"
+ 'sql' => "char(1) NOT NULL default ''",
+ 'save_callback' => [
+ \Netzmacht\Contao\Leaflet\Dca\LeafletCallbacks::callback('clearCache'),
+ ],
),
'tile_provider' => array(
'label' => &$GLOBALS['TL_LANG']['tl_leaflet_layer']['tile_provider'],
@@ -468,7 +475,7 @@ $GLOBALS['TL_DCA']['tl_leaflet_layer'] = array
'label' => &$GLOBALS['TL_LANG']['tl_leaflet_layer']['disableClusteringAtZoom'],
'exclude' => true,
'inputType' => 'select',
- 'options_callback' => array('Netzmacht\Contao\Leaflet\Dca\LeafletCallbacks', 'getZoomLevels'),
+ 'options_callback' => Netzmacht\Contao\Leaflet\Dca\LeafletCallbacks::callback('getZoomLevels'),
'default' => '',
'eval' => array(
'maxlength' => 4,
@@ -566,7 +573,7 @@ $GLOBALS['TL_DCA']['tl_leaflet_layer'] = array
'label' => &$GLOBALS['TL_LANG']['tl_leaflet_layer']['minZoom'],
'exclude' => true,
'inputType' => 'select',
- 'options_callback' => array('Netzmacht\Contao\Leaflet\Dca\LeafletCallbacks', 'getZoomLevels'),
+ 'options_callback' => Netzmacht\Contao\Leaflet\Dca\LeafletCallbacks::callback('getZoomLevels'),
'eval' => array(
'maxlength' => 4,
'rgxp' => 'digit',
@@ -581,7 +588,7 @@ $GLOBALS['TL_DCA']['tl_leaflet_layer'] = array
'label' => &$GLOBALS['TL_LANG']['tl_leaflet_layer']['maxZoom'],
'exclude' => true,
'inputType' => 'select',
- 'options_callback' => array('Netzmacht\Contao\Leaflet\Dca\LeafletCallbacks', 'getZoomLevels'),
+ 'options_callback' => Netzmacht\Contao\Leaflet\Dca\LeafletCallbacks::callback('getZoomLevels'),
'eval' => array(
'maxlength' => 4,
'rgxp' => 'digit',
@@ -596,7 +603,7 @@ $GLOBALS['TL_DCA']['tl_leaflet_layer'] = array
'label' => &$GLOBALS['TL_LANG']['tl_leaflet_layer']['maxNativeZoom'],
'exclude' => true,
'inputType' => 'select',
- 'options_callback' => array('Netzmacht\Contao\Leaflet\Dca\LeafletCallbacks', 'getZoomLevels'),
+ 'options_callback' => Netzmacht\Contao\Leaflet\Dca\LeafletCallbacks::callback('getZoomLevels'),
'eval' => array(
'maxlength' => 4,
'rgxp' => 'digit',
@@ -757,5 +764,23 @@ $GLOBALS['TL_DCA']['tl_leaflet_layer'] = array
),
'sql' => "mediumblob NULL"
),
+ 'cache' => array
+ (
+ 'label' => &$GLOBALS['TL_LANG']['tl_leaflet_layer']['cache'],
+ 'exclude' => true,
+ 'inputType' => 'checkbox',
+ 'default' => false,
+ 'eval' => array('tl_class' => 'w50 m12', 'submitOnChange' => true),
+ 'sql' => "char(1) NOT NULL default ''"
+ ),
+ 'cacheLifeTime' => array
+ (
+ 'label' => &$GLOBALS['TL_LANG']['tl_leaflet_layer']['cacheLifeTime'],
+ 'exclude' => true,
+ 'inputType' => 'text',
+ 'default' => null,
+ 'eval' => array('maxlength' => 5, 'rgxp' => 'digit', 'tl_class' => 'w50', 'nullIfEmpty' => true),
+ 'sql' => "int(9) NOT NULL default '0'"
+ ),
)
);
diff --git a/module/dca/tl_leaflet_map.php b/module/dca/tl_leaflet_map.php
index a19aa6c..8221698 100644
--- a/module/dca/tl_leaflet_map.php
+++ b/module/dca/tl_leaflet_map.php
@@ -27,7 +27,10 @@ $GLOBALS['TL_DCA']['tl_leaflet_map'] = array
function() {
\Controller::loadLanguageFile('leaflet');
}
- )
+ ),
+ 'onsubmit_callback' => [
+ \Netzmacht\Contao\Leaflet\Dca\LeafletCallbacks::callback('clearCache'),
+ ],
),
'list' => array
@@ -114,6 +117,7 @@ $GLOBALS['TL_DCA']['tl_leaflet_map'] = array
),
'expert' => array(
'options',
+ 'cache',
)
),
),
@@ -135,6 +139,9 @@ $GLOBALS['TL_DCA']['tl_leaflet_map'] = array
'locateMaximumAge',
'enableHighAccuracy'
),
+ 'cache' => array(
+ 'cacheLifeTime'
+ )
),
'fields' => array
@@ -183,7 +190,7 @@ $GLOBALS['TL_DCA']['tl_leaflet_map'] = array
\Netzmacht\Contao\Leaflet\Dca\Validator::callback('validateCoordinates'),
),
'wizard' => array(
- array('Netzmacht\Contao\Leaflet\Dca\LeafletCallbacks', 'getGeocoder')
+ Netzmacht\Contao\Leaflet\Dca\LeafletCallbacks::callback('getGeocoder')
),
'eval' => array(
'maxlength' => 255,
@@ -232,7 +239,7 @@ $GLOBALS['TL_DCA']['tl_leaflet_map'] = array
'label' => &$GLOBALS['TL_LANG']['tl_leaflet_map']['zoom'],
'exclude' => true,
'inputType' => 'select',
- 'options_callback' => array('Netzmacht\Contao\Leaflet\Dca\LeafletCallbacks', 'getZoomLevels'),
+ 'options_callback' => Netzmacht\Contao\Leaflet\Dca\LeafletCallbacks::callback('getZoomLevels'),
'default' => '',
'eval' => array(
'maxlength' => 4,
@@ -257,7 +264,7 @@ $GLOBALS['TL_DCA']['tl_leaflet_map'] = array
'label' => &$GLOBALS['TL_LANG']['tl_leaflet_map']['minZoom'],
'exclude' => true,
'inputType' => 'select',
- 'options_callback' => array('Netzmacht\Contao\Leaflet\Dca\LeafletCallbacks', 'getZoomLevels'),
+ 'options_callback' => Netzmacht\Contao\Leaflet\Dca\LeafletCallbacks::callback('getZoomLevels'),
'eval' => array(
'maxlength' => 4,
'rgxp' => 'digit',
@@ -272,7 +279,7 @@ $GLOBALS['TL_DCA']['tl_leaflet_map'] = array
'label' => &$GLOBALS['TL_LANG']['tl_leaflet_map']['maxZoom'],
'exclude' => true,
'inputType' => 'select',
- 'options_callback' => array('Netzmacht\Contao\Leaflet\Dca\LeafletCallbacks', 'getZoomLevels'),
+ 'options_callback' => Netzmacht\Contao\Leaflet\Dca\LeafletCallbacks::callback('getZoomLevels'),
'eval' => array(
'maxlength' => 4,
'rgxp' => 'digit',
@@ -503,7 +510,7 @@ $GLOBALS['TL_DCA']['tl_leaflet_map'] = array
'label' => &$GLOBALS['TL_LANG']['tl_leaflet_map']['locateMaxZoom'],
'exclude' => true,
'inputType' => 'select',
- 'options_callback' => array('Netzmacht\Contao\Leaflet\Dca\LeafletCallbacks', 'getZoomLevels'),
+ 'options_callback' => Netzmacht\Contao\Leaflet\Dca\LeafletCallbacks::callback('getZoomLevels'),
'eval' => array(
'maxlength' => 4,
'rgxp' => 'digit',
@@ -513,5 +520,23 @@ $GLOBALS['TL_DCA']['tl_leaflet_map'] = array
),
'sql' => "int(4) NULL"
),
+ 'cache' => array
+ (
+ 'label' => &$GLOBALS['TL_LANG']['tl_leaflet_map']['cache'],
+ 'exclude' => true,
+ 'inputType' => 'checkbox',
+ 'default' => false,
+ 'eval' => array('tl_class' => 'w50 m12', 'submitOnChange' => true),
+ 'sql' => "char(1) NOT NULL default ''"
+ ),
+ 'cacheLifeTime' => array
+ (
+ 'label' => &$GLOBALS['TL_LANG']['tl_leaflet_map']['cacheLifeTime'],
+ 'exclude' => true,
+ 'inputType' => 'text',
+ 'default' => null,
+ 'eval' => array('maxlength' => 5, 'rgxp' => 'digit', 'tl_class' => 'w50', 'nullIfEmpty' => true),
+ 'sql' => "int(9) NOT NULL default '0'"
+ ),
),
);
diff --git a/module/dca/tl_leaflet_marker.php b/module/dca/tl_leaflet_marker.php
index a3b359f..4aab6d3 100644
--- a/module/dca/tl_leaflet_marker.php
+++ b/module/dca/tl_leaflet_marker.php
@@ -28,7 +28,10 @@ $GLOBALS['TL_DCA']['tl_leaflet_marker'] = array
function() {
\Controller::loadLanguageFile('leaflet');
}
- )
+ ),
+ 'onsubmit_callback' => [
+ \Netzmacht\Contao\Leaflet\Dca\LeafletCallbacks::callback('clearCache'),
+ ],
),
'list' => array
@@ -196,7 +199,7 @@ $GLOBALS['TL_DCA']['tl_leaflet_marker'] = array
array('Netzmacht\Contao\Leaflet\Dca\MarkerCallbacks', 'loadCoordinates')
),
'wizard' => array(
- array('Netzmacht\Contao\Leaflet\Dca\LeafletCallbacks', 'getGeocoder')
+ Netzmacht\Contao\Leaflet\Dca\LeafletCallbacks::callback('getGeocoder')
),
'eval' => array(
'maxlength' => 255,
@@ -238,7 +241,10 @@ $GLOBALS['TL_DCA']['tl_leaflet_marker'] = array
'flag' => 12,
'inputType' => 'checkbox',
'eval' => array('tl_class' => 'w50'),
- 'sql' => "char(1) NOT NULL default ''"
+ 'sql' => "char(1) NOT NULL default ''",
+ 'save_callback' => [
+ \Netzmacht\Contao\Leaflet\Dca\LeafletCallbacks::callback('clearCache'),
+ ],
),
'tooltip' => array
(
diff --git a/module/dca/tl_leaflet_popup.php b/module/dca/tl_leaflet_popup.php
index 4290eb9..f8d89a7 100644
--- a/module/dca/tl_leaflet_popup.php
+++ b/module/dca/tl_leaflet_popup.php
@@ -21,7 +21,10 @@ $GLOBALS['TL_DCA']['tl_leaflet_popup'] = array
'id' => 'primary',
'alias' => 'unique',
)
- )
+ ),
+ 'onsubmit_callback' => [
+ \Netzmacht\Contao\Leaflet\Dca\LeafletCallbacks::callback('clearCache'),
+ ],
),
'list' => array
@@ -290,7 +293,10 @@ $GLOBALS['TL_DCA']['tl_leaflet_popup'] = array
'search' => false,
'flag' => 12,
'eval' => array('tl_class' => 'w50'),
- 'sql' => "char(1) NOT NULL default ''"
+ 'sql' => "char(1) NOT NULL default ''",
+ 'save_callback' => [
+ \Netzmacht\Contao\Leaflet\Dca\LeafletCallbacks::callback('clearCache'),
+ ],
),
),
);
diff --git a/module/dca/tl_leaflet_style.php b/module/dca/tl_leaflet_style.php
index f5ca94f..349017e 100644
--- a/module/dca/tl_leaflet_style.php
+++ b/module/dca/tl_leaflet_style.php
@@ -20,7 +20,10 @@ $GLOBALS['TL_DCA']['tl_leaflet_style'] = array
'id' => 'primary',
'alias' => 'unique',
)
- )
+ ),
+ 'onsubmit_callback' => [
+ \Netzmacht\Contao\Leaflet\Dca\LeafletCallbacks::callback('clearCache'),
+ ],
),
'list' => array
@@ -292,7 +295,10 @@ $GLOBALS['TL_DCA']['tl_leaflet_style'] = array
'search' => false,
'flag' => 12,
'eval' => array('tl_class' => 'w50'),
- 'sql' => "char(1) NOT NULL default ''"
+ 'sql' => "char(1) NOT NULL default ''",
+ 'save_callback' => [
+ \Netzmacht\Contao\Leaflet\Dca\LeafletCallbacks::callback('clearCache'),
+ ],
),
),
);
diff --git a/module/dca/tl_leaflet_vector.php b/module/dca/tl_leaflet_vector.php
index 4ff6289..09c60e3 100644
--- a/module/dca/tl_leaflet_vector.php
+++ b/module/dca/tl_leaflet_vector.php
@@ -28,7 +28,10 @@ $GLOBALS['TL_DCA']['tl_leaflet_vector'] = array
function() {
\Controller::loadLanguageFile('leaflet');
}
- )
+ ),
+ 'onsubmit_callback' => [
+ \Netzmacht\Contao\Leaflet\Dca\LeafletCallbacks::callback('clearCache'),
+ ],
),
'list' => array
@@ -240,7 +243,10 @@ $GLOBALS['TL_DCA']['tl_leaflet_vector'] = array
'search' => false,
'flag' => 12,
'eval' => array('tl_class' => 'w50'),
- 'sql' => "char(1) NOT NULL default ''"
+ 'sql' => "char(1) NOT NULL default ''",
+ 'save_callback' => [
+ \Netzmacht\Contao\Leaflet\Dca\LeafletCallbacks::callback('clearCache'),
+ ],
),
'addPopup' => array
(
@@ -314,7 +320,7 @@ $GLOBALS['TL_DCA']['tl_leaflet_vector'] = array
\Netzmacht\Contao\Leaflet\Dca\Validator::callback('validateCoordinates')
),
'wizard' => array(
- array('Netzmacht\Contao\Leaflet\Dca\LeafletCallbacks', 'getGeocoder')
+ Netzmacht\Contao\Leaflet\Dca\LeafletCallbacks::callback('getGeocoder')
),
'eval' => array(
'maxlength' => 255,
diff --git a/src/Netzmacht/Contao/Leaflet/ContaoAssets.php b/src/Netzmacht/Contao/Leaflet/ContaoAssets.php
index f9bdd72..9387989 100644
--- a/src/Netzmacht/Contao/Leaflet/ContaoAssets.php
+++ b/src/Netzmacht/Contao/Leaflet/ContaoAssets.php
@@ -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);
@@ -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('', $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'];
+ }
}
diff --git a/src/Netzmacht/Contao/Leaflet/Dca/LeafletCallbacks.php b/src/Netzmacht/Contao/Leaflet/Dca/LeafletCallbacks.php
index 3c9b706..e9c5564 100644
--- a/src/Netzmacht/Contao/Leaflet/Dca/LeafletCallbacks.php
+++ b/src/Netzmacht/Contao/Leaflet/Dca/LeafletCallbacks.php
@@ -11,10 +11,8 @@
namespace Netzmacht\Contao\Leaflet\Dca;
-use Netzmacht\Contao\Leaflet\Mapper\DefinitionMapper;
-use Netzmacht\Contao\Leaflet\Mapper\MapMapper;
use Netzmacht\Contao\Leaflet\Model\LayerModel;
-use Netzmacht\Contao\Leaflet\Model\MapModel;
+use Netzmacht\Contao\Toolkit\Dca\Callback\CallbackFactory;
use Netzmacht\LeafletPHP\Value\LatLng;
/**
@@ -24,6 +22,35 @@ use Netzmacht\LeafletPHP\Value\LatLng;
*/
class LeafletCallbacks
{
+ /**
+ * File system.
+ *
+ * @var \Files
+ */
+ private $fileSystem;
+
+ /**
+ * LeafletCallbacks constructor.
+ *
+ * @param \Files $fileSystem File system.
+ */
+ public function __construct(\Files $fileSystem)
+ {
+ $this->fileSystem = $fileSystem;
+ }
+
+ /**
+ * Generate the callback definition.
+ *
+ * @param string $methodName Callback method name.
+ *
+ * @return callable
+ */
+ public static function callback($methodName)
+ {
+ return CallbackFactory::service('leaflet.dca.common', $methodName);
+ }
+
/**
* Create the zoom range.
*
@@ -75,4 +102,18 @@ class LeafletCallbacks
return $options;
}
+
+ /**
+ * Clear the leaflet cache.
+ *
+ * @param mixed $value Value when used as save_callback.
+ *
+ * @return mixed
+ */
+ public function clearCache($value = null)
+ {
+ $this->fileSystem->rrdir('system/cache/leaflet', true);
+
+ return $value;
+ }
}
diff --git a/src/Netzmacht/Contao/Leaflet/DependencyInjection/LeafletServices.php b/src/Netzmacht/Contao/Leaflet/DependencyInjection/LeafletServices.php
index cd2cec3..4d3b2a6 100644
--- a/src/Netzmacht/Contao/Leaflet/DependencyInjection/LeafletServices.php
+++ b/src/Netzmacht/Contao/Leaflet/DependencyInjection/LeafletServices.php
@@ -10,6 +10,7 @@
namespace Netzmacht\Contao\Leaflet\DependencyInjection;
+use Doctrine\Common\Cache\Cache;
use Netzmacht\Contao\Leaflet\Boot;
use Netzmacht\Contao\Leaflet\Frontend\ValueFilter;
use Netzmacht\Contao\Leaflet\Mapper\DefinitionMapper;
@@ -40,6 +41,13 @@ class LeafletServices
*/
const BOOT = 'leaflet.boot';
+ /**
+ * Leaflet cache
+ *
+ * @return Cache
+ */
+ const CACHE = 'leaflet.cache';
+
/**
* Service name of the definition builder.
*
diff --git a/src/Netzmacht/Contao/Leaflet/MapProvider.php b/src/Netzmacht/Contao/Leaflet/MapProvider.php
index a848fb0..4e14c6c 100644
--- a/src/Netzmacht/Contao/Leaflet/MapProvider.php
+++ b/src/Netzmacht/Contao/Leaflet/MapProvider.php
@@ -11,6 +11,7 @@
namespace Netzmacht\Contao\Leaflet;
+use Doctrine\Common\Cache\Cache;
use Netzmacht\Contao\Leaflet\Event\GetJavascriptEvent;
use Netzmacht\Contao\Leaflet\Filter\Filter;
use Netzmacht\Contao\Leaflet\Frontend\DataController;
@@ -18,7 +19,6 @@ use Netzmacht\Contao\Leaflet\Frontend\RequestUrl;
use Netzmacht\Contao\Leaflet\Mapper\DefinitionMapper;
use Netzmacht\Contao\Leaflet\Model\LayerModel;
use Netzmacht\Contao\Leaflet\Model\MapModel;
-use Netzmacht\LeafletPHP\Assets;
use Netzmacht\LeafletPHP\Value\GeoJson\FeatureCollection;
use Netzmacht\LeafletPHP\Definition\Map;
use Netzmacht\LeafletPHP\Leaflet;
@@ -62,7 +62,7 @@ class MapProvider
/**
* Map assets collector.
*
- * @var Assets
+ * @var ContaoAssets
*/
private $assets;
@@ -80,6 +80,13 @@ class MapProvider
*/
private $displayErrors;
+ /**
+ * Cache.
+ *
+ * @var Cache
+ */
+ private $cache;
+
/**
* Construct.
*
@@ -87,7 +94,8 @@ class MapProvider
* @param Leaflet $leaflet The Leaflet instance.
* @param EventDispatcher $eventDispatcher The Contao event dispatcher.
* @param \Input $input Thw request input.
- * @param Assets $assets Assets handler.
+ * @param ContaoAssets $assets Assets handler.
+ * @param Cache $cache Cache.
* @param array $filters Request filters configuration.
* @param bool $displayErrors Display errors setting.
*/
@@ -96,7 +104,8 @@ class MapProvider
Leaflet $leaflet,
EventDispatcher $eventDispatcher,
\Input $input,
- Assets $assets,
+ ContaoAssets $assets,
+ Cache $cache,
array $filters,
$displayErrors
) {
@@ -107,6 +116,7 @@ class MapProvider
$this->assets = $assets;
$this->filters = $filters;
$this->displayErrors = $displayErrors;
+ $this->cache = $cache;
}
/**
@@ -165,9 +175,6 @@ class MapProvider
*
* @return string
* @throws \Exception If generating went wrong.
- *
- * @SuppressWarnings(PHPMD.UnusedFormalParameter)
- * @SuppressWarnings(PHPMD.UnusedLocalVariable)
*/
public function generate(
$mapId,
@@ -176,23 +183,38 @@ class MapProvider
$template = 'leaflet_map_js',
$style = ''
) {
- $definition = $this->getDefinition($mapId, $filter, $elementId);
- $template = \Controller::getTemplate($template);
+ if ($mapId instanceof MapModel) {
+ $model = $mapId;
+ $mapId = $mapId->id;
+ } else {
+ $model = $this->getModel($mapId);
+ }
- // @codingStandardsIgnoreStart - Set for the template.
- $javascript = $this->leaflet->build($definition, $this->assets);
- $mapId = $definition->getId();
- // @codingStandardsIgnoreEnd
+ if ($model->cache) {
+ $cacheKey = $this->getCacheKey($mapId, $filter, $elementId, $template, $style);
- ob_start();
- include $template;
- $content = ob_get_contents();
- ob_end_clean();
+ if ($this->cache->contains($cacheKey)) {
+ $cached = $this->cache->fetch($cacheKey);
+ $this->assets->fromArray($cached['assets']);
- $event = new GetJavascriptEvent($definition, $content);
- $this->eventDispatcher->dispatch($event::NAME, $event);
+ return $cached['javascript'];
+ }
+ }
- return $event->getJavascript();
+ $buffer = $this->doGenerate($mapId, $filter, $elementId, $template, $model, $style);
+
+ if ($model->cache) {
+ $this->cache->save(
+ $cacheKey,
+ [
+ 'assets' => $this->assets->toArray(),
+ 'javascript' => $buffer
+ ],
+ (int) $model->cacheLifeTime
+ );
+ }
+
+ return $buffer;
}
/**
@@ -217,7 +239,23 @@ class MapProvider
throw new \InvalidArgumentException(sprintf('Could not find layer "%s"', $layerId));
}
- return $this->mapper->handleGeoJson($model, $filter);
+ if (!$model->cache) {
+ return $this->mapper->handleGeoJson($model, $filter);
+ }
+
+ $cacheKey = 'feature_layer_' . $model->id;
+ if ($filter) {
+ $cacheKey .= '.filter_' . md5($filter->toRequest());
+ }
+
+ if ($this->cache->contains($cacheKey)) {
+ return $this->cache->fetch($cacheKey);
+ }
+
+ $collection = $this->mapper->handleGeoJson($model, $filter);
+ $this->cache->save($cacheKey, $collection, $model->cacheLifeTime);
+
+ return $collection;
}
/**
@@ -230,7 +268,6 @@ class MapProvider
* @throws \RuntimeException IF the input data does not match.
*
* @SuppressWarnings(PHPMD.ExitExpression)
- * @SuppressWarnings(PHPMD.Superglobals)
*/
public function handleAjaxRequest($identifier, $exit = true)
{
@@ -261,4 +298,75 @@ class MapProvider
}
}
}
+
+ /**
+ * Get the cache key.
+ *
+ * @param int $mapId The map database id.
+ * @param Filter|null $filter Optional request filter.
+ * @param string $elementId Optional element id. If none given the mapId or alias is used.
+ * @param string $template The template being used for generating.
+ * @param string $style Optional style attributes.
+ *
+ * @return string
+ */
+ protected function getCacheKey($mapId, $filter, $elementId, $template, $style)
+ {
+ $cacheKey = 'map_' . $mapId;
+
+ if ($filter) {
+ $cacheKey .= '.filter_' . md5($filter->toRequest());
+ }
+
+ if ($elementId) {
+ $cacheKey .= '.element_' . $elementId;
+ }
+
+ $cacheKey .= '.template_' . $template;
+
+ if ($style) {
+ $cacheKey .= '.style_' . md5($style);
+
+ return $cacheKey;
+ }
+
+ return $cacheKey;
+ }
+
+ /**
+ * Do the generating of the map.
+ *
+ * @param MapModel $model Map model.
+ * @param Filter|null $filter Optional request filter.
+ * @param string $elementId Optional element id. If none given the mapId or alias is used.
+ * @param string $template The template being used for generating.
+ * @param string $style Optional style attributes.
+ *
+ * @return string
+ *
+ * @SuppressWarnings(PHPMD.UnusedFormalParameter)
+ * @SuppressWarnings(PHPMD.UnusedLocalVariable)
+ */
+ protected function doGenerate($model, $filter, $elementId, $template, $style)
+ {
+ $definition = $this->getDefinition($model, $filter, $elementId);
+ $template = \Controller::getTemplate($template);
+
+ // @codingStandardsIgnoreStart - Set for the template.
+ $javascript = $this->leaflet->build($definition, $this->assets);
+ $mapId = $definition->getId();
+ // @codingStandardsIgnoreEnd
+
+ ob_start();
+ include $template;
+ $content = ob_get_contents();
+ ob_end_clean();
+
+ $event = new GetJavascriptEvent($definition, $content);
+ $this->eventDispatcher->dispatch($event::NAME, $event);
+
+ $buffer = $event->getJavascript();
+
+ return $buffer;
+ }
}
diff --git a/src/Netzmacht/Contao/Leaflet/Mapper/Layer/MarkerClusterLayerMapper.php b/src/Netzmacht/Contao/Leaflet/Mapper/Layer/MarkerClusterLayerMapper.php
index 1cc3167..cf36b8f 100644
--- a/src/Netzmacht/Contao/Leaflet/Mapper/Layer/MarkerClusterLayerMapper.php
+++ b/src/Netzmacht/Contao/Leaflet/Mapper/Layer/MarkerClusterLayerMapper.php
@@ -11,6 +11,7 @@
namespace Netzmacht\Contao\Leaflet\Mapper\Layer;
+use Netzmacht\Contao\Leaflet\ContaoAssets;
use Netzmacht\Contao\Leaflet\Filter\Filter;
use Netzmacht\Contao\Leaflet\Mapper\DefinitionMapper;
use Netzmacht\Contao\Leaflet\Model\LayerModel;
@@ -46,20 +47,20 @@ class MarkerClusterLayerMapper extends AbstractLayerMapper
/**
* Assets manager.
*
- * @var AssetsManager
+ * @var ContaoAssets
*/
- private $assetsManager;
+ private $assets;
/**
* MarkerClusterLayerMapper constructor.
*
- * @param AssetsManager $assetsManager Assets manager.
+ * @param ContaoAssets $assets Assets manager.
*/
- public function __construct(AssetsManager $assetsManager)
+ public function __construct(ContaoAssets $assets)
{
parent::__construct();
- $this->assetsManager = $assetsManager;
+ $this->assets = $assets;
}
/**
@@ -102,7 +103,7 @@ class MarkerClusterLayerMapper extends AbstractLayerMapper
}
if (!$model->disableDefaultStyle) {
- $this->assetsManager->addStylesheet('assets/leaflet/libs/leaflet-markercluster/MarkerCluster.Default.css');
+ $this->assets->addStylesheet('assets/leaflet/libs/leaflet-markercluster/MarkerCluster.Default.css');
}
$collection = LayerModel::findBy(
diff --git a/src/Netzmacht/Contao/Leaflet/Subscriber/BootSubscriber.php b/src/Netzmacht/Contao/Leaflet/Subscriber/BootSubscriber.php
index a4ddbbd..5809307 100644
--- a/src/Netzmacht/Contao/Leaflet/Subscriber/BootSubscriber.php
+++ b/src/Netzmacht/Contao/Leaflet/Subscriber/BootSubscriber.php
@@ -12,6 +12,7 @@
namespace Netzmacht\Contao\Leaflet\Subscriber;
use ContaoCommunityAlliance\Contao\EventDispatcher\EventDispatcherInitializer;
+use Netzmacht\Contao\Leaflet\ContaoAssets;
use Netzmacht\Contao\Leaflet\DependencyInjection\LeafletServices;
use Netzmacht\Contao\Leaflet\Event\GetJavascriptEvent;
use Netzmacht\Contao\Leaflet\Event\InitializeDefinitionMapperEvent;
@@ -59,9 +60,9 @@ class BootSubscriber implements EventSubscriberInterface
/**
* Assets manager.
*
- * @var AssetsManager
+ * @var ContaoAssets
*/
- private $assetsManager;
+ private $assets;
/**
* Definition mapper.
@@ -73,21 +74,21 @@ class BootSubscriber implements EventSubscriberInterface
/**
* BootSubscriber constructor.
*
- * @param AssetsManager $assetsManager Assets manager.
- * @param array $mappers Leaflet mapper configuration.
- * @param array $encoders Leaflet encoder configuration.
- * @param array $libraries Leaflet libraries configuration.
+ * @param ContaoAssets $assets Leaflet assets manager.
+ * @param array $mappers Leaflet mapper configuration.
+ * @param array $encoders Leaflet encoder configuration.
+ * @param array $libraries Leaflet libraries configuration.
*/
public function __construct(
- AssetsManager $assetsManager,
+ ContaoAssets $assets,
array $mappers,
array $encoders,
array $libraries
) {
- $this->assetsManager = $assetsManager;
- $this->mappers = $mappers;
- $this->encoders = $encoders;
- $this->libraries = $libraries;
+ $this->assets = $assets;
+ $this->mappers = $mappers;
+ $this->encoders = $encoders;
+ $this->libraries = $libraries;
}
/**
@@ -188,7 +189,7 @@ class BootSubscriber implements EventSubscriberInterface
*/
public function loadAssets()
{
- $this->assetsManager->addJavascript('assets/leaflet/maps/contao-leaflet.js');
+ $this->assets->addJavascript('assets/leaflet/maps/contao-leaflet.js', ContaoAssets::TYPE_FILE);
}
/**
@@ -229,7 +230,7 @@ class BootSubscriber implements EventSubscriberInterface
// @codingStandardsIgnoreStart
// TODO: Cache it.
// codingStandardsIgnoreEnd
- $this->assetsManager->addJavascript('assets/leaflet/js/icons.js');
+ $this->assets->addJavascript('assets/leaflet/js/icons.js', ContaoAssets::TYPE_FILE);
}
}