6cd9f59d099c8b3d9f88dca4b27297088b595efe
[yaffs-website] / web / modules / contrib / libraries / src / ExternalLibrary / Type / LibraryTypeBase.php
1 <?php
2
3 namespace Drupal\libraries\ExternalLibrary\Type;
4
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;
12
13 /**
14  * Provides a base class for library types.
15  */
16 abstract class LibraryTypeBase implements
17   LibraryTypeInterface,
18   LibraryCreationListenerInterface,
19   ContainerFactoryPluginInterface
20 {
21
22   use IdAccessorTrait;
23
24   /**
25    * The locator factory.
26    *
27    * @var \Drupal\Component\Plugin\Factory\FactoryInterface
28    */
29   protected $locatorFactory;
30
31   /**
32    * The version detector factory.
33    *
34    * @var \Drupal\Component\Plugin\Factory\FactoryInterface
35    */
36   protected $detectorFactory;
37
38   /**
39    * Constructs the asset library type.
40    *
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.
47    */
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;
52   }
53
54   /**
55    * {@inheritdoc}
56    */
57   public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
58     return new static(
59       $plugin_id,
60       $container->get('plugin.manager.libraries.locator'),
61       $container->get('plugin.manager.libraries.version_detector')
62     );
63   }
64
65   /**
66    * {@inheritdoc}
67    */
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);
76       }
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);
83       }
84     }
85   }
86
87 }