UiPatternsManager.php in UI Patterns 8
File
src/UiPatternsManager.php
View source
<?php
namespace Drupal\ui_patterns;
use Drupal\Component\Plugin\PluginManagerInterface;
use Drupal\Core\StringTranslation\StringTranslationTrait;
use Drupal\Core\Cache\CacheBackendInterface;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\Core\Extension\ThemeHandlerInterface;
use Drupal\Core\Plugin\DefaultPluginManager;
class UiPatternsManager extends DefaultPluginManager implements PluginManagerInterface {
use StringTranslationTrait;
protected $themeHandler;
protected $patternHooks = [];
public function __construct(\Traversable $namespaces, ModuleHandlerInterface $module_handler, ThemeHandlerInterface $theme_handler, CacheBackendInterface $cache_backend) {
parent::__construct('Plugin/UiPatterns/Pattern', $namespaces, $module_handler, 'Drupal\\ui_patterns\\Plugin\\PatternInterface', 'Drupal\\ui_patterns\\Annotation\\UiPattern');
$this->moduleHandler = $module_handler;
$this->themeHandler = $theme_handler;
$this
->alterInfo('ui_patterns_info');
$this
->setCacheBackend($cache_backend, 'ui_patterns', [
'ui_patterns',
]);
}
public function getPatterns() {
$patterns = [];
foreach ($this
->getDefinitions() as $definition) {
$patterns[] = $this
->getFactory()
->createInstance($definition
->id());
}
return $patterns;
}
public function getDefinitions() {
$definitions = $this
->getCachedDefinitions();
if (!isset($definitions)) {
$definitions = [];
foreach ($this
->findDefinitions() as $id => $definition) {
$definitions[$definition['id']] = $definition;
unset($definitions[$id]);
}
$this
->setCachedDefinitions($definitions);
}
return $definitions;
}
public function getPatternsOptions() {
return array_map(function ($option) {
return $option['label'];
}, $this
->getDefinitions());
}
public function isPatternHook($hook) {
if (empty($this->patternHooks)) {
foreach ($this
->getDefinitions() as $definition) {
$this->patternHooks[$definition
->getThemeHook()] = $definition
->getThemeHook();
}
}
return !empty($this->patternHooks[$hook]);
}
protected function providerExists($provider) {
return $this->moduleHandler
->moduleExists($provider) || $this->themeHandler
->themeExists($provider);
}
}