LibraryDeriver.php in UI Patterns 8
File
modules/ui_patterns_library/src/Plugin/Deriver/LibraryDeriver.php
View source
<?php
namespace Drupal\ui_patterns_library\Plugin\Deriver;
use Drupal\Component\Serialization\Yaml;
use Drupal\Core\Extension\ExtensionDiscovery;
use Drupal\Core\File\FileSystemInterface;
use Drupal\Core\Messenger\MessengerInterface;
use Drupal\Core\TypedData\TypedDataManager;
use Drupal\ui_patterns\Plugin\Deriver\AbstractYamlPatternsDeriver;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\Core\Extension\ThemeHandlerInterface;
class LibraryDeriver extends AbstractYamlPatternsDeriver {
protected $basePluginId;
protected $suffixes;
protected $root;
protected $themeHandler;
protected $moduleHandler;
protected $extensionDiscovery;
protected $extensionLocations = [];
protected $fileExtensions = [];
public function __construct($base_plugin_id, TypedDataManager $typed_data_manager, MessengerInterface $messenger, FileSystemInterface $file_system, $root, array $extensions, ModuleHandlerInterface $module_handler, ThemeHandlerInterface $theme_handler) {
parent::__construct($base_plugin_id, $typed_data_manager, $messenger, $file_system);
$this->root = $root;
$this->fileExtensions = $extensions;
$this->moduleHandler = $module_handler;
$this->themeHandler = $theme_handler;
$this->extensionDiscovery = new ExtensionDiscovery($root);
}
public static function create(ContainerInterface $container, $base_plugin_id) {
return new static($base_plugin_id, $container
->get('typed_data_manager'), $container
->get('messenger'), $container
->get('file_system'), $container
->get('app.root'), $container
->getParameter('ui_patterns_library.file_extensions'), $container
->get('module_handler'), $container
->get('theme_handler'));
}
public function getFileExtensions() {
return $this->fileExtensions;
}
public function getPatterns() {
$patterns = [];
foreach ($this
->getDirectories() as $provider => $directory) {
foreach ($this
->fileScanDirectory($directory) as $file_path => $file) {
$host_extension = $this
->getHostExtension($file_path);
if ($host_extension == FALSE || $host_extension == $provider) {
$content = file_get_contents($file_path);
foreach (Yaml::decode($content) as $id => $definition) {
$definition['id'] = $id;
$definition['base path'] = dirname($file_path);
$definition['file name'] = basename($file_path);
$definition['provider'] = $provider;
$patterns[] = $this
->getPatternDefinition($definition);
}
}
}
}
return $patterns;
}
protected function getDirectories() {
$default_theme = $this->themeHandler
->getDefault();
$base_themes = $this->themeHandler
->getBaseThemes($this->themeHandler
->listInfo(), $default_theme);
$theme_directories = $this->themeHandler
->getThemeDirectories();
$directories = [];
if (isset($theme_directories[$default_theme])) {
$directories[$default_theme] = $theme_directories[$default_theme];
foreach ($base_themes as $name => $theme) {
$directories[$name] = $theme_directories[$name];
}
}
return $directories + $this->moduleHandler
->getModuleDirectories();
}
protected function getHostExtension($pathname) {
$extensions = $this
->getExtensionLocations();
$parts = explode(DIRECTORY_SEPARATOR, $pathname);
while (!empty($parts)) {
$path = implode(DIRECTORY_SEPARATOR, $parts);
if (isset($extensions[$path])) {
return $extensions[$path];
}
array_pop($parts);
}
return FALSE;
}
protected function getExtensionLocations() {
if (empty($this->extensionLocations)) {
$extensions = $this->extensionDiscovery
->scan('theme') + $this->extensionDiscovery
->scan('module');
foreach ($extensions as $name => $extension) {
$this->extensionLocations[$this->root . DIRECTORY_SEPARATOR . $extension
->getPath()] = $name;
}
}
return $this->extensionLocations;
}
}