Switch to PSR-4.

This commit is contained in:
David Molineus
2017-10-05 14:16:56 +02:00
parent e3344ffd4f
commit 827c746b0d
87 changed files with 4 additions and 6 deletions

View File

@@ -0,0 +1,150 @@
<?php
/**
* @package contao-leaflet-maps
* @author David Molineus <david.molineus@netzmacht.de>
* @copyright 2014-2016 netzmacht David Molineus
* @license LGPL 3.0
* @filesource
*
*/
namespace Netzmacht\Contao\Leaflet\Frontend;
use ContaoCommunityAlliance\Translator\TranslatorInterface as Translator;
use Database\Result;
use Model\Collection;
use Netzmacht\Contao\Leaflet\MapProvider;
use Netzmacht\Contao\Leaflet\Model\MapModel;
use Netzmacht\Contao\Toolkit\Component\Hybrid\AbstractHybrid;
use Netzmacht\Contao\Toolkit\View\Template\TemplateFactory;
/**
* Class HybridTrait provides method required by the frontend module and content element the same time.
*
* @package Netzmacht\Contao\Leaflet\Frontend
*/
abstract class AbstractMapHybrid extends AbstractHybrid
{
/**
* The map provider.
*
* @var MapProvider
*/
private $mapProvider;
/**
* The user input.
*
* @var \Input
*/
private $input;
/**
* The Contao config.
*
* @var \Config
*/
private $config;
/**
* HybridTrait constructor.
*
* @param Result|\Model|Collection $model Component model.
* @param TemplateFactory $templateFactory Template factory.
* @param Translator $translator Translator.
* @param MapProvider $mapProvider Map provider.
* @param \Input $input Request Input.
* @param \Config $config Config.
* @param string $column Column in which the element appears.
*/
public function __construct(
$model,
TemplateFactory $templateFactory,
Translator $translator,
MapProvider $mapProvider,
\Input $input,
\Config $config,
$column = null
) {
parent::__construct($model, $templateFactory, $translator, $column);
$this->mapProvider = $mapProvider;
$this->input = $input;
$this->config = $config;
}
/**
* Do the frontend integration generation.
*
* @return string
*/
public function generate()
{
$this->mapProvider->handleAjaxRequest($this->getIdentifier());
if (TL_MODE === 'BE') {
$model = MapModel::findByPk($this->get('leaflet_map'));
$template = $this->getTemplateFactory()->createBackendTemplate('be_wildcard');
if ($model) {
$href = 'contao/main.php?do=leaflet&amp;table=tl_leaflet_map&amp;act=edit&amp;id=' . $model->id;
$template->set('wildcard', '### LEAFLET MAP ' . $model->title . ' ###');
$template->set('title', $this->get('headline'));
$template->set('id', $model->id);
$template->set('link', $model->title);
$template->set('href', $href);
}
return $template->parse();
}
return parent::generate();
}
/**
* Do the frontend integration compiling.
*
* @return void
*
* @throws \Exception If the map could not be created.
*
* @SuppressWarnings(PHPMD.Superglobals)
*/
protected function compile()
{
try {
$template = $this->get('leaflet_template') ?: 'leaflet_map_js';
$mapId = $this->getIdentifier();
$map = $this->mapProvider->generate($this->get('leaflet_map'), null, $mapId, $template);
$this->template->set('javascript', $map);
$this->template->set('mapId', $mapId);
$style = '';
$height = deserialize($this->get('leaflet_height'), true);
$width = deserialize($this->get('leaflet_width'), true);
if (!empty($width['value'])) {
$style .= 'width:' . $width['value'] . $width['unit'] . ';';
}
if (!empty($height['value'])) {
$style .= 'height:' . $height['value'] . $height['unit'] . ';';
}
$this->template->set('mapStyle', $style);
} catch (\Exception $e) {
throw $e;
}
}
/**
* Get the component identifier which is used as unique name.
*
* @return string
*/
abstract protected function getIdentifier();
}

View File

@@ -0,0 +1,176 @@
<?php
/**
* @package contao-leaflet-maps
* @author David Molineus <david.molineus@netzmacht.de>
* @copyright 2014-2016 netzmacht David Molineus
* @license LGPL 3.0
* @filesource
*
*/
namespace Netzmacht\Contao\Leaflet\Frontend;
use Netzmacht\Contao\Leaflet\Filter\Filter;
use Netzmacht\Contao\Leaflet\MapProvider;
/**
* The data controller handles ajax request for sub data.
*
* @package Netzmacht\Contao\Leaflet\Frontend
*/
class DataController
{
/**
* The map provider.
*
* @var MapProvider
*/
private $mapProvider;
/**
* The user input data.
*
* @var array
*/
private $input = array(
'format' => 'geojson',
'type' => 'layer',
'id' => null,
'filter' => null,
'values' => null
);
/**
* Filters configuration.
*
* @var array
*/
private $filters;
/**
* Display errors.
*
* @var bool
*/
private $displayErrors;
/**
* Construct.
*
* @param MapProvider $mapProvider The map provider.
* @param array $filters Filters configuration.
* @param bool $displayErrors Display errors.
*/
public function __construct(MapProvider $mapProvider, array $filters, $displayErrors)
{
$this->mapProvider = $mapProvider;
$this->filters = $filters;
$this->displayErrors = $displayErrors;
}
/**
* Execute the controller and create the data response.
*
* @param array $input The user input as array.
*
* @return void
*
* @throws \Exception If anything went wrong.
*/
public function execute(array $input)
{
$input = array_merge($this->input, $input);
try {
if ($input['filter']) {
$filter = $this->createFilter($input);
} else {
$filter = null;
}
list($data, $error) = $this->loadData($input['type'], $input['id'], $filter);
$this->encodeData($input['format'], $data);
} catch (\Exception $e) {
if ($this->displayErrors) {
throw $e;
}
$error = true;
}
if ($error) {
header('HTTP/1.1 500 Internal Server Error');
}
}
/**
* Encode the data.
*
* @param string $format The requested format.
* @param mixed $data The given data.
*
* @return void
*/
public function encodeData($format, $data)
{
switch ($format) {
case 'geojson':
header('Content-Type: application/json');
echo json_encode($data, JSON_UNESCAPED_SLASHES);
break;
default:
// Blame the code sniffer.
}
}
/**
* Load the data.
*
* @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)
{
$error = false;
$data = null;
switch ($type) {
case 'layer':
$data = $this->mapProvider->getFeatureCollection($dataId, $filter);
break;
default:
$error = true;
return array($data, $error);
}
return array($data, $error);
}
/**
* Create a filter.
*
* @param array $input The user input as array.
*
* @return Filter
* @throws \RuntimeException If the filter is not defined.
*
* @SuppressWarnings(PHPMD.Superglobals)
*/
private function createFilter($input)
{
if (!isset($this->filters[$input['filter']])) {
throw new \RuntimeException(sprintf('Undefined filter "%s".', $input['filter']));
}
/** @var Filter $filter */
$filter = $this->filters[$input['filter']];
return $filter::fromRequest($input['values']);
}
}

View File

@@ -0,0 +1,113 @@
<?php
/**
* @package contao-leaflet-maps
* @author David Molineus <david.molineus@netzmacht.de>
* @copyright 2014-2016 netzmacht David Molineus
* @license LGPL 3.0
* @filesource
*
*/
namespace Netzmacht\Contao\Leaflet\Frontend\InsertTag;
use Netzmacht\Contao\Leaflet\MapProvider;
use Netzmacht\Contao\Toolkit\InsertTag\Parser;
/**
* LeafletInsertTagParser parses the leaflet insert tag.
*
* By default it creates the html template, so the script and html are rendered.
*
* Supported formats are:
* - {{leaflet::id|alias}} The map id or alias.
* - {{leaflet::id::style}} The style attribute, useful to pass the height and width of the container.
* - {{leaflet::id::style::template}} Optional template. Look at leaflet_map_js and leaflet_map_html as example.
*
* @package Netzmacht\Contao\Leaflet\Frontend\InsertTag
*/
class LeafletInsertTagParser implements Parser
{
/**
* The map service.
*
* @var MapProvider
*/
private $mapProvider;
/**
* Debug mode.
*
* @var bool
*/
private $debugMode;
/**
* LeafletInsertTagParser constructor.
*
* @param MapProvider $mapProvider Map provider.
* @param bool $debugMode Debug mode.
*/
public function __construct(MapProvider $mapProvider, $debugMode)
{
$this->mapProvider = $mapProvider;
$this->debugMode = $debugMode;
}
/**
* {@inheritDoc}
*/
public static function getTags()
{
return ['leaflet'];
}
/**
* {@inheritDoc}
*/
public function supports($tag)
{
return in_array($tag, static::getTags());
}
/**
* {@inheritDoc}
*/
public function parse($raw, $tag, $params = null, $cache = true)
{
$parts = explode('::', $params);
if (empty($parts[0])) {
return false;
}
$style = empty($parts[1]) ? 'width:400px;height:300px' : $parts[1];
$template = empty($parts[2]) ? 'leaflet_map_html' : $parts[2];
return $this->generateMap($parts[0], $template, $style);
}
/**
* Generate the map.
*
* @param string|int $mapId The map id/alias.
* @param string $template The template.
* @param string $style Optional style attribute.
*
* @return bool|string
*
* @throws \Exception If debug mode is enabled and something went wrong.
*/
private function generateMap($mapId, $template, $style)
{
try {
return $this->mapProvider->generate($mapId, null, $mapId, $template, $style);
} catch (\Exception $e) {
if ($this->debugMode) {
throw $e;
}
}
return false;
}
}

View File

@@ -0,0 +1,45 @@
<?php
/**
* @package contao-leaflet-maps
* @author David Molineus <david.molineus@netzmacht.de>
* @copyright 2014-2016 netzmacht David Molineus
* @license LGPL 3.0
* @filesource
*
*/
namespace Netzmacht\Contao\Leaflet\Frontend;
/**
* The content element for the leaflet map.
*
* @property int leaflet_map
*/
class MapElement extends AbstractMapHybrid
{
/**
* Template name.
*
* @var string
*/
protected $templateName = 'ce_leaflet_map';
/**
* Get the identifier.
*
* @return string
*/
protected function getIdentifier()
{
if ($this->get('leaflet_mapId')) {
return $this->get('leaflet_mapId');
}
if ($this->get('cssID')[0]) {
return 'map_' . $this->get('cssID')[0];
}
return 'map_ce_' . $this->get('id');
}
}

View File

@@ -0,0 +1,45 @@
<?php
/**
* @package contao-leaflet-maps
* @author David Molineus <david.molineus@netzmacht.de>
* @copyright 2014-2016 netzmacht David Molineus
* @license LGPL 3.0
* @filesource
*
*/
namespace Netzmacht\Contao\Leaflet\Frontend;
/**
* The frontend module for the Leaflet map.
*
* @package Netzmacht\Contao\Leaflet\Frontend
*/
class MapModule extends AbstractMapHybrid
{
/**
* Template name.
*
* @var string
*/
protected $templateName = 'mod_leaflet_map';
/**
* Get the identifier.
*
* @return string
*/
protected function getIdentifier()
{
if ($this->get('leaflet_mapId')) {
return $this->get('leaflet_mapId');
}
if ($this->get('cssID')[0]) {
return 'map_' . $this->get('cssID')[0];
}
return 'map_mod_' . $this->get('id');
}
}

155
src/Frontend/RequestUrl.php Normal file
View File

@@ -0,0 +1,155 @@
<?php
/**
* @package contao-leaflet-maps
* @author David Molineus <david.molineus@netzmacht.de>
* @copyright 2014-2016 netzmacht David Molineus
* @license LGPL 3.0
* @filesource
*
*/
namespace Netzmacht\Contao\Leaflet\Frontend;
use Netzmacht\Contao\Leaflet\Filter\Filter;
/**
* Class RequestUrl creates the request url.
*
* @package Netzmacht\Contao\Leaflet\Request
*/
class RequestUrl implements \JsonSerializable
{
/**
* The for param is the identifier to the responsible frontend module or content element.
*
* @var string
*/
private static $for;
/**
* The leaflet hash.
*
* @var string
*/
private $hash;
/**
* The request url as url path.
*
* @var string
*/
private $url;
/**
* Request filter.
*
* @var Filter|null
*/
private $filter;
/**
* Create the request url.
*
* It combines the params and creates an hash for it.
*
* @param int $dataId The data object id.
* @param string|null $type Object type. If empty it assumes a layer.
* @param string|null $format Data format. If empty it assumes geojson.
* @param Filter $filter Optional filter.
*
* @return RequestUrl
*/
public static function create($dataId, $type = null, $format = null, Filter $filter = null)
{
$params = array(
'for' => static::$for,
'type' => $type != 'layer' ? $type : null,
'id' => $dataId,
'format' => $format != 'geojson' ? $format : null
);
$hash = base64_encode(implode(',', $params));
$query = 'leaflet=' . $hash;
if ($filter) {
$query .= '&amp;f=' . $filter->getName() . '&amp;v=' . $filter->toRequest();
}
$url = \Config::get('websitePath') . '/' . \Frontend::addToUrl($query, false);
return new static($url, $hash, $filter);
}
/**
* Set the for param.
*
* @param string $for The identifier which has the responsibility listen to the request.
*
* @return void
*/
public static function setFor($for)
{
static::$for = $for;
}
/**
* Construct.
*
* @param string $url The request url.
* @param string $hash The leaflet hash.
*/
public function __construct($url, $hash)
{
$this->url = $url;
$this->hash = $hash;
}
/**
* Get the leaflet url hash.
*
* @return string
*/
public function getHash()
{
return $this->hash;
}
/**
* Get the whole url.
*
* @return string
*/
public function getUrl()
{
return $this->url;
}
/**
* Get request filter.
*
* @return Filter|null
*/
public function getFilter()
{
return $this->filter;
}
/**
* Convert to string will always return the whole url.
*
* @return string
*/
public function __toString()
{
return $this->getUrl();
}
/**
* {@inheritdoc}
*/
public function jsonSerialize()
{
return $this->getUrl();
}
}

View File

@@ -0,0 +1,54 @@
<?php
/**
* @package contao-leaflet-maps
* @author David Molineus <david.molineus@netzmacht.de>
* @copyright 2014-2016 netzmacht David Molineus
* @license LGPL 3.0
* @filesource
*
*/
namespace Netzmacht\Contao\Leaflet\Frontend;
use Netzmacht\Contao\Toolkit\InsertTag\Replacer;
/**
* Class ValueFilter is a service class which can be used to filter values before passing them to an definition object.
*
* @package Netzmacht\Contao\Leaflet\Frontend
*/
class ValueFilter
{
/**
* The insert tag replacer.
*
* @var Replacer
*/
private $insertTagReplacer;
/**
* Construct.
*
* @param Replacer $replacer The insert tag replacer.
*/
public function __construct($replacer)
{
$this->insertTagReplacer = $replacer;
}
/**
* Filter a value so it can be passed to the frontend.
*
* The idea behind this extra method is that we just have to change one place if anything else than the
* insert tags has to be replaced.
*
* @param string $value The given value.
*
* @return string
*/
public function filter($value)
{
return $this->insertTagReplacer->replace($value);
}
}