aa93b003c2976c84935cb04afb770a8b88c22d33
[yaffs-website] / web / modules / contrib / libraries / src / Plugin / libraries / Type / PhpFileLibraryType.php
1 <?php
2
3 namespace Drupal\libraries\Plugin\libraries\Type;
4
5 use Drupal\Component\Plugin\Factory\FactoryInterface;
6 use Drupal\libraries\ExternalLibrary\LibraryInterface;
7 use Drupal\libraries\ExternalLibrary\Type\LibraryLoadingListenerInterface;
8 use Drupal\libraries\ExternalLibrary\PhpFile\PhpFileLibrary;
9 use Drupal\libraries\ExternalLibrary\PhpFile\PhpFileLoaderInterface;
10 use Drupal\libraries\ExternalLibrary\Type\LibraryTypeBase;
11 use Symfony\Component\DependencyInjection\ContainerInterface;
12
13 /**
14  * @LibraryType("php_file")
15  */
16 class PhpFileLibraryType extends LibraryTypeBase implements LibraryLoadingListenerInterface {
17
18   /**
19    * The PHP file loader.
20    *
21    * @var \Drupal\libraries\ExternalLibrary\PhpFile\PhpFileLoaderInterface
22    */
23   protected $phpFileLoader;
24
25   /**
26    * Constructs the PHP file library type.
27    *
28    * @param string $plugin_id
29    *   The plugin ID taken from the class annotation.
30    * @param \Drupal\Component\Plugin\Factory\FactoryInterface $locator_factory
31    *   The locator factory.
32    * @param \Drupal\Component\Plugin\Factory\FactoryInterface $detector_factory
33    *   The version detector factory.
34    * @param \Drupal\libraries\ExternalLibrary\PhpFile\PhpFileLoaderInterface $php_file_loader
35    *   The PHP file loader.
36    */
37   public function __construct($plugin_id, FactoryInterface $locator_factory, FactoryInterface $detector_factory, PhpFileLoaderInterface $php_file_loader) {
38     parent::__construct($plugin_id, $locator_factory, $detector_factory);
39     $this->phpFileLoader = $php_file_loader;
40   }
41
42   /**
43    * {@inheritdoc}
44    */
45   public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
46     return new static(
47       $plugin_id,
48       $container->get('plugin.manager.libraries.locator'),
49       $container->get('plugin.manager.libraries.version_detector'),
50       $container->get('libraries.php_file_loader')
51     );
52   }
53
54   /**
55    * {@inheritdoc}
56    */
57   public function getLibraryClass() {
58     return PhpFileLibrary::class;
59   }
60
61   /**
62    * {@inheritdoc}
63    */
64   public function onLibraryLoad(LibraryInterface $library) {
65     /** @var \Drupal\libraries\ExternalLibrary\PhpFile\PhpFileLibraryInterface $library */
66     // @todo Prevent loading a library multiple times.
67     foreach ($library->getPhpFiles() as $file) {
68       $this->phpFileLoader->load($file);
69     }
70   }
71
72 }