Skip to content

Commit feaafe7

Browse files
committed
Merge pull request #41 from phpcr/lstree
tree command, reimplementation of `doctrine:phpcr:node:dump` command
2 parents eba8e58 + d4528f7 commit feaafe7

File tree

3 files changed

+110
-15
lines changed

3 files changed

+110
-15
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
## Features
44

55
- New command: `file:import`: - import files into the repository.
6+
- `node:list`: Added `--level` option to rescursively show children nodes and properties
67

78
## Improvements
89

src/PHPCR/Shell/Console/Command/Phpcr/NodeListCommand.php

Lines changed: 41 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ class NodeListCommand extends Command
1212
{
1313
protected $formatter;
1414
protected $filters;
15+
protected $textHelper;
16+
protected $maxLevel;
1517

1618
protected function configure()
1719
{
@@ -21,6 +23,7 @@ protected function configure()
2123
$this->addOption('children', null, InputOption::VALUE_NONE, 'List only the children of this node');
2224
$this->addOption('properties', null, InputOption::VALUE_NONE, 'List only the properties of this node');
2325
$this->addOption('filter', 'f', InputOption::VALUE_REQUIRED|InputOption::VALUE_IS_ARRAY, 'Optional filter to apply');
26+
$this->addOption('level', 'L', InputOption::VALUE_REQUIRED, 'Depth of tree to show');
2427
$this->setHelp(<<<HERE
2528
List both or one of the children and properties of this node.
2629
HERE
@@ -32,53 +35,76 @@ public function execute(InputInterface $input, OutputInterface $output)
3235
$this->formatter = $this->getHelper('result_formatter');
3336
$this->textHelper = $this->getHelper('text');
3437
$this->filters = $input->getOption('filter');
38+
$this->maxLevel = $input->getOption('level');
3539

36-
$showChildren = $input->getOption('children');
37-
$showProperties = $input->getOption('properties');
40+
$this->showChildren = $input->getOption('children');
41+
$this->showProperties = $input->getOption('properties');
3842

3943
$session = $this->getHelper('phpcr')->getSession();
4044

4145
$path = $session->getAbsPath($input->getArgument('path'));
4246
$currentNode = $session->getNode($path);
4347

44-
if (!$showChildren && !$showProperties) {
45-
$showChildren = true;
46-
$showProperties = true;
48+
if (!$this->showChildren && !$this->showProperties) {
49+
$this->showChildren = true;
50+
$this->showProperties = true;
4751
}
4852

4953
$table = clone $this->getHelper('table');
5054

51-
if ($showChildren) {
52-
$this->renderChildren($currentNode, $table);
53-
}
55+
$this->renderNode($currentNode, $table);
5456

55-
if ($showProperties) {
56-
$this->renderProperties($currentNode, $table);
57+
$table->render($output);
58+
}
59+
60+
private function renderNode($currentNode, $table, $spacers = array())
61+
{
62+
if ($this->showChildren) {
63+
$this->renderChildren($currentNode, $table, $spacers);
5764
}
5865

59-
$table->render($output);
66+
if ($this->showProperties) {
67+
$this->renderProperties($currentNode, $table, $spacers);
68+
}
6069
}
6170

62-
private function renderChildren($currentNode, $table)
71+
private function renderChildren($currentNode, $table, $spacers)
6372
{
6473
$children = $currentNode->getNodes($this->filters ? : null);
6574

75+
$i = 0;
6676
foreach ($children as $child) {
77+
$i++;
78+
$isLast = count($children) === $i;
79+
6780
$table->addRow(array(
68-
'<node>' . $this->formatter->formatNodeName($child) . '</node>',
81+
'<node>' . implode('', $spacers) . $this->formatter->formatNodeName($child) . '</node>',
6982
$child->getPrimaryNodeType()->getName(),
7083
'',
7184
));
85+
86+
if (count($spacers) < $this->maxLevel) {
87+
$newSpacers = $spacers;
88+
if ($isLast) {
89+
$newSpacers[] = ' ';
90+
} else {
91+
$newSpacers[] = '| ';
92+
}
93+
94+
$this->renderNode($child, $table, $newSpacers);
95+
}
7296
}
7397
}
7498

75-
private function renderProperties($currentNode, $table)
99+
private function renderProperties($currentNode, $table, $spacers)
76100
{
77101
$properties = $currentNode->getProperties($this->filters ? : null);
78102

103+
$i = 0;
79104
foreach ($properties as $name => $property) {
105+
$i++;
80106
$table->addRow(array(
81-
'<property>' . $name . '</property>',
107+
'<property>' . implode('', $spacers). $name . '</property>',
82108
'<property-type>' . $this->formatter->getPropertyTypeName($property->getType()) . '</property-type>',
83109
$this->textHelper->truncate($this->formatter->formatValue($property), 55),
84110
));
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
<?php
2+
3+
namespace PHPCR\Shell\Console\Command\Phpcr;
4+
5+
use Symfony\Component\Console\Command\Command;
6+
use Symfony\Component\Console\Input\InputInterface;
7+
use Symfony\Component\Console\Output\OutputInterface;
8+
use Symfony\Component\Console\Input\InputArgument;
9+
use Symfony\Component\Console\Input\InputOption;
10+
use PHPCR\NodeInterface;
11+
12+
class NodeTreeCommand extends NodeListCommand
13+
{
14+
protected $textHelper;
15+
protected $session;
16+
17+
protected $showChildren;
18+
protected $showProperties;
19+
20+
protected function configure()
21+
{
22+
parent::configure();
23+
$this->setName('node:tree');
24+
$this->setDescription('Display the current node as a tree');
25+
$this->addArgument('path', InputArgument::OPTIONAL, 'Path of node', '.');
26+
$this->addOption('filter', 'f', InputOption::VALUE_REQUIRED|InputOption::VALUE_IS_ARRAY, 'Optional filter to apply');
27+
$this->addOption('level', 'L', InputOption::VALUE_REQUIRED, 'Depth of tree to show');
28+
$this->setHelp(<<<HERE
29+
HERE
30+
);
31+
}
32+
33+
public function execute(InputInterface $input, OutputInterface $output)
34+
{
35+
$this->formatter = $this->getHelper('result_formatter');
36+
$this->textHelper = $this->getHelper('text');
37+
$this->session = $this->getHelper('phpcr')->getSession();
38+
39+
$this->filters = $input->getOption('filter');
40+
$this->level = $input->getOption('level');
41+
42+
$this->showChildren = $input->getOption('children');
43+
$this->showProperties = $input->getOption('properties');
44+
45+
$path = $session->getAbsPath($input->getArgument('path'));
46+
$currentNode = $session->getNode($path);
47+
48+
if (!$showChildren && !$showProperties) {
49+
$this->showChildren = true;
50+
$this->showProperties = true;
51+
}
52+
53+
$output->writeln('<node>.</node>');
54+
$this->renderNode($currentNode, 1);
55+
}
56+
57+
protected function renderNode(NodeInterface $node, OutputInterface $output)
58+
{
59+
foreach ($node->getNodes($this->filters ? : null) as $child) {
60+
$output->writeln($this->formatNode($child));
61+
}
62+
}
63+
64+
protected function formatNode(NodeInterface $node)
65+
{
66+
return '<node>' . $this->formatter->formatNodeName($child) . '</node>';
67+
}
68+
}

0 commit comments

Comments
 (0)