From f172c42426ba995efe543cdb74814378e33fe164 Mon Sep 17 00:00:00 2001 From: David Molineus Date: Fri, 6 Oct 2017 11:47:54 +0200 Subject: [PATCH] Work on service definitions. --- src/Alias/DefaultAliasGeneratorFactory.php | 66 +++++++++++++++++++ src/Alias/ParentAliasGeneratorFactory.php | 66 +++++++++++++++++++ src/Resources/config/encoders.yml | 3 + src/Resources/config/services.yml | 73 +++++++++++++++++++++- 4 files changed, 206 insertions(+), 2 deletions(-) create mode 100644 src/Alias/DefaultAliasGeneratorFactory.php create mode 100644 src/Alias/ParentAliasGeneratorFactory.php diff --git a/src/Alias/DefaultAliasGeneratorFactory.php b/src/Alias/DefaultAliasGeneratorFactory.php new file mode 100644 index 0000000..5c486c8 --- /dev/null +++ b/src/Alias/DefaultAliasGeneratorFactory.php @@ -0,0 +1,66 @@ + + * @copyright 2016-2017 netzmacht David Molineus. All rights reserved. + * @license LGPL-3.0 https://github.com/netzmacht/contao-leaflet-maps/blob/master/LICENSE + * @filesource + */ + +declare(strict_types=1); + +namespace Netzmacht\Contao\Leaflet\Alias; + +use Doctrine\DBAL\Connection; +use Netzmacht\Contao\Toolkit\Data\Alias\AliasGenerator; +use Netzmacht\Contao\Toolkit\Data\Alias\Factory\AliasGeneratorFactory; +use Netzmacht\Contao\Toolkit\Data\Alias\Filter\ExistingAliasFilter; +use Netzmacht\Contao\Toolkit\Data\Alias\Filter\SlugifyFilter; +use Netzmacht\Contao\Toolkit\Data\Alias\Filter\SuffixFilter; +use Netzmacht\Contao\Toolkit\Data\Alias\FilterBasedAliasGenerator; +use Netzmacht\Contao\Toolkit\Data\Alias\Validator\UniqueDatabaseValueValidator; + +/** + * Class DefaultAliasGeneratorFactory. + * + * @package Netzmacht\Contao\Leaflet\Alias + */ +class DefaultAliasGeneratorFactory implements AliasGeneratorFactory +{ + /** + * Database connection. + * + * @var Connection + */ + private $connection; + + /** + * DefaultAliasGeneratorFactory constructor. + * + * @param Connection $connection Database connection. + */ + public function __construct(Connection $connection) + { + $this->connection = $connection; + } + + /** + * {@inheritDoc} + */ + public function create(string $dataContainerName, string $aliasField, array $fields): AliasGenerator + { + $filters = [ + new ExistingAliasFilter(), + new SlugifyFilter($fields), + new DefaultAliasFilter($dataContainerName), + new SuffixFilter(), + ]; + + $validator = new UniqueDatabaseValueValidator($this->connection, $dataContainerName, $aliasField); + + return new FilterBasedAliasGenerator($filters, $validator, $dataContainerName, $aliasField, '_'); + } +} diff --git a/src/Alias/ParentAliasGeneratorFactory.php b/src/Alias/ParentAliasGeneratorFactory.php new file mode 100644 index 0000000..5a2a8b6 --- /dev/null +++ b/src/Alias/ParentAliasGeneratorFactory.php @@ -0,0 +1,66 @@ + + * @copyright 2016-2017 netzmacht David Molineus. All rights reserved. + * @license LGPL-3.0 https://github.com/netzmacht/contao-leaflet-maps/blob/master/LICENSE + * @filesource + */ + +declare(strict_types=1); + +namespace Netzmacht\Contao\Leaflet\Alias; + +use Doctrine\DBAL\Connection; +use Netzmacht\Contao\Toolkit\Data\Alias\AliasGenerator; +use Netzmacht\Contao\Toolkit\Data\Alias\Factory\AliasGeneratorFactory; +use Netzmacht\Contao\Toolkit\Data\Alias\Filter\ExistingAliasFilter; +use Netzmacht\Contao\Toolkit\Data\Alias\Filter\SlugifyFilter; +use Netzmacht\Contao\Toolkit\Data\Alias\Filter\SuffixFilter; +use Netzmacht\Contao\Toolkit\Data\Alias\FilterBasedAliasGenerator; +use Netzmacht\Contao\Toolkit\Data\Alias\Validator\UniqueDatabaseValueValidator; + +/** + * Alias generator validating against the parent id (pid). + * + * @package Netzmacht\Contao\Leaflet\Alias + */ +class ParentAliasGeneratorFactory implements AliasGeneratorFactory +{ + /** + * Database connection. + * + * @var Connection + */ + private $connection; + + /** + * DefaultAliasGeneratorFactory constructor. + * + * @param Connection $connection Database connection. + */ + public function __construct(Connection $connection) + { + $this->connection = $connection; + } + + /** + * {@inheritDoc} + */ + public function create(string $dataContainerName, string $aliasField, array $fields): AliasGenerator + { + $filters = [ + new ExistingAliasFilter(), + new SlugifyFilter($fields), + new DefaultAliasFilter($dataContainerName), + new SuffixFilter(), + ]; + + $validator = new UniqueDatabaseValueValidator($this->connection, $dataContainerName, $aliasField, ['pid']); + + return new FilterBasedAliasGenerator($filters, $validator, $dataContainerName, $aliasField, '_'); + } +} diff --git a/src/Resources/config/encoders.yml b/src/Resources/config/encoders.yml index 5d13566..1d9c81f 100644 --- a/src/Resources/config/encoders.yml +++ b/src/Resources/config/encoders.yml @@ -3,6 +3,9 @@ # The encoders transforms the definitions into javascript. The encoders has to be an implementation of the # EventSubscriberInterface of the event dispatcher. services: + _defaults: + public: false + netzmacht.contao_leaflet_maps.encoder.map: class: Netzmacht\LeafletPHP\Encoder\MapEncoder tags: diff --git a/src/Resources/config/services.yml b/src/Resources/config/services.yml index e0f2c45..66f1e17 100644 --- a/src/Resources/config/services.yml +++ b/src/Resources/config/services.yml @@ -1,6 +1,75 @@ +parameters: + netzmacht.contao_leaflet_maps.cache_dir: '%kernel.cache_dir%/leaflet' + + # JSON_UNESCAPED_SLASHES ^ Netzmacht\JavascriptBuilder\Flags::BUILD_STACK + netzmacht.contao_leaflet_maps.encoding_flags: 16448 + services: + netzmacht.contao_leaflet_maps.map.provider: + class: Netzmacht\Contao\Leaflet\MapProvider + arguments: + - '@netzmacht.contao_leaflet_maps.definition.mapper' + - '@netzmacht.contao_leaflet_maps.definition.builder' + - '@event_dispatcher' + - '@netzmacht.contao_toolkit.contao.input' + - '@netzmacht.contao_leaflet_maps.map.assets' + - '@netzmacht.contao_leaflet_maps.cache' + - '' # TODO: Rework filter handling. + - '%kernel.debug%' + + netzmacht.contao_leaflet_maps.cache: + alias: 'netzmacht.contao_leaflet_maps.cache.default' + + netzmacht.contao_leaflet_maps.cache.default: + class: Doctrine\Common\Cache\FilesystemCache + arguments: + - '%netzmacht.contao_leaflet_maps.cache_dir%' + + netzmacht.contao_leaflet_maps.cache.debug: + class: Doctrine\Common\Cache\ArrayCache + netzmacht.contao_leaflet_maps.frontend.value_filter: - class: + class: Netzmacht\Contao\Leaflet\Frontend\ValueFilter + arguments: + - '@netzmacht.contao_toolkit.insert_tag.replacer' netzmacht.contao_leaflet_maps.map.assets: - class: + class: Netzmacht\Contao\Leaflet\ContaoAssets + arguments: + - '@netzmacht.contao_toolkit.assets_manager' + + netzmacht.contao_leaflet_maps.definition.builder: + class: Netzmacht\LeafletPHP\Leaflet + arguments: + - '@netzmacht.contao_leaflet_maps.definition.builder.javascript_builder' + - '@netzmacht.contao_leaflet_maps.definition.builder.event_dispatcher' + - [] + - '%netzmacht.contao_leaflet_maps.encoding_flags%' + + netzmacht.contao_leaflet_maps.definition.builder.javascript_builder: + class: Netzmacht\JavascriptBuilder\Builder + arguments: + - '@netzmacht.contao_leaflet_maps.definition.encoder_factory' + + netzmacht.contao_leaflet_maps.definition.builder.event_dispatcher: + class: Symfony\Component\EventDispatcher\EventDispatcher + + netzmacht.contao_leaflet_maps.definition.mapper: + class: Netzmacht\Contao\Leaflet\Mapper\DefinitionMapper + arguments: + - '@event_dispatcher' + + netzmacht.contao_leaflet_maps.definition.encoder_factory: + class: Netzmacht\Contao\Leaflet\Encoder\EncoderFactory + arguments: + - '@netzmacht.contao_leaflet_maps.definition.builder.event_dispatcher' + + netzmacht.contao_leaflet_maps.definition.alias_generator.factory_default: + class: Netzmacht\Contao\Leaflet\Alias\DefaultAliasGeneratorFactory + arguments: + - '@database_connection' + + netzmacht.contao_leaflet_maps.definition.alias_generator.factory_parent: + class: Netzmacht\Contao\Leaflet\Alias\ParentAliasGeneratorFactory + arguments: + - '@database_connection'