Further modules included.
[yaffs-website] / web / modules / contrib / libraries / src / Plugin / libraries / Locator / GlobalLocator.php
1 <?php
2
3 namespace Drupal\libraries\Plugin\libraries\Locator;
4
5 use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
6 use Drupal\Core\Config\ConfigFactoryInterface;
7 use Drupal\Component\Plugin\Factory\FactoryInterface;
8 use Drupal\libraries\ExternalLibrary\Local\LocalLibraryInterface;
9 use Drupal\libraries\ExternalLibrary\Local\LocatorInterface;
10 use Symfony\Component\DependencyInjection\ContainerInterface;
11
12 /**
13  * Provides a locator based on global configuration.
14  *
15  * @Locator("global")
16  *
17  * @see \Drupal\libraries\ExternalLibrary\Local\LocatorInterface
18  */
19 class GlobalLocator implements LocatorInterface, ContainerFactoryPluginInterface {
20
21   /**
22    * The Drupal config factory service.
23    *
24    * @var \Drupal\Core\Config\ConfigFactoryInterface
25    */
26   protected $configFactory;
27
28   /**
29    * The locator factory.
30    *
31    * @var \Drupal\Component\Plugin\Factory\FactoryInterface
32    */
33   protected $locatorFactory;
34
35   /**
36    * Constructs a global locator.
37    *
38    * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
39    *   The Drupal config factory service.
40    * @param \Drupal\Component\Plugin\Factory\FactoryInterface $locator_factory
41    *   The locator factory.
42    */
43   public function __construct(ConfigFactoryInterface $config_factory, FactoryInterface $locator_factory) {
44     $this->configFactory = $config_factory;
45     $this->locatorFactory = $locator_factory;
46   }
47
48   /**
49    * {@inheritdoc}
50    */
51   public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
52     return new static(
53       $container->get('config.factory'),
54       $container->get('plugin.manager.libraries.locator')
55     );
56   }
57
58   /**
59    * Locates a library.
60    *
61    * @param \Drupal\libraries\ExternalLibrary\Local\LocalLibraryInterface $library
62    *   The library to locate.
63    *
64    * @see \Drupal\libraries\ExternalLibrary\Local\LocatorInterface::locate()
65    */
66   public function locate(LocalLibraryInterface $library) {
67     foreach ($this->configFactory->get('libraries.settings')->get('global_locators') as $locator) {
68       $this->locatorFactory->createInstance($locator['id'], $locator['configuration'])->locate($library);
69       if ($library->isInstalled()) {
70         return;
71       }
72     }
73     $library->setUninstalled();
74   }
75
76 }