Skip to content
Snippets Groups Projects
Commit b1a99e1c authored by Tobias Zimmermann's avatar Tobias Zimmermann
Browse files

by tstoeckler: Refactor the way that library type plugin IDs and class names are retrieved

parent 8ac6a7c6
No related branches found
No related tags found
No related merge requests found
Showing with 107 additions and 50 deletions
......@@ -7,29 +7,14 @@
namespace Drupal\libraries\ExternalLibrary;
use Drupal\libraries\ExternalLibrary\Utility\IdAccessorTrait;
/**
* Provides a base external library implementation.
*/
trait LibraryTrait {
/**
* The library ID.
*
* @var string
*/
protected $id;
/**
* Returns the ID of the library.
*
* @return string
* The library ID. This must be unique among all known libraries.
*
* @see \Drupal\libraries\ExternalLibrary\LibraryInterface::getId()
*/
public function getId() {
return $this->id;
}
use IdAccessorTrait;
/**
* Returns the currently installed version of the library.
......
......@@ -10,11 +10,12 @@ namespace Drupal\libraries\ExternalLibrary\LibraryType;
use Drupal\Core\Cache\CacheBackendInterface;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\Core\Plugin\DefaultPluginManager;
use Drupal\libraries\Annotation\LibraryType;
/**
* Provides a plugin manager for library type plugins.
*/
class LibraryTypeManager extends DefaultPluginManager {
class LibraryTypeManager extends DefaultPluginManager implements LibraryTypeManagerInterface {
/**
* Constructs a locator manager.
......@@ -28,9 +29,19 @@ class LibraryTypeManager extends DefaultPluginManager {
* The module handler to invoke the alter hook with.
*/
public function __construct(\Traversable $namespaces, CacheBackendInterface $cache_backend, ModuleHandlerInterface $module_handler) {
parent::__construct('Plugin/libraries/LibraryType', $namespaces, $module_handler, 'Drupal\libraries\ExternalLibrary\LibraryType\LibraryTypeInterface', 'Drupal\libraries\Annotation\LibraryType');
parent::__construct('Plugin/libraries/LibraryType', $namespaces, $module_handler, LibraryTypeInterface::class, LibraryType::class);
// @todo Document this hook.
$this->alterInfo('libraries_library_type_info');
$this->setCacheBackend($cache_backend, 'libraries_library_type_info');
}
/**
* {@inheritdoc}
*/
public function getLibraryClass(LibraryTypeInterface $library_type) {
// @todo Make this alter-able.
return $library_type->getLibraryClass();
}
}
<?php
/**
* @file
* Contains \Drupal\libraries\ExternalLibrary\LibraryType\LibraryTypeManagerInterface.
*/
namespace Drupal\libraries\ExternalLibrary\LibraryType;
use Drupal\Component\Plugin\Factory\FactoryInterface;
/**
* Provides an interface for library type managers.
*/
interface LibraryTypeManagerInterface extends FactoryInterface {
/**
* Gets the library class to use for a given library type.
*
* @param \Drupal\libraries\ExternalLibrary\LibraryType\LibraryTypeInterface $library_type
* The library type to return the library class for.
*
* @return string
* The library class.
*/
public function getLibraryClass(LibraryTypeInterface $library_type);
}
......@@ -10,6 +10,7 @@ namespace Drupal\libraries\ExternalLibrary\Local;
use Drupal\Core\Cache\CacheBackendInterface;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\Core\Plugin\DefaultPluginManager;
use Drupal\libraries\Annotation\Locator;
/**
* Provides a plugin manager for library locator plugins.
......@@ -30,7 +31,8 @@ class LocatorManager extends DefaultPluginManager {
* The module handler to invoke the alter hook with.
*/
public function __construct(\Traversable $namespaces, CacheBackendInterface $cache_backend, ModuleHandlerInterface $module_handler) {
parent::__construct('Plugin/libraries/Locator', $namespaces, $module_handler, 'Drupal\libraries\ExternalLibrary\Local\LocatorInterface', 'Drupal\libraries\Annotation\Locator');
parent::__construct('Plugin/libraries/Locator', $namespaces, $module_handler, LocatorInterface::class, Locator::class);
// @todo Document this hook.
$this->alterInfo('libraries_locator_info');
$this->setCacheBackend($cache_backend, 'libraries_locator_info');
}
......
......@@ -58,7 +58,7 @@ class LibraryRegistry implements LibraryRegistryInterface {
public function getLibrary($id) {
$library_type = $this->getLibraryType($id);
$class = $library_type->getLibraryClass();
$class = $this->libraryTypeFactory->getLibraryClass($library_type);
// @todo Make sure that the library class implements the correct interface.
$library = $class::create($id, $this->getDefinition($id));
......
<?php
/**
* @file
* Contains \Drupal\libraries\ExternalLibrary\Utility\LibraryIdAccessorTrait.
*/
namespace Drupal\libraries\ExternalLibrary\Utility;
/**
* Provides a trait for classes that have a string identifier.
*/
trait IdAccessorTrait {
/**
* The ID.
*
* @var string
*/
protected $id;
/**
* Returns the ID.
*
* @return string
* The ID.
*
* @see \Drupal\libraries\ExternalLibrary\LibraryInterface::getId()
* @see \Drupal\libraries\ExternalLibrary\LibraryType\LibraryTypeInterface::getId()
*/
public function getId() {
return $this->id;
}
}
......@@ -13,14 +13,14 @@ namespace Drupal\libraries\ExternalLibrary\Utility;
trait LibraryIdAccessorTrait {
/**
* The library ID of the library that caused the exception.
* The ID of the library.
*
* @var string
*/
protected $libraryId;
/**
* Returns the library ID of the library that caused the exception.
* Returns the ID of the library.
*
* @return string
* The library ID.
......
......@@ -9,10 +9,12 @@ namespace Drupal\libraries\Plugin\libraries\LibraryType;
use Drupal\Component\Plugin\Factory\FactoryInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\libraries\ExternalLibrary\Asset\AssetLibrary;
use Drupal\libraries\ExternalLibrary\LibraryInterface;
use Drupal\libraries\ExternalLibrary\LibraryType\LibraryCreationListenerInterface;
use Drupal\libraries\ExternalLibrary\LibraryType\LibraryTypeInterface;
use Drupal\libraries\ExternalLibrary\Local\LocalLibraryInterface;
use Drupal\libraries\ExternalLibrary\Utility\IdAccessorTrait;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
......@@ -24,6 +26,8 @@ class AssetLibraryType implements
ContainerFactoryPluginInterface
{
use IdAccessorTrait;
/**
* The locator factory.
*
......@@ -32,12 +36,15 @@ class AssetLibraryType implements
protected $locatorFactory;
/**
* Constructs the PHP file library type.
* Constructs the asset library type.
*
* @param string $plugin_id
* The plugin ID taken from the class annotation.
* @param \Drupal\Component\Plugin\Factory\FactoryInterface $locator_factory
* The locator factory.
*/
public function __construct(FactoryInterface $locator_factory) {
public function __construct($plugin_id, FactoryInterface $locator_factory) {
$this->id = $plugin_id;
$this->locatorFactory = $locator_factory;
}
......@@ -45,23 +52,14 @@ class AssetLibraryType implements
* {@inheritdoc}
*/
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static($container->get('plugin.manager.libraries.locator'));
}
/**
* {@inheritdoc}
*/
public function getId() {
// @todo Remove the duplication with the annotation.
return 'asset';
return new static($plugin_id, $container->get('plugin.manager.libraries.locator'));
}
/**
* {@inheritdoc}
*/
public function getLibraryClass() {
// @todo Make this alter-able.
return 'Drupal\libraries\ExternalLibrary\Asset\AssetLibrary';
return AssetLibrary::class;
}
/**
......
......@@ -13,7 +13,9 @@ use Drupal\libraries\ExternalLibrary\LibraryInterface;
use Drupal\libraries\ExternalLibrary\LibraryType\LibraryCreationListenerInterface;
use Drupal\libraries\ExternalLibrary\LibraryType\LibraryLoadingListenerInterface;
use Drupal\libraries\ExternalLibrary\LibraryType\LibraryTypeInterface;
use Drupal\libraries\ExternalLibrary\PhpFile\PhpFileLibrary;
use Drupal\libraries\ExternalLibrary\PhpFile\PhpFileLoaderInterface;
use Drupal\libraries\ExternalLibrary\Utility\IdAccessorTrait;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
......@@ -26,6 +28,8 @@ class PhpFileLibraryType implements
ContainerFactoryPluginInterface
{
use IdAccessorTrait;
/**
* The locator factory.
*
......@@ -43,12 +47,15 @@ class PhpFileLibraryType implements
/**
* Constructs the PHP file library type.
*
* @param string $plugin_id
* The plugin ID taken from the class annotation.
* @param \Drupal\Component\Plugin\Factory\FactoryInterface $locator_factory
* The locator factory.
* @param \Drupal\libraries\ExternalLibrary\PhpFile\PhpFileLoaderInterface $php_file_loader
* The PHP file loader.
*/
public function __construct(FactoryInterface $locator_factory, PhpFileLoaderInterface $php_file_loader) {
public function __construct($plugin_id, FactoryInterface $locator_factory, PhpFileLoaderInterface $php_file_loader) {
$this->id = $plugin_id;
$this->locatorFactory = $locator_factory;
$this->phpFileLoader = $php_file_loader;
}
......@@ -58,25 +65,17 @@ class PhpFileLibraryType implements
*/
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static(
$plugin_id,
$container->get('plugin.manager.libraries.locator'),
$container->get('libraries.php_file_loader')
);
}
/**
* {@inheritdoc}
*/
public function getId() {
// @todo Remove the duplication with the annotation.
return 'php_file';
}
/**
* {@inheritdoc}
*/
public function getLibraryClass() {
// @todo Make this alter-able.
return 'Drupal\libraries\ExternalLibrary\PhpFile\PhpFileLibrary';
return PhpFileLibrary::class;
}
/**
......
id: test_asset_library
type: asset
remote_url: http://example.com
css:
......
id: test_php_file_library
type: php_file
files: [test_php_file_library.php]
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment