Skip to content

Commit

Permalink
Merge pull request #4619 from neos/bugfix/NodeTypesCommandController
Browse files Browse the repository at this point in the history
BUGFIX: Fix and make `nodeTypes:show` command usable
  • Loading branch information
mhsdesign authored Nov 8, 2023
2 parents 0a62a8e + b7abfbc commit 6848c6c
Showing 1 changed file with 43 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,22 +27,30 @@ class NodeTypesCommandController extends CommandController
* Shows the merged configuration (including supertypes) of a NodeType
*
* @param string $nodeTypeName The name of the NodeType to show
* @param ?string $path Path of the NodeType-configuration which will be shown
* @param string $path Path of the NodeType-configuration which will be shown
* @param int $level Truncate the configuration at this depth and show '...' (Usefully for only seeing the keys of the properties)
*/
public function showCommand(string $nodeTypeName, ?string $path = null): void
public function showCommand(string $nodeTypeName, string $path = '', int $level = 0): void
{
if (!$this->nodeTypeManager->hasNodeType($nodeTypeName)) {
$this->outputLine('<error>NodeType "%s" was not found!</error>', [$nodeTypeName]);
$this->quit();
}

$nodeType = $this->nodeTypeManager->getNodeType($nodeTypeName);
if (!$nodeType) {
$this->outputLine('<b>NodeType "%s" was not found!</b>', [$nodeTypeName]);

if ($path && !$nodeType->hasConfiguration($path)) {
$this->outputLine('<b>NodeType "%s" does not have configuration "%s".</b>', [$nodeTypeName, $path]);
$this->quit();
}
$yaml = Yaml::dump(
$path
? $nodeType->getConfiguration($path)
: [$nodeTypeName => $nodeType->getFullConfiguration()],
99
);
$this->outputLine('<b>NodeType Configuration "%s":</b>', [$nodeTypeName . ($path ? ("." . $path) : "")]);

$configuration = $path
? self::truncateArrayAtLevel($nodeType->getConfiguration($path), $level)
: [$nodeTypeName => self::truncateArrayAtLevel($nodeType->getFullConfiguration(), $level)];

$yaml = Yaml::dump($configuration, 99);

$this->outputLine('<b>NodeType configuration "%s":</b>', [$nodeTypeName . ($path ? ("." . $path) : "")]);
$this->outputLine();
$this->outputLine($yaml);
$this->outputLine();
Expand Down Expand Up @@ -78,4 +86,28 @@ public function listCommand(?string $filter = null, bool $includeAbstract = true
}
}
}

/**
* @param int $truncateLevel 0 for no truncation and 1 to only show the first keys of the array
* @param int $currentLevel 1 for the start and will be incremented recursively
*/
private static function truncateArrayAtLevel(array $array, int $truncateLevel, int $currentLevel = 1): array
{
if ($truncateLevel <= 0) {
return $array;
}
$truncatedArray = [];
foreach ($array as $key => $value) {
if ($currentLevel >= $truncateLevel) {
$truncatedArray[$key] = '...'; // truncated
continue;
}
if (!is_array($value)) {
$truncatedArray[$key] = $value;
continue;
}
$truncatedArray[$key] = self::truncateArrayAtLevel($value, $truncateLevel, $currentLevel + 1);
}
return $truncatedArray;
}
}

0 comments on commit 6848c6c

Please sign in to comment.