diff --git a/module/config/config.php b/module/config/config.php index 6fd26d2..3f3d6fc 100644 --- a/module/config/config.php +++ b/module/config/config.php @@ -104,39 +104,39 @@ $GLOBALS['LEAFLET_MAPPERS'][] = 'Netzmacht\Contao\Leaflet\Mapper\Control\Loading $GLOBALS['LEAFLET_MAPPERS'][] = 'Netzmacht\Contao\Leaflet\Mapper\Control\FullscreenControlMapper'; // Vector mappers. -$GLOBALS['LEAFLET_MAPPERS'][] = function (\Interop\Container\ContainerInterface $container) { +$GLOBALS['LEAFLET_MAPPERS'][] = function () { return new Netzmacht\Contao\Leaflet\Mapper\Vector\PolylineMapper( - $container->get(\Netzmacht\Contao\Leaflet\DependencyInjection\LeafletServices::FRONTEND_VALUE_FILTER) + $GLOBALS['container'][\Netzmacht\Contao\Leaflet\DependencyInjection\LeafletServices::FRONTEND_VALUE_FILTER] ); }; -$GLOBALS['LEAFLET_MAPPERS'][] = function (\Interop\Container\ContainerInterface $container) { +$GLOBALS['LEAFLET_MAPPERS'][] = function () { return new Netzmacht\Contao\Leaflet\Mapper\Vector\MultiPolylineMapper( - $container->get(\Netzmacht\Contao\Leaflet\DependencyInjection\LeafletServices::FRONTEND_VALUE_FILTER) + $GLOBALS['container'][\Netzmacht\Contao\Leaflet\DependencyInjection\LeafletServices::FRONTEND_VALUE_FILTER] ); }; -$GLOBALS['LEAFLET_MAPPERS'][] = function (\Interop\Container\ContainerInterface $container) { +$GLOBALS['LEAFLET_MAPPERS'][] = function () { return new Netzmacht\Contao\Leaflet\Mapper\Vector\PolygonMapper( - $container->get(\Netzmacht\Contao\Leaflet\DependencyInjection\LeafletServices::FRONTEND_VALUE_FILTER) + $GLOBALS['container'][\Netzmacht\Contao\Leaflet\DependencyInjection\LeafletServices::FRONTEND_VALUE_FILTER] ); }; -$GLOBALS['LEAFLET_MAPPERS'][] = function (\Interop\Container\ContainerInterface $container) { +$GLOBALS['LEAFLET_MAPPERS'][] = function () { return new Netzmacht\Contao\Leaflet\Mapper\Vector\MultiPolygonMapper( - $container->get(\Netzmacht\Contao\Leaflet\DependencyInjection\LeafletServices::FRONTEND_VALUE_FILTER) + $GLOBALS['container'][\Netzmacht\Contao\Leaflet\DependencyInjection\LeafletServices::FRONTEND_VALUE_FILTER] ); }; -$GLOBALS['LEAFLET_MAPPERS'][] = function (\Interop\Container\ContainerInterface $container) { +$GLOBALS['LEAFLET_MAPPERS'][] = function () { return new Netzmacht\Contao\Leaflet\Mapper\Vector\CircleMapper( - $container->get(\Netzmacht\Contao\Leaflet\DependencyInjection\LeafletServices::FRONTEND_VALUE_FILTER) + $GLOBALS['container'][\Netzmacht\Contao\Leaflet\DependencyInjection\LeafletServices::FRONTEND_VALUE_FILTER] ); }; -$GLOBALS['LEAFLET_MAPPERS'][] = function (\Interop\Container\ContainerInterface $container) { +$GLOBALS['LEAFLET_MAPPERS'][] = function () { return new Netzmacht\Contao\Leaflet\Mapper\Vector\CircleMarkerMapper( - $container->get(\Netzmacht\Contao\Leaflet\DependencyInjection\LeafletServices::FRONTEND_VALUE_FILTER) + $GLOBALS['container'][\Netzmacht\Contao\Leaflet\DependencyInjection\LeafletServices::FRONTEND_VALUE_FILTER] ); }; -$GLOBALS['LEAFLET_MAPPERS'][] = function (\Interop\Container\ContainerInterface $container) { +$GLOBALS['LEAFLET_MAPPERS'][] = function () { return new Netzmacht\Contao\Leaflet\Mapper\Vector\RectangleMapper( - $container->get(\Netzmacht\Contao\Leaflet\DependencyInjection\LeafletServices::FRONTEND_VALUE_FILTER) + $GLOBALS['container'][\Netzmacht\Contao\Leaflet\DependencyInjection\LeafletServices::FRONTEND_VALUE_FILTER] ); }; @@ -145,9 +145,9 @@ $GLOBALS['LEAFLET_MAPPERS'][] = 'Netzmacht\Contao\Leaflet\Mapper\UI\PopupMapper' $GLOBALS['LEAFLET_MAPPERS'][] = 'Netzmacht\Contao\Leaflet\Mapper\Type\ImageIconMapper'; $GLOBALS['LEAFLET_MAPPERS'][] = 'Netzmacht\Contao\Leaflet\Mapper\Type\DivIconMapper'; $GLOBALS['LEAFLET_MAPPERS'][] = 'Netzmacht\Contao\Leaflet\Mapper\Style\FixedStyleMapper'; -$GLOBALS['LEAFLET_MAPPERS'][] = function (\Interop\Container\ContainerInterface $container) { +$GLOBALS['LEAFLET_MAPPERS'][] = function () { return new \Netzmacht\Contao\Leaflet\Mapper\UI\MarkerMapper( - $container->get(\Netzmacht\Contao\Leaflet\DependencyInjection\LeafletServices::FRONTEND_VALUE_FILTER) + $GLOBALS['container'][\Netzmacht\Contao\Leaflet\DependencyInjection\LeafletServices::FRONTEND_VALUE_FILTER] ); }; diff --git a/module/config/event_subscribers.php b/module/config/event_subscribers.php index e432193..5a9546c 100644 --- a/module/config/event_subscribers.php +++ b/module/config/event_subscribers.php @@ -9,7 +9,7 @@ * */ return array( - 'Netzmacht\Contao\Leaflet\Subscriber\BootSubscriber', + $GLOBALS['container']['leaflet.boot.subscriber'], 'Netzmacht\Contao\Leaflet\Subscriber\HashSubscriber', 'Netzmacht\Contao\Leaflet\Subscriber\GeoJsonSubscriber', ); diff --git a/module/config/services.php b/module/config/services.php index a04a4a5..cc0dc93 100644 --- a/module/config/services.php +++ b/module/config/services.php @@ -23,6 +23,7 @@ use Netzmacht\Contao\Leaflet\Frontend\MapModule; use Netzmacht\Contao\Leaflet\Frontend\ValueFilter; use Netzmacht\Contao\Leaflet\Mapper\DefinitionMapper; use Netzmacht\Contao\Leaflet\MapProvider; +use Netzmacht\Contao\Leaflet\Subscriber\BootSubscriber; use Netzmacht\Contao\Toolkit\Data\Alias\Filter\ExistingAliasFilter; use Netzmacht\Contao\Toolkit\Data\Alias\Filter\SlugifyFilter; use Netzmacht\Contao\Toolkit\Data\Alias\Filter\SuffixFilter; @@ -69,6 +70,15 @@ $container[LeafletServices::BOOT] = $container->share(function ($container) { return new Boot($container[Services::EVENT_DISPATCHER]); }); +$container['leaflet.boot.subscriber'] = $container->share(function ($container) { + return new BootSubscriber( + $container[Services::ASSETS_MANAGER], + $GLOBALS['LEAFLET_MAPPERS'], + $GLOBALS['LEAFLET_ENCODERS'], + $GLOBALS['LEAFLET_LIBRARIES'] + ); +}); + /* * The definition mapper. diff --git a/src/Netzmacht/Contao/Leaflet/Subscriber/BootSubscriber.php b/src/Netzmacht/Contao/Leaflet/Subscriber/BootSubscriber.php index fd192c5..a4ddbbd 100644 --- a/src/Netzmacht/Contao/Leaflet/Subscriber/BootSubscriber.php +++ b/src/Netzmacht/Contao/Leaflet/Subscriber/BootSubscriber.php @@ -23,6 +23,7 @@ use Netzmacht\Contao\Leaflet\Mapper\Mapper; use Netzmacht\Contao\Leaflet\Model\IconModel; use Netzmacht\Contao\Toolkit\Boot\Event\InitializeSystemEvent; use Netzmacht\Contao\Toolkit\DependencyInjection\Services; +use Netzmacht\Contao\Toolkit\View\Assets\AssetsManager; use Netzmacht\LeafletPHP\Assets; use Netzmacht\LeafletPHP\Definition\Type\ImageIcon; use Symfony\Component\EventDispatcher\EventSubscriberInterface; @@ -34,6 +35,61 @@ use Symfony\Component\EventDispatcher\EventSubscriberInterface; */ class BootSubscriber implements EventSubscriberInterface { + /** + * Leaflet mapper configuration. + * + * @var array + */ + private $mappers; + + /** + * Leaflet encoder configuration. + * + * @var array + */ + private $encoders; + + /** + * Leaflet libraries configuration. + * + * @var array + */ + private $libraries; + + /** + * Assets manager. + * + * @var AssetsManager + */ + private $assetsManager; + + /** + * Definition mapper. + * + * @var DefinitionMapper + */ + private $definitionMapper; + + /** + * BootSubscriber constructor. + * + * @param AssetsManager $assetsManager Assets manager. + * @param array $mappers Leaflet mapper configuration. + * @param array $encoders Leaflet encoder configuration. + * @param array $libraries Leaflet libraries configuration. + */ + public function __construct( + AssetsManager $assetsManager, + array $mappers, + array $encoders, + array $libraries + ) { + $this->assetsManager = $assetsManager; + $this->mappers = $mappers; + $this->encoders = $encoders; + $this->libraries = $libraries; + } + /** * {@inheritdoc} */ @@ -71,14 +127,13 @@ class BootSubscriber implements EventSubscriberInterface * @param InitializeDefinitionMapperEvent $event The subscribed event. * * @return void - * - * @SuppressWarnings(PHPMD.Superglobals) */ public function initializeDefinitionMapper(InitializeDefinitionMapperEvent $event) { - $mapper = $event->getDefinitionMapper(); + $mapper = $event->getDefinitionMapper(); + $this->definitionMapper = $mapper; - foreach ($GLOBALS['LEAFLET_MAPPERS'] as $className) { + foreach ($this->mappers as $className) { if (is_array($className)) { $mapper->register($this->createMapper($className[0]), $className[1]); } else { @@ -93,15 +148,13 @@ class BootSubscriber implements EventSubscriberInterface * @param InitializeEventDispatcherEvent $event The subscribed event. * * @return void - * - * @SuppressWarnings(PHPMD.Superglobals) */ public function initializeEventDispatcher(InitializeEventDispatcherEvent $event) { $dispatcher = $event->getEventDispatcher(); $initializer = new EventDispatcherInitializer(); - $initializer->addSubscribers($dispatcher, $GLOBALS['LEAFLET_ENCODERS']); + $initializer->addSubscribers($dispatcher, $this->encoders); } /** @@ -110,14 +163,12 @@ class BootSubscriber implements EventSubscriberInterface * @param InitializeLeafletBuilderEvent $event The subscribed event. * * @return void - * - * @SuppressWarnings(PHPMD.Superglobals) */ public function initializeLeafletBuilder(InitializeLeafletBuilderEvent $event) { $builder = $event->getBuilder(); - foreach ($GLOBALS['LEAFLET_LIBRARIES'] as $name => $assets) { + foreach ($this->libraries as $name => $assets) { if (!empty($assets['css'])) { list ($source, $type) = (array) $assets['css']; $builder->registerStylesheet($name, $source, $type ?: Assets::TYPE_FILE); @@ -134,34 +185,32 @@ class BootSubscriber implements EventSubscriberInterface * Load Contao leaflet assets. * * @return void - * - * @SuppressWarnings(PHPMD.Superglobals) */ public function loadAssets() { - $GLOBALS['TL_JAVASCRIPT'][] = 'assets/leaflet/maps/contao-leaflet.js' . $this->staticFlag(); + $this->assetsManager->addJavascript('assets/leaflet/maps/contao-leaflet.js'); } /** * Load icons. * * @return void - * - * @SuppressWarnings(PHPMD.Superglobals) */ public function loadIcons() { + if (!$this->definitionMapper) { + return; + } + $collection = IconModel::findBy('active', true); if ($collection) { - /** @var DefinitionMapper $mapper */ - $mapper = $GLOBALS['container'][LeafletServices::DEFINITION_MAPPER]; $buffer = ''; $icons = array(); foreach ($collection as $model) { /** @var ImageIcon $icon */ - $icon = $mapper->handle($model); + $icon = $this->definitionMapper->handle($model); $icons[] = array( 'id' => $icon->getId(), 'type' => lcfirst($icon->getType()), @@ -180,39 +229,21 @@ class BootSubscriber implements EventSubscriberInterface // @codingStandardsIgnoreStart // TODO: Cache it. // codingStandardsIgnoreEnd - $GLOBALS['TL_JAVASCRIPT'][] = 'assets/leaflet/js/icons.js' . $this->staticFlag(); + $this->assetsManager->addJavascript('assets/leaflet/js/icons.js'); } } - /** - * Set the static flag. - * - * @return string - */ - private function staticFlag() - { - if (\Config::get('debugMode') || TL_MODE !== 'FE') { - return ''; - } - - return '|static'; - } - /** * Create a new mapper. * * @param mixed $mapper The mapper class or callable factory. * * @return Mapper - * - * @SuppressWarnings(PHPMD.Superglobals) */ private function createMapper($mapper) { if (is_callable($mapper)) { - $container = $GLOBALS['container'][Services::CONTAINER]; - - return $mapper($container); + return $mapper(); } return new $mapper;