Skip to content

Commit 4691870

Browse files
committed
Replacing node dump command
Fixes #6
1 parent 9ff68ed commit 4691870

File tree

3 files changed

+117
-4
lines changed

3 files changed

+117
-4
lines changed

src/PHPCR/Shell/Console/Application/ShellApplication.php

+5-2
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
use PHPCR\Shell\Console\Command\Shell\ExitCommand;
3535
use PHPCR\Shell\Console\TransportInterface;
3636
use PHPCR\Shell\Console\Command\Shell\WorkspaceChangeCommand;
37+
use PHPCR\Shell\Console\Command\Shell\ListTreeCommand;
3738

3839
class ShellApplication extends Application
3940
{
@@ -60,12 +61,14 @@ public function __construct($appName, $version, InputInterface $input, $transpor
6061

6162
// wrap phpcr-util commands
6263
$this->add($this->wrap(new NodeDumpCommand())
63-
->setName('ls')
64+
->setName('dump')
6465
->setDescription('Alias for dump')
6566
);
66-
$ls = $this->get('ls');
67+
$ls = $this->get('dump');
6768
$ls->getDefinition()->getArgument('identifier')->setDefault(null);
6869

70+
$this->add(new ListTreeCommand());
71+
6972
$this->add($this->wrap(new NodeMoveCommand())
7073
->setName('mv')
7174
);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
<?php
2+
3+
namespace PHPCR\Shell\Console\Command\Shell;
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\InputOption;
9+
use PHPCR\NodeInterface;
10+
use PHPCR\Util\NodeHelper;
11+
12+
class ListTreeCommand extends Command
13+
{
14+
protected $maxDepth;
15+
protected $maxResults;
16+
protected $showSystem;
17+
18+
protected $nodeCount = 0;
19+
20+
public function configure()
21+
{
22+
$this->setName('ls');
23+
$this->setDescription('List tree');
24+
$this->addOption('max-depth', 'd', InputOption::VALUE_REQUIRED, 'Depth', 1);
25+
$this->addOption('limit', 'l', InputOption::VALUE_REQUIRED, 'Limit results', null);
26+
$this->addOption('show-system', null, InputOption::VALUE_NONE, 'Show system nodes');
27+
}
28+
29+
public function execute(InputInterface $input, OutputInterface $output)
30+
{
31+
$this->nodeCount = 0;
32+
33+
$session = $this->getHelper('phpcr')->getSession();
34+
$node = $session->getNode($session->getCwd());
35+
$this->maxDepth = $input->getOption('max-depth');
36+
$this->maxResults = $input->getOption('limit');
37+
$this->showSystem = $input->getOption('show-system');
38+
39+
$rows = new \ArrayObject();
40+
$this->iterateTree($node, $rows);
41+
42+
$table = clone $this->getHelper('table');
43+
$table->setHeaders(array('Node / Prop', 'Type', 'Value'));
44+
45+
foreach ($rows as $row) {
46+
$table->addRow($row);
47+
}
48+
49+
$table->render($output);
50+
51+
$output->writeln('');
52+
$output->writeln($this->nodeCount . ' node(s)');
53+
}
54+
55+
public function iterateTree(NodeInterface $node, $rows, $depth = -1)
56+
{
57+
if (null !== $this->maxResults && $this->nodeCount >= $this->maxResults) {
58+
return;
59+
}
60+
61+
if (true === NodeHelper::isSystemItem($node) && false === $this->showSystem) {
62+
return;
63+
}
64+
65+
$this->nodeCount++;
66+
67+
$formatter = $this->getHelper('result_formatter');
68+
$properties = $node->getProperties();
69+
70+
$rows[] = array(
71+
str_repeat(' ', $depth + 1) . '<info>' . $node->getName() . '/</info>',
72+
'<info>' . $node->getPrimaryNodeType()->getName() . '</info>',
73+
'',
74+
);
75+
76+
$depth++;
77+
if ($depth >= $this->maxDepth) {
78+
return;
79+
}
80+
81+
foreach ($properties as $key => $property) {
82+
if (true === NodeHelper::isSystemItem($property) && false === $this->showSystem) {
83+
continue;
84+
}
85+
86+
$rows[] = array(
87+
sprintf('%s -<comment>%s</comment>', str_repeat(' ', $depth), $key),
88+
$formatter->getPropertyTypeName($property->getType()) . ($property->isMultiple() ? '[]' : ''),
89+
substr($formatter->formatValue($property), 0, 55),
90+
);
91+
}
92+
93+
foreach ($node->getNodes() as $child) {
94+
if ($depth < $this->maxDepth) {
95+
$this->iterateTree($child, $rows, $depth);
96+
}
97+
}
98+
}
99+
}
100+

src/PHPCR/Shell/Console/Helper/ResultFormatterHelper.php

+12-2
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,16 @@ public function getName()
1616
return 'result_formatter';
1717
}
1818

19+
public function getPropertyTypeName($typeInteger)
20+
{
21+
$refl = new \ReflectionClass('PHPCR\PropertyType');
22+
foreach ($refl->getConstants() as $key => $value) {
23+
if ($typeInteger == $value) {
24+
return $key;
25+
}
26+
}
27+
}
28+
1929
public function format(QueryResultInterface $result, OutputInterface $output, $elapsed)
2030
{
2131
$selectorNames = $result->getSelectorNames();
@@ -50,7 +60,7 @@ public function format(QueryResultInterface $result, OutputInterface $output, $e
5060
$output->writeln(sprintf('%s rows in set (%s sec)', count($result->getRows()), number_format($elapsed, 2)));
5161
}
5262

53-
protected function formatValue($value)
63+
public function formatValue($value)
5464
{
5565
$v = $value->getValue();
5666
if (is_array($v)) {
@@ -86,7 +96,7 @@ protected function formatValue($value)
8696
return $value->getValue()->format('c');
8797
case PropertyType::REFERENCE :
8898
case PropertyType::WEAKREFERENCE :
89-
return $value->getValue()->getPath();
99+
return $value->getValue()->getIdentifier();
90100
case PropertyType::URI :
91101
case PropertyType::STRING :
92102
case PropertyType::NAME :

0 commit comments

Comments
 (0)