Switch to PSR-4.

This commit is contained in:
David Molineus
2017-10-05 14:16:56 +02:00
parent e3344ffd4f
commit 827c746b0d
87 changed files with 4 additions and 6 deletions

View File

@@ -0,0 +1,294 @@
<?php
/**
* @package contao-leaflet-maps
* @author David Molineus <david.molineus@netzmacht.de>
* @copyright 2014-2016 netzmacht David Molineus
* @license LGPL 3.0
* @filesource
*
*/
namespace Netzmacht\Contao\Leaflet\Subscriber;
use ContaoCommunityAlliance\Contao\EventDispatcher\EventDispatcherInitializer;
use Netzmacht\Contao\Leaflet\ContaoAssets;
use Netzmacht\Contao\Leaflet\DependencyInjection\LeafletServices;
use Netzmacht\Contao\Leaflet\Event\GetJavascriptEvent;
use Netzmacht\Contao\Leaflet\Event\InitializeDefinitionMapperEvent;
use Netzmacht\Contao\Leaflet\Event\InitializeEventDispatcherEvent;
use Netzmacht\Contao\Leaflet\Event\InitializeLeafletBuilderEvent;
use Netzmacht\Contao\Leaflet\Frontend\InsertTag\LeafletInsertTagParser;
use Netzmacht\Contao\Leaflet\Mapper\DefinitionMapper;
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\LeafletPHP\Assets;
use Netzmacht\LeafletPHP\Definition\Type\Icon;
use Netzmacht\LeafletPHP\Definition\Type\ImageIcon;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
/**
* Class BootSubscriber provides handlers for leaflet boot process.
*
* @package Netzmacht\Contao\Leaflet\Subscriber
*/
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 ContaoAssets
*/
private $assets;
/**
* Definition mapper.
*
* @var DefinitionMapper
*/
private $definitionMapper;
/**
* Flag for loading icon file.
*
* @var bool
*/
private $loadIconFile = false;
/**
* BootSubscriber constructor.
*
* @param ContaoAssets $assets Leaflet assets manager.
* @param array $mappers Leaflet mapper configuration.
* @param array $encoders Leaflet encoder configuration.
* @param array $libraries Leaflet libraries configuration.
*/
public function __construct(
ContaoAssets $assets,
array $mappers,
array $encoders,
array $libraries
) {
$this->assets = $assets;
$this->mappers = $mappers;
$this->encoders = $encoders;
$this->libraries = $libraries;
}
/**
* {@inheritdoc}
*/
public static function getSubscribedEvents()
{
return array(
InitializeSystemEvent::NAME => 'initializeInsertTagParser',
InitializeDefinitionMapperEvent::NAME => 'initializeDefinitionMapper',
InitializeEventDispatcherEvent::NAME => 'initializeEventDispatcher',
InitializeLeafletBuilderEvent::NAME => 'initializeLeafletBuilder',
GetJavascriptEvent::NAME => array(array('loadIcons'), array('loadAssets')),
);
}
/**
* Initialize the leaflet insert tag parser.
*
* @param InitializeSystemEvent $event The event.
*
* @return void
*/
public function initializeInsertTagParser(InitializeSystemEvent $event)
{
$container = $event->getContainer();
$debugMode = $container->get(Services::CONFIG)->get('debugMode');
$mapProvider = $container->get(LeafletServices::MAP_PROVIDER);
$parser = new LeafletInsertTagParser($mapProvider, $debugMode);
$container->get(Services::INSERT_TAG_REPLACER)->registerParser($parser);
}
/**
* Create and register all configured mappers.
*
* @param InitializeDefinitionMapperEvent $event The subscribed event.
*
* @return void
*/
public function initializeDefinitionMapper(InitializeDefinitionMapperEvent $event)
{
$mapper = $event->getDefinitionMapper();
$this->definitionMapper = $mapper;
foreach ($this->mappers as $className) {
if (is_array($className)) {
$mapper->register($this->createMapper($className[0]), $className[1]);
} else {
$mapper->register($this->createMapper($className));
}
}
}
/**
* Register all leaflet encoders.
*
* @param InitializeEventDispatcherEvent $event The subscribed event.
*
* @return void
*/
public function initializeEventDispatcher(InitializeEventDispatcherEvent $event)
{
$dispatcher = $event->getEventDispatcher();
$initializer = new EventDispatcherInitializer();
$initializer->addSubscribers($dispatcher, $this->encoders);
}
/**
* Register all libraries assets.
*
* @param InitializeLeafletBuilderEvent $event The subscribed event.
*
* @return void
*/
public function initializeLeafletBuilder(InitializeLeafletBuilderEvent $event)
{
$builder = $event->getBuilder();
foreach ($this->libraries as $name => $assets) {
if (!empty($assets['css'])) {
list ($source, $type) = (array) $assets['css'];
$builder->registerStylesheet($name, $source, $type ?: Assets::TYPE_FILE);
}
if (!empty($assets['javascript'])) {
list ($source, $type) = (array) $assets['javascript'];
$builder->registerJavascript($name, $source, $type ?: Assets::TYPE_FILE);
}
}
}
/**
* Load Contao leaflet assets.
*
* @return void
*/
public function loadAssets()
{
$this->assets->addJavascript('assets/leaflet/maps/contao-leaflet.js', ContaoAssets::TYPE_FILE);
if ($this->loadIconFile) {
$this->assets->addJavascript('assets/leaflet/js/icons.js', ContaoAssets::TYPE_FILE);
}
}
/**
* Load icons.
*
* @return void
*/
public function loadIcons()
{
if (!$this->definitionMapper) {
return;
}
$collection = IconModel::findBy('active', true);
if ($collection) {
$buffer = '';
$icons = array();
foreach ($collection as $model) {
/** @var ImageIcon $icon */
$icon = $this->definitionMapper->handle($model);
$icons[] = array(
'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->loadIconFile = true;
}
}
/**
* Create a new mapper.
*
* @param mixed $mapper The mapper class or callable factory.
*
* @return Mapper
*/
private function createMapper($mapper)
{
if (is_callable($mapper)) {
return $mapper();
}
return new $mapper;
}
/**
* Load all libraries for an icon.
*
* @param Icon $icon Icon definition
*
* @return void.
*/
protected function loadIconsLibraries($icon)
{
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);
}
}
}
}

View File

@@ -0,0 +1,126 @@
<?php
/**
* @package contao-leaflet-maps
* @author David Molineus <david.molineus@netzmacht.de>
* @copyright 2014-2016 netzmacht David Molineus
* @license LGPL 3.0
* @filesource
*
*/
namespace Netzmacht\Contao\Leaflet\Subscriber;
use Netzmacht\Contao\Leaflet\Frontend\RequestUrl;
use Netzmacht\JavascriptBuilder\Encoder;
use Netzmacht\JavascriptBuilder\Flags;
use Netzmacht\JavascriptBuilder\Symfony\Event\EncodeValueEvent;
use Netzmacht\JavascriptBuilder\Symfony\Event\EncodeReferenceEvent;
use Netzmacht\JavascriptBuilder\Exception\EncodeValueFailed;
use Netzmacht\LeafletPHP\Definition\Type\Icon;
use Netzmacht\LeafletPHP\Plugins\Omnivore\OmnivoreLayer;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
/**
* Class EncoderSubscriber subscribes to the internal encoding event dispatcher.
*
* @package Netzmacht\Contao\Leaflet\Subscriber
*/
class EncoderSubscriber implements EventSubscriberInterface
{
/**
* {@inheritdoc}
*/
public static function getSubscribedEvents()
{
return array(
EncodeValueEvent::NAME => array(
array('encodeIcons', 1000),
array('loadLayer', 100),
),
EncodeReferenceEvent::NAME => array('referenceIcon', 100),
);
}
/**
* Create icon reference to the contao leaflet icon registry.
*
* @param EncodeReferenceEvent $event The subscribed event.
*
* @return void
*/
public function referenceIcon(EncodeReferenceEvent $event)
{
$value = $event->getObject();
if ($value instanceof Icon) {
$event->setReference('L.contao.getIcon(\'' . $value->getId() . '\')');
$event->stopPropagation();
}
}
/**
* Force that icons are encoded as reference to the L.contao icon registry.
*
* @param EncodeValueEvent $event The subscribed event.
*
* @return void
*/
public function encodeIcons(EncodeValueEvent $event)
{
$value = $event->getValue();
if ($value instanceof Icon) {
// Do not encode the icon, as it is generated in an separate icon file.
$event->setSuccessful();
$event->stopPropagation();
}
}
/**
* Encode OmnivoreLayers so that the internal used contao.loadLayer method is used.
*
* @param EncodeValueEvent $event The subscribed event.
*
* @return void
* @throws EncodeValueFailed If encoding failed.
*/
public function loadLayer(EncodeValueEvent $event)
{
$value = $event->getValue();
$encoder = $event->getEncoder();
$template = 'L.contao.load(%s, %s, %s, %s, map);';
if ($value instanceof OmnivoreLayer) {
$url = $value->getUrl();
if ($url instanceof RequestUrl) {
$url = $url->getHash();
} elseif (strpos($url, '/') !== false) {
// Slash found, not a Contao leaflet hash, do not replace encoding.
return;
}
if ($value->getCustomLayer()) {
$ref = $encoder->encodeReference($value->getCustomLayer());
} else {
$template = $encoder->encodeReference($value) . ' = ' . $template;
$ref = 'null';
}
$event->addLine(
sprintf(
$template,
$encoder->encodeValue($url),
$encoder->encodeValue(strtolower(str_replace('Omnivore.', '', $value->getType()))),
$encoder->encodeArray($value->getOptions(), JSON_FORCE_OBJECT),
$ref
)
);
foreach ($value->getMethodCalls() as $call) {
$event->addLine($call->encode($encoder, Flags::CLOSE_STATEMENT));
}
}
}
}

View File

@@ -0,0 +1,237 @@
<?php
/**
* @package contao-leaflet-maps
* @author David Molineus <david.molineus@netzmacht.de>
* @copyright 2014-2016 netzmacht David Molineus
* @license LGPL 3.0
* @filesource
*
*/
namespace Netzmacht\Contao\Leaflet\Subscriber;
use Netzmacht\Contao\Leaflet\Dca\VectorCallbacks;
use Netzmacht\Contao\Leaflet\Event\ConvertToGeoJsonEvent;
use Netzmacht\Contao\Leaflet\Model\LayerModel;
use Netzmacht\LeafletPHP\Value\GeoJson\Feature;
use Netzmacht\LeafletPHP\Definition\HasPopup;
use Netzmacht\LeafletPHP\Definition\UI\Marker;
use Netzmacht\LeafletPHP\Definition\Vector\Circle;
use Netzmacht\LeafletPHP\Definition\Vector\CircleMarker;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
/**
* Class GeoJsonSubscriber provides subscribers when a definition is converted to a geo json feature.
*
* @package Netzmacht\Contao\Leaflet\Subscriber
*/
class GeoJsonSubscriber implements EventSubscriberInterface
{
/**
* Property mapping between models and features.
*
* @var array
*/
private $featureModelProperties;
/**
* GeoJsonSubscriber constructor.
*
* @param array $featureModelProperties Property mapping between models and features.
*/
public function __construct(array $featureModelProperties)
{
$this->featureModelProperties = $featureModelProperties;
}
/**
* {@inheritdoc}
*/
public static function getSubscribedEvents()
{
return array(
ConvertToGeoJsonEvent::NAME => array(
array('addPopup'),
array('enrichObjects'),
array('enrichCircle'),
array('setModelData')
)
);
}
/**
* Add popup property for definitions with an popup.
*
* @param ConvertToGeoJsonEvent $event The subscribed event.
*
* @return void
*/
public function addPopup(ConvertToGeoJsonEvent $event)
{
$feature = $event->getGeoJson();
$definition = $event->getDefinition();
if ($definition instanceof HasPopup && $feature instanceof Feature) {
if ($definition->getPopup()) {
$feature->setProperty('popup', $definition->getPopup());
}
if ($definition->getPopupContent()) {
$feature->setProperty('popupContent', $definition->getPopupContent());
}
if ($definition->getPopupOptions()) {
$feature->setProperty('popupOptions', $definition->getPopupOptions());
}
}
}
/**
* Enrich map object with feature data and bounds information.
*
* @param ConvertToGeoJsonEvent $event The subscribed event.
*
* @return void
*/
public function enrichObjects(ConvertToGeoJsonEvent $event)
{
$feature = $event->getGeoJson();
$definition = $event->getDefinition();
$model = $event->getModel();
if (($definition instanceof Marker || $definition instanceof VectorCallbacks)
&& $model instanceof \Model && $feature instanceof Feature) {
$this->setDataProperty($model, $feature);
$this->setBoundsInformation($model, $feature);
}
}
/**
* Enrich the the circle with constructor arguments.
*
* @param ConvertToGeoJsonEvent $event The subscribed events.
*
* @return void
*/
public function enrichCircle(ConvertToGeoJsonEvent $event)
{
$feature = $event->getGeoJson();
$definition = $event->getDefinition();
if ($definition instanceof Circle && !$definition instanceof CircleMarker && $feature instanceof Feature) {
$feature->setProperty('arguments', array($definition->getLatLng(), $definition->getRadius()));
}
}
/**
* Pass configured properties on an model to the properties.model key.
*
* @param ConvertToGeoJsonEvent $event The subscribed events.
*
* @return void
*/
public function setModelData(ConvertToGeoJsonEvent $event)
{
$feature = $event->getGeoJson();
$model = $event->getModel();
if (!$model instanceof \Model || !$feature instanceof Feature
|| empty($this->featureModelProperties[$model->getTable()])) {
return;
}
$mapping = $this->featureModelProperties[$model->getTable()];
$data = (array) $feature->getProperty('model');
foreach ((array) $mapping as $property) {
$value = $this->parseModelValue($model, $property);
// Important: Do not combine with line above as the property can be modified if it's an array.
$data[$property] = $value;
}
$feature->setProperty('model', $data);
}
/**
* Parse the model value based on the config.
*
* @param \Model $model The model.
* @param mixed $property The property config.
*
* @return array|mixed|null
*/
private function parseModelValue(\Model $model, &$property)
{
if (is_array($property)) {
list($property, $type) = $property;
$value = $model->$property;
switch ($type) {
case 'array':
case 'object':
$value = deserialize($value, true);
break;
case 'file':
$file = \FilesModel::findByUuid($value);
$value = $file->path;
break;
case 'files':
$collection = \FilesModel::findMultipleByUuids(deserialize($value, true));
if ($collection) {
$value = $collection->fetchEach('path');
} else {
$value = array();
}
break;
default:
$value = null;
}
} else {
$value = $model->$property;
}
return $value;
}
/**
* Set the bounds information.
*
* @param \Model $model The model.
* @param Feature $feature The feature.
*
* @return void
*/
protected function setBoundsInformation($model, $feature)
{
if ($model->ignoreForBounds) {
$feature->setProperty('ignoreForBounds', true);
} else {
$parent = LayerModel::findByPk($model->pid);
if ($parent && $parent->boundsMode !== 'extend') {
$feature->setProperty('ignoreForBounds', true);
}
}
}
/**
* Set the data property.
*
* @param \Model $model The model.
* @param Feature $feature The feature.
*
* @return void
*/
protected function setDataProperty($model, $feature)
{
if ($model->featureData) {
$feature->setProperty('data', json_decode($model->featureData, true));
}
}
}

View File

@@ -0,0 +1,74 @@
<?php
/**
* @package contao-leaflet-maps
* @author David Molineus <david.molineus@netzmacht.de>
* @copyright 2014-2016 netzmacht David Molineus
* @license LGPL 3.0
* @filesource
*
*/
namespace Netzmacht\Contao\Leaflet\Subscriber;
use Netzmacht\Contao\Leaflet\Event\GetHashEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
/**
* Class HashSubscriber creates hashes for models and for data which does not have a hash created by other subscribers.
*
* @package Netzmacht\Contao\Leaflet\Subscriber
*/
class HashSubscriber implements EventSubscriberInterface
{
/**
* {@inheritdoc}
*/
public static function getSubscribedEvents()
{
return array(
GetHashEvent::NAME => array(
array('getModelHash'),
array('getFallback', -100)
)
);
}
/**
* Get hash for a model object.
*
* @param GetHashEvent $event The subscribed event.
*
* @return void
*/
public function getModelHash(GetHashEvent $event)
{
$data = $event->getData();
if ($data instanceof \Model) {
$event->setHash($data->getTable() . '::' . $data->{$data->getPk()});
}
}
/**
* Get hash fallback if no hash was created so far.
*
* @param GetHashEvent $event The subscribed event.
*
* @return void
*/
public function getFallback(GetHashEvent $event)
{
if ($event->getHash()) {
return;
}
$data = $event->getData();
if (is_object($data)) {
$event->setHash(spl_object_hash($data));
} else {
$event->setHash(md5(json_encode($data)));
}
}
}