Skip to content
Snippets Groups Projects
Commit b9394492 authored by Antonio De Marco's avatar Antonio De Marco
Browse files

#91: Refactor pattern base class.

parent 110ae34a
No related branches found
No related tags found
No related merge requests found
......@@ -4,9 +4,8 @@ namespace Drupal\ui_patterns_library\Plugin\UiPatterns\Pattern;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\Core\Extension\ThemeHandlerInterface;
use Drupal\Core\TypedData\TypedDataManager;
use Drupal\ui_patterns\Definition\PatternDefinition;
use Drupal\ui_patterns\UiPatternBase;
use Drupal\ui_patterns\Plugin\PatternBase;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
......@@ -21,7 +20,7 @@ use Symfony\Component\DependencyInjection\ContainerInterface;
* deriver = "\Drupal\ui_patterns_library\Plugin\Deriver\LibraryDeriver"
* )
*/
class LibraryPattern extends UiPatternBase {
class LibraryPattern extends PatternBase {
/**
* Theme handler.
......@@ -33,8 +32,8 @@ class LibraryPattern extends UiPatternBase {
/**
* UiPatternsManager constructor.
*/
public function __construct(array $configuration, $plugin_id, $plugin_definition, $root, TypedDataManager $typed_data_manager, ModuleHandlerInterface $module_handler, ThemeHandlerInterface $theme_handler) {
parent::__construct($configuration, $plugin_id, $plugin_definition, $root, $typed_data_manager, $module_handler);
public function __construct(array $configuration, $plugin_id, $plugin_definition, $root, ModuleHandlerInterface $module_handler, ThemeHandlerInterface $theme_handler) {
parent::__construct($configuration, $plugin_id, $plugin_definition, $root, $module_handler);
$this->themeHandler = $theme_handler;
}
......@@ -47,7 +46,6 @@ class LibraryPattern extends UiPatternBase {
$plugin_id,
$plugin_definition,
$container->get('app.root'),
$container->get('typed_data_manager'),
$container->get('module_handler'),
$container->get('theme_handler')
);
......
<?php
namespace Drupal\ui_patterns;
namespace Drupal\ui_patterns\Plugin;
use Drupal\Component\Plugin\PluginBase;
use Drupal\Core\Extension\ModuleHandlerInterface;
......@@ -10,16 +10,11 @@ use Drupal\ui_patterns\Definition\PatternDefinition;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
* Class UiPatternBase.
* Class PatternBase.
*
* @package Drupal\ui_patterns
* @package Drupal\ui_patterns\Plugin
*/
abstract class UiPatternBase extends PluginBase implements UiPatternInterface, ContainerFactoryPluginInterface {
/**
* Prefix for locally defined libraries.
*/
const LIBRARY_PREFIX = 'ui_patterns';
abstract class PatternBase extends PluginBase implements PatternInterface, ContainerFactoryPluginInterface {
/**
* The app root.
......@@ -28,13 +23,6 @@ abstract class UiPatternBase extends PluginBase implements UiPatternInterface, C
*/
protected $root;
/**
* Typed data manager service.
*
* @var \Drupal\Core\TypedData\TypedDataManager
*/
protected $typedDataManager;
/**
* Module handler.
*
......@@ -45,10 +33,9 @@ abstract class UiPatternBase extends PluginBase implements UiPatternInterface, C
/**
* UiPatternsManager constructor.
*/
public function __construct(array $configuration, $plugin_id, $plugin_definition, $root, TypedDataManager $typed_data_manager, ModuleHandlerInterface $module_handler) {
public function __construct(array $configuration, $plugin_id, $plugin_definition, $root, ModuleHandlerInterface $module_handler) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->root = $root;
$this->typedDataManager = $typed_data_manager;
$this->moduleHandler = $module_handler;
}
......@@ -61,22 +48,10 @@ abstract class UiPatternBase extends PluginBase implements UiPatternInterface, C
$plugin_id,
$plugin_definition,
$container->get('app.root'),
$container->get('typed_data_manager'),
$container->get('module_handler')
);
}
/**
* {@inheritdoc}
*/
public function getFieldsAsOptions() {
$options = [];
foreach ($this->getPluginDefinition()['fields'] as $field) {
$options[$field['name']] = $field['label'];
}
return $options;
}
/**
* {@inheritdoc}
*/
......
<?php
namespace Drupal\ui_patterns;
namespace Drupal\ui_patterns\Plugin;
/**
* Interface UiPatternInterface.
* Interface PatternInterface.
*
* @package Drupal\ui_patterns
*/
interface UiPatternInterface {
interface PatternInterface {
}
......@@ -37,7 +37,7 @@ class UiPatternsManager extends DefaultPluginManager implements PluginManagerInt
* Cache backend service.
*/
public function __construct(\Traversable $namespaces, ModuleHandlerInterface $module_handler, ThemeHandlerInterface $theme_handler, CacheBackendInterface $cache_backend) {
parent::__construct('Plugin/UiPatterns/Pattern', $namespaces, $module_handler, 'Drupal\ui_patterns\UiPatternInterface', 'Drupal\ui_patterns\Annotation\UiPattern');
parent::__construct('Plugin/UiPatterns/Pattern', $namespaces, $module_handler, 'Drupal\ui_patterns\Plugin\PatternInterface', 'Drupal\ui_patterns\Annotation\UiPattern');
$this->moduleHandler = $module_handler;
$this->themeHandler = $theme_handler;
$this->alterInfo('ui_patterns_info');
......@@ -50,7 +50,7 @@ class UiPatternsManager extends DefaultPluginManager implements PluginManagerInt
* @param string $id
* Pattern ID.
*
* @return \Drupal\ui_patterns\UiPatternBase
* @return \Drupal\ui_patterns\Plugin\PatternBase
* Pattern object.
*/
public function getPattern($id) {
......@@ -61,7 +61,7 @@ class UiPatternsManager extends DefaultPluginManager implements PluginManagerInt
/**
* Get pattern objects.
*
* @return \Drupal\ui_patterns\UiPatternBase[]
* @return \Drupal\ui_patterns\Plugin\PatternBase[]
* Pattern objects.
*/
public function getPatterns() {
......
<?php
namespace Drupal\Tests\ui_patterns\Kernel;
namespace Drupal\Tests\ui_patterns\Kernel\Plugin;
use function bovigo\assert\assert;
use function bovigo\assert\predicate\equals;
use Drupal\Component\Serialization\Yaml;
use Drupal\ui_patterns\UiPatternBase;
use Drupal\Tests\ui_patterns\Kernel\AbstractUiPatternsTest;
use Drupal\ui_patterns\Plugin\PatternBase;
/**
* @coversDefaultClass \Drupal\ui_patterns\UiPatternBase
* @coversDefaultClass \Drupal\ui_patterns\Plugin\PatternBase
*
* @group ui_patterns
*/
class UiPatternBaseTest extends AbstractUiPatternsTest {
class PatternBaseTest extends AbstractUiPatternsTest {
/**
* Test hookLibraryInfoBuild.
......@@ -20,19 +21,19 @@ class UiPatternBaseTest extends AbstractUiPatternsTest {
* @covers ::hookLibraryInfoBuild
*/
public function testHookLibraryInfoBuild() {
$items = Yaml::decode(file_get_contents(dirname(__FILE__) . '/../fixtures/libraries.yml'));
$items = Yaml::decode(file_get_contents($this->getFixturePath() . '/libraries.yml'));
foreach ($items as $item) {
$pattern = $this->getUiPatternBaseMock($item['actual']);
/** @var \Drupal\ui_patterns\UiPatternBase $pattern */
/** @var \Drupal\ui_patterns\Plugin\PatternBase $pattern */
$libraries = $pattern->getLibraryDefinitions();
assert($libraries, equals($item['expected']));
}
}
/**
* Get UiPatternBase mock.
* Get PatternBase mock.
*
* @param array $plugin_definition
* Plugin definition.
......@@ -43,12 +44,11 @@ class UiPatternBaseTest extends AbstractUiPatternsTest {
* Mock object.
*/
protected function getUiPatternBaseMock(array $plugin_definition = [], array $methods = []) {
return $this->getMockForAbstractClass(UiPatternBase::class, [
return $this->getMockForAbstractClass(PatternBase::class, [
[],
'plugin_id',
$plugin_definition,
\Drupal::service('app.root'),
\Drupal::service('typed_data_manager'),
\Drupal::service('module_handler'),
], '', TRUE, TRUE, TRUE, $methods);
}
......
......@@ -11,7 +11,7 @@ use Drupal\ui_patterns\UiPatterns;
* Implements hook_theme().
*/
function ui_patterns_theme() {
/** @var \Drupal\ui_patterns\UiPatternBase $pattern */
/** @var \Drupal\ui_patterns\Plugin\PatternBase $pattern */
$items = [
'patterns_destination' => [
'variables' => ['sources' => NULL, 'context' => NULL],
......@@ -31,7 +31,7 @@ function ui_patterns_theme() {
* Implements hook_library_info_build()
*/
function ui_patterns_library_info_build() {
/** @var \Drupal\ui_patterns\UiPatternBase $pattern */
/** @var \Drupal\ui_patterns\Plugin\PatternBase $pattern */
$definitions = [];
foreach (UiPatterns::getManager()->getPatterns() as $pattern) {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment