Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BUGFIX: Fix and make nodeTypes:show command usable #4619

Merged
merged 4 commits into from
Nov 8, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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;
}
}