Commit f40502ba authored by Tino Goratsch's avatar Tino Goratsch

refactored the ColumnTypeStrategyFactory so that it doesn't depend on the service container anymore

parent e363c154
......@@ -8,6 +8,7 @@ namespace ACP3\Core\DependencyInjection;
use ACP3\Core\Environment\ApplicationPath;
use ACP3\Core\Helpers\DataGrid\DependencyInjection\RegisterColumnRendererPass;
use ACP3\Core\Model\DataProcessor\DependencyInjection\RegisterColumnTypesCompilerPass;
use ACP3\Core\Modules;
use ACP3\Core\Validation\DependencyInjection\RegisterValidationRulesPass;
use ACP3\Core\View\Renderer\Smarty\DependencyInjection\RegisterSmartyPluginsPass;
......@@ -77,6 +78,7 @@ class ServiceContainerBuilder extends ContainerBuilder
$this->addCompilerPass(new RegisterColumnRendererPass());
$this->addCompilerPass(new RegisterValidationRulesPass());
$this->addCompilerPass(new RegisterWysiwygEditorsCompilerPass());
$this->addCompilerPass(new RegisterColumnTypesCompilerPass());
$loader = new YamlFileLoader($this, new FileLocator(__DIR__));
$loader->load($this->applicationPath->getClassesDir() . 'config/services.yml');
......
......@@ -8,22 +8,24 @@ namespace ACP3\Core\Model\DataProcessor;
use ACP3\Core\Model\DataProcessor\ColumnType\ColumnTypeStrategyInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
class ColumnTypeStrategyFactory
{
/**
* @var ContainerInterface
* @var ColumnTypeStrategyInterface[]
*/
protected $container;
protected $columnTypes = [];
/**
* ColumnTypeStrategyFactory constructor.
* @param ContainerInterface $container
* @param ColumnTypeStrategyInterface $columnTypeStrategy
* @param $columnTypeName
* @return $this
*/
public function __construct(ContainerInterface $container)
public function registerColumnType(ColumnTypeStrategyInterface $columnTypeStrategy, $columnTypeName)
{
$this->container = $container;
$this->columnTypes[$columnTypeName] = $columnTypeStrategy;
return $this;
}
/**
......@@ -32,9 +34,10 @@ class ColumnTypeStrategyFactory
*/
public function getStrategy($columnType)
{
$serviceId = 'core.model.column_type.' . $columnType . '_column_type_strategy';
/** @var ColumnTypeStrategyInterface $service */
$service = $this->container->get($serviceId);
return $service;
if (!isset($this->columnTypes[$columnType])) {
throw new \InvalidArgumentException('Invalid column type constraint given: ' . $columnType);
}
return $this->columnTypes[$columnType];
}
}
<?php
/**
* Copyright (c) 2016 by the ACP3 Developers.
* See the LICENCE file at the top-level module directory for licencing details.
*/
namespace ACP3\Core\Model\DataProcessor\DependencyInjection;
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Reference;
class RegisterColumnTypesCompilerPass implements CompilerPassInterface
{
/**
* @param ContainerBuilder $container
*/
public function process(ContainerBuilder $container)
{
$definition = $container->findDefinition('core.model.column_type_strategy_factory');
$plugins = $container->findTaggedServiceIds('core.model.column_type');
foreach ($plugins as $serviceId => $tags) {
$definition->addMethodCall(
'registerColumnType',
[new Reference($serviceId), reset($tags)['columnType']]
);
}
}
}
......@@ -11,7 +11,6 @@ use ACP3\Core\Model\DataProcessor\ColumnType\ColumnTypeStrategyInterface;
use ACP3\Core\Model\DataProcessor\ColumnTypes;
use ACP3\Core\Model\DataProcessor\ColumnTypeStrategyFactory;
use ACP3\Core\Test\Model\CreateRawColumnTypeMockTrait;
use Symfony\Component\DependencyInjection\ContainerInterface;
class ColumnTypeStrategyFactoryTest extends \PHPUnit_Framework_TestCase
{
......@@ -21,35 +20,25 @@ class ColumnTypeStrategyFactoryTest extends \PHPUnit_Framework_TestCase
* @var ColumnTypeStrategyFactory
*/
private $columnTypeStrategyFactory;
/**
* @var \PHPUnit_Framework_MockObject_MockObject
*/
private $containerMock;
protected function setUp()
{
$this->containerMock = $this->getMockBuilder(ContainerInterface::class)
->setMethods(['set', 'get', 'has', 'initialized', 'setParameter', 'getParameter', 'hasParameter'])
->getMock();
$this->columnTypeStrategyFactory = new ColumnTypeStrategyFactory($this->containerMock);
$this->columnTypeStrategyFactory = new ColumnTypeStrategyFactory();
}
public function testGetStrategyWithValidColumnType()
{
$this->setUpContainerMockExpectations();
$columnTypeMock = $this->getRawColumnTypeInstance($this);
$this->columnTypeStrategyFactory->registerColumnType($columnTypeMock, 'raw');
$this->assertInstanceOf(ColumnTypeStrategyInterface::class, $this->columnTypeStrategyFactory->getStrategy(ColumnTypes::COLUMN_TYPE_RAW));
}
private function setUpContainerMockExpectations()
/**
* @expectedException \InvalidArgumentException
*/
public function testGetStrategyWithInvalidColumnType()
{
$serviceId = 'core.model.column_type.' . ColumnTypes::COLUMN_TYPE_RAW . '_column_type_strategy';
$this->containerMock
->expects($this->once())
->method('get')
->with($serviceId)
->willReturn($this->getRawColumnTypeInstance($this));
$this->columnTypeStrategyFactory->getStrategy(ColumnTypes::COLUMN_TYPE_RAW);
}
}
services:
core.model.column_type.boolean_column_type_strategy:
class: ACP3\Core\Model\DataProcessor\ColumnType\BooleanColumnType
tags:
- { name: core.model.column_type, columnType: 'boolean' }
core.model.column_type.date_time_column_type_strategy:
class: ACP3\Core\Model\DataProcessor\ColumnType\DateTimeColumnType
arguments:
- '@core.date'
tags:
- { name: core.model.column_type, columnType: 'date_time' }
core.model.column_type.double_column_type_strategy:
class: ACP3\Core\Model\DataProcessor\ColumnType\DoubleColumnType
tags:
- { name: core.model.column_type, columnType: 'double' }
core.model.column_type.integer_column_type_strategy:
class: ACP3\Core\Model\DataProcessor\ColumnType\IntegerColumnType
tags:
- { name: core.model.column_type, columnType: 'integer' }
core.model.column_type.raw_column_type_strategy:
class: ACP3\Core\Model\DataProcessor\ColumnType\RawColumnType
tags:
- { name: core.model.column_type, columnType: 'raw' }
core.model.column_type.text_column_type_strategy:
class: ACP3\Core\Model\DataProcessor\ColumnType\TextColumnType
arguments:
- '@core.helpers.secure'
tags:
- { name: core.model.column_type, columnType: 'text' }
core.model.column_type.text_wysiwyg_column_type_strategy:
class: ACP3\Core\Model\DataProcessor\ColumnType\TextWysiwygColumnType
arguments:
- '@core.helpers.secure'
tags:
- { name: core.model.column_type, columnType: 'text_wysiwyg' }
core.model.data_processor:
class: ACP3\Core\Model\DataProcessor
......@@ -33,5 +47,3 @@ services:
core.model.column_type_strategy_factory:
class: ACP3\Core\Model\DataProcessor\ColumnTypeStrategyFactory
arguments:
- '@service_container'
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment