Get rid of ServiceContainerTrait. Use dependency injection where possible.

This commit is contained in:
David Molineus
2015-02-02 08:44:17 +01:00
parent 64b0aa5389
commit f703b27a6b
6 changed files with 95 additions and 58 deletions

View File

@@ -110,20 +110,36 @@ $GLOBALS['LEAFLET_MAPPERS'][] = 'Netzmacht\Contao\Leaflet\Mapper\Control\Loading
$GLOBALS['LEAFLET_MAPPERS'][] = 'Netzmacht\Contao\Leaflet\Mapper\Control\FullscreenControlMapper';
// Vector mappers.
$GLOBALS['LEAFLET_MAPPERS'][] = 'Netzmacht\Contao\Leaflet\Mapper\Vector\PolylineMapper';
$GLOBALS['LEAFLET_MAPPERS'][] = 'Netzmacht\Contao\Leaflet\Mapper\Vector\MultiPolylineMapper';
$GLOBALS['LEAFLET_MAPPERS'][] = 'Netzmacht\Contao\Leaflet\Mapper\Vector\PolygonMapper';
$GLOBALS['LEAFLET_MAPPERS'][] = 'Netzmacht\Contao\Leaflet\Mapper\Vector\MultiPolygonMapper';
$GLOBALS['LEAFLET_MAPPERS'][] = 'Netzmacht\Contao\Leaflet\Mapper\Vector\CircleMapper';
$GLOBALS['LEAFLET_MAPPERS'][] = 'Netzmacht\Contao\Leaflet\Mapper\Vector\CircleMarkerMapper';
$GLOBALS['LEAFLET_MAPPERS'][] = 'Netzmacht\Contao\Leaflet\Mapper\Vector\RectangleMapper';
$GLOBALS['LEAFLET_MAPPERS'][] = function (\Netzmacht\Contao\Leaflet\ServiceContainer $container) {
return new Netzmacht\Contao\Leaflet\Mapper\Vector\PolylineMapper($container->getFrontendValueFilter());
};
$GLOBALS['LEAFLET_MAPPERS'][] = function (\Netzmacht\Contao\Leaflet\ServiceContainer $container) {
return new Netzmacht\Contao\Leaflet\Mapper\Vector\MultiPolylineMapper($container->getFrontendValueFilter());
};
$GLOBALS['LEAFLET_MAPPERS'][] = function (\Netzmacht\Contao\Leaflet\ServiceContainer $container) {
return new Netzmacht\Contao\Leaflet\Mapper\Vector\PolygonMapper($container->getFrontendValueFilter());
};
$GLOBALS['LEAFLET_MAPPERS'][] = function (\Netzmacht\Contao\Leaflet\ServiceContainer $container) {
return new Netzmacht\Contao\Leaflet\Mapper\Vector\MultiPolygonMapper($container->getFrontendValueFilter());
};
$GLOBALS['LEAFLET_MAPPERS'][] = function (\Netzmacht\Contao\Leaflet\ServiceContainer $container) {
return new Netzmacht\Contao\Leaflet\Mapper\Vector\CircleMapper($container->getFrontendValueFilter());
};
$GLOBALS['LEAFLET_MAPPERS'][] = function (\Netzmacht\Contao\Leaflet\ServiceContainer $container) {
return new Netzmacht\Contao\Leaflet\Mapper\Vector\CircleMarkerMapper($container->getFrontendValueFilter());
};
$GLOBALS['LEAFLET_MAPPERS'][] = function (\Netzmacht\Contao\Leaflet\ServiceContainer $container) {
return new Netzmacht\Contao\Leaflet\Mapper\Vector\RectangleMapper($container->getFrontendValueFilter());
};
// Miscellaneous mappers.
$GLOBALS['LEAFLET_MAPPERS'][] = 'Netzmacht\Contao\Leaflet\Mapper\UI\MarkerMapper';
$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 (\Netzmacht\Contao\Leaflet\ServiceContainer $container) {
return new \Netzmacht\Contao\Leaflet\Mapper\UI\MarkerMapper($container->getFrontendValueFilter());
};
/*
* Leaflet encoders.

View File

@@ -18,7 +18,6 @@ use Netzmacht\Contao\Leaflet\MapService;
use Netzmacht\Contao\Leaflet\ServiceContainer;
use Netzmacht\JavascriptBuilder\Builder;
use Netzmacht\JavascriptBuilder\Encoder;
use Netzmacht\JavascriptBuilder\Encoder\Chain\MultipleChain;
use Netzmacht\JavascriptBuilder\Encoder\ChainEncoder;
use Netzmacht\JavascriptBuilder\Encoder\JavascriptEncoder;
use Netzmacht\JavascriptBuilder\Encoder\MultipleObjectsEncoder;

View File

@@ -12,11 +12,11 @@
namespace Netzmacht\Contao\Leaflet\Mapper\UI;
use Netzmacht\Contao\Leaflet\Filter\Filter;
use Netzmacht\Contao\Leaflet\Frontend\ValueFilter;
use Netzmacht\Contao\Leaflet\Mapper\AbstractMapper;
use Netzmacht\Contao\Leaflet\Mapper\DefinitionMapper;
use Netzmacht\Contao\Leaflet\Model\IconModel;
use Netzmacht\Contao\Leaflet\Model\PopupModel;
use Netzmacht\Contao\Leaflet\ServiceContainerTrait;
use Netzmacht\LeafletPHP\Definition;
use Netzmacht\LeafletPHP\Definition\Type\ImageIcon;
use Netzmacht\LeafletPHP\Definition\UI\Marker;
@@ -29,8 +29,6 @@ use Netzmacht\LeafletPHP\Definition\UI\Popup;
*/
class MarkerMapper extends AbstractMapper
{
use ServiceContainerTrait;
/**
* Class of the model being build.
*
@@ -45,6 +43,25 @@ class MarkerMapper extends AbstractMapper
*/
protected static $definitionClass = 'Netzmacht\LeafletPHP\Definition\UI\Marker';
/**
* Frontend filter.
*
* @var ValueFilter
*/
protected $valueFilter;
/**
* Construct.
*
* @param ValueFilter $valueFilter Frontend filter.
*/
public function __construct(ValueFilter $valueFilter)
{
parent::__construct();
$this->valueFilter = $valueFilter;
}
/**
* {@inheritdoc}
*/
@@ -85,10 +102,7 @@ class MarkerMapper extends AbstractMapper
if ($definition instanceof Marker) {
if ($model->addPopup) {
$popup = null;
$content = $this
->getServiceContainer()
->getFrontendValueFilter()
->filter($model->popupContent);
$content = $this->valueFilter->filter($model->popupContent);
if ($model->popup) {
$popupModel = PopupModel::findActiveByPK($model->popup);

View File

@@ -13,11 +13,11 @@ namespace Netzmacht\Contao\Leaflet\Mapper\Vector;
use Netzmacht\Contao\Leaflet\Definition\Style;
use Netzmacht\Contao\Leaflet\Filter\Filter;
use Netzmacht\Contao\Leaflet\Frontend\ValueFilter;
use Netzmacht\Contao\Leaflet\Mapper\AbstractTypeMapper;
use Netzmacht\Contao\Leaflet\Mapper\DefinitionMapper;
use Netzmacht\Contao\Leaflet\Model\PopupModel;
use Netzmacht\Contao\Leaflet\Model\StyleModel;
use Netzmacht\Contao\Leaflet\ServiceContainerTrait;
use Netzmacht\LeafletPHP\Definition;
use Netzmacht\LeafletPHP\Definition\HasPopup;
use Netzmacht\LeafletPHP\Definition\UI\Popup;
@@ -30,8 +30,6 @@ use Netzmacht\LeafletPHP\Definition\Vector\Path;
*/
class AbstractVectorMapper extends AbstractTypeMapper
{
use ServiceContainerTrait;
/**
* Class of the model being build.
*
@@ -39,6 +37,25 @@ class AbstractVectorMapper extends AbstractTypeMapper
*/
protected static $modelClass = 'Netzmacht\Contao\Leaflet\Model\VectorModel';
/**
* Frontend filter.
*
* @var ValueFilter
*/
protected $valueFilter;
/**
* Construct.
*
* @param ValueFilter $valueFilter Frontend filter.
*/
public function __construct(ValueFilter $valueFilter)
{
parent::__construct();
$this->valueFilter = $valueFilter;
}
/**
* {@inheritdoc}
*/
@@ -84,10 +101,7 @@ class AbstractVectorMapper extends AbstractTypeMapper
) {
if ($definition instanceof HasPopup && $model->addPopup) {
$popup = null;
$content = $this
->getServiceContainer()
->getFrontendValueFilter()
->filter($model->popupContent);
$content = $this->valueFilter->filter($model->popupContent);
if ($model->popup) {
$popupModel = PopupModel::findActiveByPK($model->popup);

View File

@@ -1,31 +0,0 @@
<?php
/**
* @package dev
* @author David Molineus <david.molineus@netzmacht.de>
* @copyright 2015 netzmacht creative David Molineus
* @license LGPL 3.0
* @filesource
*
*/
namespace Netzmacht\Contao\Leaflet;
/**
* Class ServiceContainerTrait provides simple access to the service container.
*
* @package Netzmacht\Contao\Leaflet
*/
trait ServiceContainerTrait
{
/**
* Get the service container.
*
* @return ServiceContainer
* @SuppressWarnings(PHPMD.Superglobals)
*/
protected function getServiceContainer()
{
return $GLOBALS['container']['leaflet.service-container'];
}
}

View File

@@ -17,6 +17,7 @@ use Netzmacht\Contao\Leaflet\Event\InitializeDefinitionMapperEvent;
use Netzmacht\Contao\Leaflet\Event\InitializeEventDispatcherEvent;
use Netzmacht\Contao\Leaflet\Event\InitializeLeafletBuilderEvent;
use Netzmacht\Contao\Leaflet\Mapper\DefinitionMapper;
use Netzmacht\Contao\Leaflet\Mapper\Mapper;
use Netzmacht\Contao\Leaflet\Model\IconModel;
use Netzmacht\LeafletPHP\Assets;
use Netzmacht\LeafletPHP\Definition\Type\ImageIcon;
@@ -53,14 +54,13 @@ class BootSubscriber implements EventSubscriberInterface
*/
public function initializeDefinitionMapper(InitializeDefinitionMapperEvent $event)
{
$mapper = $event->getDefinitionMapper();
$container = $GLOBALS['container']['leaflet.service-container'];
$mapper = $event->getDefinitionMapper();
foreach ($GLOBALS['LEAFLET_MAPPERS'] as $className) {
if (is_array($className)) {
$mapper->register(new $className[0], $className[1]);
$mapper->register($this->createMapper($className[0]), $className[1]);
} else {
$mapper->register(new $className($container));
$mapper->register($this->createMapper($className));
}
}
}
@@ -162,6 +162,11 @@ class BootSubscriber implements EventSubscriberInterface
}
}
/**
* Set the static flag.
*
* @return string
*/
private function staticFlag()
{
if (\Config::get('debugMode') || TL_MODE !== 'FE') {
@@ -170,4 +175,24 @@ class BootSubscriber implements EventSubscriberInterface
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']['leaflet.service-container'];
return $mapper($container);
}
return new $mapper;
}
}