From edf212b850eceafc3c5c8bb7e01d67a69a9bf018 Mon Sep 17 00:00:00 2001 From: David Molineus Date: Mon, 9 Oct 2017 15:36:11 +0200 Subject: [PATCH] Rework libraries handling so that libraries don't have to be ported to contao 4.4 only yet. --- .../Assets/LibrariesConfiguration.php | 95 +++++++++++++++++++ src/Listener/LoadAssetsListener.php | 16 ++-- src/Listener/RegisterLibrariesListener.php | 72 ++++++++++++++ src/Resources/config/config.yml | 3 - src/Resources/config/listeners.yml | 9 +- src/Resources/config/services.yml | 5 + src/Resources/contao/config/config.php | 8 ++ src/Resources/public/js/contao-leaflet.js | 1 + 8 files changed, 199 insertions(+), 10 deletions(-) create mode 100644 src/Frontend/Assets/LibrariesConfiguration.php create mode 100644 src/Listener/RegisterLibrariesListener.php create mode 100644 src/Resources/public/js/contao-leaflet.js diff --git a/src/Frontend/Assets/LibrariesConfiguration.php b/src/Frontend/Assets/LibrariesConfiguration.php new file mode 100644 index 0000000..2709f7c --- /dev/null +++ b/src/Frontend/Assets/LibrariesConfiguration.php @@ -0,0 +1,95 @@ + + * @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\Frontend\Assets; + +use Contao\CoreBundle\Framework\ContaoFrameworkInterface as ContaoFramework; +use Traversable; + +/** + * Class LibrariesConfiguration + * + * @package Netzmacht\Contao\Leaflet\Frontend\Assets + */ +class LibrariesConfiguration implements \IteratorAggregate, \ArrayAccess +{ + /** + * @var ContaoFramework + */ + private $framework; + + /** + * LibrariesConfiguration constructor. + * + * @param ContaoFramework $framework + */ + public function __construct(ContaoFramework $framework) + { + $this->framework = $framework; + } + + /** + * {@inheritdoc} + * + * @SuppressWarnings(PHPMD.Superglobals) + */ + public function getIterator() + { + $this->framework->initialize(); + + return new \ArrayIterator($GLOBALS['LEAFLET_LIBRARIES']); + } + + /** + * {@inheritdoc} + * + * @SuppressWarnings(PHPMD.Superglobals) + */ + public function offsetExists($offset) + { + $this->framework->initialize(); + + return isset($GLOBALS['LEAFLET_LIBRARIES'][$offset]); + } + + /** + * {@inheritdoc} + * + * @SuppressWarnings(PHPMD.Superglobals) + */ + public function offsetGet($offset) + { + return $GLOBALS['LEAFLET_LIBRARIES'][$offset]; + } + + /** + * {@inheritdoc} + * + * @SuppressWarnings(PHPMD.Superglobals) + */ + public function offsetSet($offset, $value) + { + $GLOBALS['LEAFLET_LIBRARIES'][$offset] = $value; + } + + /** + * {@inheritdoc} + * + * @SuppressWarnings(PHPMD.Superglobals) + */ + public function offsetUnset($offset) + { + unset($GLOBALS['LEAFLET_LIBRARIES'][$offset]); + } +} diff --git a/src/Listener/LoadAssetsListener.php b/src/Listener/LoadAssetsListener.php index 1192315..58535d6 100644 --- a/src/Listener/LoadAssetsListener.php +++ b/src/Listener/LoadAssetsListener.php @@ -15,6 +15,7 @@ declare(strict_types=1); namespace Netzmacht\Contao\Leaflet\Listener; use Netzmacht\Contao\Leaflet\ContaoAssets; +use Netzmacht\Contao\Leaflet\Frontend\Assets\LibrariesConfiguration; use Netzmacht\Contao\Leaflet\Mapper\DefinitionMapper; use Netzmacht\Contao\Leaflet\Model\IconModel; use Netzmacht\LeafletPHP\Assets; @@ -45,18 +46,18 @@ class LoadAssetsListener /** * Libraries. * - * @var array + * @var LibrariesConfiguration */ private $libraries; /** * LoadAssetsListener constructor. * - * @param Assets $assets Assets. - * @param DefinitionMapper $definitionMapper Definition mapper. - * @param array $libraries Libraries. + * @param Assets $assets Assets. + * @param DefinitionMapper $definitionMapper Definition mapper. + * @param LibrariesConfiguration $libraries Libraries. */ - public function __construct(Assets $assets, DefinitionMapper $definitionMapper, array $libraries) + public function __construct(Assets $assets, DefinitionMapper $definitionMapper, LibrariesConfiguration $libraries) { $this->assets = $assets; $this->definitionMapper = $definitionMapper; @@ -70,7 +71,10 @@ class LoadAssetsListener */ public function onGetJavascriptEvent(): void { - $this->assets->addJavascript('assets/leaflet/maps/contao-leaflet.js', ContaoAssets::TYPE_FILE); + $this->assets->addJavascript( + 'web/bundles/netzmachtcontaoleaflet/js/contao-leaflet.js', + ContaoAssets::TYPE_FILE + ); $collection = IconModel::findBy('active', true); diff --git a/src/Listener/RegisterLibrariesListener.php b/src/Listener/RegisterLibrariesListener.php new file mode 100644 index 0000000..fc5ea75 --- /dev/null +++ b/src/Listener/RegisterLibrariesListener.php @@ -0,0 +1,72 @@ + + * @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\Frontend\Assets\LibrariesConfiguration; +use Netzmacht\LeafletPHP\Assets; +use Netzmacht\LeafletPHP\Leaflet; + +/** + * Class RegisterLibrariesListener. + * + * @package Netzmacht\Contao\Leaflet\Listener + */ +final class RegisterLibrariesListener +{ + /** + * Libraries configuration. + * + * @var LibrariesConfiguration + */ + private $libraries; + + /** + * Leaflet builder. + * + * @var Leaflet + */ + private $leaflet; + + /** + * RegisterLibrariesListener constructor. + * + * @param LibrariesConfiguration $libraries Libraries configuration. + * @param Leaflet $leaflet Leaflet builder. + */ + public function __construct(LibrariesConfiguration $libraries, Leaflet $leaflet) + { + $this->libraries = $libraries; + $this->leaflet = $leaflet; + } + + /** + * Handle the on initialize system hook. + * + * @return void + */ + public function onInitializeSystem(): void + { + foreach ($this->libraries as $name => $assets) { + if (!empty($assets['css'])) { + list ($source, $type) = (array) $assets['css']; + $this->leaflet->registerStylesheet($name, $source, $type ?: Assets::TYPE_FILE); + } + if (!empty($assets['javascript'])) { + list ($source, $type) = (array) $assets['javascript']; + $this->leaflet->registerJavascript($name, $source, $type ?: Assets::TYPE_FILE); + } + } + } +} diff --git a/src/Resources/config/config.yml b/src/Resources/config/config.yml index 3deac1f..1624280 100644 --- a/src/Resources/config/config.yml +++ b/src/Resources/config/config.yml @@ -58,6 +58,3 @@ 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 cb909d6..9cf1389 100644 --- a/src/Resources/config/listeners.yml +++ b/src/Resources/config/listeners.yml @@ -65,6 +65,13 @@ services: arguments: - '@netzmacht.contao_leaflet_maps.map.assets' - '@netzmacht.contao_leaflet_maps.definition.mapper' - - '%netzmacht.contao_leaflet_maps.libraries%' + - '@netzmacht.contao_leaflet_maps.libraries' tags: - { name: 'kernel.event_listener', event: 'netzmacht.contao_leaflet.get_javascript', method: 'onGetJavascriptEvent' } + + netzmacht.contao_leaflet_maps.listeners.register_libraries: + class: Netzmacht\Contao\Leaflet\Listener\RegisterLibrariesListener + arguments: + - '@netzmacht.contao_leaflet_maps.libraries' + - '@netzmacht.contao_leaflet_maps.definition.builder' + diff --git a/src/Resources/config/services.yml b/src/Resources/config/services.yml index c655129..6ddfc8b 100644 --- a/src/Resources/config/services.yml +++ b/src/Resources/config/services.yml @@ -17,6 +17,11 @@ services: - [] # TODO: Rework filter handling. - '%kernel.debug%' + netzmacht.contao_leaflet_maps.libraries: + class: Netzmacht\Contao\Leaflet\Frontend\Assets\LibrariesConfiguration + arguments: + - '@contao.framework' + netzmacht.contao_leaflet_maps.cache: alias: 'netzmacht.contao_leaflet_maps.cache.default' diff --git a/src/Resources/contao/config/config.php b/src/Resources/contao/config/config.php index 9dda154..2e00219 100644 --- a/src/Resources/contao/config/config.php +++ b/src/Resources/contao/config/config.php @@ -84,3 +84,11 @@ $GLOBALS['TL_MODELS']['tl_leaflet_marker'] = \Netzmacht\Contao\Leaflet\Model\Ma $GLOBALS['TL_MODELS']['tl_leaflet_popup'] = \Netzmacht\Contao\Leaflet\Model\PopupModel::class; $GLOBALS['TL_MODELS']['tl_leaflet_style'] = \Netzmacht\Contao\Leaflet\Model\StyleModel::class; $GLOBALS['TL_MODELS']['tl_leaflet_vector'] = \Netzmacht\Contao\Leaflet\Model\VectorModel::class; + +/* + * Hooks. + */ +$GLOBALS['TL_HOOKS']['initializeSystem'][] = [ + 'netzmacht.contao_leaflet_maps.listeners.register_libraries', + 'onInitializeSystem' +]; diff --git a/src/Resources/public/js/contao-leaflet.js b/src/Resources/public/js/contao-leaflet.js new file mode 100644 index 0000000..9414982 --- /dev/null +++ b/src/Resources/public/js/contao-leaflet.js @@ -0,0 +1 @@ +L.Contao=L.Class.extend({includes:L.Mixin.Events,statics:{ATTRIBUTION:' | netzmacht'},maps:{},icons:{},initialize:function(){L.Icon.Default.imagePath="assets/leaflet/libs/leaflet/images/",this.setGeoJsonListeners(L.GeoJSON)},addMap:function(t,o){return this.maps[t]=o,this.fire("map:added",{id:t,map:o}),this},getMap:function(t){return"undefined"==typeof this.maps[t]?null:this.maps[t]},addIcon:function(t,o){return this.icons[t]=o,this.fire("icon:added",{id:t,icon:o}),this},loadIcons:function(t){for(var o=0;o=200&&t<300||304===t}function i(){void 0===a.status||n(a.status)?o.call(a,null,a):o.call(a,a,null)}var s=!1;if("undefined"==typeof window.XMLHttpRequest)return o(Error("Browser not supported"));if("undefined"==typeof e){var r=t.match(/^\s*https?:\/\/[^\/]*/);e=r&&r[0]!==location.protocol+"//"+location.hostname+(location.port?":"+location.port:"")}var a=new window.XMLHttpRequest;if(e&&!("withCredentials"in a)){a=new window.XDomainRequest;var p=o;o=function(){if(s)p.apply(this,arguments);else{var t=this,o=arguments;setTimeout(function(){p.apply(t,o)},0)}}}return"onload"in a?a.onload=i:a.onreadystatechange=function(){4===a.readyState&&i()},a.onerror=function(t){o.call(this,t||!0,null),o=function(){}},a.onprogress=function(){},a.ontimeout=function(t){o.call(this,t,null),o=function(){}},a.onabort=function(t){o.call(this,t,null),o=function(){}},a.open("GET",t,!0),a.send(null),s=!0,a}}); \ No newline at end of file