diff --git a/CHANGELOG.md b/CHANGELOG.md index 9c449747..434d09cc 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,7 @@ ## Features - New command: `file:import`: - import files into the repository. +- `node:list`: Added `--level` option to rescursively show children nodes and properties ## Improvements diff --git a/src/PHPCR/Shell/Console/Command/Phpcr/NodeListCommand.php b/src/PHPCR/Shell/Console/Command/Phpcr/NodeListCommand.php index 163366c7..d7ab4933 100644 --- a/src/PHPCR/Shell/Console/Command/Phpcr/NodeListCommand.php +++ b/src/PHPCR/Shell/Console/Command/Phpcr/NodeListCommand.php @@ -12,6 +12,8 @@ class NodeListCommand extends Command { protected $formatter; protected $filters; + protected $textHelper; + protected $maxLevel; protected function configure() { @@ -21,6 +23,7 @@ protected function configure() $this->addOption('children', null, InputOption::VALUE_NONE, 'List only the children of this node'); $this->addOption('properties', null, InputOption::VALUE_NONE, 'List only the properties of this node'); $this->addOption('filter', 'f', InputOption::VALUE_REQUIRED|InputOption::VALUE_IS_ARRAY, 'Optional filter to apply'); + $this->addOption('level', 'L', InputOption::VALUE_REQUIRED, 'Depth of tree to show'); $this->setHelp(<<formatter = $this->getHelper('result_formatter'); $this->textHelper = $this->getHelper('text'); $this->filters = $input->getOption('filter'); + $this->maxLevel = $input->getOption('level'); - $showChildren = $input->getOption('children'); - $showProperties = $input->getOption('properties'); + $this->showChildren = $input->getOption('children'); + $this->showProperties = $input->getOption('properties'); $session = $this->getHelper('phpcr')->getSession(); $path = $session->getAbsPath($input->getArgument('path')); $currentNode = $session->getNode($path); - if (!$showChildren && !$showProperties) { - $showChildren = true; - $showProperties = true; + if (!$this->showChildren && !$this->showProperties) { + $this->showChildren = true; + $this->showProperties = true; } $table = clone $this->getHelper('table'); - if ($showChildren) { - $this->renderChildren($currentNode, $table); - } + $this->renderNode($currentNode, $table); - if ($showProperties) { - $this->renderProperties($currentNode, $table); + $table->render($output); + } + + private function renderNode($currentNode, $table, $spacers = array()) + { + if ($this->showChildren) { + $this->renderChildren($currentNode, $table, $spacers); } - $table->render($output); + if ($this->showProperties) { + $this->renderProperties($currentNode, $table, $spacers); + } } - private function renderChildren($currentNode, $table) + private function renderChildren($currentNode, $table, $spacers) { $children = $currentNode->getNodes($this->filters ? : null); + $i = 0; foreach ($children as $child) { + $i++; + $isLast = count($children) === $i; + $table->addRow(array( - '' . $this->formatter->formatNodeName($child) . '', + '' . implode('', $spacers) . $this->formatter->formatNodeName($child) . '', $child->getPrimaryNodeType()->getName(), '', )); + + if (count($spacers) < $this->maxLevel) { + $newSpacers = $spacers; + if ($isLast) { + $newSpacers[] = ' '; + } else { + $newSpacers[] = '| '; + } + + $this->renderNode($child, $table, $newSpacers); + } } } - private function renderProperties($currentNode, $table) + private function renderProperties($currentNode, $table, $spacers) { $properties = $currentNode->getProperties($this->filters ? : null); + $i = 0; foreach ($properties as $name => $property) { + $i++; $table->addRow(array( - '' . $name . '', + '' . implode('', $spacers). $name . '', '' . $this->formatter->getPropertyTypeName($property->getType()) . '', $this->textHelper->truncate($this->formatter->formatValue($property), 55), )); diff --git a/src/PHPCR/Shell/Console/Command/Phpcr/NodeTreeCommand.php b/src/PHPCR/Shell/Console/Command/Phpcr/NodeTreeCommand.php new file mode 100644 index 00000000..2662d4e4 --- /dev/null +++ b/src/PHPCR/Shell/Console/Command/Phpcr/NodeTreeCommand.php @@ -0,0 +1,68 @@ +setName('node:tree'); + $this->setDescription('Display the current node as a tree'); + $this->addArgument('path', InputArgument::OPTIONAL, 'Path of node', '.'); + $this->addOption('filter', 'f', InputOption::VALUE_REQUIRED|InputOption::VALUE_IS_ARRAY, 'Optional filter to apply'); + $this->addOption('level', 'L', InputOption::VALUE_REQUIRED, 'Depth of tree to show'); + $this->setHelp(<<formatter = $this->getHelper('result_formatter'); + $this->textHelper = $this->getHelper('text'); + $this->session = $this->getHelper('phpcr')->getSession(); + + $this->filters = $input->getOption('filter'); + $this->level = $input->getOption('level'); + + $this->showChildren = $input->getOption('children'); + $this->showProperties = $input->getOption('properties'); + + $path = $session->getAbsPath($input->getArgument('path')); + $currentNode = $session->getNode($path); + + if (!$showChildren && !$showProperties) { + $this->showChildren = true; + $this->showProperties = true; + } + + $output->writeln('.'); + $this->renderNode($currentNode, 1); + } + + protected function renderNode(NodeInterface $node, OutputInterface $output) + { + foreach ($node->getNodes($this->filters ? : null) as $child) { + $output->writeln($this->formatNode($child)); + } + } + + protected function formatNode(NodeInterface $node) + { + return '' . $this->formatter->formatNodeName($child) . ''; + } +}