diff --git a/module/config/config.php b/module/config/config.php index 1cd3c47..adda96d 100644 --- a/module/config/config.php +++ b/module/config/config.php @@ -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. */ diff --git a/src/Netzmacht/Contao/Leaflet/Frontend/Hooks.php b/src/Netzmacht/Contao/Leaflet/Frontend/Hooks.php deleted file mode 100644 index 2518e99..0000000 --- a/src/Netzmacht/Contao/Leaflet/Frontend/Hooks.php +++ /dev/null @@ -1,91 +0,0 @@ - - * @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; - } - } -} diff --git a/src/Netzmacht/Contao/Leaflet/Frontend/InsertTag/LeafletInsertTagParser.php b/src/Netzmacht/Contao/Leaflet/Frontend/InsertTag/LeafletInsertTagParser.php new file mode 100644 index 0000000..2882837 --- /dev/null +++ b/src/Netzmacht/Contao/Leaflet/Frontend/InsertTag/LeafletInsertTagParser.php @@ -0,0 +1,113 @@ + + * @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; + } +} diff --git a/src/Netzmacht/Contao/Leaflet/Subscriber/BootSubscriber.php b/src/Netzmacht/Contao/Leaflet/Subscriber/BootSubscriber.php index 35746f4..48bdd36 100644 --- a/src/Netzmacht/Contao/Leaflet/Subscriber/BootSubscriber.php +++ b/src/Netzmacht/Contao/Leaflet/Subscriber/BootSubscriber.php @@ -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. *