From 524678f56ec20dd86390214d4f4a19afea490e16 Mon Sep 17 00:00:00 2001 From: David Molineus Date: Fri, 6 Oct 2017 15:50:54 +0200 Subject: [PATCH] Implement assets loading. --- src/Listener/LoadAssetsListener.php | 135 ++++++++++++++++++++++++++++ src/Resources/config/config.yml | 3 + src/Resources/config/listeners.yml | 9 ++ 3 files changed, 147 insertions(+) create mode 100644 src/Listener/LoadAssetsListener.php diff --git a/src/Listener/LoadAssetsListener.php b/src/Listener/LoadAssetsListener.php new file mode 100644 index 0000000..62907a2 --- /dev/null +++ b/src/Listener/LoadAssetsListener.php @@ -0,0 +1,135 @@ + + * @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\Listener; + +use Netzmacht\Contao\Leaflet\ContaoAssets; +use Netzmacht\Contao\Leaflet\Mapper\DefinitionMapper; +use Netzmacht\Contao\Leaflet\Model\IconModel; +use Netzmacht\LeafletPHP\Assets; +use Netzmacht\LeafletPHP\Definition\Type\Icon; +use Netzmacht\LeafletPHP\Definition\Type\ImageIcon; + +/** + * Class LoadAssetsListener. + * + * @package Netzmacht\Contao\Leaflet\Listener + */ +class LoadAssetsListener +{ + /** + * Assets. + * + * @var Assets + */ + private $assets; + + /** + * Definition mapper. + * + * @var DefinitionMapper + */ + private $definitionMapper; + + /** + * Libraries. + * + * @var array + */ + private $libraries; + + /** + * LoadAssetsListener constructor. + * + * @param Assets $assets Assets. + * @param DefinitionMapper $definitionMapper Definition mapper. + * @param array $libraries Libraries. + */ + public function __construct(Assets $assets, DefinitionMapper $definitionMapper, array $libraries) + { + $this->assets = $assets; + $this->definitionMapper = $definitionMapper; + } + + /** + * Handle the get javascript event. + * + * @return void + */ + public function onGetJavascriptEvent(): void + { + $this->assets->addJavascript('assets/leaflet/maps/contao-leaflet.js', ContaoAssets::TYPE_FILE); + + $collection = IconModel::findBy('active', true); + + if ($collection) { + $buffer = ''; + $icons = []; + + foreach ($collection as $model) { + /** @var ImageIcon $icon */ + $icon = $this->definitionMapper->handle($model); + $icons[] = [ + 'id' => $icon->getId(), + 'type' => lcfirst($icon->getType()), + 'options' => $icon->getOptions(), + ]; + + $this->loadIconsLibraries($icon); + } + + if ($icons) { + $buffer = sprintf('L.contao.loadIcons(%s);', json_encode($icons)); + } + + // @codingStandardsIgnoreStart + // TODO: Cache it. + // codingStandardsIgnoreEnd + + $file = new \File('assets/leaflet/js/icons.js'); + $file->write($buffer); + $file->close(); + + $this->assets->addJavascript('assets/leaflet/js/icons.js', ContaoAssets::TYPE_FILE); + } + } + + /** + * Load all libraries for an icon. + * + * @param Icon $icon Icon definition. + * + * @return void + */ + protected function loadIconsLibraries($icon): void + { + foreach ($icon::getRequiredLibraries() as $library) { + if (!isset($this->libraries[$library])) { + continue; + } + + $assets = $this->libraries[$library]; + + if (!empty($assets['css'])) { + list ($source, $type) = (array)$assets['css']; + $this->assets->addStylesheet($source, $type ?: Assets::TYPE_FILE); + } + + if (!empty($assets['javascript'])) { + list ($source, $type) = (array)$assets['javascript']; + $this->assets->addJavascript($source, $type ?: Assets::TYPE_FILE); + } + } + } +} diff --git a/src/Resources/config/config.yml b/src/Resources/config/config.yml index 1624280..3deac1f 100644 --- a/src/Resources/config/config.yml +++ b/src/Resources/config/config.yml @@ -58,3 +58,6 @@ parameters: - 'id' - 'title' - 'alias' + + # Leaflet libraries files + netzmacht.contao_leaflet_maps.libraries: [] diff --git a/src/Resources/config/listeners.yml b/src/Resources/config/listeners.yml index e49b95e..99cd010 100644 --- a/src/Resources/config/listeners.yml +++ b/src/Resources/config/listeners.yml @@ -54,3 +54,12 @@ services: - '%netzmacht.contao_leaflet_maps.feature_model_properties%' tags: - { name: 'kernel.event_subscriber' } + + netzmacht.contao_leaflet_maps.listeners.load_assets: + class: Netzmacht\Contao\Leaflet\Listener\LoadAssetsListener + arguments: + - '@netzmacht.contao_leaflet_maps.map.assets' + - '@netzmacht.contao_leaflet_maps.definition.mapper' + - '%netzmacht.contao_leaflet_maps.libraries%' + tags: + - { name: 'kernel.event_listener', event: 'netzmacht.contao_leaflet.get_javascript' }