From 9dfb6698cfe900327dad3e0ca89ba889f42627ee Mon Sep 17 00:00:00 2001 From: David Molineus Date: Thu, 22 Jan 2015 12:24:22 +0100 Subject: [PATCH] Replace insert tags and masked entities. (Fix #17) --- module/config/services.php | 11 ++++ .../Leaflet/Frontend/Helper/FrontendApi.php | 28 +++++++++ .../Contao/Leaflet/Frontend/ValueFilter.php | 54 ++++++++++++++++ .../Contao/Leaflet/Mapper/UI/MarkerMapper.php | 10 ++- .../Mapper/Vector/AbstractVectorMapper.php | 10 ++- .../Contao/Leaflet/ServiceContainer.php | 61 +++++++++++++++++++ .../Contao/Leaflet/ServiceContainerTrait.php | 30 +++++++++ 7 files changed, 202 insertions(+), 2 deletions(-) create mode 100644 src/Netzmacht/Contao/Leaflet/Frontend/Helper/FrontendApi.php create mode 100644 src/Netzmacht/Contao/Leaflet/Frontend/ValueFilter.php create mode 100644 src/Netzmacht/Contao/Leaflet/ServiceContainer.php create mode 100644 src/Netzmacht/Contao/Leaflet/ServiceContainerTrait.php diff --git a/module/config/services.php b/module/config/services.php index 07142f1..c9959b5 100644 --- a/module/config/services.php +++ b/module/config/services.php @@ -10,8 +10,11 @@ */ use Netzmacht\Contao\Leaflet\Boot; +use Netzmacht\Contao\Leaflet\Frontend\Helper\FrontendApi; +use Netzmacht\Contao\Leaflet\Frontend\ValueFilter; use Netzmacht\Contao\Leaflet\Mapper\DefinitionMapper; use Netzmacht\Contao\Leaflet\MapService; +use Netzmacht\Contao\Leaflet\ServiceContainer; use Netzmacht\JavascriptBuilder\Builder; use Netzmacht\JavascriptBuilder\Encoder; use Netzmacht\JavascriptBuilder\Encoder\Chain\MultipleChain; @@ -100,3 +103,11 @@ $container['leaflet.definition.builder'] = $container->share(function($container return $boot->initializeLeafletBuilder($leaflet); }); + +$container['leaflet.frontend.value-filter'] = $container->share(function() { + return new ValueFilter(new FrontendApi()); +}); + +$container['leaflet.service-container'] = $container->share(function($container) { + return new ServiceContainer($container); +}); diff --git a/src/Netzmacht/Contao/Leaflet/Frontend/Helper/FrontendApi.php b/src/Netzmacht/Contao/Leaflet/Frontend/Helper/FrontendApi.php new file mode 100644 index 0000000..c7afa1a --- /dev/null +++ b/src/Netzmacht/Contao/Leaflet/Frontend/Helper/FrontendApi.php @@ -0,0 +1,28 @@ + + * @copyright 2015 netzmacht creative David Molineus + * @license LGPL 3.0 + * @filesource + * + */ + +namespace Netzmacht\Contao\Leaflet\Frontend\Helper; + +/** + * Class FrontendApi provides access to the frontend api of contao. + * + * @package Netzmacht\Contao\Leaflet\Frontend\Helper + */ +class FrontendApi extends \Frontend +{ + /** + * {@inheritdoc} + */ + public function replaceInsertTags($strBuffer, $blnCache = true) + { + return parent::replaceInsertTags($strBuffer, $blnCache); + } +} diff --git a/src/Netzmacht/Contao/Leaflet/Frontend/ValueFilter.php b/src/Netzmacht/Contao/Leaflet/Frontend/ValueFilter.php new file mode 100644 index 0000000..ccd682d --- /dev/null +++ b/src/Netzmacht/Contao/Leaflet/Frontend/ValueFilter.php @@ -0,0 +1,54 @@ + + * @copyright 2015 netzmacht creative David Molineus + * @license LGPL 3.0 + * @filesource + * + */ + +namespace Netzmacht\Contao\Leaflet\Frontend; + +use Netzmacht\Contao\Leaflet\Frontend\Helper\FrontendApi; + +/** + * Class ValueFilter is a service class which can be used to filter values before passing them to an definition object. + * + * @package Netzmacht\Contao\Leaflet\Frontend + */ +class ValueFilter +{ + /** + * The frontend api of Contao. + * + * @var FrontendApi + */ + private $api; + + /** + * Construct. + * + * @param FrontendApi $api The frontend api of Contao. + */ + public function __construct($api) + { + $this->api = $api; + } + + /** + * Filter a value so it can be passed to the frontend. + * + * The idea behind this extra method is that we just have to change one place if anything else than the + * insert tags has to be replaced. + * + * @param string $value The given value + * + * @return string + */ + public function filter($value) + { + return $this->api->replaceInsertTags($value); + } +} diff --git a/src/Netzmacht/Contao/Leaflet/Mapper/UI/MarkerMapper.php b/src/Netzmacht/Contao/Leaflet/Mapper/UI/MarkerMapper.php index bdf35ef..692acd9 100644 --- a/src/Netzmacht/Contao/Leaflet/Mapper/UI/MarkerMapper.php +++ b/src/Netzmacht/Contao/Leaflet/Mapper/UI/MarkerMapper.php @@ -14,6 +14,7 @@ namespace Netzmacht\Contao\Leaflet\Mapper\UI; use Netzmacht\Contao\Leaflet\Mapper\AbstractMapper; use Netzmacht\Contao\Leaflet\Mapper\DefinitionMapper; use Netzmacht\Contao\Leaflet\Model\IconModel; +use Netzmacht\Contao\Leaflet\ServiceContainerTrait; use Netzmacht\LeafletPHP\Definition; use Netzmacht\LeafletPHP\Definition\Type\ImageIcon; use Netzmacht\LeafletPHP\Definition\Type\LatLngBounds; @@ -26,6 +27,8 @@ use Netzmacht\LeafletPHP\Definition\UI\Marker; */ class MarkerMapper extends AbstractMapper { + use ServiceContainerTrait; + /** * Class of the model being build. * @@ -79,7 +82,12 @@ class MarkerMapper extends AbstractMapper ) { if ($definition instanceof Marker) { if ($model->addPopup) { - $definition->bindPopup($model->popupContent); + $content = $this + ->getServiceContainer() + ->getFrontendValueFilter() + ->filter($model->popupContent); + + $definition->bindPopup($content); } if ($model->customIcon) { diff --git a/src/Netzmacht/Contao/Leaflet/Mapper/Vector/AbstractVectorMapper.php b/src/Netzmacht/Contao/Leaflet/Mapper/Vector/AbstractVectorMapper.php index 5e57d7d..406fa6a 100644 --- a/src/Netzmacht/Contao/Leaflet/Mapper/Vector/AbstractVectorMapper.php +++ b/src/Netzmacht/Contao/Leaflet/Mapper/Vector/AbstractVectorMapper.php @@ -15,6 +15,7 @@ use Netzmacht\Contao\Leaflet\Definition\Style; use Netzmacht\Contao\Leaflet\Mapper\AbstractTypeMapper; use Netzmacht\Contao\Leaflet\Mapper\DefinitionMapper; use Netzmacht\Contao\Leaflet\Model\StyleModel; +use Netzmacht\Contao\Leaflet\ServiceContainerTrait; use Netzmacht\LeafletPHP\Definition; use Netzmacht\LeafletPHP\Definition\HasPopup; use Netzmacht\LeafletPHP\Definition\Type\LatLngBounds; @@ -27,6 +28,8 @@ use Netzmacht\LeafletPHP\Definition\Vector\Path; */ class AbstractVectorMapper extends AbstractTypeMapper { + use ServiceContainerTrait; + /** * Class of the model being build. * @@ -59,7 +62,12 @@ class AbstractVectorMapper extends AbstractTypeMapper } if ($definition instanceof HasPopup && $model->addPopup) { - $definition->bindPopup($model->popupContent); + $content = $this + ->getServiceContainer() + ->getFrontendValueFilter() + ->filter($model->popupContent); + + $definition->bindPopup($content); } } } diff --git a/src/Netzmacht/Contao/Leaflet/ServiceContainer.php b/src/Netzmacht/Contao/Leaflet/ServiceContainer.php new file mode 100644 index 0000000..d948ecc --- /dev/null +++ b/src/Netzmacht/Contao/Leaflet/ServiceContainer.php @@ -0,0 +1,61 @@ + + * @copyright 2015 netzmacht creative David Molineus + * @license LGPL 3.0 + * @filesource + * + */ + +namespace Netzmacht\Contao\Leaflet; + +use Netzmacht\Contao\Leaflet\Frontend\ValueFilter; + +/** + * Class ServiceContainer + * + * @package Netzmacht\Contao\Leaflet + */ +class ServiceContainer +{ + /** + * The global service container. + * + * @var \Pimple + */ + private $container; + + /** + * Construct. + * + * @param \Pimple $container The global service container. + */ + public function __construct(\Pimple $container) + { + $this->container = $container; + } + + /** + * Get the value filter service. + * + * @return ValueFilter + */ + public function getFrontendValueFilter() + { + return $this->getService('leaflet.frontend.value-filter'); + } + + /** + * Get a service from the container. + * + * @param string $name The service name. + * + * @return mixed + */ + public function getService($name) + { + return $this->container[$name]; + } +} diff --git a/src/Netzmacht/Contao/Leaflet/ServiceContainerTrait.php b/src/Netzmacht/Contao/Leaflet/ServiceContainerTrait.php new file mode 100644 index 0000000..b789358 --- /dev/null +++ b/src/Netzmacht/Contao/Leaflet/ServiceContainerTrait.php @@ -0,0 +1,30 @@ + + * @copyright 2015 netzmacht creative David Molineus + * @license LGPL 3.0 + * @filesource + * + */ + +namespace Netzmacht\Contao\Leaflet; + +/** + * Class ServiceContainerTrait provides simple access to the service container. + * + * @package Netzmacht\Contao\Leaflet + */ +trait ServiceContainerTrait +{ + /** + * Get the service container. + * + * @return ServiceContainer + */ + protected function getServiceContainer() + { + return $GLOBALS['container']['leaflet.service-container']; + } +}