Files
contao-leaflet-maps/src/Netzmacht/Contao/Leaflet/Subscriber/BootSubscriber.php

164 lines
4.9 KiB
PHP
Raw Normal View History

2014-12-29 12:17:40 +01:00
<?php
/**
* @package dev
* @author David Molineus <david.molineus@netzmacht.de>
* @copyright 2014 netzmacht creative David Molineus
* @license LGPL 3.0
* @filesource
*
*/
namespace Netzmacht\Contao\Leaflet\Subscriber;
use ContaoCommunityAlliance\Contao\EventDispatcher\EventDispatcherInitializer;
use Netzmacht\Contao\Leaflet\Event\GetJavascriptEvent;
use Netzmacht\Contao\Leaflet\Event\InitializeDefinitionMapperEvent;
use Netzmacht\Contao\Leaflet\Event\InitializeEventDispatcherEvent;
use Netzmacht\Contao\Leaflet\Event\InitializeLeafletBuilderEvent;
2015-01-06 21:30:57 +01:00
use Netzmacht\Contao\Leaflet\Mapper\DefinitionMapper;
use Netzmacht\Contao\Leaflet\Model\IconModel;
2015-01-08 12:44:42 +01:00
use Netzmacht\LeafletPHP\Assets;
2015-01-06 21:30:57 +01:00
use Netzmacht\LeafletPHP\Definition\Type\Icon;
2014-12-29 12:17:40 +01:00
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
/**
* Class BootSubscriber
*
* @package Netzmacht\Contao\Leaflet\Subscriber
*/
class BootSubscriber implements EventSubscriberInterface
{
/**
* {@inheritdoc}
*/
public static function getSubscribedEvents()
{
return array(
InitializeDefinitionMapperEvent::NAME => 'initializeDefinitionMapper',
InitializeEventDispatcherEvent::NAME => 'initializeEventDispatcher',
InitializeLeafletBuilderEvent::NAME => 'initializeLeafletBuilder',
2015-01-06 21:30:57 +01:00
GetJavascriptEvent::NAME => array(array('loadAssets'), array('loadIcons')),
2014-12-29 12:17:40 +01:00
);
}
/**
* Create and register all configured mappers.
*
* @param InitializeDefinitionMapperEvent $event The subscribed event.
*
* @return void
*
* @SuppressWarnings(PHPMD.Superglobals)
*/
public function initializeDefinitionMapper(InitializeDefinitionMapperEvent $event)
{
$mapper = $event->getDefinitionMapper();
foreach ($GLOBALS['LEAFLET_MAPPERS'] as $className) {
if (is_array($className)) {
$mapper->register(new $className[0], $className[1]);
} else {
$mapper->register(new $className());
}
}
}
/**
* Register all leaflet encoders.
*
* @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']);
}
/**
* Register all libraries assets.
*
* @param InitializeLeafletBuilderEvent $event The subscribed event.
*
* @return void
*
* @SuppressWarnings(PHPMD.Superglobals)
*/
public function initializeLeafletBuilder(InitializeLeafletBuilderEvent $event)
{
$builder = $event->getBuilder();
foreach ($GLOBALS['LEAFLET_ASSETS'] as $name => $assets) {
if (!empty($assets['css'])) {
2015-01-08 12:44:42 +01:00
list ($source, $type) = (array) $assets['css'];
$builder->registerStylesheet($name, $source, $type ?: Assets::TYPE_FILE);
2014-12-29 12:17:40 +01:00
}
if (!empty($assets['javascript'])) {
2015-01-08 12:44:42 +01:00
list ($source, $type) = (array) $assets['javascript'];
$builder->registerJavascript($name, $source, $type ?: Assets::TYPE_FILE);
2014-12-29 12:17:40 +01:00
}
}
}
/**
* Load Contao leaflet assets.
*
* @return void
*/
public function loadAssets()
{
2015-01-08 12:44:42 +01:00
$GLOBALS['TL_JAVASCRIPT'][] = 'assets/leaflet/libs/contao/contao-leaflet.js' . (\Config::get('debugMode')
? ''
: '|static'
);
2014-12-29 12:17:40 +01:00
}
2015-01-06 21:30:57 +01:00
/**
* Load icons.
*
* @throws \Netzmacht\Javascript\Exception\EncodeValueFailed
*/
public function loadIcons()
{
$collection = IconModel::findBy('active', true);
if ($collection) {
/** @var DefinitionMapper $mapper */
2015-01-07 09:32:14 +01:00
$mapper = $GLOBALS['container']['leaflet.definition.mapper'];
$buffer = '';
$icons = array();
2015-01-06 21:30:57 +01:00
foreach ($collection as $model) {
/** @var Icon $icon */
2015-01-07 09:32:14 +01:00
$icon = $mapper->handle($model);
$icons[] = array(
'id' => $icon->getId(),
'type' => lcfirst($icon->getType()),
'options' => $icon->getOptions(),
2015-01-06 21:30:57 +01:00
);
}
2015-01-07 09:32:14 +01:00
if ($icons) {
2015-01-08 12:44:42 +01:00
$buffer = sprintf('ContaoLeaflet.loadIcons(%s);', json_encode($icons));
2015-01-06 21:30:57 +01:00
}
2015-01-07 09:32:14 +01:00
$file = new \File('assets/leaflet/js/icons.js');
$file->write($buffer);
$file->close();
// TODO: Cache it.
2015-01-08 12:44:42 +01:00
$GLOBALS['TL_JAVASCRIPT'][] = 'assets/leaflet/js/icons.js' . (\Config::get('debugMode')
? ''
: '|static'
);
2015-01-06 21:30:57 +01:00
}
}
2014-12-29 12:17:40 +01:00
}