Rewrite the insert tag replacer using the new toolkit 3.0 features.

This commit is contained in:
David Molineus
2017-10-11 11:11:30 +02:00
parent 5a1ea75623
commit 572d2e74a0
2 changed files with 25 additions and 16 deletions

View File

@@ -15,7 +15,9 @@ declare(strict_types=1);
namespace Netzmacht\Contao\Leaflet\Frontend\InsertTag; namespace Netzmacht\Contao\Leaflet\Frontend\InsertTag;
use Netzmacht\Contao\Leaflet\MapProvider; use Netzmacht\Contao\Leaflet\MapProvider;
use Netzmacht\Contao\Toolkit\InsertTag\Parser; use Netzmacht\Contao\Toolkit\InsertTag\AbstractSingleInsertTagParser;
use Netzmacht\Contao\Toolkit\InsertTag\ArgumentParser;
use Netzmacht\Contao\Toolkit\InsertTag\ArgumentParserPlugin;
/** /**
* LeafletInsertTagParser parses the leaflet insert tag. * LeafletInsertTagParser parses the leaflet insert tag.
@@ -29,8 +31,17 @@ use Netzmacht\Contao\Toolkit\InsertTag\Parser;
* *
* @package Netzmacht\Contao\Leaflet\Frontend\InsertTag * @package Netzmacht\Contao\Leaflet\Frontend\InsertTag
*/ */
class LeafletInsertTagParser implements Parser final class LeafletInsertTagParser extends AbstractSingleInsertTagParser
{ {
use ArgumentParserPlugin;
/**
* Insert tag name.
*
* @var string
*/
protected $tagName = 'leaflet';
/** /**
* The map service. * The map service.
* *
@@ -60,26 +71,24 @@ class LeafletInsertTagParser implements Parser
/** /**
* {@inheritDoc} * {@inheritDoc}
*/ */
public function supports(string $tag): bool protected function parseTag(array $arguments, string $tag, string $raw)
{ {
return $tag === 'leaflet'; if (empty($arguments['mapId'])) {
return '';
}
$style = empty($arguments['style']) ? 'width:400px;height:300px' : $arguments['style'];
$template = empty($arguments['template']) ? 'leaflet_map_html' : $arguments['template'];
return $this->generateMap($arguments['mapId'], $template, $style);
} }
/** /**
* {@inheritDoc} * {@inheritDoc}
*/ */
public function parse(string $raw, string $tag, string $params = null, bool $cache = true): string protected function createArgumentParser(): ArgumentParser
{ {
$parts = explode('::', $params); return ArgumentParser::create()->splitBy('::', ['mapId', 'style', 'template']);
if (empty($parts[0])) {
return '';
}
$style = empty($parts[1]) ? 'width:400px;height:300px' : $parts[1];
$template = empty($parts[2]) ? 'leaflet_map_html' : $parts[2];
return $this->generateMap($parts[0], $template, $style);
} }
/** /**

View File

@@ -99,4 +99,4 @@ services:
- '@netzmacht.contao_leaflet_maps.map.provider' - '@netzmacht.contao_leaflet_maps.map.provider'
- '%kernel.debug%' - '%kernel.debug%'
tags: tags:
- { name: 'netzmacht.contao_toolkit.insert_tag_parser' } - { name: 'contao.hook', hook: 'replaceInsertTags', method: 'replace' }