Skip to content

[WIP] Replacing node dump command with ls #6

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

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file not shown.
Binary file not shown.
7 changes: 5 additions & 2 deletions src/PHPCR/Shell/Console/Application/ShellApplication.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
use PHPCR\Shell\Console\Command\Shell\ExitCommand;
use PHPCR\Shell\Console\TransportInterface;
use PHPCR\Shell\Console\Command\Shell\WorkspaceChangeCommand;
use PHPCR\Shell\Console\Command\Shell\ListTreeCommand;

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

// wrap phpcr-util commands
$this->add($this->wrap(new NodeDumpCommand())
->setName('ls')
->setName('dump')
->setDescription('Alias for dump')
);
$ls = $this->get('ls');
$ls = $this->get('dump');
$ls->getDefinition()->getArgument('identifier')->setDefault(null);

$this->add(new ListTreeCommand());

$this->add($this->wrap(new NodeMoveCommand())
->setName('mv')
);
Expand Down
96 changes: 96 additions & 0 deletions src/PHPCR/Shell/Console/Command/Shell/ListTreeCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
<?php

namespace PHPCR\Shell\Console\Command\Shell;

use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Input\InputOption;
use PHPCR\NodeInterface;
use PHPCR\Util\NodeHelper;

class ListTreeCommand extends Command
{
protected $maxDepth;
protected $maxResults;
protected $showSystem;

protected $nodeCount = 0;

public function configure()
{
$this->setName('ls');
$this->setDescription('List tree');
$this->addOption('max-depth', 'd', InputOption::VALUE_REQUIRED, 'Depth', 1);
$this->addOption('limit', 'l', InputOption::VALUE_REQUIRED, 'Limit results', null);
$this->addOption('show-system', null, InputOption::VALUE_NONE, 'Show system nodes');
}

public function execute(InputInterface $input, OutputInterface $output)
{
$this->nodeCount = 0;

$session = $this->getHelper('phpcr')->getSession();
$node = $session->getNode($session->getCwd());
$this->maxDepth = $input->getOption('max-depth');
$this->maxResults = $input->getOption('limit');
$this->showSystem = $input->getOption('show-system');

$rows = new \ArrayObject();
$this->iterateTree($node, $rows);

$table = clone $this->getHelper('table');
$table->setHeaders(array('Node / Prop', 'Type', 'Value'));

foreach ($rows as $row) {
$table->addRow($row);
}

$table->render($output);

$output->writeln('');
$output->writeln($this->nodeCount . ' node(s)');
}

public function iterateTree(NodeInterface $node, $rows, $depth = -1)
{
if (null !== $this->maxResults && $this->nodeCount >= $this->maxResults) {
return;
}

if (true === NodeHelper::isSystemItem($node) && false === $this->showSystem) {
return;
}

$this->nodeCount++;

$formatter = $this->getHelper('result_formatter');
$properties = $node->getProperties();

$rows[] = array(
str_repeat(' ', $depth + 1) . '<info>' . $node->getName() . '/</info>',
'<info>' . $node->getPrimaryNodeType()->getName() . '</info>',
'',
);

$depth++;
if ($depth >= $this->maxDepth) {
return;
}

foreach ($properties as $key => $property) {
$rows[] = array(
sprintf('%s -<comment>%s</comment>', str_repeat(' ', $depth), $key),
$formatter->getPropertyTypeName($property->getType()) . ($property->isMultiple() ? '[]' : ''),
substr($formatter->formatValue($property), 0, 55),
);
}

foreach ($node->getNodes() as $child) {
if ($depth < $this->maxDepth) {
$this->iterateTree($child, $rows, $depth);
}
}
}
}

12 changes: 11 additions & 1 deletion src/PHPCR/Shell/Console/Helper/ResultFormatterHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,16 @@ public function getName()
return 'result_formatter';
}

public function getPropertyTypeName($typeInteger)
{
$refl = new \ReflectionClass('PHPCR\PropertyType');
foreach ($refl->getConstants() as $key => $value) {
if ($typeInteger == $value) {
return $key;
}
}
}

public function format(QueryResultInterface $result, OutputInterface $output, $elapsed)
{
$selectorNames = $result->getSelectorNames();
Expand Down Expand Up @@ -50,7 +60,7 @@ public function format(QueryResultInterface $result, OutputInterface $output, $e
$output->writeln(sprintf('%s rows in set (%s sec)', count($result->getRows()), number_format($elapsed, 2)));
}

protected function formatValue($value)
public function formatValue($value)
{
$v = $value->getValue();
if (is_array($v)) {
Expand Down