Make the data controller a service.

This commit is contained in:
David Molineus
2017-10-11 15:42:49 +02:00
parent 6bfaf63d4f
commit 56a68175fe
3 changed files with 36 additions and 49 deletions

View File

@@ -14,8 +14,7 @@ services:
- '@netzmacht.contao_toolkit.contao.input_adapter' - '@netzmacht.contao_toolkit.contao.input_adapter'
- '@netzmacht.contao_leaflet.map.assets' - '@netzmacht.contao_leaflet.map.assets'
- '@netzmacht.contao_leaflet.cache' - '@netzmacht.contao_leaflet.cache'
- '@netzmacht.contao_leaflet.filter_factory' - '@netzmacht.contao_leaflet.frontend.data_controller'
- '%kernel.debug%'
netzmacht.contao_leaflet.libraries: netzmacht.contao_leaflet.libraries:
class: Netzmacht\Contao\Leaflet\Frontend\Assets\LibrariesConfiguration class: Netzmacht\Contao\Leaflet\Frontend\Assets\LibrariesConfiguration
@@ -105,3 +104,9 @@ services:
- '%kernel.debug%' - '%kernel.debug%'
tags: tags:
- { name: 'contao.hook', hook: 'replaceInsertTags', method: 'replace' } - { name: 'contao.hook', hook: 'replaceInsertTags', method: 'replace' }
netzmacht.contao_leaflet.frontend.data_controller:
class: Netzmacht\Contao\Leaflet\Frontend\DataController
arguments:
- '@netzmacht.contao_leaflet.filter_factory'
- '%kernel.debug%'

View File

@@ -23,13 +23,6 @@ use Netzmacht\Contao\Leaflet\MapProvider;
*/ */
class DataController class DataController
{ {
/**
* The map provider.
*
* @var MapProvider
*/
private $mapProvider;
/** /**
* The user input data. * The user input data.
* *
@@ -44,11 +37,11 @@ class DataController
); );
/** /**
* Display errors. * Debug mode.
* *
* @var bool * @var bool
*/ */
private $displayErrors; private $debugMode;
/** /**
* Filter factory. * Filter factory.
@@ -60,14 +53,12 @@ class DataController
/** /**
* Construct. * Construct.
* *
* @param MapProvider $mapProvider The map provider.
* @param FilterFactory $filterFactory Filter factory. * @param FilterFactory $filterFactory Filter factory.
* @param bool $displayErrors Display errors. * @param bool $debugMode Debug mode.
*/ */
public function __construct(MapProvider $mapProvider, FilterFactory $filterFactory, $displayErrors) public function __construct(FilterFactory $filterFactory, $debugMode)
{ {
$this->mapProvider = $mapProvider; $this->debugMode = $debugMode;
$this->displayErrors = $displayErrors;
$this->filterFactory = $filterFactory; $this->filterFactory = $filterFactory;
} }
@@ -75,12 +66,12 @@ class DataController
* Execute the controller and create the data response. * Execute the controller and create the data response.
* *
* @param array $input The user input as array. * @param array $input The user input as array.
* @param MapProvider $mapProvider Map provider.
* *
* @return void * @return void
*
* @throws \Exception If anything went wrong. * @throws \Exception If anything went wrong.
*/ */
public function execute(array $input) public function execute(array $input, MapProvider $mapProvider)
{ {
$input = array_merge($this->input, $input); $input = array_merge($this->input, $input);
@@ -91,10 +82,10 @@ class DataController
$filter = null; $filter = null;
} }
list($data, $error) = $this->loadData($input['type'], $input['id'], $filter); list($data, $error) = $this->loadData($mapProvider, $input['type'], $input['id'], $filter);
$this->encodeData($input['format'], $data); $this->encodeData($input['format'], $data);
} catch (\Exception $e) { } catch (\Exception $e) {
if ($this->displayErrors) { if ($this->debugMode) {
throw $e; throw $e;
} }
$error = true; $error = true;
@@ -129,20 +120,21 @@ class DataController
/** /**
* Load the data. * Load the data.
* *
* @param MapProvider $mapProvider Map provider.
* @param string $type The data type. * @param string $type The data type.
* @param mixed $dataId The data id. * @param mixed $dataId The data id.
* @param Filter $filter Optional request filter. * @param Filter $filter Optional request filter.
* *
* @return array * @return array
*/ */
public function loadData($type, $dataId, Filter $filter = null) public function loadData(MapProvider $mapProvider, $type, $dataId, Filter $filter = null)
{ {
$error = false; $error = false;
$data = null; $data = null;
switch ($type) { switch ($type) {
case 'layer': case 'layer':
$data = $this->mapProvider->getFeatureCollection($dataId, $filter); $data = $mapProvider->getFeatureCollection($dataId, $filter);
break; break;
default: default:

View File

@@ -17,7 +17,6 @@ use Doctrine\Common\Cache\Cache;
use Netzmacht\Contao\Leaflet\Encoder\ContaoAssets; use Netzmacht\Contao\Leaflet\Encoder\ContaoAssets;
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\Filter\FilterFactory;
use Netzmacht\Contao\Leaflet\Frontend\DataController; use Netzmacht\Contao\Leaflet\Frontend\DataController;
use Netzmacht\Contao\Leaflet\Mapper\DefinitionMapper; use Netzmacht\Contao\Leaflet\Mapper\DefinitionMapper;
use Netzmacht\Contao\Leaflet\Model\LayerModel; use Netzmacht\Contao\Leaflet\Model\LayerModel;
@@ -70,20 +69,6 @@ class MapProvider
*/ */
private $assets; private $assets;
/**
* Filter factory.
*
* @var FilterFactory
*/
private $filterFactory;
/**
* Display errors setting.
*
* @var bool
*/
private $displayErrors;
/** /**
* Cache. * Cache.
* *
@@ -91,6 +76,13 @@ class MapProvider
*/ */
private $cache; private $cache;
/**
* Data controller.
*
* @var DataController
*/
private $dataController;
/** /**
* Construct. * Construct.
* *
@@ -100,8 +92,9 @@ class MapProvider
* @param Input $input Thw request input. * @param Input $input Thw request input.
* @param ContaoAssets $assets Assets handler. * @param ContaoAssets $assets Assets handler.
* @param Cache $cache Cache. * @param Cache $cache Cache.
* @param FilterFactory $filterFactory Filter factory. * @param DataController $dataController Data controller.
* @param bool $displayErrors Display errors setting. *
* @internal param FilterFactory $filterFactory Filter factory.
*/ */
public function __construct( public function __construct(
DefinitionMapper $mapper, DefinitionMapper $mapper,
@@ -110,17 +103,15 @@ class MapProvider
$input, $input,
ContaoAssets $assets, ContaoAssets $assets,
Cache $cache, Cache $cache,
FilterFactory $filterFactory, DataController $dataController
$displayErrors
) { ) {
$this->mapper = $mapper; $this->mapper = $mapper;
$this->leaflet = $leaflet; $this->leaflet = $leaflet;
$this->eventDispatcher = $eventDispatcher; $this->eventDispatcher = $eventDispatcher;
$this->input = $input; $this->input = $input;
$this->assets = $assets; $this->assets = $assets;
$this->filterFactory = $filterFactory;
$this->displayErrors = $displayErrors;
$this->cache = $cache; $this->cache = $cache;
$this->dataController = $dataController;
} }
/** /**
@@ -295,8 +286,7 @@ class MapProvider
return; return;
} }
$controller = new DataController($this, $this->filterFactory, $this->displayErrors); $this->dataController->execute($data, $this);
$controller->execute($data);
if ($exit) { if ($exit) {
exit; exit;