|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace PHPCR\Shell\Console\Command\Shell; |
| 4 | + |
| 5 | +use Symfony\Component\Console\Command\Command; |
| 6 | +use Symfony\Component\Console\Input\InputInterface; |
| 7 | +use Symfony\Component\Console\Output\OutputInterface; |
| 8 | +use Symfony\Component\Console\Input\InputOption; |
| 9 | +use PHPCR\NodeInterface; |
| 10 | +use PHPCR\Util\NodeHelper; |
| 11 | + |
| 12 | +class ListTreeCommand extends Command |
| 13 | +{ |
| 14 | + protected $maxDepth; |
| 15 | + protected $maxResults; |
| 16 | + protected $showSystem; |
| 17 | + |
| 18 | + protected $nodeCount = 0; |
| 19 | + |
| 20 | + public function configure() |
| 21 | + { |
| 22 | + $this->setName('ls'); |
| 23 | + $this->setDescription('List tree'); |
| 24 | + $this->addOption('max-depth', 'd', InputOption::VALUE_REQUIRED, 'Depth', 1); |
| 25 | + $this->addOption('limit', 'l', InputOption::VALUE_REQUIRED, 'Limit results', null); |
| 26 | + $this->addOption('show-system', null, InputOption::VALUE_NONE, 'Show system nodes'); |
| 27 | + } |
| 28 | + |
| 29 | + public function execute(InputInterface $input, OutputInterface $output) |
| 30 | + { |
| 31 | + $this->nodeCount = 0; |
| 32 | + |
| 33 | + $session = $this->getHelper('phpcr')->getSession(); |
| 34 | + $node = $session->getNode($session->getCwd()); |
| 35 | + $this->maxDepth = $input->getOption('max-depth'); |
| 36 | + $this->maxResults = $input->getOption('limit'); |
| 37 | + $this->showSystem = $input->getOption('show-system'); |
| 38 | + |
| 39 | + $rows = new \ArrayObject(); |
| 40 | + $this->iterateTree($node, $rows); |
| 41 | + |
| 42 | + $table = clone $this->getHelper('table'); |
| 43 | + $table->setHeaders(array('Node / Prop', 'Type', 'Value')); |
| 44 | + |
| 45 | + foreach ($rows as $row) { |
| 46 | + $table->addRow($row); |
| 47 | + } |
| 48 | + |
| 49 | + $table->render($output); |
| 50 | + |
| 51 | + $output->writeln(''); |
| 52 | + $output->writeln($this->nodeCount . ' node(s)'); |
| 53 | + } |
| 54 | + |
| 55 | + public function iterateTree(NodeInterface $node, $rows, $depth = -1) |
| 56 | + { |
| 57 | + if (null !== $this->maxResults && $this->nodeCount >= $this->maxResults) { |
| 58 | + return; |
| 59 | + } |
| 60 | + |
| 61 | + if (true === NodeHelper::isSystemItem($node) && false === $this->showSystem) { |
| 62 | + return; |
| 63 | + } |
| 64 | + |
| 65 | + $this->nodeCount++; |
| 66 | + |
| 67 | + $formatter = $this->getHelper('result_formatter'); |
| 68 | + $properties = $node->getProperties(); |
| 69 | + |
| 70 | + $rows[] = array( |
| 71 | + str_repeat(' ', $depth + 1) . '<info>' . $node->getName() . '/</info>', |
| 72 | + '<info>' . $node->getPrimaryNodeType()->getName() . '</info>', |
| 73 | + '', |
| 74 | + ); |
| 75 | + |
| 76 | + $depth++; |
| 77 | + if ($depth >= $this->maxDepth) { |
| 78 | + return; |
| 79 | + } |
| 80 | + |
| 81 | + foreach ($properties as $key => $property) { |
| 82 | + if (true === NodeHelper::isSystemItem($property) && false === $this->showSystem) { |
| 83 | + continue; |
| 84 | + } |
| 85 | + |
| 86 | + $rows[] = array( |
| 87 | + sprintf('%s -<comment>%s</comment>', str_repeat(' ', $depth), $key), |
| 88 | + $formatter->getPropertyTypeName($property->getType()) . ($property->isMultiple() ? '[]' : ''), |
| 89 | + substr($formatter->formatValue($property), 0, 55), |
| 90 | + ); |
| 91 | + } |
| 92 | + |
| 93 | + foreach ($node->getNodes() as $child) { |
| 94 | + if ($depth < $this->maxDepth) { |
| 95 | + $this->iterateTree($child, $rows, $depth); |
| 96 | + } |
| 97 | + } |
| 98 | + } |
| 99 | +} |
| 100 | + |
0 commit comments