Files
contao-leaflet-maps/src/Netzmacht/Contao/Leaflet/Subscriber/EncoderSubscriber.php

149 lines
4.5 KiB
PHP
Raw Normal View History

2014-12-29 12:17:40 +01:00
<?php
/**
* @package dev
* @author David Molineus <david.molineus@netzmacht.de>
* @copyright 2014 netzmacht creative David Molineus
* @license LGPL 3.0
* @filesource
*
*/
namespace Netzmacht\Contao\Leaflet\Subscriber;
use Netzmacht\Contao\Leaflet\Frontend\RequestUrl;
use Netzmacht\JavascriptBuilder\Encoder;
use Netzmacht\JavascriptBuilder\Symfony\Event\EncodeValueEvent;
use Netzmacht\JavascriptBuilder\Symfony\Event\EncodeReferenceEvent;
use Netzmacht\JavascriptBuilder\Exception\EncodeValueFailed;
use Netzmacht\LeafletPHP\Definition\Group\GeoJson;
2015-01-12 15:11:35 +01:00
use Netzmacht\LeafletPHP\Definition\Type\Icon;
use Netzmacht\LeafletPHP\Plugins\Omnivore\OmnivoreLayer;
2014-12-29 12:17:40 +01:00
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
/**
* Class EncoderSubscriber subscribes to the internal encoding event dispatcher.
*
* @package Netzmacht\Contao\Leaflet\Subscriber
*/
class EncoderSubscriber implements EventSubscriberInterface
{
/**
* {@inheritdoc}
*/
public static function getSubscribedEvents()
{
return array(
2015-01-12 15:11:35 +01:00
EncodeValueEvent::NAME => array(
array('encodeIcons', 1000),
array('loadLayer', 100),
2015-01-12 15:11:35 +01:00
),
EncodeReferenceEvent::NAME => array('referenceIcon', 100),
2014-12-29 12:17:40 +01:00
);
}
2015-01-15 15:30:16 +01:00
/**
* Create icon reference to the contao leaflet icon registry.
*
* @param EncodeReferenceEvent $event The subscribed event.
2015-01-15 15:30:16 +01:00
*
* @return void
*/
public function referenceIcon(EncodeReferenceEvent $event)
{
$value = $event->getObject();
if ($value instanceof Icon) {
2015-01-15 13:01:58 +01:00
$event->setReference('L.contao.getIcon(\'' . $value->getId() . '\')');
$event->stopPropagation();
}
}
2015-01-12 19:03:29 +01:00
/**
2015-01-15 13:01:58 +01:00
* Force that icons are encoded as reference to the L.contao icon registry.
2015-01-12 19:03:29 +01:00
*
* @param EncodeValueEvent $event The subscribed event.
*
* @return void
*/
2015-01-12 15:11:35 +01:00
public function encodeIcons(EncodeValueEvent $event)
{
$value = $event->getValue();
if ($value instanceof Icon) {
2015-01-15 15:30:16 +01:00
// Do not encode the icon, as it is generated in an separate icon file.
$event->setSuccessful();
2015-01-15 18:07:27 +01:00
$event->stopPropagation();
2015-01-12 15:11:35 +01:00
}
}
2015-01-15 15:30:16 +01:00
/**
* 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();
$encoder = $event->getEncoder();
$ref = $encoder->encodeReference($value);
if ($value instanceof OmnivoreLayer) {
$url = $value->getUrl();
if ($url instanceof RequestUrl) {
$url = $url->getHash();
2015-01-15 15:30:16 +01:00
} elseif (strpos($url, '/') !== false) {
// Slash found, not contao leaflet hash, do not replace encoding.
return;
}
$event->addLine(
sprintf(
2015-01-15 13:01:58 +01:00
'%s = L.contao.loadLayer(%s, %s, %s, %s, map);',
$ref,
$encoder->encodeValue($url),
$encoder->encodeValue(strtolower(str_replace('Omnivore.', '', $value->getType()))),
2015-01-15 13:01:58 +01:00
$encoder->encodeArray($value->getOptions(), JSON_FORCE_OBJECT),
$this->encodeCustomLayer($value, $encoder)
)
);
foreach ($value->getLayers() as $layer) {
$event->addLine(sprintf('%s.addLayer(%s);', $ref, $encoder->encodeReference($layer)));
}
foreach ($value->getMethodCalls() as $call) {
$event->addLine($call->encode($encoder, $encoder->getOutput()));
}
}
}
/**
* Encode a custom layer for the omnivore plugin.
*
* @param OmnivoreLayer $layer The layer.
* @param Encoder $encoder The javascript encoder.
*
* @return string
*/
protected function encodeCustomLayer(OmnivoreLayer $layer, Encoder $encoder)
{
$customLayer = $layer->getCustomLayer();
if ($customLayer instanceof GeoJson && !$customLayer->getMethodCalls()) {
return sprintf(
'L.geoJson(null, %s)',
$encoder->encodeValue($customLayer->getOptions())
);
} elseif ($customLayer) {
return $encoder->encodeReference($customLayer);
}
return 'null';
}
2014-12-29 12:17:40 +01:00
}