Code style.

This commit is contained in:
David Molineus
2015-01-15 15:30:16 +01:00
parent 2a7d95b37a
commit 04c0c75e14
9 changed files with 82 additions and 29 deletions

View File

@@ -65,9 +65,8 @@ class DataController
$this->encodeData($this->input['format'], $data); $this->encodeData($this->input['format'], $data);
} catch (\Exception $e) { } catch (\Exception $e) {
if (\Config::get('debugMode') || \Config::get('displayErrors')) { if (\Config::get('debugMode') || \Config::get('displayErrors')) {
throw $e;
} }
throw $e;
$error = true; $error = true;
} }

View File

@@ -47,18 +47,10 @@ class Hooks
return false; return false;
} }
$mapService = $this->getMapService(); $style = empty($parts[2]) ? 'width:400px;height:300px' : $parts[2];
$style = empty($parts[2]) ? 'width:400px;height:300px' : $parts[2]; $template = empty($parts[3]) ? 'leaflet_map_html' : $parts[3];
$template = empty($parts[3]) ? 'leaflet_map_html' : $parts[3];
try { return $this->generateMap($parts[1], $template, $style);
return $mapService->generate($parts[1], null, $parts[1], $template, $style);
} catch (\Exception $e) {
if (static::getService('config')->get('debugMode')) {
throw $e;
}
return false;
}
} }
/** /**
@@ -70,4 +62,30 @@ class Hooks
{ {
return static::getService('leaflet.map.service'); 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;
}
}
} }

View File

@@ -94,7 +94,7 @@ trait HybridTrait
* *
* @return void * @return void
* *
* @throws \Exception If something went wrong. * @throws \Exception If the map could not be created.
* *
* @SuppressWarnings(PHPMD.Superglobals) * @SuppressWarnings(PHPMD.Superglobals)
*/ */
@@ -102,7 +102,7 @@ trait HybridTrait
{ {
try { try {
RequestUrl::setFor($this->getIdentifier()); 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); $map = $this->mapService->generate($this->leaflet_map, null, $mapId);
RequestUrl::setFor(null); RequestUrl::setFor(null);
@@ -131,7 +131,10 @@ trait HybridTrait
/** /**
* Handle ajax request if leaflet parameter is given. * 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() private function handleAjaxRequest()
{ {
@@ -139,7 +142,12 @@ trait HybridTrait
// Handle ajax request. // Handle ajax request.
if ($input) { 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_combine(array('for', 'type', 'id', 'format'), $data);
$data = array_filter($data); $data = array_filter($data);

View File

@@ -40,6 +40,11 @@ class MapElement extends \ContentElement
$this->construct($objElement, $strColumn); $this->construct($objElement, $strColumn);
} }
/**
* Get the identifier.
*
* @return string
*/
protected function getIdentifier() protected function getIdentifier()
{ {
return 'ce_' . $this->id; return 'ce_' . $this->id;

View File

@@ -39,6 +39,11 @@ class MapModule extends \Module
$this->construct($objElement, $strColumn); $this->construct($objElement, $strColumn);
} }
/**
* Get the identifier.
*
* @return string
*/
protected function getIdentifier() protected function getIdentifier()
{ {
return 'mod_' . $this->id; return 'mod_' . $this->id;

View File

@@ -56,7 +56,7 @@ class RequestUrl implements \JsonSerializable
'for' => static::$for, 'for' => static::$for,
'type' => $type != 'layer' ? $type : null, 'type' => $type != 'layer' ? $type : null,
'id' => $dataId, 'id' => $dataId,
'format' => $format != 'geojson' ? $format: null 'format' => $format != 'geojson' ? $format : null
); );
$hash = base64_encode(implode(',', $params)); $hash = base64_encode(implode(',', $params));
@@ -68,7 +68,9 @@ class RequestUrl implements \JsonSerializable
/** /**
* Set the for param. * 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) public static function setFor($for)
{ {
@@ -104,7 +106,7 @@ class RequestUrl implements \JsonSerializable
*/ */
public function getUrl() public function getUrl()
{ {
return ; return $this->url;
} }
/** /**
@@ -120,7 +122,7 @@ class RequestUrl implements \JsonSerializable
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
function jsonSerialize() public function jsonSerialize()
{ {
return $this->getUrl(); return $this->getUrl();
} }

View File

@@ -114,7 +114,10 @@ class MapService
* @param string $style Optional style attributes. * @param string $style Optional style attributes.
* *
* @return string * @return string
* @throws \Exception * @throws \Exception If generating went wrong.
*
* @SuppressWarnings(PHPMD.UnusedLocalVariables)
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
*/ */
public function generate( public function generate(
$mapId, $mapId,

View File

@@ -73,10 +73,6 @@ class MarkersLayerMapper extends AbstractLayerMapper implements GeoJsonMapper
return array($this->getElementId($model, $elementId), RequestUrl::create($model->id), array(), $layer); 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)); return array($this->getElementId($model, $elementId), RequestUrl::create($model->id));
} }

View File

@@ -15,6 +15,7 @@ use Netzmacht\Contao\Leaflet\Frontend\RequestUrl;
use Netzmacht\Javascript\Encoder; use Netzmacht\Javascript\Encoder;
use Netzmacht\Javascript\Event\EncodeValueEvent; use Netzmacht\Javascript\Event\EncodeValueEvent;
use Netzmacht\Javascript\Event\GetReferenceEvent; use Netzmacht\Javascript\Event\GetReferenceEvent;
use Netzmacht\Javascript\Exception\EncodeValueFailed;
use Netzmacht\LeafletPHP\Definition\Group\GeoJson; use Netzmacht\LeafletPHP\Definition\Group\GeoJson;
use Netzmacht\LeafletPHP\Definition\Type\Icon; use Netzmacht\LeafletPHP\Definition\Type\Icon;
use Netzmacht\LeafletPHP\Plugins\Omnivore\OmnivoreLayer; 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) public function referenceIcon(GetReferenceEvent $event)
{ {
$value = $event->getObject(); $value = $event->getObject();
@@ -49,7 +57,6 @@ class EncoderSubscriber implements EventSubscriberInterface
$event->setReference('L.contao.getIcon(\'' . $value->getId() . '\')'); $event->setReference('L.contao.getIcon(\'' . $value->getId() . '\')');
$event->stopPropagation(); $event->stopPropagation();
} }
} }
/** /**
@@ -64,12 +71,19 @@ class EncoderSubscriber implements EventSubscriberInterface
$value = $event->getValue(); $value = $event->getValue();
if ($value instanceof Icon) { 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->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) public function loadLayer(EncodeValueEvent $event)
{ {
$value = $event->getValue(); $value = $event->getValue();
@@ -81,6 +95,9 @@ class EncoderSubscriber implements EventSubscriberInterface
if ($url instanceof RequestUrl) { if ($url instanceof RequestUrl) {
$url = $url->getHash(); $url = $url->getHash();
} elseif (strpos($url, '/') !== false) {
// Slash found, not contao leaflet hash, do not replace encoding.
return;
} }
$event->addLine( $event->addLine(