Skip to content

Commit 602c504

Browse files
committed
Merge pull request #50 from dantleech/expand_references
Expand references in phpcr:dump
2 parents e8c2b45 + 9cc0852 commit 602c504

File tree

2 files changed

+62
-7
lines changed

2 files changed

+62
-7
lines changed

src/PHPCR/Util/Console/Command/DumpCommand.php

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ protected function configure()
6161
->addOption('props', null, InputOption::VALUE_NONE, 'Use to dump the node properties')
6262
->addOption('depth', null, InputOption::VALUE_OPTIONAL, 'Set to a number to limit how deep into the tree to recurse', "-1")
6363
->addOption('identifiers', null, InputOption::VALUE_NONE, 'Use to also output node UUID')
64+
->addOption('ref-format', 'uuid', InputOption::VALUE_REQUIRED, 'Set the way references should be displayed when dumping reference properties - either "uuid" (default) or "path"')
6465
->addArgument('identifier', InputArgument::OPTIONAL, 'Path of the node to dump', '/')
6566
->setDescription('Dump the content repository')
6667
->setHelp(<<<EOF
@@ -69,10 +70,10 @@ protected function configure()
6970
7071
If the <info>props</info> option is used the nodes properties are
7172
displayed as yaml arrays.
73+
7274
By default the command filters out system nodes and properties (i.e. nodes and
7375
properties with names starting with 'jcr:'), the <info>sys_nodes</info> option
7476
allows to turn this filter off.
75-
allows to turn this filter off.
7677
EOF
7778
)
7879
;
@@ -103,8 +104,18 @@ protected function execute(InputInterface $input, OutputInterface $output)
103104
$nodeVisitor = new ConsoleDumperNodeVisitor($output, $identifiers);
104105

105106
$propVisitor = null;
107+
$refFormat = $input->getOption('ref-format');
108+
109+
if (null !== $refFormat && !in_array($refFormat, array('uuid', 'path'))) {
110+
throw new \Exception('The ref-format option must be set to either "path" or "uuid"');
111+
}
112+
106113
if ($input->hasParameterOption('--props')) {
107-
$propVisitor = new ConsoleDumperPropertyVisitor($output);
114+
$options = array();
115+
if ($refFormat) {
116+
$options['ref_format'] = $refFormat;
117+
}
118+
$propVisitor = new ConsoleDumperPropertyVisitor($output, $options);
108119
}
109120

110121
$walker = new TreeWalker($nodeVisitor, $propVisitor);

src/PHPCR/Util/Console/Helper/TreeDumper/ConsoleDumperPropertyVisitor.php

Lines changed: 49 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525

2626
use PHPCR\ItemInterface;
2727
use PHPCR\PropertyInterface;
28+
use PHPCR\PropertyType;
2829

2930
/**
3031
* TODO: this should base on the TraversingItemVisitor
@@ -38,21 +39,30 @@ class ConsoleDumperPropertyVisitor extends ConsoleDumperItemVisitor
3839
*
3940
* @var int
4041
*/
41-
protected $maxLineLength = 120;
42+
protected $maxLineLength;
43+
44+
/**
45+
* Show the full path for each reference
46+
*/
47+
protected $expandReferences;
4248

4349
/**
4450
* Instantiate property visitor
4551
*
4652
* @param OutputInterface $output
4753
* @param int $maxLineLength
4854
*/
49-
public function __construct(OutputInterface $output, $maxLineLength = null)
55+
public function __construct(OutputInterface $output, $options = array())
5056
{
57+
$options = array_merge(array(
58+
'max_line_length' => 120,
59+
'ref_format' => 'uuid',
60+
), $options);
61+
5162
parent::__construct($output);
5263

53-
if (null !== $maxLineLength) {
54-
$this->maxLineLength = $maxLineLength;
55-
}
64+
$this->maxLineLength = $options['max_line_length'];
65+
$this->refFormat = $options['ref_format'];
5666
}
5767

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

89+
$referrers = array();
90+
91+
if (in_array($item->getType(), array(
92+
PropertyType::WEAKREFERENCE,
93+
PropertyType::REFERENCE
94+
))) {
95+
$referenceStrings = array();
96+
97+
98+
if ('path' == $this->refFormat) {
99+
$references = (array) $item->getValue();
100+
101+
foreach ($references as $reference) {
102+
$referenceStrings[] = $reference->getPath();
103+
}
104+
} else {
105+
$referenceStrings = (array) $item->getString();
106+
}
107+
108+
$value = '';
109+
}
110+
79111
$value = str_replace(array("\n", "\t"), '', $value);
80112

81113
$this->output->writeln(str_repeat(' ', $this->level + 1) . '- <info>' . $item->getName() . '</info> = ' . $value);
114+
115+
if (isset($referenceStrings)) {
116+
foreach ($referenceStrings as $referenceString) {
117+
$this->output->writeln(sprintf(
118+
'%s - <info>%s</info>: %s',
119+
str_repeat(' ', $this->level + 1),
120+
$this->refFormat,
121+
$referenceString
122+
));
123+
}
124+
}
82125
}
126+
83127
}

0 commit comments

Comments
 (0)