From e8edeb8021dbfb7a689855cdda4478d1154a4d9c Mon Sep 17 00:00:00 2001 From: David Molineus Date: Fri, 6 Oct 2017 15:36:04 +0200 Subject: [PATCH] Register libraries. --- .../Pass/RegisterLibrariesPass.php | 56 +++++++++++++++++++ src/NetzmachtContaoLeafletBundle.php | 3 +- 2 files changed, 58 insertions(+), 1 deletion(-) create mode 100644 src/DependencyInjection/Pass/RegisterLibrariesPass.php diff --git a/src/DependencyInjection/Pass/RegisterLibrariesPass.php b/src/DependencyInjection/Pass/RegisterLibrariesPass.php new file mode 100644 index 0000000..f8e275f --- /dev/null +++ b/src/DependencyInjection/Pass/RegisterLibrariesPass.php @@ -0,0 +1,56 @@ + + * @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\DependencyInjection\Pass; + +use Netzmacht\LeafletPHP\Assets; +use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface; +use Symfony\Component\DependencyInjection\ContainerBuilder; + +/** + * Class RegisterLibrariesPass. + * + * @package Netzmacht\Contao\Leaflet\DependencyInjection\Pass + */ +class RegisterLibrariesPass implements CompilerPassInterface +{ + /** + * {@inheritDoc} + */ + public function process(ContainerBuilder $container) + { + if (!$container->hasDefinition('netzmacht.contao_leaflet_maps.definition.builder')) { + return; + } + + if (!$container->hasParameter('netzmacht.contao_leaflet_maps.libraries')) { + return; + } + + $definition = $container->getDefinition('netzmacht.contao_leaflet_maps.definition.builder'); + $libraries = $container->getParameter('netzmacht.contao_leaflet_maps.libraries'); + + foreach ($libraries as $name => $assets) { + if (!empty($assets['css'])) { + list ($source, $type) = (array) $assets['css']; + $definition->addMethodCall('registerStylesheet', [$name, $source, $type ?: Assets::TYPE_FILE]); + } + + if (!empty($assets['javascript'])) { + list ($source, $type) = (array) $assets['javascript']; + $definition->addMethodCall('registerJavascript', [$name, $source, $type ?: Assets::TYPE_FILE]); + } + } + } +} diff --git a/src/NetzmachtContaoLeafletBundle.php b/src/NetzmachtContaoLeafletBundle.php index 77e1336..b7fcd34 100644 --- a/src/NetzmachtContaoLeafletBundle.php +++ b/src/NetzmachtContaoLeafletBundle.php @@ -14,6 +14,7 @@ declare(strict_types=1); namespace Netzmacht\Contao\Leaflet; +use Netzmacht\Contao\Leaflet\DependencyInjection\Pass\RegisterLibrariesPass; use Netzmacht\Contao\Leaflet\DependencyInjection\Pass\RegisterDefinitionMapperPass; use Netzmacht\Contao\Leaflet\DependencyInjection\Pass\RegisterEncodersPass; use Netzmacht\Contao\Toolkit\DependencyInjection\CompilerPass\AddTaggedServicesAsArgumentCompilerPass; @@ -34,7 +35,7 @@ class NetzmachtContaoLeafletBundle extends Bundle { $container->addCompilerPass(new RegisterDefinitionMapperPass()); $container->addCompilerPass(new RegisterEncodersPass()); - + $container->addCompilerPass(new RegisterLibrariesPass()); $container->addCompilerPass( new AddTaggedServicesAsArgumentCompilerPass( 'netzmacht.contao_leaflet_maps.layer_label_renderer',