mirror of
https://github.com/netzmacht/contao-leaflet-maps.git
synced 2025-11-29 11:33:46 +01:00
Refactor data controller to fully use dependency injection
This commit is contained in:
@@ -41,38 +41,55 @@ class DataController
|
|||||||
'values' => null
|
'values' => null
|
||||||
);
|
);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Filters configuration.
|
||||||
|
*
|
||||||
|
* @var array
|
||||||
|
*/
|
||||||
|
private $filters;
|
||||||
|
/**
|
||||||
|
* @var bool
|
||||||
|
*/
|
||||||
|
private $displayErrors;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Construct.
|
* Construct.
|
||||||
*
|
*
|
||||||
* @param MapProvider $mapProvider The map provider.
|
* @param MapProvider $mapProvider The map provider.
|
||||||
* @param array $input The user input as array.
|
* @param array $filters Filters configuration.
|
||||||
|
* @param bool $displayErrors Display errors.
|
||||||
*/
|
*/
|
||||||
public function __construct(MapProvider $mapProvider, $input)
|
public function __construct(MapProvider $mapProvider, array $filters, $displayErrors)
|
||||||
{
|
{
|
||||||
$this->mapProvider = $mapProvider;
|
$this->mapProvider = $mapProvider;
|
||||||
$this->input = array_merge($this->input, $input);
|
$this->filters = $filters;
|
||||||
|
$this->displayErrors = $displayErrors;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Execute the controller and create the data response.
|
* Execute the controller and create the data response.
|
||||||
*
|
*
|
||||||
|
* @param array $input The user input as array.
|
||||||
|
*
|
||||||
* @return void
|
* @return void
|
||||||
*
|
*
|
||||||
* @throws \Exception If anything went wrong.
|
* @throws \Exception If anything went wrong.
|
||||||
*/
|
*/
|
||||||
public function execute()
|
public function execute(array $input)
|
||||||
{
|
{
|
||||||
|
$input = array_merge($this->input, $input);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
if ($this->input['filter']) {
|
if ($input['filter']) {
|
||||||
$filter = $this->createFilter();
|
$filter = $this->createFilter($input);
|
||||||
} else {
|
} else {
|
||||||
$filter = null;
|
$filter = null;
|
||||||
}
|
}
|
||||||
|
|
||||||
list($data, $error) = $this->loadData($this->input['type'], $this->input['id'], $filter);
|
list($data, $error) = $this->loadData($input['type'], $input['id'], $filter);
|
||||||
$this->encodeData($this->input['format'], $data);
|
$this->encodeData($input['format'], $data);
|
||||||
} catch (\Exception $e) {
|
} catch (\Exception $e) {
|
||||||
if (\Config::get('debugMode') || \Config::get('displayErrors')) {
|
if ($this->displayErrors) {
|
||||||
throw $e;
|
throw $e;
|
||||||
}
|
}
|
||||||
$error = true;
|
$error = true;
|
||||||
@@ -135,20 +152,22 @@ class DataController
|
|||||||
/**
|
/**
|
||||||
* Create a filter.
|
* Create a filter.
|
||||||
*
|
*
|
||||||
|
* @param array $input The user input as array.
|
||||||
|
*
|
||||||
* @return Filter
|
* @return Filter
|
||||||
* @throws \RuntimeException If the filter is not defined.
|
* @throws \RuntimeException If the filter is not defined.
|
||||||
*
|
*
|
||||||
* @SuppressWarnings(PHPMD.Superglobals)
|
* @SuppressWarnings(PHPMD.Superglobals)
|
||||||
*/
|
*/
|
||||||
private function createFilter()
|
private function createFilter($input)
|
||||||
{
|
{
|
||||||
if (!isset($GLOBALS['LEAFLET_FILTERS'][$this->input['filter']])) {
|
if (!isset($this->filters[$input['filter']])) {
|
||||||
throw new \RuntimeException(sprintf('Undefined filter "%s".', $this->input['filter']));
|
throw new \RuntimeException(sprintf('Undefined filter "%s".', $input['filter']));
|
||||||
}
|
}
|
||||||
|
|
||||||
/** @var Filter $filter */
|
/** @var Filter $filter */
|
||||||
$filter = $GLOBALS['LEAFLET_FILTERS'][$this->input['filter']];
|
$filter = $this->filters[$input['filter']];
|
||||||
|
|
||||||
return $filter::fromRequest($this->input['values']);
|
return $filter::fromRequest($input['values']);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -232,8 +232,13 @@ class MapProvider
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
$controller = new DataController($this, $data);
|
$controller = new DataController(
|
||||||
$controller->execute();
|
$this,
|
||||||
|
$GLOBALS['LEAFLET_FILTERS'],
|
||||||
|
\Config::get('debugMode') || \Config::get('displayErrors')
|
||||||
|
);
|
||||||
|
|
||||||
|
$controller->execute($data);
|
||||||
|
|
||||||
if ($exit) {
|
if ($exit) {
|
||||||
exit;
|
exit;
|
||||||
|
|||||||
Reference in New Issue
Block a user