Refactor boot subscriber.

This commit is contained in:
David Molineus
2016-10-06 08:53:23 +02:00
parent 2c9dd0ad48
commit b1cfbb3b04
4 changed files with 96 additions and 55 deletions

View File

@@ -104,39 +104,39 @@ $GLOBALS['LEAFLET_MAPPERS'][] = 'Netzmacht\Contao\Leaflet\Mapper\Control\Loading
$GLOBALS['LEAFLET_MAPPERS'][] = 'Netzmacht\Contao\Leaflet\Mapper\Control\FullscreenControlMapper'; $GLOBALS['LEAFLET_MAPPERS'][] = 'Netzmacht\Contao\Leaflet\Mapper\Control\FullscreenControlMapper';
// Vector mappers. // Vector mappers.
$GLOBALS['LEAFLET_MAPPERS'][] = function (\Interop\Container\ContainerInterface $container) { $GLOBALS['LEAFLET_MAPPERS'][] = function () {
return new Netzmacht\Contao\Leaflet\Mapper\Vector\PolylineMapper( 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( 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( 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( 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( 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( 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( 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\ImageIconMapper';
$GLOBALS['LEAFLET_MAPPERS'][] = 'Netzmacht\Contao\Leaflet\Mapper\Type\DivIconMapper'; $GLOBALS['LEAFLET_MAPPERS'][] = 'Netzmacht\Contao\Leaflet\Mapper\Type\DivIconMapper';
$GLOBALS['LEAFLET_MAPPERS'][] = 'Netzmacht\Contao\Leaflet\Mapper\Style\FixedStyleMapper'; $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( 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]
); );
}; };

View File

@@ -9,7 +9,7 @@
* *
*/ */
return array( return array(
'Netzmacht\Contao\Leaflet\Subscriber\BootSubscriber', $GLOBALS['container']['leaflet.boot.subscriber'],
'Netzmacht\Contao\Leaflet\Subscriber\HashSubscriber', 'Netzmacht\Contao\Leaflet\Subscriber\HashSubscriber',
'Netzmacht\Contao\Leaflet\Subscriber\GeoJsonSubscriber', 'Netzmacht\Contao\Leaflet\Subscriber\GeoJsonSubscriber',
); );

View File

@@ -23,6 +23,7 @@ use Netzmacht\Contao\Leaflet\Frontend\MapModule;
use Netzmacht\Contao\Leaflet\Frontend\ValueFilter; use Netzmacht\Contao\Leaflet\Frontend\ValueFilter;
use Netzmacht\Contao\Leaflet\Mapper\DefinitionMapper; use Netzmacht\Contao\Leaflet\Mapper\DefinitionMapper;
use Netzmacht\Contao\Leaflet\MapProvider; 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\ExistingAliasFilter;
use Netzmacht\Contao\Toolkit\Data\Alias\Filter\SlugifyFilter; use Netzmacht\Contao\Toolkit\Data\Alias\Filter\SlugifyFilter;
use Netzmacht\Contao\Toolkit\Data\Alias\Filter\SuffixFilter; 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]); 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. * The definition mapper.

View File

@@ -23,6 +23,7 @@ use Netzmacht\Contao\Leaflet\Mapper\Mapper;
use Netzmacht\Contao\Leaflet\Model\IconModel; use Netzmacht\Contao\Leaflet\Model\IconModel;
use Netzmacht\Contao\Toolkit\Boot\Event\InitializeSystemEvent; use Netzmacht\Contao\Toolkit\Boot\Event\InitializeSystemEvent;
use Netzmacht\Contao\Toolkit\DependencyInjection\Services; use Netzmacht\Contao\Toolkit\DependencyInjection\Services;
use Netzmacht\Contao\Toolkit\View\Assets\AssetsManager;
use Netzmacht\LeafletPHP\Assets; use Netzmacht\LeafletPHP\Assets;
use Netzmacht\LeafletPHP\Definition\Type\ImageIcon; use Netzmacht\LeafletPHP\Definition\Type\ImageIcon;
use Symfony\Component\EventDispatcher\EventSubscriberInterface; use Symfony\Component\EventDispatcher\EventSubscriberInterface;
@@ -34,6 +35,61 @@ use Symfony\Component\EventDispatcher\EventSubscriberInterface;
*/ */
class BootSubscriber implements 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} * {@inheritdoc}
*/ */
@@ -71,14 +127,13 @@ class BootSubscriber implements EventSubscriberInterface
* @param InitializeDefinitionMapperEvent $event The subscribed event. * @param InitializeDefinitionMapperEvent $event The subscribed event.
* *
* @return void * @return void
*
* @SuppressWarnings(PHPMD.Superglobals)
*/ */
public function initializeDefinitionMapper(InitializeDefinitionMapperEvent $event) 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)) { if (is_array($className)) {
$mapper->register($this->createMapper($className[0]), $className[1]); $mapper->register($this->createMapper($className[0]), $className[1]);
} else { } else {
@@ -93,15 +148,13 @@ class BootSubscriber implements EventSubscriberInterface
* @param InitializeEventDispatcherEvent $event The subscribed event. * @param InitializeEventDispatcherEvent $event The subscribed event.
* *
* @return void * @return void
*
* @SuppressWarnings(PHPMD.Superglobals)
*/ */
public function initializeEventDispatcher(InitializeEventDispatcherEvent $event) public function initializeEventDispatcher(InitializeEventDispatcherEvent $event)
{ {
$dispatcher = $event->getEventDispatcher(); $dispatcher = $event->getEventDispatcher();
$initializer = new EventDispatcherInitializer(); $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. * @param InitializeLeafletBuilderEvent $event The subscribed event.
* *
* @return void * @return void
*
* @SuppressWarnings(PHPMD.Superglobals)
*/ */
public function initializeLeafletBuilder(InitializeLeafletBuilderEvent $event) public function initializeLeafletBuilder(InitializeLeafletBuilderEvent $event)
{ {
$builder = $event->getBuilder(); $builder = $event->getBuilder();
foreach ($GLOBALS['LEAFLET_LIBRARIES'] as $name => $assets) { foreach ($this->libraries as $name => $assets) {
if (!empty($assets['css'])) { if (!empty($assets['css'])) {
list ($source, $type) = (array) $assets['css']; list ($source, $type) = (array) $assets['css'];
$builder->registerStylesheet($name, $source, $type ?: Assets::TYPE_FILE); $builder->registerStylesheet($name, $source, $type ?: Assets::TYPE_FILE);
@@ -134,34 +185,32 @@ class BootSubscriber implements EventSubscriberInterface
* Load Contao leaflet assets. * Load Contao leaflet assets.
* *
* @return void * @return void
*
* @SuppressWarnings(PHPMD.Superglobals)
*/ */
public function loadAssets() 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. * Load icons.
* *
* @return void * @return void
*
* @SuppressWarnings(PHPMD.Superglobals)
*/ */
public function loadIcons() public function loadIcons()
{ {
if (!$this->definitionMapper) {
return;
}
$collection = IconModel::findBy('active', true); $collection = IconModel::findBy('active', true);
if ($collection) { if ($collection) {
/** @var DefinitionMapper $mapper */
$mapper = $GLOBALS['container'][LeafletServices::DEFINITION_MAPPER];
$buffer = ''; $buffer = '';
$icons = array(); $icons = array();
foreach ($collection as $model) { foreach ($collection as $model) {
/** @var ImageIcon $icon */ /** @var ImageIcon $icon */
$icon = $mapper->handle($model); $icon = $this->definitionMapper->handle($model);
$icons[] = array( $icons[] = array(
'id' => $icon->getId(), 'id' => $icon->getId(),
'type' => lcfirst($icon->getType()), 'type' => lcfirst($icon->getType()),
@@ -180,39 +229,21 @@ class BootSubscriber implements EventSubscriberInterface
// @codingStandardsIgnoreStart // @codingStandardsIgnoreStart
// TODO: Cache it. // TODO: Cache it.
// codingStandardsIgnoreEnd // 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. * Create a new mapper.
* *
* @param mixed $mapper The mapper class or callable factory. * @param mixed $mapper The mapper class or callable factory.
* *
* @return Mapper * @return Mapper
*
* @SuppressWarnings(PHPMD.Superglobals)
*/ */
private function createMapper($mapper) private function createMapper($mapper)
{ {
if (is_callable($mapper)) { if (is_callable($mapper)) {
$container = $GLOBALS['container'][Services::CONTAINER]; return $mapper();
return $mapper($container);
} }
return new $mapper; return new $mapper;