Loading src/Component/ComponentDiscovery.php +21 −5 Changes for src/Component/ComponentDiscovery.php: 21 added lines, 5 removed lines. Original line number Diff line number Diff line Loading @@ -148,9 +148,20 @@ class ComponentDiscovery { if (isset($this->components)) { return array_values($this->components); } $components_by_key = fn(array $data) => array_reduce( $components_by_id = static fn(array $data) => array_reduce( $data, static fn(array $all, Component $c) => [...$all, $c->getId() => $c], static function (array $all, Component $c) { // If there are multiple candidates with the same ID only save the one // with the lowest weight. $existing_component = $all[$c->getId()] ?? NULL; if (!$existing_component) { return [...$all, $c->getId() => $c]; } assert($existing_component instanceof Component); $existing_weight = $existing_component->getMetadata()->getWeight(); $candidate_weight = $c->getMetadata()->getWeight(); return $existing_weight > $candidate_weight ? [...$all, $c->getId() => $c] : $all; }, [] ); $cache_ids = $this->cache->get('all-cache-ids'); Loading @@ -158,7 +169,7 @@ class ComponentDiscovery { $cache_entries = $this->cache->getMultiple($cache_ids->data); // Get the data of the cache properties. $cache_data = array_map( fn(object $item) => $item->data, static fn(object $item) => $item->data, $cache_entries ); // Ensure cache data deserializes to a Component. Loading @@ -166,13 +177,13 @@ class ComponentDiscovery { $cache_data, static fn(mixed $item) => $item instanceof Component ); $this->components = $components_by_key($cached_components); $this->components = $components_by_id($cached_components); return array_values($this->components); } $paths = $this->findAllPaths(); $components = array_map([$this, 'createComponent'], $paths); $components = array_filter($components); $this->components = $components_by_key($components); $this->components = $components_by_id($components); $this->cache->set( 'all-cache-ids', array_map( Loading Loading @@ -367,6 +378,11 @@ class ComponentDiscovery { $components, static fn(Component $c) => $c->getId() === $id ); // Make sure all components that match the ID are sorted by weight. usort( $matches, static fn(Component $a, Component $b) => $b->getMetadata()->getWeight() - $a->getMetadata()->getWeight() ); $component = reset($matches); if ($component instanceof Component) { return $component; Loading src/Component/ComponentMetadata.php +26 −5 Changes for src/Component/ComponentMetadata.php: 26 added lines, 5 removed lines. Original line number Diff line number Diff line Loading @@ -142,6 +142,13 @@ final class ComponentMetadata { */ private string $phpPath; /** * The weight of the component. * * @var int */ private int $weight; /** * ComponentMetadata constructor. * Loading Loading @@ -179,6 +186,7 @@ final class ComponentMetadata { $this->status = $metadata_info['status'] ?? static::COMPONENT_STATUS_WIP; $this->componentType = $metadata_info['componentType'] ?? static::COMPONENT_TYPE_ORGANISM; $this->libraryDependencies = $metadata_info['libraryDependencies'] ?? []; $this->weight = (int) ($metadata_info['weight'] ?? 0); // Load the PNG. $thumbnail_path = sprintf('%s/thumbnail.png', $path); Loading @@ -192,11 +200,6 @@ final class ComponentMetadata { $this->parseSchemaInfo($metadata_info); // Parse the hooks. $this->safeName = preg_replace('@[^a-z0-9]+@', '_', strtolower($this->machineName)); $php_file = new ComponentPhpFile($this->path, $this->machineName, $this->safeName); $this->phpPath = $php_file->path; if ($php_file->hasPhpFile()) { $this->hooks = $php_file->parseHooks(); } } /** Loading Loading @@ -505,8 +508,16 @@ final class ComponentMetadata { * * @return string[] * The hook names. * * @throws \Drupal\cl_components\Exception\ComponentSyntaxException */ public function getHooks(): array { if (isset($this->hooks)) { return $this->hooks; } $php_file = new ComponentPhpFile($this->path, $this->machineName, $this->safeName); $this->phpPath = $php_file->path; $this->hooks = $php_file->hasPhpFile() ? $php_file->parseHooks() : []; return $this->hooks; } Loading @@ -520,4 +531,14 @@ final class ComponentMetadata { return $this->phpPath; } /** * Gets the component weight when more than one by ID exist. * * @return int * The weight. */ public function getWeight(): int { return $this->weight; } } src/metadata.schema.json +5 −0 Changes for src/metadata.schema.json: 5 added lines, 0 removed lines. Original line number Diff line number Diff line Loading @@ -62,6 +62,11 @@ "DEPRECATED" ] }, "weight": { "type": "integer", "title": "Weight", "description": "When two components share the same ID the one with the lowest weight will be rendered." }, "variants": { "type": "array", "title": "Variants", Loading Loading
src/Component/ComponentDiscovery.php +21 −5 Changes for src/Component/ComponentDiscovery.php: 21 added lines, 5 removed lines. Original line number Diff line number Diff line Loading @@ -148,9 +148,20 @@ class ComponentDiscovery { if (isset($this->components)) { return array_values($this->components); } $components_by_key = fn(array $data) => array_reduce( $components_by_id = static fn(array $data) => array_reduce( $data, static fn(array $all, Component $c) => [...$all, $c->getId() => $c], static function (array $all, Component $c) { // If there are multiple candidates with the same ID only save the one // with the lowest weight. $existing_component = $all[$c->getId()] ?? NULL; if (!$existing_component) { return [...$all, $c->getId() => $c]; } assert($existing_component instanceof Component); $existing_weight = $existing_component->getMetadata()->getWeight(); $candidate_weight = $c->getMetadata()->getWeight(); return $existing_weight > $candidate_weight ? [...$all, $c->getId() => $c] : $all; }, [] ); $cache_ids = $this->cache->get('all-cache-ids'); Loading @@ -158,7 +169,7 @@ class ComponentDiscovery { $cache_entries = $this->cache->getMultiple($cache_ids->data); // Get the data of the cache properties. $cache_data = array_map( fn(object $item) => $item->data, static fn(object $item) => $item->data, $cache_entries ); // Ensure cache data deserializes to a Component. Loading @@ -166,13 +177,13 @@ class ComponentDiscovery { $cache_data, static fn(mixed $item) => $item instanceof Component ); $this->components = $components_by_key($cached_components); $this->components = $components_by_id($cached_components); return array_values($this->components); } $paths = $this->findAllPaths(); $components = array_map([$this, 'createComponent'], $paths); $components = array_filter($components); $this->components = $components_by_key($components); $this->components = $components_by_id($components); $this->cache->set( 'all-cache-ids', array_map( Loading Loading @@ -367,6 +378,11 @@ class ComponentDiscovery { $components, static fn(Component $c) => $c->getId() === $id ); // Make sure all components that match the ID are sorted by weight. usort( $matches, static fn(Component $a, Component $b) => $b->getMetadata()->getWeight() - $a->getMetadata()->getWeight() ); $component = reset($matches); if ($component instanceof Component) { return $component; Loading
src/Component/ComponentMetadata.php +26 −5 Changes for src/Component/ComponentMetadata.php: 26 added lines, 5 removed lines. Original line number Diff line number Diff line Loading @@ -142,6 +142,13 @@ final class ComponentMetadata { */ private string $phpPath; /** * The weight of the component. * * @var int */ private int $weight; /** * ComponentMetadata constructor. * Loading Loading @@ -179,6 +186,7 @@ final class ComponentMetadata { $this->status = $metadata_info['status'] ?? static::COMPONENT_STATUS_WIP; $this->componentType = $metadata_info['componentType'] ?? static::COMPONENT_TYPE_ORGANISM; $this->libraryDependencies = $metadata_info['libraryDependencies'] ?? []; $this->weight = (int) ($metadata_info['weight'] ?? 0); // Load the PNG. $thumbnail_path = sprintf('%s/thumbnail.png', $path); Loading @@ -192,11 +200,6 @@ final class ComponentMetadata { $this->parseSchemaInfo($metadata_info); // Parse the hooks. $this->safeName = preg_replace('@[^a-z0-9]+@', '_', strtolower($this->machineName)); $php_file = new ComponentPhpFile($this->path, $this->machineName, $this->safeName); $this->phpPath = $php_file->path; if ($php_file->hasPhpFile()) { $this->hooks = $php_file->parseHooks(); } } /** Loading Loading @@ -505,8 +508,16 @@ final class ComponentMetadata { * * @return string[] * The hook names. * * @throws \Drupal\cl_components\Exception\ComponentSyntaxException */ public function getHooks(): array { if (isset($this->hooks)) { return $this->hooks; } $php_file = new ComponentPhpFile($this->path, $this->machineName, $this->safeName); $this->phpPath = $php_file->path; $this->hooks = $php_file->hasPhpFile() ? $php_file->parseHooks() : []; return $this->hooks; } Loading @@ -520,4 +531,14 @@ final class ComponentMetadata { return $this->phpPath; } /** * Gets the component weight when more than one by ID exist. * * @return int * The weight. */ public function getWeight(): int { return $this->weight; } }
src/metadata.schema.json +5 −0 Changes for src/metadata.schema.json: 5 added lines, 0 removed lines. Original line number Diff line number Diff line Loading @@ -62,6 +62,11 @@ "DEPRECATED" ] }, "weight": { "type": "integer", "title": "Weight", "description": "When two components share the same ID the one with the lowest weight will be rendered." }, "variants": { "type": "array", "title": "Variants", Loading