Skip to content

tree command, reimplementation of doctrine:phpcr:node:dump command #41

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

Merged
merged 1 commit into from
Jun 8, 2014
Merged
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
56 changes: 41 additions & 15 deletions src/PHPCR/Shell/Console/Command/Phpcr/NodeListCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ class NodeListCommand extends Command
{
protected $formatter;
protected $filters;
protected $textHelper;
protected $maxLevel;

protected function configure()
{
Expand All @@ -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(<<<HERE
List both or one of the children and properties of this node.
HERE
Expand All @@ -32,53 +35,76 @@ public function execute(InputInterface $input, OutputInterface $output)
$this->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(
'<node>' . $this->formatter->formatNodeName($child) . '</node>',
'<node>' . implode('', $spacers) . $this->formatter->formatNodeName($child) . '</node>',
$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(
'<property>' . $name . '</property>',
'<property>' . implode('', $spacers). $name . '</property>',
'<property-type>' . $this->formatter->getPropertyTypeName($property->getType()) . '</property-type>',
$this->textHelper->truncate($this->formatter->formatValue($property), 55),
));
Expand Down
68 changes: 68 additions & 0 deletions src/PHPCR/Shell/Console/Command/Phpcr/NodeTreeCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
<?php

namespace PHPCR\Shell\Console\Command\Phpcr;

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

class NodeTreeCommand extends NodeListCommand
{
protected $textHelper;
protected $session;

protected $showChildren;
protected $showProperties;

protected function configure()
{
parent::configure();
$this->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(<<<HERE
HERE
);
}

public function execute(InputInterface $input, OutputInterface $output)
{
$this->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('<node>.</node>');
$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 '<node>' . $this->formatter->formatNodeName($child) . '</node>';
}
}