From a375a73668736e1b889a6ee460e137f69834a909 Mon Sep 17 00:00:00 2001 From: David Molineus Date: Thu, 6 Oct 2016 08:58:35 +0200 Subject: [PATCH] Refactor GeoJson subscriber. --- module/config/event_subscribers.php | 2 +- module/config/services.php | 7 ++++++ .../Leaflet/Subscriber/GeoJsonSubscriber.php | 22 ++++++++++++++++--- 3 files changed, 27 insertions(+), 4 deletions(-) diff --git a/module/config/event_subscribers.php b/module/config/event_subscribers.php index 5a9546c..346868c 100644 --- a/module/config/event_subscribers.php +++ b/module/config/event_subscribers.php @@ -11,5 +11,5 @@ return array( $GLOBALS['container']['leaflet.boot.subscriber'], 'Netzmacht\Contao\Leaflet\Subscriber\HashSubscriber', - 'Netzmacht\Contao\Leaflet\Subscriber\GeoJsonSubscriber', + $GLOBALS['container']['leaflet.subscriber.geo-json'], ); diff --git a/module/config/services.php b/module/config/services.php index cc0dc93..74a443e 100644 --- a/module/config/services.php +++ b/module/config/services.php @@ -24,6 +24,7 @@ use Netzmacht\Contao\Leaflet\Frontend\ValueFilter; use Netzmacht\Contao\Leaflet\Mapper\DefinitionMapper; use Netzmacht\Contao\Leaflet\MapProvider; use Netzmacht\Contao\Leaflet\Subscriber\BootSubscriber; +use Netzmacht\Contao\Leaflet\Subscriber\GeoJsonSubscriber; use Netzmacht\Contao\Toolkit\Data\Alias\Filter\ExistingAliasFilter; use Netzmacht\Contao\Toolkit\Data\Alias\Filter\SlugifyFilter; use Netzmacht\Contao\Toolkit\Data\Alias\Filter\SuffixFilter; @@ -264,3 +265,9 @@ $container[Services::MODULES_MAP]['leaflet'] = function ($model, $column, Contai $column ); }; + +$container['leaflet.subscriber.geo-json'] = $container->share(function () { + return new GeoJsonSubscriber( + $GLOBALS['LEAFLET_FEATURE_MODEL_PROPERTIES'] + ); +}); diff --git a/src/Netzmacht/Contao/Leaflet/Subscriber/GeoJsonSubscriber.php b/src/Netzmacht/Contao/Leaflet/Subscriber/GeoJsonSubscriber.php index c16d0dd..5c61296 100644 --- a/src/Netzmacht/Contao/Leaflet/Subscriber/GeoJsonSubscriber.php +++ b/src/Netzmacht/Contao/Leaflet/Subscriber/GeoJsonSubscriber.php @@ -28,6 +28,23 @@ use Symfony\Component\EventDispatcher\EventSubscriberInterface; */ class GeoJsonSubscriber implements EventSubscriberInterface { + /** + * Property mapping between models and features. + * + * @var array + */ + private $featureModelProperties; + + /** + * GeoJsonSubscriber constructor. + * + * @param array $featureModelProperties Property mapping between models and features. + */ + public function __construct(array $featureModelProperties) + { + $this->featureModelProperties = $featureModelProperties; + } + /** * {@inheritdoc} */ @@ -113,7 +130,6 @@ class GeoJsonSubscriber implements EventSubscriberInterface * @param ConvertToGeoJsonEvent $event The subscribed events. * * @return void - * @SuppressWarnings(PHPMD.Superglobals) */ public function setModelData(ConvertToGeoJsonEvent $event) { @@ -121,11 +137,11 @@ class GeoJsonSubscriber implements EventSubscriberInterface $model = $event->getModel(); if (!$model instanceof \Model || !$feature instanceof Feature - || empty($GLOBALS['LEAFLET_FEATURE_MODEL_PROPERTIES'][$model->getTable()])) { + || empty($this->featureModelProperties[$model->getTable()])) { return; } - $mapping = $GLOBALS['LEAFLET_FEATURE_MODEL_PROPERTIES'][$model->getTable()]; + $mapping = $this->featureModelProperties[$model->getTable()]; $data = (array) $feature->getProperty('model'); foreach ((array) $mapping as $property) {