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

@@ -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'][] = 'Netzmacht\Contao\Leaflet\Mapper\Layer\ReferenceLayerMapper';
$GLOBALS['LEAFLET_MAPPERS'][] = function () { $GLOBALS['LEAFLET_MAPPERS'][] = function () {
return new \Netzmacht\Contao\Leaflet\Mapper\Layer\MarkerClusterLayerMapper( 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]
); );
}; };

View File

@@ -9,6 +9,8 @@
* *
*/ */
use Doctrine\Common\Cache\ArrayCache;
use Doctrine\Common\Cache\FilesystemCache;
use Interop\Container\ContainerInterface; use Interop\Container\ContainerInterface;
use Netzmacht\Contao\Leaflet\Alias\DefaultAliasFilter; use Netzmacht\Contao\Leaflet\Alias\DefaultAliasFilter;
use Netzmacht\Contao\Leaflet\Boot; 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\ControlCallbacks;
use Netzmacht\Contao\Leaflet\Dca\FrontendIntegration; use Netzmacht\Contao\Leaflet\Dca\FrontendIntegration;
use Netzmacht\Contao\Leaflet\Dca\LayerCallbacks; use Netzmacht\Contao\Leaflet\Dca\LayerCallbacks;
use Netzmacht\Contao\Leaflet\Dca\LeafletCallbacks;
use Netzmacht\Contao\Leaflet\Dca\MapCallbacks; use Netzmacht\Contao\Leaflet\Dca\MapCallbacks;
use Netzmacht\Contao\Leaflet\Dca\Validator; use Netzmacht\Contao\Leaflet\Dca\Validator;
use Netzmacht\Contao\Leaflet\Dca\VectorCallbacks; use Netzmacht\Contao\Leaflet\Dca\VectorCallbacks;
@@ -56,6 +59,7 @@ $container[LeafletServices::MAP_PROVIDER] = $container->share(function ($contain
$container[Services::EVENT_DISPATCHER], $container[Services::EVENT_DISPATCHER],
$container[Services::INPUT], $container[Services::INPUT],
$container[LeafletServices::MAP_ASSETS], $container[LeafletServices::MAP_ASSETS],
$container[LeafletServices::CACHE],
$GLOBALS['LEAFLET_FILTERS'], $GLOBALS['LEAFLET_FILTERS'],
\Config::get('debugMode') || \Config::get('displayErrors') \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) { $container['leaflet.boot.subscriber'] = $container->share(function ($container) {
return new BootSubscriber( return new BootSubscriber(
$container[Services::ASSETS_MANAGER], $container[LeafletServices::MAP_ASSETS],
$GLOBALS['LEAFLET_MAPPERS'], $GLOBALS['LEAFLET_MAPPERS'],
$GLOBALS['LEAFLET_ENCODERS'], $GLOBALS['LEAFLET_ENCODERS'],
$GLOBALS['LEAFLET_LIBRARIES'] $GLOBALS['LEAFLET_LIBRARIES']
@@ -144,6 +148,21 @@ $container[LeafletServices::FRONTEND_VALUE_FILTER] = $container->share(function(
return new ValueFilter($container[Services::INSERT_TAG_REPLACER]); 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. * 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. * Validator helper class.
* *

View File

@@ -23,7 +23,10 @@ $GLOBALS['TL_DCA']['tl_leaflet_control'] = array
'id' => 'primary', 'id' => 'primary',
'pid' => 'index', 'pid' => 'index',
) )
) ),
'onsubmit_callback' => [
\Netzmacht\Contao\Leaflet\Dca\LeafletCallbacks::callback('clearCache'),
],
), ),
// List // List
@@ -211,7 +214,10 @@ $GLOBALS['TL_DCA']['tl_leaflet_control'] = array
'inputType' => 'checkbox', 'inputType' => 'checkbox',
'filter' => true, 'filter' => true,
'eval' => array('tl_class' => 'w50'), '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 'zoomInText' => array
( (

View File

@@ -21,7 +21,10 @@ $GLOBALS['TL_DCA']['tl_leaflet_icon'] = array
'id' => 'primary', 'id' => 'primary',
'alias' => 'unique', 'alias' => 'unique',
) )
) ),
'onsubmit_callback' => [
\Netzmacht\Contao\Leaflet\Dca\LeafletCallbacks::callback('clearCache'),
],
), ),
'list' => array 'list' => array
@@ -207,7 +210,10 @@ $GLOBALS['TL_DCA']['tl_leaflet_icon'] = array
'exclude' => true, 'exclude' => true,
'inputType' => 'checkbox', 'inputType' => 'checkbox',
'eval' => array('tl_class' => 'w50'), '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 'iconImage' => array
( (

View File

@@ -31,7 +31,10 @@ $GLOBALS['TL_DCA']['tl_leaflet_layer'] = array
function() { function() {
\Controller::loadLanguageFile('leaflet'); \Controller::loadLanguageFile('leaflet');
} }
) ),
'onsubmit_callback' => [
\Netzmacht\Contao\Leaflet\Dca\LeafletCallbacks::callback('clearCache'),
],
), ),
'list' => array 'list' => array
( (
@@ -150,7 +153,7 @@ $GLOBALS['TL_DCA']['tl_leaflet_layer'] = array
'default' => array( 'default' => array(
'title' => array('title', 'alias', 'type'), 'title' => array('title', 'alias', 'type'),
'config' => array(), 'config' => array(),
'expert' => array(':hide'), 'expert' => array(':hide', 'cache'),
'active' => array('active'), 'active' => array('active'),
), ),
'markers extends default' => array( 'markers extends default' => array(
@@ -224,7 +227,8 @@ $GLOBALS['TL_DCA']['tl_leaflet_layer'] = array
), ),
'metasubpalettes' => array( 'metasubpalettes' => array(
'spiderfyOnMaxZoom' => array('spiderfyDistanceMultiplier') 'spiderfyOnMaxZoom' => array('spiderfyDistanceMultiplier'),
'cache' => array('cacheLifeTime')
), ),
'fields' => array 'fields' => array
@@ -297,7 +301,10 @@ $GLOBALS['TL_DCA']['tl_leaflet_layer'] = array
'inputType' => 'checkbox', 'inputType' => 'checkbox',
'filter' => true, 'filter' => true,
'eval' => array('tl_class' => 'w50'), '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( 'tile_provider' => array(
'label' => &$GLOBALS['TL_LANG']['tl_leaflet_layer']['tile_provider'], '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'], 'label' => &$GLOBALS['TL_LANG']['tl_leaflet_layer']['disableClusteringAtZoom'],
'exclude' => true, 'exclude' => true,
'inputType' => 'select', 'inputType' => 'select',
'options_callback' => array('Netzmacht\Contao\Leaflet\Dca\LeafletCallbacks', 'getZoomLevels'), 'options_callback' => Netzmacht\Contao\Leaflet\Dca\LeafletCallbacks::callback('getZoomLevels'),
'default' => '', 'default' => '',
'eval' => array( 'eval' => array(
'maxlength' => 4, 'maxlength' => 4,
@@ -566,7 +573,7 @@ $GLOBALS['TL_DCA']['tl_leaflet_layer'] = array
'label' => &$GLOBALS['TL_LANG']['tl_leaflet_layer']['minZoom'], 'label' => &$GLOBALS['TL_LANG']['tl_leaflet_layer']['minZoom'],
'exclude' => true, 'exclude' => true,
'inputType' => 'select', 'inputType' => 'select',
'options_callback' => array('Netzmacht\Contao\Leaflet\Dca\LeafletCallbacks', 'getZoomLevels'), 'options_callback' => Netzmacht\Contao\Leaflet\Dca\LeafletCallbacks::callback('getZoomLevels'),
'eval' => array( 'eval' => array(
'maxlength' => 4, 'maxlength' => 4,
'rgxp' => 'digit', 'rgxp' => 'digit',
@@ -581,7 +588,7 @@ $GLOBALS['TL_DCA']['tl_leaflet_layer'] = array
'label' => &$GLOBALS['TL_LANG']['tl_leaflet_layer']['maxZoom'], 'label' => &$GLOBALS['TL_LANG']['tl_leaflet_layer']['maxZoom'],
'exclude' => true, 'exclude' => true,
'inputType' => 'select', 'inputType' => 'select',
'options_callback' => array('Netzmacht\Contao\Leaflet\Dca\LeafletCallbacks', 'getZoomLevels'), 'options_callback' => Netzmacht\Contao\Leaflet\Dca\LeafletCallbacks::callback('getZoomLevels'),
'eval' => array( 'eval' => array(
'maxlength' => 4, 'maxlength' => 4,
'rgxp' => 'digit', 'rgxp' => 'digit',
@@ -596,7 +603,7 @@ $GLOBALS['TL_DCA']['tl_leaflet_layer'] = array
'label' => &$GLOBALS['TL_LANG']['tl_leaflet_layer']['maxNativeZoom'], 'label' => &$GLOBALS['TL_LANG']['tl_leaflet_layer']['maxNativeZoom'],
'exclude' => true, 'exclude' => true,
'inputType' => 'select', 'inputType' => 'select',
'options_callback' => array('Netzmacht\Contao\Leaflet\Dca\LeafletCallbacks', 'getZoomLevels'), 'options_callback' => Netzmacht\Contao\Leaflet\Dca\LeafletCallbacks::callback('getZoomLevels'),
'eval' => array( 'eval' => array(
'maxlength' => 4, 'maxlength' => 4,
'rgxp' => 'digit', 'rgxp' => 'digit',
@@ -757,5 +764,23 @@ $GLOBALS['TL_DCA']['tl_leaflet_layer'] = array
), ),
'sql' => "mediumblob NULL" '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'"
),
) )
); );

View File

@@ -27,7 +27,10 @@ $GLOBALS['TL_DCA']['tl_leaflet_map'] = array
function() { function() {
\Controller::loadLanguageFile('leaflet'); \Controller::loadLanguageFile('leaflet');
} }
) ),
'onsubmit_callback' => [
\Netzmacht\Contao\Leaflet\Dca\LeafletCallbacks::callback('clearCache'),
],
), ),
'list' => array 'list' => array
@@ -114,6 +117,7 @@ $GLOBALS['TL_DCA']['tl_leaflet_map'] = array
), ),
'expert' => array( 'expert' => array(
'options', 'options',
'cache',
) )
), ),
), ),
@@ -135,6 +139,9 @@ $GLOBALS['TL_DCA']['tl_leaflet_map'] = array
'locateMaximumAge', 'locateMaximumAge',
'enableHighAccuracy' 'enableHighAccuracy'
), ),
'cache' => array(
'cacheLifeTime'
)
), ),
'fields' => array 'fields' => array
@@ -183,7 +190,7 @@ $GLOBALS['TL_DCA']['tl_leaflet_map'] = array
\Netzmacht\Contao\Leaflet\Dca\Validator::callback('validateCoordinates'), \Netzmacht\Contao\Leaflet\Dca\Validator::callback('validateCoordinates'),
), ),
'wizard' => array( 'wizard' => array(
array('Netzmacht\Contao\Leaflet\Dca\LeafletCallbacks', 'getGeocoder') Netzmacht\Contao\Leaflet\Dca\LeafletCallbacks::callback('getGeocoder')
), ),
'eval' => array( 'eval' => array(
'maxlength' => 255, 'maxlength' => 255,
@@ -232,7 +239,7 @@ $GLOBALS['TL_DCA']['tl_leaflet_map'] = array
'label' => &$GLOBALS['TL_LANG']['tl_leaflet_map']['zoom'], 'label' => &$GLOBALS['TL_LANG']['tl_leaflet_map']['zoom'],
'exclude' => true, 'exclude' => true,
'inputType' => 'select', 'inputType' => 'select',
'options_callback' => array('Netzmacht\Contao\Leaflet\Dca\LeafletCallbacks', 'getZoomLevels'), 'options_callback' => Netzmacht\Contao\Leaflet\Dca\LeafletCallbacks::callback('getZoomLevels'),
'default' => '', 'default' => '',
'eval' => array( 'eval' => array(
'maxlength' => 4, 'maxlength' => 4,
@@ -257,7 +264,7 @@ $GLOBALS['TL_DCA']['tl_leaflet_map'] = array
'label' => &$GLOBALS['TL_LANG']['tl_leaflet_map']['minZoom'], 'label' => &$GLOBALS['TL_LANG']['tl_leaflet_map']['minZoom'],
'exclude' => true, 'exclude' => true,
'inputType' => 'select', 'inputType' => 'select',
'options_callback' => array('Netzmacht\Contao\Leaflet\Dca\LeafletCallbacks', 'getZoomLevels'), 'options_callback' => Netzmacht\Contao\Leaflet\Dca\LeafletCallbacks::callback('getZoomLevels'),
'eval' => array( 'eval' => array(
'maxlength' => 4, 'maxlength' => 4,
'rgxp' => 'digit', 'rgxp' => 'digit',
@@ -272,7 +279,7 @@ $GLOBALS['TL_DCA']['tl_leaflet_map'] = array
'label' => &$GLOBALS['TL_LANG']['tl_leaflet_map']['maxZoom'], 'label' => &$GLOBALS['TL_LANG']['tl_leaflet_map']['maxZoom'],
'exclude' => true, 'exclude' => true,
'inputType' => 'select', 'inputType' => 'select',
'options_callback' => array('Netzmacht\Contao\Leaflet\Dca\LeafletCallbacks', 'getZoomLevels'), 'options_callback' => Netzmacht\Contao\Leaflet\Dca\LeafletCallbacks::callback('getZoomLevels'),
'eval' => array( 'eval' => array(
'maxlength' => 4, 'maxlength' => 4,
'rgxp' => 'digit', 'rgxp' => 'digit',
@@ -503,7 +510,7 @@ $GLOBALS['TL_DCA']['tl_leaflet_map'] = array
'label' => &$GLOBALS['TL_LANG']['tl_leaflet_map']['locateMaxZoom'], 'label' => &$GLOBALS['TL_LANG']['tl_leaflet_map']['locateMaxZoom'],
'exclude' => true, 'exclude' => true,
'inputType' => 'select', 'inputType' => 'select',
'options_callback' => array('Netzmacht\Contao\Leaflet\Dca\LeafletCallbacks', 'getZoomLevels'), 'options_callback' => Netzmacht\Contao\Leaflet\Dca\LeafletCallbacks::callback('getZoomLevels'),
'eval' => array( 'eval' => array(
'maxlength' => 4, 'maxlength' => 4,
'rgxp' => 'digit', 'rgxp' => 'digit',
@@ -513,5 +520,23 @@ $GLOBALS['TL_DCA']['tl_leaflet_map'] = array
), ),
'sql' => "int(4) NULL" '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'"
),
), ),
); );

View File

@@ -28,7 +28,10 @@ $GLOBALS['TL_DCA']['tl_leaflet_marker'] = array
function() { function() {
\Controller::loadLanguageFile('leaflet'); \Controller::loadLanguageFile('leaflet');
} }
) ),
'onsubmit_callback' => [
\Netzmacht\Contao\Leaflet\Dca\LeafletCallbacks::callback('clearCache'),
],
), ),
'list' => array 'list' => array
@@ -196,7 +199,7 @@ $GLOBALS['TL_DCA']['tl_leaflet_marker'] = array
array('Netzmacht\Contao\Leaflet\Dca\MarkerCallbacks', 'loadCoordinates') array('Netzmacht\Contao\Leaflet\Dca\MarkerCallbacks', 'loadCoordinates')
), ),
'wizard' => array( 'wizard' => array(
array('Netzmacht\Contao\Leaflet\Dca\LeafletCallbacks', 'getGeocoder') Netzmacht\Contao\Leaflet\Dca\LeafletCallbacks::callback('getGeocoder')
), ),
'eval' => array( 'eval' => array(
'maxlength' => 255, 'maxlength' => 255,
@@ -238,7 +241,10 @@ $GLOBALS['TL_DCA']['tl_leaflet_marker'] = array
'flag' => 12, 'flag' => 12,
'inputType' => 'checkbox', 'inputType' => 'checkbox',
'eval' => array('tl_class' => 'w50'), '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 'tooltip' => array
( (

View File

@@ -21,7 +21,10 @@ $GLOBALS['TL_DCA']['tl_leaflet_popup'] = array
'id' => 'primary', 'id' => 'primary',
'alias' => 'unique', 'alias' => 'unique',
) )
) ),
'onsubmit_callback' => [
\Netzmacht\Contao\Leaflet\Dca\LeafletCallbacks::callback('clearCache'),
],
), ),
'list' => array 'list' => array
@@ -290,7 +293,10 @@ $GLOBALS['TL_DCA']['tl_leaflet_popup'] = array
'search' => false, 'search' => false,
'flag' => 12, 'flag' => 12,
'eval' => array('tl_class' => 'w50'), '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'),
],
), ),
), ),
); );

View File

@@ -20,7 +20,10 @@ $GLOBALS['TL_DCA']['tl_leaflet_style'] = array
'id' => 'primary', 'id' => 'primary',
'alias' => 'unique', 'alias' => 'unique',
) )
) ),
'onsubmit_callback' => [
\Netzmacht\Contao\Leaflet\Dca\LeafletCallbacks::callback('clearCache'),
],
), ),
'list' => array 'list' => array
@@ -292,7 +295,10 @@ $GLOBALS['TL_DCA']['tl_leaflet_style'] = array
'search' => false, 'search' => false,
'flag' => 12, 'flag' => 12,
'eval' => array('tl_class' => 'w50'), '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'),
],
), ),
), ),
); );

View File

@@ -28,7 +28,10 @@ $GLOBALS['TL_DCA']['tl_leaflet_vector'] = array
function() { function() {
\Controller::loadLanguageFile('leaflet'); \Controller::loadLanguageFile('leaflet');
} }
) ),
'onsubmit_callback' => [
\Netzmacht\Contao\Leaflet\Dca\LeafletCallbacks::callback('clearCache'),
],
), ),
'list' => array 'list' => array
@@ -240,7 +243,10 @@ $GLOBALS['TL_DCA']['tl_leaflet_vector'] = array
'search' => false, 'search' => false,
'flag' => 12, 'flag' => 12,
'eval' => array('tl_class' => 'w50'), '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 'addPopup' => array
( (
@@ -314,7 +320,7 @@ $GLOBALS['TL_DCA']['tl_leaflet_vector'] = array
\Netzmacht\Contao\Leaflet\Dca\Validator::callback('validateCoordinates') \Netzmacht\Contao\Leaflet\Dca\Validator::callback('validateCoordinates')
), ),
'wizard' => array( 'wizard' => array(
array('Netzmacht\Contao\Leaflet\Dca\LeafletCallbacks', 'getGeocoder') Netzmacht\Contao\Leaflet\Dca\LeafletCallbacks::callback('getGeocoder')
), ),
'eval' => array( 'eval' => array(
'maxlength' => 255, 'maxlength' => 255,

View File

@@ -35,6 +35,17 @@ class ContaoAssets implements Assets
*/ */
private $assetsManager; private $assetsManager;
/**
* Cached assets.
*
* @var array
*/
private $cache = [
'stylesheets' => [],
'javascripts' => [],
'map' => []
];
/** /**
* ContaoAssets constructor. * ContaoAssets constructor.
* *
@@ -52,6 +63,8 @@ class ContaoAssets implements Assets
*/ */
public function addJavascript($script, $type = self::TYPE_SOURCE) public function addJavascript($script, $type = self::TYPE_SOURCE)
{ {
$this->cache['javascripts'][] = [$script, $type];
switch ($type) { switch ($type) {
case static::TYPE_SOURCE: case static::TYPE_SOURCE:
$GLOBALS['TL_HEAD'][] = sprintf('<script>%s</script>', $script); $GLOBALS['TL_HEAD'][] = sprintf('<script>%s</script>', $script);
@@ -70,6 +83,8 @@ class ContaoAssets implements Assets
*/ */
public function addStylesheet($stylesheet, $type = self::TYPE_FILE) public function addStylesheet($stylesheet, $type = self::TYPE_FILE)
{ {
$this->cache['stylesheets'][] = [$stylesheet, $type];
switch ($type) { switch ($type) {
case static::TYPE_SOURCE: case static::TYPE_SOURCE:
$GLOBALS['TL_HEAD'][] = sprintf('<style>%s</style>', $stylesheet); $GLOBALS['TL_HEAD'][] = sprintf('<style>%s</style>', $stylesheet);
@@ -94,8 +109,39 @@ class ContaoAssets implements Assets
*/ */
public function setMap($map) public function setMap($map)
{ {
$this->map = $map; $this->cache['map'] = $map;
$this->map = $map;
return $this; 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'];
}
} }

View File

@@ -11,10 +11,8 @@
namespace Netzmacht\Contao\Leaflet\Dca; 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\LayerModel;
use Netzmacht\Contao\Leaflet\Model\MapModel; use Netzmacht\Contao\Toolkit\Dca\Callback\CallbackFactory;
use Netzmacht\LeafletPHP\Value\LatLng; use Netzmacht\LeafletPHP\Value\LatLng;
/** /**
@@ -24,6 +22,35 @@ use Netzmacht\LeafletPHP\Value\LatLng;
*/ */
class LeafletCallbacks 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. * Create the zoom range.
* *
@@ -75,4 +102,18 @@ class LeafletCallbacks
return $options; 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;
}
} }

View File

@@ -10,6 +10,7 @@
namespace Netzmacht\Contao\Leaflet\DependencyInjection; namespace Netzmacht\Contao\Leaflet\DependencyInjection;
use Doctrine\Common\Cache\Cache;
use Netzmacht\Contao\Leaflet\Boot; use Netzmacht\Contao\Leaflet\Boot;
use Netzmacht\Contao\Leaflet\Frontend\ValueFilter; use Netzmacht\Contao\Leaflet\Frontend\ValueFilter;
use Netzmacht\Contao\Leaflet\Mapper\DefinitionMapper; use Netzmacht\Contao\Leaflet\Mapper\DefinitionMapper;
@@ -40,6 +41,13 @@ class LeafletServices
*/ */
const BOOT = 'leaflet.boot'; const BOOT = 'leaflet.boot';
/**
* Leaflet cache
*
* @return Cache
*/
const CACHE = 'leaflet.cache';
/** /**
* Service name of the definition builder. * Service name of the definition builder.
* *

View File

@@ -11,6 +11,7 @@
namespace Netzmacht\Contao\Leaflet; namespace Netzmacht\Contao\Leaflet;
use Doctrine\Common\Cache\Cache;
use Netzmacht\Contao\Leaflet\Event\GetJavascriptEvent; use Netzmacht\Contao\Leaflet\Event\GetJavascriptEvent;
use Netzmacht\Contao\Leaflet\Filter\Filter; use Netzmacht\Contao\Leaflet\Filter\Filter;
use Netzmacht\Contao\Leaflet\Frontend\DataController; 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\Mapper\DefinitionMapper;
use Netzmacht\Contao\Leaflet\Model\LayerModel; use Netzmacht\Contao\Leaflet\Model\LayerModel;
use Netzmacht\Contao\Leaflet\Model\MapModel; use Netzmacht\Contao\Leaflet\Model\MapModel;
use Netzmacht\LeafletPHP\Assets;
use Netzmacht\LeafletPHP\Value\GeoJson\FeatureCollection; use Netzmacht\LeafletPHP\Value\GeoJson\FeatureCollection;
use Netzmacht\LeafletPHP\Definition\Map; use Netzmacht\LeafletPHP\Definition\Map;
use Netzmacht\LeafletPHP\Leaflet; use Netzmacht\LeafletPHP\Leaflet;
@@ -62,7 +62,7 @@ class MapProvider
/** /**
* Map assets collector. * Map assets collector.
* *
* @var Assets * @var ContaoAssets
*/ */
private $assets; private $assets;
@@ -80,6 +80,13 @@ class MapProvider
*/ */
private $displayErrors; private $displayErrors;
/**
* Cache.
*
* @var Cache
*/
private $cache;
/** /**
* Construct. * Construct.
* *
@@ -87,7 +94,8 @@ class MapProvider
* @param Leaflet $leaflet The Leaflet instance. * @param Leaflet $leaflet The Leaflet instance.
* @param EventDispatcher $eventDispatcher The Contao event dispatcher. * @param EventDispatcher $eventDispatcher The Contao event dispatcher.
* @param \Input $input Thw request input. * @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 array $filters Request filters configuration.
* @param bool $displayErrors Display errors setting. * @param bool $displayErrors Display errors setting.
*/ */
@@ -96,7 +104,8 @@ class MapProvider
Leaflet $leaflet, Leaflet $leaflet,
EventDispatcher $eventDispatcher, EventDispatcher $eventDispatcher,
\Input $input, \Input $input,
Assets $assets, ContaoAssets $assets,
Cache $cache,
array $filters, array $filters,
$displayErrors $displayErrors
) { ) {
@@ -107,6 +116,7 @@ class MapProvider
$this->assets = $assets; $this->assets = $assets;
$this->filters = $filters; $this->filters = $filters;
$this->displayErrors = $displayErrors; $this->displayErrors = $displayErrors;
$this->cache = $cache;
} }
/** /**
@@ -165,9 +175,6 @@ class MapProvider
* *
* @return string * @return string
* @throws \Exception If generating went wrong. * @throws \Exception If generating went wrong.
*
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
* @SuppressWarnings(PHPMD.UnusedLocalVariable)
*/ */
public function generate( public function generate(
$mapId, $mapId,
@@ -176,23 +183,38 @@ class MapProvider
$template = 'leaflet_map_js', $template = 'leaflet_map_js',
$style = '' $style = ''
) { ) {
$definition = $this->getDefinition($mapId, $filter, $elementId); if ($mapId instanceof MapModel) {
$template = \Controller::getTemplate($template); $model = $mapId;
$mapId = $mapId->id;
} else {
$model = $this->getModel($mapId);
}
// @codingStandardsIgnoreStart - Set for the template. if ($model->cache) {
$javascript = $this->leaflet->build($definition, $this->assets); $cacheKey = $this->getCacheKey($mapId, $filter, $elementId, $template, $style);
$mapId = $definition->getId();
// @codingStandardsIgnoreEnd
ob_start(); if ($this->cache->contains($cacheKey)) {
include $template; $cached = $this->cache->fetch($cacheKey);
$content = ob_get_contents(); $this->assets->fromArray($cached['assets']);
ob_end_clean();
$event = new GetJavascriptEvent($definition, $content); return $cached['javascript'];
$this->eventDispatcher->dispatch($event::NAME, $event); }
}
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)); 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. * @throws \RuntimeException IF the input data does not match.
* *
* @SuppressWarnings(PHPMD.ExitExpression) * @SuppressWarnings(PHPMD.ExitExpression)
* @SuppressWarnings(PHPMD.Superglobals)
*/ */
public function handleAjaxRequest($identifier, $exit = true) 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;
}
} }

View File

@@ -11,6 +11,7 @@
namespace Netzmacht\Contao\Leaflet\Mapper\Layer; namespace Netzmacht\Contao\Leaflet\Mapper\Layer;
use Netzmacht\Contao\Leaflet\ContaoAssets;
use Netzmacht\Contao\Leaflet\Filter\Filter; use Netzmacht\Contao\Leaflet\Filter\Filter;
use Netzmacht\Contao\Leaflet\Mapper\DefinitionMapper; use Netzmacht\Contao\Leaflet\Mapper\DefinitionMapper;
use Netzmacht\Contao\Leaflet\Model\LayerModel; use Netzmacht\Contao\Leaflet\Model\LayerModel;
@@ -46,20 +47,20 @@ class MarkerClusterLayerMapper extends AbstractLayerMapper
/** /**
* Assets manager. * Assets manager.
* *
* @var AssetsManager * @var ContaoAssets
*/ */
private $assetsManager; private $assets;
/** /**
* MarkerClusterLayerMapper constructor. * MarkerClusterLayerMapper constructor.
* *
* @param AssetsManager $assetsManager Assets manager. * @param ContaoAssets $assets Assets manager.
*/ */
public function __construct(AssetsManager $assetsManager) public function __construct(ContaoAssets $assets)
{ {
parent::__construct(); parent::__construct();
$this->assetsManager = $assetsManager; $this->assets = $assets;
} }
/** /**
@@ -102,7 +103,7 @@ class MarkerClusterLayerMapper extends AbstractLayerMapper
} }
if (!$model->disableDefaultStyle) { 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( $collection = LayerModel::findBy(

View File

@@ -12,6 +12,7 @@
namespace Netzmacht\Contao\Leaflet\Subscriber; namespace Netzmacht\Contao\Leaflet\Subscriber;
use ContaoCommunityAlliance\Contao\EventDispatcher\EventDispatcherInitializer; use ContaoCommunityAlliance\Contao\EventDispatcher\EventDispatcherInitializer;
use Netzmacht\Contao\Leaflet\ContaoAssets;
use Netzmacht\Contao\Leaflet\DependencyInjection\LeafletServices; use Netzmacht\Contao\Leaflet\DependencyInjection\LeafletServices;
use Netzmacht\Contao\Leaflet\Event\GetJavascriptEvent; use Netzmacht\Contao\Leaflet\Event\GetJavascriptEvent;
use Netzmacht\Contao\Leaflet\Event\InitializeDefinitionMapperEvent; use Netzmacht\Contao\Leaflet\Event\InitializeDefinitionMapperEvent;
@@ -59,9 +60,9 @@ class BootSubscriber implements EventSubscriberInterface
/** /**
* Assets manager. * Assets manager.
* *
* @var AssetsManager * @var ContaoAssets
*/ */
private $assetsManager; private $assets;
/** /**
* Definition mapper. * Definition mapper.
@@ -73,21 +74,21 @@ class BootSubscriber implements EventSubscriberInterface
/** /**
* BootSubscriber constructor. * BootSubscriber constructor.
* *
* @param AssetsManager $assetsManager Assets manager. * @param ContaoAssets $assets Leaflet assets manager.
* @param array $mappers Leaflet mapper configuration. * @param array $mappers Leaflet mapper configuration.
* @param array $encoders Leaflet encoder configuration. * @param array $encoders Leaflet encoder configuration.
* @param array $libraries Leaflet libraries configuration. * @param array $libraries Leaflet libraries configuration.
*/ */
public function __construct( public function __construct(
AssetsManager $assetsManager, ContaoAssets $assets,
array $mappers, array $mappers,
array $encoders, array $encoders,
array $libraries array $libraries
) { ) {
$this->assetsManager = $assetsManager; $this->assets = $assets;
$this->mappers = $mappers; $this->mappers = $mappers;
$this->encoders = $encoders; $this->encoders = $encoders;
$this->libraries = $libraries; $this->libraries = $libraries;
} }
/** /**
@@ -188,7 +189,7 @@ class BootSubscriber implements EventSubscriberInterface
*/ */
public function loadAssets() 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 // @codingStandardsIgnoreStart
// TODO: Cache it. // TODO: Cache it.
// codingStandardsIgnoreEnd // codingStandardsIgnoreEnd
$this->assetsManager->addJavascript('assets/leaflet/js/icons.js'); $this->assets->addJavascript('assets/leaflet/js/icons.js', ContaoAssets::TYPE_FILE);
} }
} }