Skip to content
Snippets Groups Projects

Issue #3365714: move code out of sdc.module

Files

@@ -12,7 +12,9 @@
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\Core\Serialization\Yaml;
use Drupal\Core\StreamWrapper\StreamWrapperManagerInterface;
use Drupal\Core\Theme\ComponentPluginManager;
use Drupal\Core\Theme\ThemeManagerInterface;
use Drupal\sdc\Plugin\Component;
/**
* Parses library files to get extension data.
@@ -355,12 +357,14 @@ public function buildByExtension($extension) {
* Thrown when a parser exception got thrown.
*/
protected function parseLibraryInfo($extension, $path) {
$libraries = [];
$libraries = $extension === 'core'
? $this->librariesForComponents()
: [];
$library_file = $path . '/' . $extension . '.libraries.yml';
if (file_exists($this->root . '/' . $library_file)) {
try {
$libraries = Yaml::decode(file_get_contents($this->root . '/' . $library_file)) ?? [];
$libraries += Yaml::decode(file_get_contents($this->root . '/' . $library_file)) ?? [];
}
catch (InvalidDataTypeException $e) {
// Rethrow a more helpful exception to provide context.
@@ -381,6 +385,54 @@ protected function parseLibraryInfo($extension, $path) {
return $libraries;
}
/**
* Builds the dynamic library definitions for single directory components.
*
* @return array
* The core library definitions for Single Directory Components.
*/
protected function librariesForComponents(): array {
// Iterate over all the components to get the CSS and JS files.
$components = $this->componentPluginManager()->getAllComponents();
$libraries = array_reduce(
$components,
static function (array $libraries, Component $component) {
$library = $component->library;
if (empty($library)) {
return $libraries;
}
$library_name = $component->getLibraryName();
[, $library_id] = explode('/', $library_name);
return array_merge($libraries, [$library_id => $library]);
},
[]
);
$libraries['components.all'] = [
'dependencies' => array_map(
static fn(Component $component) => $component->getLibraryName(),
$components
),
];
return $libraries;
}
/**
* Get the component plugin manager.
*
* The library discovery parser is a public service, and we cannot alter the
* constructor signature to inject this dependency without breaking backwards
* compatibility in Drupal 10.
*
* @return \Drupal\Core\Theme\ComponentPluginManager
* The component plugin manager.
*/
private function componentPluginManager(): ComponentPluginManager {
if (!isset($this->componentPluginManager)) {
$this->componentPluginManager = \Drupal::service('plugin.manager.sdc');
}
return $this->componentPluginManager;
}
/**
* Apply libraries overrides specified for the current active theme.
*
Loading