Interim commit.
[yaffs-website] / web / modules / contrib / bootstrap_layouts / src / BootstrapLayoutsPluginManager.php
1 <?php
2
3 namespace Drupal\bootstrap_layouts;
4
5 use Drupal\Core\Cache\CacheBackendInterface;
6 use Drupal\Core\DependencyInjection\ContainerInjectionInterface;
7 use Drupal\Core\Extension\ModuleHandlerInterface;
8 use Drupal\Core\Extension\ThemeHandlerInterface;
9 use Drupal\Core\Plugin\DefaultPluginManager;
10 use Drupal\Core\StringTranslation\StringTranslationTrait;
11 use Drupal\Core\Theme\ThemeManagerInterface;
12 use Symfony\Component\DependencyInjection\ContainerAwareInterface;
13 use Symfony\Component\DependencyInjection\ContainerAwareTrait;
14 use Symfony\Component\DependencyInjection\ContainerInterface;
15
16 class BootstrapLayoutsPluginManager extends DefaultPluginManager implements ContainerInjectionInterface, ContainerAwareInterface {
17
18   use ContainerAwareTrait;
19   use StringTranslationTrait;
20
21   /**
22    * @var \Drupal\Core\Extension\ThemeHandlerInterface
23    */
24   protected $themeHandler;
25
26   /**
27    * @var \Drupal\Core\Theme\ThemeManagerInterface
28    */
29   protected $themeManager;
30
31   /**
32    * Base plugin manager for Bootstrap Layouts plugin managers.
33    *
34    * The "container.namespaces" service does not contain theme namespaces
35    * since themes are not registered in the container. To allow themes to be
36    * able to participate in these plugins, the normal "namespaces" provided
37    * must be appending with the missing autoloader prefixes of the themes.
38    *
39    * @param \Traversable $namespaces
40    *   An object that implements \Traversable which contains the root paths
41    *   keyed by the corresponding namespace to look for plugin implementations.
42    * @param \Drupal\Core\Cache\CacheBackendInterface $cache_backend
43    *   Cache backend instance to use.
44    * @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler
45    *   The module handler to invoke the alter hook with.
46    * @param \Drupal\Core\Extension\ThemeHandlerInterface $theme_handler
47    *   The theme manager used to invoke the alter hook with.
48    * @param \Drupal\Core\Theme\ThemeManagerInterface $theme_manager
49    *   The theme manager used to invoke the alter hook with.
50    * @param string|null $plugin_interface
51    *   (optional) The interface each plugin should implement.
52    * @param string $plugin_definition_annotation_name
53    *   (optional) The name of the annotation that contains the plugin definition.
54    *   Defaults to 'Drupal\Component\Annotation\Plugin'.
55    */
56   public function __construct(\Traversable $namespaces, CacheBackendInterface $cache_backend, ModuleHandlerInterface $module_handler, ThemeHandlerInterface $theme_handler, ThemeManagerInterface $theme_manager, $plugin_interface = NULL, $plugin_definition_annotation_name = 'Drupal\Component\Annotation\Plugin') {
57     /** @var \Composer\Autoload\ClassLoader $class_loader */
58     $class_loader = \Drupal::service('class_loader');
59
60     /** @var \ArrayObject $namespaces */
61     $ns = $namespaces->getArrayCopy();
62
63     foreach ($class_loader->getPrefixesPsr4() as $prefix => $paths) {
64       // Remove trailing path separators.
65       $prefix = trim($prefix, '\\');
66
67       // Remove the DRUPAL_ROOT prefix.
68       $path = str_replace(\Drupal::root() . '/', '', reset($paths));
69
70       // Only add missing contrib theme namespaces.
71       if (preg_match('/^(core|vendor)/', $path) === 0 && !isset($namespaces[$prefix])) {
72         $ns[$prefix] = $path;
73       }
74     }
75
76     // Replace the namespaces data.
77     $namespaces->exchangeArray($ns);
78
79     // Construct the plugin manager now.
80     parent::__construct('Plugin/BootstrapLayouts', $namespaces, $module_handler, $plugin_interface, $plugin_definition_annotation_name);
81
82     // Set the theme handler and manager.
83     $this->themeHandler = $theme_handler;
84     $this->themeManager = $theme_manager;
85   }
86
87   /**
88    * {@inheritdoc}
89    */
90   public static function create(ContainerInterface $container) {
91     return new static(
92       $container->get('container.namespaces'),
93       $container->get('cache.discovery'),
94       $container->get('module_handler'),
95       $container->get('theme_handler'),
96       $container->get('theme.manager')
97     );
98   }
99
100   /**
101    * {@inheritdoc}
102    */
103   protected function alterDefinitions(&$definitions) {
104     if ($this->alterHook) {
105       $this->moduleHandler->alter($this->alterHook, $definitions);
106       $this->themeManager->alter($this->alterHook, $definitions);
107     }
108   }
109
110   /**
111    * {@inheritdoc}
112    */
113   protected function providerExists($provider) {
114     return $this->moduleHandler->moduleExists($provider) || $this->themeHandler->themeExists($provider);
115   }
116
117 }