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_leaflet.map.assets'
- '@netzmacht.contao_leaflet.cache'
- '@netzmacht.contao_leaflet.filter_factory'
- '%kernel.debug%'
- '@netzmacht.contao_leaflet.frontend.data_controller'
netzmacht.contao_leaflet.libraries:
class: Netzmacht\Contao\Leaflet\Frontend\Assets\LibrariesConfiguration
@@ -105,3 +104,9 @@ services:
- '%kernel.debug%'
tags:
- { 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
{
/**
* The map provider.
*
* @var MapProvider
*/
private $mapProvider;
/**
* The user input data.
*
@@ -44,11 +37,11 @@ class DataController
);
/**
* Display errors.
* Debug mode.
*
* @var bool
*/
private $displayErrors;
private $debugMode;
/**
* Filter factory.
@@ -60,27 +53,25 @@ class DataController
/**
* Construct.
*
* @param MapProvider $mapProvider The map provider.
* @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->displayErrors = $displayErrors;
$this->debugMode = $debugMode;
$this->filterFactory = $filterFactory;
}
/**
* 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
*
* @throws \Exception If anything went wrong.
*/
public function execute(array $input)
public function execute(array $input, MapProvider $mapProvider)
{
$input = array_merge($this->input, $input);
@@ -91,10 +82,10 @@ class DataController
$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);
} catch (\Exception $e) {
if ($this->displayErrors) {
if ($this->debugMode) {
throw $e;
}
$error = true;
@@ -129,20 +120,21 @@ class DataController
/**
* Load the data.
*
* @param string $type The data type.
* @param mixed $dataId The data id.
* @param Filter $filter Optional request filter.
* @param MapProvider $mapProvider Map provider.
* @param string $type The data type.
* @param mixed $dataId The data id.
* @param Filter $filter Optional request filter.
*
* @return array
*/
public function loadData($type, $dataId, Filter $filter = null)
public function loadData(MapProvider $mapProvider, $type, $dataId, Filter $filter = null)
{
$error = false;
$data = null;
switch ($type) {
case 'layer':
$data = $this->mapProvider->getFeatureCollection($dataId, $filter);
$data = $mapProvider->getFeatureCollection($dataId, $filter);
break;
default:

View File

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