3 namespace Drupal\libraries\ExternalLibrary\Type;
5 use Drupal\Component\Plugin\Factory\FactoryInterface;
6 use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
7 use Drupal\libraries\ExternalLibrary\LibraryInterface;
8 use Drupal\libraries\ExternalLibrary\Local\LocalLibraryInterface;
9 use Drupal\libraries\ExternalLibrary\Utility\IdAccessorTrait;
10 use Drupal\libraries\ExternalLibrary\Version\VersionedLibraryInterface;
11 use Symfony\Component\DependencyInjection\ContainerInterface;
14 * Provides a base class for library types.
16 abstract class LibraryTypeBase implements
18 LibraryCreationListenerInterface,
19 ContainerFactoryPluginInterface
25 * The locator factory.
27 * @var \Drupal\Component\Plugin\Factory\FactoryInterface
29 protected $locatorFactory;
32 * The version detector factory.
34 * @var \Drupal\Component\Plugin\Factory\FactoryInterface
36 protected $detectorFactory;
39 * Constructs the asset library type.
41 * @param string $plugin_id
42 * The plugin ID taken from the class annotation.
43 * @param \Drupal\Component\Plugin\Factory\FactoryInterface $locator_factory
44 * The locator factory.
45 * @param \Drupal\Component\Plugin\Factory\FactoryInterface $detector_factory
46 * The version detector factory.
48 public function __construct($plugin_id, FactoryInterface $locator_factory, FactoryInterface $detector_factory) {
49 $this->id = $plugin_id;
50 $this->locatorFactory = $locator_factory;
51 $this->detectorFactory = $detector_factory;
57 public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
60 $container->get('plugin.manager.libraries.locator'),
61 $container->get('plugin.manager.libraries.version_detector')
68 public function onLibraryCreate(LibraryInterface $library) {
69 if ($library instanceof LocalLibraryInterface) {
70 $library->getLocator($this->locatorFactory)->locate($library);
71 // Fallback on global locators.
72 // @todo Consider if global locators should be checked as a fallback or as
73 // the primary locator source.
74 if (!$library->isInstalled()) {
75 $this->locatorFactory->createInstance('global')->locate($library);
77 // Also fetch version information.
78 if ($library instanceof VersionedLibraryInterface) {
79 // @todo Consider if this should be wrapped in some conditional logic
80 // or exception handling so that version detection errors do not
81 // prevent a library from being loaded.
82 $library->getVersionDetector($this->detectorFactory)->detectVersion($library);