Add providers plugin.

This commit is contained in:
David Molineus
2014-12-29 16:19:43 +01:00
parent 633be6a2bc
commit 9059c74e62
4 changed files with 266 additions and 4 deletions

View File

@@ -0,0 +1,23 @@
<?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\Mapper;
abstract class AbstractLayerMapper extends AbstractMapper
{
protected static $type;
public function match(\Model $model)
{
return parent::match($model) && $model->type === static::$type;
}
}

View File

@@ -169,7 +169,10 @@ abstract class AbstractMapper implements Mapper
*
* @return void
*/
abstract protected function initialize();
protected function initialize()
{
}
/**
* Use for specific build methods.
@@ -271,12 +274,18 @@ abstract class AbstractMapper implements Mapper
* @param Definition $definition The definition being built.
* @param \Model $model The model.
*/
private function applyOptions($options, $definition, $model)
protected function applyOptions($options, $definition, $model)
{
foreach ($options as $option => $mapping) {
$setter = 'set' . ucfirst($option);
$setter = 'set' . ucfirst($option);
$default = $this->getDefaultOption($option, $definition);
if ($model->$option != $this->getDefaultOption($option, $definition)) {
if ($model->$mapping === '1' || $model->$mapping === '') {
if (((bool) $model->$option) !== $default) {
$definition->$setter($model->$mapping);
}
}
elseif ($model->$mapping !== $default) {
$definition->$setter($model->$mapping);
}
}

View File

@@ -0,0 +1,103 @@
<?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\Mapper\Layer;
use Netzmacht\Contao\Leaflet\Mapper\AbstractLayerMapper;
use Netzmacht\Contao\Leaflet\Mapper\DefinitionMapper;
use Netzmacht\LeafletPHP\Definition;
class ProviderLayerMapper extends AbstractLayerMapper
{
/**
* Class of the model being build.
*
* @var string
*/
protected static $modelClass = 'Netzmacht\Contao\Leaflet\Model\LayerModel';
/**
* Class of the definition being created.
*
* @var string
*/
protected static $definitionClass = 'Netzmacht\LeafletPHP\Plugins\LeafletProviders\Provider';
/**
* Layer type.
*
* @var string
*/
protected static $type = 'provider';
/**
* Registered providers.
*
* @var array
*/
private $providers;
/**
* Construct.
*
* @SuppressWarnings(PHPMD.Superglobals)
*/
public function __construct()
{
$this->providers = &$GLOBALS['LEAFLET_TILE_PROVIDERS'];
parent::__construct();
}
/**
* {@inheritdoc}
*/
protected function createInstance(\Model $model, DefinitionMapper $mapper)
{
if (isset($this->providers[$model->tile_provider]['class'])) {
$class = $this->providers[$model->tile_provider]['class'];
} else {
$class = static::$definitionClass;
}
$reflector = new \ReflectionClass($class);
$instance = $reflector->newInstanceArgs($this->buildConstructArguments($model, $mapper));
return $instance;
}
/**
* {@inheritdoc}
*/
protected function doBuild(Definition $definition, \Model $model, DefinitionMapper $builder)
{
if (!empty($this->providers[$model->tile_provider]['options'])) {
$this->applyOptions(
$this->providers[$model->tile_provider]['options'],
$definition,
$model
);
}
}
/**
* {@inheritdoc}
*/
protected function buildConstructArguments(\Model $model, DefinitionMapper $mapper)
{
return array(
$model->alias ?: ('layer_' . $model->id),
$model->tile_provider,
$model->tile_provider_variant ?: null
);
}
}