From 04c0c75e14b4d7aeea7e21f1f517ca9eb4d34052 Mon Sep 17 00:00:00 2001 From: David Molineus Date: Thu, 15 Jan 2015 15:30:16 +0100 Subject: [PATCH] Code style. --- .../Leaflet/Frontend/DataController.php | 3 +- .../Contao/Leaflet/Frontend/Hooks.php | 40 ++++++++++++++----- .../Contao/Leaflet/Frontend/HybridTrait.php | 16 ++++++-- .../Contao/Leaflet/Frontend/MapElement.php | 5 +++ .../Contao/Leaflet/Frontend/MapModule.php | 5 +++ .../Contao/Leaflet/Frontend/RequestUrl.php | 10 +++-- src/Netzmacht/Contao/Leaflet/MapService.php | 5 ++- .../Mapper/Layer/MarkersLayerMapper.php | 4 -- .../Leaflet/Subscriber/EncoderSubscriber.php | 23 +++++++++-- 9 files changed, 82 insertions(+), 29 deletions(-) diff --git a/src/Netzmacht/Contao/Leaflet/Frontend/DataController.php b/src/Netzmacht/Contao/Leaflet/Frontend/DataController.php index bdfa84a..0ddd2c3 100644 --- a/src/Netzmacht/Contao/Leaflet/Frontend/DataController.php +++ b/src/Netzmacht/Contao/Leaflet/Frontend/DataController.php @@ -65,9 +65,8 @@ class DataController $this->encodeData($this->input['format'], $data); } catch (\Exception $e) { if (\Config::get('debugMode') || \Config::get('displayErrors')) { - + throw $e; } - throw $e; $error = true; } diff --git a/src/Netzmacht/Contao/Leaflet/Frontend/Hooks.php b/src/Netzmacht/Contao/Leaflet/Frontend/Hooks.php index 37c6004..7d1c856 100644 --- a/src/Netzmacht/Contao/Leaflet/Frontend/Hooks.php +++ b/src/Netzmacht/Contao/Leaflet/Frontend/Hooks.php @@ -47,18 +47,10 @@ class Hooks return false; } - $mapService = $this->getMapService(); - $style = empty($parts[2]) ? 'width:400px;height:300px' : $parts[2]; - $template = empty($parts[3]) ? 'leaflet_map_html' : $parts[3]; + $style = empty($parts[2]) ? 'width:400px;height:300px' : $parts[2]; + $template = empty($parts[3]) ? 'leaflet_map_html' : $parts[3]; - try { - return $mapService->generate($parts[1], null, $parts[1], $template, $style); - } catch (\Exception $e) { - if (static::getService('config')->get('debugMode')) { - throw $e; - } - return false; - } + return $this->generateMap($parts[1], $template, $style); } /** @@ -70,4 +62,30 @@ class Hooks { return static::getService('leaflet.map.service'); } + + /** + * 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 { + $mapService = $this->getMapService(); + + return $mapService->generate($mapId, null, $mapId, $template, $style); + } catch (\Exception $e) { + if (static::getService('config')->get('debugMode')) { + throw $e; + } + + return false; + } + } } diff --git a/src/Netzmacht/Contao/Leaflet/Frontend/HybridTrait.php b/src/Netzmacht/Contao/Leaflet/Frontend/HybridTrait.php index 9f842e0..65d9b98 100644 --- a/src/Netzmacht/Contao/Leaflet/Frontend/HybridTrait.php +++ b/src/Netzmacht/Contao/Leaflet/Frontend/HybridTrait.php @@ -94,7 +94,7 @@ trait HybridTrait * * @return void * - * @throws \Exception If something went wrong. + * @throws \Exception If the map could not be created. * * @SuppressWarnings(PHPMD.Superglobals) */ @@ -102,7 +102,7 @@ trait HybridTrait { try { RequestUrl::setFor($this->getIdentifier()); - $mapId = 'map_' . ($this->cssID[0] ?: ('ce_' . $this->id)); + $mapId = 'map_' . ($this->cssID[0] ?: ($this->getIdentifier())); $map = $this->mapService->generate($this->leaflet_map, null, $mapId); RequestUrl::setFor(null); @@ -131,7 +131,10 @@ trait HybridTrait /** * Handle ajax request if leaflet parameter is given. * - * @throws \Exception + * @return void + * + * @throws \HttpRequestException If a bad leaflet param hash is given. + * @SuppressWarnings(ExitExpression) */ private function handleAjaxRequest() { @@ -139,7 +142,12 @@ trait HybridTrait // Handle ajax request. if ($input) { - $data = (array) explode(',', base64_decode($input)); + $data = explode(',', base64_decode($input)); + + if (count($data) != 4) { + throw new \HttpRequestException('Bad request. Could not resolve query params'); + } + $data = array_combine(array('for', 'type', 'id', 'format'), $data); $data = array_filter($data); diff --git a/src/Netzmacht/Contao/Leaflet/Frontend/MapElement.php b/src/Netzmacht/Contao/Leaflet/Frontend/MapElement.php index 094660a..67754bf 100644 --- a/src/Netzmacht/Contao/Leaflet/Frontend/MapElement.php +++ b/src/Netzmacht/Contao/Leaflet/Frontend/MapElement.php @@ -40,6 +40,11 @@ class MapElement extends \ContentElement $this->construct($objElement, $strColumn); } + /** + * Get the identifier. + * + * @return string + */ protected function getIdentifier() { return 'ce_' . $this->id; diff --git a/src/Netzmacht/Contao/Leaflet/Frontend/MapModule.php b/src/Netzmacht/Contao/Leaflet/Frontend/MapModule.php index 05cd2f5..0769393 100644 --- a/src/Netzmacht/Contao/Leaflet/Frontend/MapModule.php +++ b/src/Netzmacht/Contao/Leaflet/Frontend/MapModule.php @@ -39,6 +39,11 @@ class MapModule extends \Module $this->construct($objElement, $strColumn); } + /** + * Get the identifier. + * + * @return string + */ protected function getIdentifier() { return 'mod_' . $this->id; diff --git a/src/Netzmacht/Contao/Leaflet/Frontend/RequestUrl.php b/src/Netzmacht/Contao/Leaflet/Frontend/RequestUrl.php index bda697a..20befaa 100644 --- a/src/Netzmacht/Contao/Leaflet/Frontend/RequestUrl.php +++ b/src/Netzmacht/Contao/Leaflet/Frontend/RequestUrl.php @@ -56,7 +56,7 @@ class RequestUrl implements \JsonSerializable 'for' => static::$for, 'type' => $type != 'layer' ? $type : null, 'id' => $dataId, - 'format' => $format != 'geojson' ? $format: null + 'format' => $format != 'geojson' ? $format : null ); $hash = base64_encode(implode(',', $params)); @@ -68,7 +68,9 @@ class RequestUrl implements \JsonSerializable /** * Set the for param. * - * @param $for + * @param string $for The identifier which has the responsibility listen to the request. + * + * @return void */ public static function setFor($for) { @@ -104,7 +106,7 @@ class RequestUrl implements \JsonSerializable */ public function getUrl() { - return ; + return $this->url; } /** @@ -120,7 +122,7 @@ class RequestUrl implements \JsonSerializable /** * {@inheritdoc} */ - function jsonSerialize() + public function jsonSerialize() { return $this->getUrl(); } diff --git a/src/Netzmacht/Contao/Leaflet/MapService.php b/src/Netzmacht/Contao/Leaflet/MapService.php index 3b0e4b6..db0a44a 100644 --- a/src/Netzmacht/Contao/Leaflet/MapService.php +++ b/src/Netzmacht/Contao/Leaflet/MapService.php @@ -114,7 +114,10 @@ class MapService * @param string $style Optional style attributes. * * @return string - * @throws \Exception + * @throws \Exception If generating went wrong. + * + * @SuppressWarnings(PHPMD.UnusedLocalVariables) + * @SuppressWarnings(PHPMD.UnusedFormalParameter) */ public function generate( $mapId, diff --git a/src/Netzmacht/Contao/Leaflet/Mapper/Layer/MarkersLayerMapper.php b/src/Netzmacht/Contao/Leaflet/Mapper/Layer/MarkersLayerMapper.php index a3c1049..7b33545 100644 --- a/src/Netzmacht/Contao/Leaflet/Mapper/Layer/MarkersLayerMapper.php +++ b/src/Netzmacht/Contao/Leaflet/Mapper/Layer/MarkersLayerMapper.php @@ -73,10 +73,6 @@ class MarkersLayerMapper extends AbstractLayerMapper implements GeoJsonMapper return array($this->getElementId($model, $elementId), RequestUrl::create($model->id), array(), $layer); } - if (!empty($options)) { - - } - return array($this->getElementId($model, $elementId), RequestUrl::create($model->id)); } diff --git a/src/Netzmacht/Contao/Leaflet/Subscriber/EncoderSubscriber.php b/src/Netzmacht/Contao/Leaflet/Subscriber/EncoderSubscriber.php index 56a62ab..c06016e 100644 --- a/src/Netzmacht/Contao/Leaflet/Subscriber/EncoderSubscriber.php +++ b/src/Netzmacht/Contao/Leaflet/Subscriber/EncoderSubscriber.php @@ -15,6 +15,7 @@ use Netzmacht\Contao\Leaflet\Frontend\RequestUrl; use Netzmacht\Javascript\Encoder; use Netzmacht\Javascript\Event\EncodeValueEvent; use Netzmacht\Javascript\Event\GetReferenceEvent; +use Netzmacht\Javascript\Exception\EncodeValueFailed; use Netzmacht\LeafletPHP\Definition\Group\GeoJson; use Netzmacht\LeafletPHP\Definition\Type\Icon; use Netzmacht\LeafletPHP\Plugins\Omnivore\OmnivoreLayer; @@ -41,6 +42,13 @@ class EncoderSubscriber implements EventSubscriberInterface ); } + /** + * Create icon reference to the contao leaflet icon registry. + * + * @param GetReferenceEvent $event The subscribed event. + * + * @return void + */ public function referenceIcon(GetReferenceEvent $event) { $value = $event->getObject(); @@ -49,7 +57,6 @@ class EncoderSubscriber implements EventSubscriberInterface $event->setReference('L.contao.getIcon(\'' . $value->getId() . '\')'); $event->stopPropagation(); } - } /** @@ -64,12 +71,19 @@ class EncoderSubscriber implements EventSubscriberInterface $value = $event->getValue(); if ($value instanceof Icon) { - //$event->addLine('L.contao.getIcon(\'' . $value->getId() . '\')'); + // Do not encode the icon, as it is generated in an separate icon file. $event->setSuccessful(); - $event->stopPropagation(); } } + /** + * Encode OmnivoreLayers so that the internal used contao.loadLayer method is used. + * + * @param EncodeValueEvent $event The subscribed event. + * + * @return void + * @throws EncodeValueFailed If encoding failed. + */ public function loadLayer(EncodeValueEvent $event) { $value = $event->getValue(); @@ -81,6 +95,9 @@ class EncoderSubscriber implements EventSubscriberInterface if ($url instanceof RequestUrl) { $url = $url->getHash(); + } elseif (strpos($url, '/') !== false) { + // Slash found, not contao leaflet hash, do not replace encoding. + return; } $event->addLine(