Skip to content

Commit

Permalink
Added "expand references" option to dump command
Browse files Browse the repository at this point in the history
- If specified, the full path to each referrer is listed beneath each
  reference node instead of the normal "print_r" value.
- Really good for debugging...
  • Loading branch information
dantleech committed Apr 16, 2013
1 parent 65cce1c commit c73a2a9
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 6 deletions.
6 changes: 5 additions & 1 deletion src/PHPCR/Util/Console/Command/DumpCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ protected function configure()
->addOption('props', null, InputOption::VALUE_NONE, 'Use to dump the node properties')
->addOption('depth', null, InputOption::VALUE_OPTIONAL, 'Set to a number to limit how deep into the tree to recurse', "-1")
->addOption('identifiers', null, InputOption::VALUE_NONE, 'Use to also output node UUID')
->addOption('expand-references', null, InputOption::VALUE_NONE, 'Use to also output node references')
->addArgument('identifier', InputArgument::OPTIONAL, 'Path of the node to dump', '/')
->setDescription('Dump the content repository')
->setHelp(<<<EOF
Expand Down Expand Up @@ -104,7 +105,10 @@ protected function execute(InputInterface $input, OutputInterface $output)

$propVisitor = null;
if ($input->hasParameterOption('--props')) {
$propVisitor = new ConsoleDumperPropertyVisitor($output);
$options = array(
'expand_references' => $input->hasParameterOption('--expand-references')
);
$propVisitor = new ConsoleDumperPropertyVisitor($output, $options);
}

$walker = new TreeWalker($nodeVisitor, $propVisitor);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@

use PHPCR\ItemInterface;
use PHPCR\PropertyInterface;
use PHPCR\PropertyType;

/**
* TODO: this should base on the TraversingItemVisitor
Expand All @@ -38,21 +39,30 @@ class ConsoleDumperPropertyVisitor extends ConsoleDumperItemVisitor
*
* @var int
*/
protected $maxLineLength = 120;
protected $maxLineLength;

/**
* Show the full path for each reference
*/
protected $expandReferences;

/**
* Instantiate property visitor
*
* @param OutputInterface $output
* @param int $maxLineLength
*/
public function __construct(OutputInterface $output, $maxLineLength = null)
public function __construct(OutputInterface $output, $options = array())
{
$options = array_merge(array(
'max_line_length' => 120,
'expand_references' => false,
), $options);

parent::__construct($output);

if (null !== $maxLineLength) {
$this->maxLineLength = $maxLineLength;
}
$this->maxLineLength = $options['max_line_length'];
$this->expandReferences = $options['expand_references'];
}

/**
Expand All @@ -76,8 +86,28 @@ public function visit(ItemInterface $item)
$value = substr($value, 0, $this->maxLineLength) . '...';
}

$referrers = array();

if (true === $this->expandReferences) {
if (in_array($item->getType(), array(
PropertyType::WEAKREFERENCE,
PropertyType::REFERENCE
))) {
$referrers = $item->getValue();
if (!is_array($referrers)) {
$referrers = array($referrers);
}
$value = '';
}
}


$value = str_replace(array("\n", "\t"), '', $value);

$this->output->writeln(str_repeat(' ', $this->level + 1) . '- <info>' . $item->getName() . '</info> = ' . $value);

foreach ($referrers as $referrer) {
$this->output->writeln(str_repeat(' ', $this->level + 1). ' - <info>ref</info>: '.$referrer->getPath().'');
}
}
}

0 comments on commit c73a2a9

Please sign in to comment.