Refactor GeoJson subscriber.

This commit is contained in:
David Molineus
2016-10-06 08:58:35 +02:00
parent b1cfbb3b04
commit a375a73668
3 changed files with 27 additions and 4 deletions

View File

@@ -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'],
);

View File

@@ -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']
);
});

View File

@@ -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) {