Skip to content
Snippets Groups Projects
Unverified Commit 96230f0f authored by Mateu Aguiló Bosch's avatar Mateu Aguiló Bosch Committed by Mateu Aguiló Bosch
Browse files

Issue #3309773 by e0ipso: Forking a component can cause hooks to re-declare functions

parent 00a96de9
No related branches found
No related tags found
No related merge requests found
......@@ -43,7 +43,9 @@ final class ComponentAudit extends ControllerBase {
* The render array.
*/
public function audit(): array {
$components = $this->discovery->findAll();
$paths = $this->discovery->findAllPaths();
$build_component_card = fn(string $path) => $this->buildComponentCard($path, $components);
return [
'logs' => [
'#prefix' => '<p>',
......@@ -57,7 +59,7 @@ final class ComponentAudit extends ControllerBase {
'panels' => [
'#theme' => 'item_list',
'#title' => $this->t('Detected Components'),
'#items' => array_map([$this, 'buildComponentCard'], $paths),
'#items' => array_map($build_component_card, $paths),
],
'recommendations' => [
'#prefix' => '<p>',
......@@ -111,13 +113,19 @@ final class ComponentAudit extends ControllerBase {
*
* @param string $path
* The path to the component folder.
* @param Component[] $components
* The components without duplications (for forked components).
*
* @return array[]
* The card render array.
*/
private function buildComponentCard(string $path): array {
private function buildComponentCard(string $path, array $components): array {
try {
$component = $this->discovery->instantiateComponent($path);
$matches = array_filter($components, static fn (Component $cmp) => $cmp->getId() === $component->getId());
// We should only have one match.
$match = reset($matches);
$is_forked = $match->getMetadata()->getPath() !== $component->getMetadata()->getPath();
}
catch (InvalidComponentException $e) {
$component = NULL;
......@@ -178,24 +186,32 @@ final class ComponentAudit extends ControllerBase {
]
),
];
$hooks = $metadata->getHooks();
$hooks_message = empty($hooks)
? [
if (!$is_forked) {
$hooks_message = empty($hooks)
? [
'#type' => 'html_tag',
'#tag' => 'em',
'#value' => $this->t('No hooks in <code>@filename</code>.', ['@filename' => $component->getId() . '.php']),
]
: [
'#theme' => 'item_list',
'#items' => array_map(
static fn(string $hook) => ['#type' => 'html_tag', '#tag' => 'code', '#value' => $hook],
$hooks
),
];
}
else {
$hooks_message = [
'#type' => 'html_tag',
'#tag' => 'em',
'#value' => $this->t('No hooks in <code>@filename</code>.', ['@filename' => $component->getId() . '.php']),
]
: [
'#theme' => 'item_list',
'#items' => array_map(
static fn(string $hook) => ['#type' => 'html_tag', '#tag' => 'code', '#value' => $hook],
$hooks
),
];
'#value' => $this->t('Forked at <code>@filename</code>.', ['@filename' => $match->getMetadata()->getPath()]),
];
}
$card_build = [
'title' => [
'#theme' => 'cl_label_with_link',
'#label' => $metadata->getName(),
'#label' => $metadata->getName() . ($is_forked ? ' ❗' : ''),
'#href' => '#' . $component->getId(),
],
'description' => $metadata
......@@ -209,6 +225,9 @@ final class ComponentAudit extends ControllerBase {
'#markup' => $path,
'#suffix' => '</pre>',
],
'forked' => $is_forked ? [
'#markup' => $this->t('When rendering this component by ID, this other component will render instead: <code>@filename</code>.', ['@filename' => $match->getMetadata()->getPath()]),
] : [],
'table' => [
'#theme' => 'table',
'#header' => [
......@@ -216,7 +235,7 @@ final class ComponentAudit extends ControllerBase {
$this->t('Default Template'),
$this->t('Variants'),
$this->t('Assets'),
$this->t('Implemented Hooks'),
$is_forked ? $this->t('🍴 Component Forked 🍴') : $this->t('Implemented Hooks'),
],
'#rows' => [
[
......@@ -229,12 +248,14 @@ final class ComponentAudit extends ControllerBase {
],
],
'#wrapper_attributes' => [
'class' => 'panel-item',
'class' => ['panel-item', $is_forked ? 'forked' : 'not-forked'],
'id' => $component->getId(),
],
];
$this->moduleHandler()
->alter('cl_component_audit', $card_build, $component);
if (!$is_forked) {
$this->moduleHandler()
->alter('cl_component_audit', $card_build, $component);
}
return $card_build;
}
......
......@@ -15,6 +15,9 @@
box-shadow: 1px 1px 5px rgba(0, 0, 0, 0.3);
}
.item-list li.panel-item.forked {
background-image: repeating-linear-gradient(60deg, rgba(255, 238, 204, 0.3), rgba(255, 238, 204, 0.3) 30px, transparent 30px, transparent 60px);
}
h4 a.panel-item--anchor {
visibility: hidden;
margin-left: 0.5rem;
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment