mirror of
https://github.com/netzmacht/contao-leaflet-maps.git
synced 2025-12-01 04:24:02 +01:00
Refactor the insert tag replacer.
This commit is contained in:
@@ -65,12 +65,6 @@ $GLOBALS['TL_CTE']['includes']['leaflet'] = 'Netzmacht\Contao\Leaflet\Frontend\M
|
||||
$GLOBALS['FE_MOD']['includes']['leaflet'] = 'Netzmacht\Contao\Leaflet\Frontend\MapModule';
|
||||
|
||||
|
||||
/*
|
||||
* Hooks
|
||||
*/
|
||||
$GLOBALS['TL_HOOKS']['replaceInsertTags'][] = array('Netzmacht\Contao\Leaflet\Frontend\Hooks', 'replaceInsertTags');
|
||||
|
||||
|
||||
/*
|
||||
* Models.
|
||||
*/
|
||||
|
||||
@@ -1,91 +0,0 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* @package dev
|
||||
* @author David Molineus <david.molineus@netzmacht.de>
|
||||
* @copyright 2015 netzmacht creative David Molineus
|
||||
* @license LGPL 3.0
|
||||
* @filesource
|
||||
*
|
||||
*/
|
||||
|
||||
namespace Netzmacht\Contao\Leaflet\Frontend;
|
||||
|
||||
use Netzmacht\Contao\Toolkit\ServiceContainerTrait;
|
||||
use Netzmacht\Contao\Leaflet\MapService;
|
||||
|
||||
/**
|
||||
* Class Hooks contains hooks for the frontend manipulation.
|
||||
*
|
||||
* @package Netzmacht\Contao\Leaflet\Frontend
|
||||
*/
|
||||
class Hooks
|
||||
{
|
||||
use ServiceContainerTrait;
|
||||
|
||||
/**
|
||||
* Replace the leaflet insert tag and returns the generated map.
|
||||
*
|
||||
* 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.
|
||||
*
|
||||
* @param string $tag The given insert tag.
|
||||
*
|
||||
* @return bool|string
|
||||
*
|
||||
* @throws \Exception If debug mode is enabled and anything went wrong.
|
||||
*/
|
||||
public function replaceInsertTags($tag)
|
||||
{
|
||||
$parts = explode('::', $tag);
|
||||
|
||||
if ($parts[0] !== 'leaflet' || empty($parts[1])) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$style = empty($parts[2]) ? 'width:400px;height:300px' : $parts[2];
|
||||
$template = empty($parts[3]) ? 'leaflet_map_html' : $parts[3];
|
||||
|
||||
return $this->generateMap($parts[1], $template, $style);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the map service.
|
||||
*
|
||||
* @return MapService
|
||||
*/
|
||||
protected function getMapService()
|
||||
{
|
||||
return static::getServiceContainer()->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::getServiceContainer()->getConfig()->get('debugMode')) {
|
||||
throw $e;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,113 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* @package dev
|
||||
* @author David Molineus <david.molineus@netzmacht.de>
|
||||
* @copyright 2015 netzmacht creative David Molineus
|
||||
* @license LGPL 3.0
|
||||
* @filesource
|
||||
*
|
||||
*/
|
||||
|
||||
namespace Netzmacht\Contao\Leaflet\Frontend\InsertTag;
|
||||
|
||||
use Netzmacht\Contao\Leaflet\MapService;
|
||||
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 MapService
|
||||
*/
|
||||
private $mapService;
|
||||
|
||||
/**
|
||||
* Debug mode.
|
||||
*
|
||||
* @var bool
|
||||
*/
|
||||
private $debugMode;
|
||||
|
||||
/**
|
||||
* LeafletInsertTagParser constructor.
|
||||
*
|
||||
* @param MapService $mapService Map service.
|
||||
* @param bool $debugMode Debug mode.
|
||||
*/
|
||||
public function __construct(MapService $mapService, $debugMode)
|
||||
{
|
||||
$this->mapService = $mapService;
|
||||
$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[1], $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->mapService->generate($mapId, null, $mapId, $template, $style);
|
||||
} catch (\Exception $e) {
|
||||
if ($this->debugMode) {
|
||||
throw $e;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
@@ -16,9 +16,11 @@ use Netzmacht\Contao\Leaflet\Event\GetJavascriptEvent;
|
||||
use Netzmacht\Contao\Leaflet\Event\InitializeDefinitionMapperEvent;
|
||||
use Netzmacht\Contao\Leaflet\Event\InitializeEventDispatcherEvent;
|
||||
use Netzmacht\Contao\Leaflet\Event\InitializeLeafletBuilderEvent;
|
||||
use Netzmacht\Contao\Leaflet\Frontend\InsertTag\LeafletInsertTagParser;
|
||||
use Netzmacht\Contao\Leaflet\Mapper\DefinitionMapper;
|
||||
use Netzmacht\Contao\Leaflet\Mapper\Mapper;
|
||||
use Netzmacht\Contao\Leaflet\Model\IconModel;
|
||||
use Netzmacht\Contao\Toolkit\Event\InitializeSystemEvent;
|
||||
use Netzmacht\LeafletPHP\Assets;
|
||||
use Netzmacht\LeafletPHP\Definition\Type\ImageIcon;
|
||||
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
|
||||
@@ -36,6 +38,7 @@ class BootSubscriber implements EventSubscriberInterface
|
||||
public static function getSubscribedEvents()
|
||||
{
|
||||
return array(
|
||||
InitializeSystemEvent::NAME => 'initializeInsertTagParser',
|
||||
InitializeDefinitionMapperEvent::NAME => 'initializeDefinitionMapper',
|
||||
InitializeEventDispatcherEvent::NAME => 'initializeEventDispatcher',
|
||||
InitializeLeafletBuilderEvent::NAME => 'initializeLeafletBuilder',
|
||||
@@ -43,6 +46,23 @@ class BootSubscriber implements EventSubscriberInterface
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Initialize the leaflet insert tag parser.
|
||||
*
|
||||
* @param InitializeSystemEvent $event The event.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function initializeInsertTagParser(InitializeSystemEvent $event)
|
||||
{
|
||||
$container = $event->getServiceContainer();
|
||||
$debugMode = $container->getConfig()->get('debugMode');
|
||||
$mapService = $container->getService('leaflet.map.service');
|
||||
$parser = new LeafletInsertTagParser($mapService, $debugMode);
|
||||
|
||||
$container->getInsertTagReplacer()->registerParser($parser);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create and register all configured mappers.
|
||||
*
|
||||
|
||||
Reference in New Issue
Block a user