Skip to content
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
15 changes: 13 additions & 2 deletions 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('ref-format', 'uuid', InputOption::VALUE_REQUIRED, 'Set the way references should be displayed when dumping reference properties - either "uuid" (default) or "path"')
->addArgument('identifier', InputArgument::OPTIONAL, 'Path of the node to dump', '/')
->setDescription('Dump the content repository')
->setHelp(<<<EOF
Expand All @@ -69,10 +70,10 @@ protected function configure()

If the <info>props</info> option is used the nodes properties are
displayed as yaml arrays.

By default the command filters out system nodes and properties (i.e. nodes and
properties with names starting with 'jcr:'), the <info>sys_nodes</info> option
allows to turn this filter off.
allows to turn this filter off.
EOF
)
;
Expand Down Expand Up @@ -103,8 +104,18 @@ protected function execute(InputInterface $input, OutputInterface $output)
$nodeVisitor = new ConsoleDumperNodeVisitor($output, $identifiers);

$propVisitor = null;
$refFormat = $input->getOption('ref-format');

if (null !== $refFormat && !in_array($refFormat, array('uuid', 'path'))) {
throw new \Exception('The ref-format option must be set to either "path" or "uuid"');
}

if ($input->hasParameterOption('--props')) {
$propVisitor = new ConsoleDumperPropertyVisitor($output);
$options = array();
if ($refFormat) {
$options['ref_format'] = $refFormat;
}
$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())
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I generally prefer to pass in an $options array and do an array_merge with defaults afterwards to avoid argument hell, not sure what other people prefer. But, maybe an argument for "expandReferences" makes more sense here anyhow.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i am fine with this approach for scalar options.

{
$options = array_merge(array(
'max_line_length' => 120,
'ref_format' => 'uuid',
), $options);

parent::__construct($output);

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

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

$referrers = array();

if (in_array($item->getType(), array(
PropertyType::WEAKREFERENCE,
PropertyType::REFERENCE
))) {
$referenceStrings = array();


if ('path' == $this->refFormat) {
$references = (array) $item->getValue();

foreach ($references as $reference) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i saw @lsmith77 using (array) to simplify this. found http://www.php.net/manual/en/language.types.array.php#language.types.array.casting that explains this feature. this could further shorten your code here.

$referenceStrings[] = $reference->getPath();
}
} else {
$referenceStrings = (array) $item->getString();
}

$value = '';
}

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

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

if (isset($referenceStrings)) {
foreach ($referenceStrings as $referenceString) {
$this->output->writeln(sprintf(
'%s - <info>%s</info>: %s',
str_repeat(' ', $this->level + 1),
$this->refFormat,
$referenceString
));
}
}
}

}