Code Coverage |
||||||||||||||||
Lines |
Branches |
Paths |
Functions and Methods |
Classes and Traits |
||||||||||||
| Total | |
0.00% |
0 / 18 |
|
0.00% |
0 / 10 |
|
0.00% |
0 / 7 |
|
0.00% |
0 / 2 |
CRAP | |
0.00% |
0 / 1 |
| LibrariesSwitcher | |
0.00% |
0 / 18 |
|
0.00% |
0 / 10 |
|
0.00% |
0 / 7 |
|
0.00% |
0 / 2 |
42 | |
0.00% |
0 / 1 |
| __construct | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| switchLibraries | |
0.00% |
0 / 17 |
|
0.00% |
0 / 9 |
|
0.00% |
0 / 6 |
|
0.00% |
0 / 1 |
30 | |||
| 1 | <?php |
| 2 | |
| 3 | declare(strict_types=1); |
| 4 | |
| 5 | namespace Drupal\display_builder\Hook; |
| 6 | |
| 7 | use Drupal\Core\Hook\Attribute\Hook; |
| 8 | use Drupal\Core\State\StateInterface; |
| 9 | |
| 10 | /** |
| 11 | * Hook implementations for display_builder. |
| 12 | */ |
| 13 | class LibrariesSwitcher { |
| 14 | |
| 15 | private const STATE_STORAGE_KEY = 'display_builder.asset_libraries_local'; |
| 16 | |
| 17 | public function __construct( |
| 18 | protected StateInterface $state, |
| 19 | ) {} |
| 20 | |
| 21 | /** |
| 22 | * Switch libraries according to environment. |
| 23 | * |
| 24 | * @param array $libraries |
| 25 | * An associative array of libraries, passed by reference. The array key |
| 26 | * for any particular library will be the name from *.libraries.yml. |
| 27 | * @param string $extension |
| 28 | * Can either be 'core' or the machine name of the extension that registered |
| 29 | * the libraries. |
| 30 | */ |
| 31 | #[Hook('library_info_alter')] |
| 32 | public function switchLibraries(array &$libraries, string $extension): void { |
| 33 | if ($extension !== 'display_builder') { |
| 34 | return; |
| 35 | } |
| 36 | |
| 37 | // By default, libraries are retrieved from CDN. Switch to local mode with |
| 38 | // a state value. |
| 39 | // This is needed for tests or disconnected environments. |
| 40 | $is_local = $this->state->get(self::STATE_STORAGE_KEY, FALSE); |
| 41 | |
| 42 | if (!$is_local) { |
| 43 | return; |
| 44 | } |
| 45 | |
| 46 | if (isset($libraries['shoelace']['dependencies'])) { |
| 47 | $libraries['shoelace']['dependencies'] = \array_filter( |
| 48 | $libraries['shoelace']['dependencies'], |
| 49 | static fn ($dependency) => $dependency !== 'display_builder/_shoelace_cdn' |
| 50 | ); |
| 51 | $libraries['shoelace']['dependencies'][] = 'display_builder/_shoelace_local'; |
| 52 | } |
| 53 | |
| 54 | if (isset($libraries['htmx_sse']['dependencies'])) { |
| 55 | $libraries['htmx_sse']['dependencies'] = \array_filter( |
| 56 | $libraries['htmx_sse']['dependencies'], |
| 57 | static fn ($dependency) => $dependency !== 'display_builder/_htmx_sse_cdn' |
| 58 | ); |
| 59 | $libraries['htmx_sse']['dependencies'][] = 'display_builder/_htmx_sse_local'; |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | } |