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

97 lines
2.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\Javascript\Event\BuildEvent;
2015-01-12 15:11:35 +01:00
use Netzmacht\Javascript\Event\EncodeValueEvent;
2014-12-29 12:17:40 +01:00
use Netzmacht\LeafletPHP\Definition\Map;
2015-01-12 15:11:35 +01:00
use Netzmacht\LeafletPHP\Definition\Type\Icon;
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(
BuildEvent::NAME => array(
2015-01-12 15:11:35 +01:00
array('startWrapper', 100),
array('endWrapper', -100),
),
EncodeValueEvent::NAME => array(
array('encodeIcons', 100)
),
2014-12-29 12:17:40 +01:00
);
}
/**
* Start the wrapper.
*
* The encoded map is wrapped so that it is added to window.ContaoLeaflet. You can subscribe the
* "mapadded" event on window.ContaoLeaflet if you can to do some customize stuff.
*
* @param BuildEvent $event The subscribed event.
*
* @return void
*/
public function startWrapper(BuildEvent $event)
{
$object = $event->getObject();
if ($object instanceof Map) {
2015-01-06 18:49:22 +01:00
$line = sprintf('ContaoLeaflet.addMap(\'%s\', (function() {', $object->getId());
2014-12-29 12:17:40 +01:00
$event->getOutput()->addLine($line);
}
}
/**
* End the wrapper.
*
* @param BuildEvent $event The subscribed event.
*
* @return void
*/
public function endWrapper(BuildEvent $event)
{
$object = $event->getObject();
if ($object instanceof Map) {
$line = 'return map; })());';
$event->getOutput()->addLine($line);
}
}
2015-01-12 15:11:35 +01:00
2015-01-12 19:03:29 +01:00
/**
* Force that icons are encoded as reference to the ContaoLeaflet icon registry.
*
* @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) {
$event->addLine('ContaoLeaflet.getIcon(\'' . $value->getId() . '\')');
$event->stopPropagation();
}
}
2014-12-29 12:17:40 +01:00
}