Skip to content

Commit 00613ee

Browse files
committed
Wildcard support
Support wildcards via. the CMF resource bundles PhpcrTraversalFinder class.
1 parent e040c53 commit 00613ee

File tree

6 files changed

+119
-21
lines changed

6 files changed

+119
-21
lines changed

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@
88
"phpcr/phpcr-utils": "~1.2",
99
"symfony/finder": "~2.3",
1010
"symfony/serializer": "~2.3",
11-
"symfony/yaml": "~2.3"
11+
"symfony/yaml": "~2.3",
12+
"symfony-cmf/resource": "dev-master"
1213
},
1314
"minimum-stability": "dev",
1415
"require-dev": {

features/phpcr_node_list.feature

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,3 +76,62 @@ Feature: List properites and chidren of current nodeA
7676
"""
7777
One or more weak reference targets have not been found
7878
"""
79+
80+
Scenario: Wildcard on name
81+
Given I execute the "node:list /tests_general_base/numberPropertyNode/jcr:con*" command
82+
Then the command should not fail
83+
And I should see the following:
84+
"""
85+
+-------------+-----------------+--+
86+
| jcr:content | nt:unstructured | |
87+
+-------------+-----------------+--+
88+
"""
89+
90+
Scenario: Wildcard on directory
91+
Given I execute the "node:list /tests_general_base/*/jcr:content" command
92+
Then the command should not fail
93+
And I should see the following:
94+
"""
95+
/tests_general_base/index.txt
96+
+-------------+-----------------+--+
97+
| jcr:content | nt:unstructured | |
98+
+-------------+-----------------+--+
99+
/tests_general_base/idExample
100+
+--------------+-----------------+--+
101+
| jcr:content/ | nt:unstructured | |
102+
+--------------+-----------------+--+
103+
/tests_general_base/numberPropertyNode
104+
+-------------+-----------------+--+
105+
| jcr:content | nt:unstructured | |
106+
+-------------+-----------------+--+
107+
/tests_general_base/NumberPropertyNodeToCompare1
108+
+-------------+-----------------+--+
109+
| jcr:content | nt:unstructured | |
110+
+-------------+-----------------+--+
111+
/tests_general_base/NumberPropertyNodeToCompare2
112+
+-------------+-----------------+--+
113+
| jcr:content | nt:unstructured | |
114+
+-------------+-----------------+--+
115+
"""
116+
117+
Scenario: Wildcard from relative path
118+
Given the current node is "/tests_general_base"
119+
And I execute the "node:list numberPropertyNode/jcr:con*" command
120+
Then the command should not fail
121+
And I should see the following:
122+
"""
123+
+-------------+-----------------+--+
124+
| jcr:content | nt:unstructured | |
125+
+-------------+-----------------+--+
126+
"""
127+
128+
Scenario: Wildcard from relative path 2
129+
Given the current node is "/tests_general_base"
130+
And I execute the "node:list num*" command
131+
Then the command should not fail
132+
And I should see the following:
133+
"""
134+
+---------------------+---------+--------------+
135+
| numberPropertyNode/ | nt:file | +jcr:content |
136+
+---------------------+---------+--------------+
137+
"""

src/PHPCR/Shell/Console/Command/Phpcr/NodeListCommand.php

Lines changed: 32 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
class NodeListCommand extends Command
1616
{
1717
protected $formatter;
18-
protected $filters;
1918
protected $textHelper;
2019
protected $maxLevel;
2120

@@ -26,7 +25,6 @@ protected function configure()
2625
$this->addArgument('path', InputArgument::OPTIONAL, 'Path of node', '.');
2726
$this->addOption('children', null, InputOption::VALUE_NONE, 'List only the children of this node');
2827
$this->addOption('properties', null, InputOption::VALUE_NONE, 'List only the properties of this node');
29-
$this->addOption('filter', 'f', InputOption::VALUE_REQUIRED|InputOption::VALUE_IS_ARRAY, 'Optional filter to apply');
3028
$this->addOption('level', 'L', InputOption::VALUE_REQUIRED, 'Depth of tree to show');
3129
$this->addOption('template', 't', InputOption::VALUE_NONE, 'Show template nodes and properties');
3230
$this->setHelp(<<<HERE
@@ -37,10 +35,12 @@ protected function configure()
3735
The <info>node:list</info> command can also shows template nodes and properties as defined a nodes node-type by
3836
using the <info>--template</info> option. Template nodes and properties are prefixed with the "@" symbol.
3937
40-
The command accepts wither a path (relative or absolute) to the node or a UUID.
38+
The command accepts either a path (relative or absolute) to the node, a UUID or a pattern:
4139
4240
PHPCRSH> node:list 842e61c0-09ab-42a9-87c0-308ccc90e6f4
4341
PHPCRSH> node:list /tests/foobar
42+
PHPCRSH> node:list /tests/*/foobar
43+
PHPCRSH> node:list /tests/*/foo*
4444
HERE
4545
);
4646
}
@@ -49,7 +49,6 @@ public function execute(InputInterface $input, OutputInterface $output)
4949
{
5050
$this->formatter = $this->getHelper('result_formatter');
5151
$this->textHelper = $this->getHelper('text');
52-
$this->filters = $input->getOption('filter');
5352
$this->maxLevel = $input->getOption('level');
5453

5554
$this->showChildren = $input->getOption('children');
@@ -58,35 +57,53 @@ public function execute(InputInterface $input, OutputInterface $output)
5857

5958
$session = $this->getHelper('phpcr')->getSession();
6059
$path = $input->getArgument('path');
60+
$filter = null;
6161

62-
$currentNode = $session->getNodeByPathOrIdentifier($path);
62+
try {
63+
$nodes = array($session->getNodeByPathOrIdentifier($path));
64+
} catch (\Exception $e) {
65+
$parentPath = $this->getHelper('path')->getParentPath($path);
66+
67+
$filter = substr($path, strlen($parentPath));
68+
69+
if ($filter[0] == '/') {
70+
$filter = substr($filter, 1);
71+
}
72+
73+
$nodes = $session->findNodes($parentPath);
74+
}
6375

6476
if (!$this->showChildren && !$this->showProperties) {
6577
$this->showChildren = true;
6678
$this->showProperties = true;
6779
}
6880

69-
$table = $this->getHelper('table')->create();
81+
foreach ($nodes as $node) {
82+
$table = $this->getHelper('table')->create();
83+
$this->renderNode($node, $table, array(), $filter);
7084

71-
$this->renderNode($currentNode, $table);
85+
if ($table->getNumberOfRows() > 0) {
86+
$output->writeln('<info>' . $node->getPath() . '</info>');
87+
$table->render($output);
88+
}
89+
}
7290

73-
$table->render($output);
7491
}
7592

76-
private function renderNode($currentNode, $table, $spacers = array())
93+
private function renderNode($currentNode, $table, $spacers = array(), $filter = null)
7794
{
7895
if ($this->showChildren) {
79-
$this->renderChildren($currentNode, $table, $spacers);
96+
$this->renderChildren($currentNode, $table, $spacers, $filter);
8097
}
8198

8299
if ($this->showProperties) {
83-
$this->renderProperties($currentNode, $table, $spacers);
100+
$this->renderProperties($currentNode, $table, $spacers, $filter);
84101
}
85102
}
86103

87-
private function renderChildren($currentNode, $table, $spacers)
104+
private function renderChildren($currentNode, $table, $spacers, $filter = null)
88105
{
89-
$children = $currentNode->getNodes($this->filters ? : null);
106+
$children = $currentNode->getNodes($filter ? : null);
90107

91108
$nodeType = $currentNode->getPrimaryNodeType();
92109
$childNodeDefinitions = $nodeType->getDeclaredChildNodeDefinitions();
@@ -147,9 +164,9 @@ private function renderChildren($currentNode, $table, $spacers)
147164
}
148165
}
149166

150-
private function renderProperties($currentNode, $table, $spacers)
167+
private function renderProperties($currentNode, $table, $spacers, $filter = null)
151168
{
152-
$properties = $currentNode->getProperties($this->filters ? : null);
169+
$properties = $currentNode->getProperties($filter ? : null);
153170

154171
try {
155172
$primaryItem = $currentNode->getPrimaryItem();

src/PHPCR/Shell/Console/Helper/PhpcrHelper.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
use PHPCR\SimpleCredentials;
1010
use PHPCR\Shell\Transport\TransportRegistryInterface;
1111
use PHPCR\SessionInterface;
12+
use PHPCR\Shell\Query\PhpcrRepository;
1213

1314
/**
1415
* Helper for managing PHPCR sessions

src/PHPCR/Shell/Console/Helper/TableHelper.php

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,23 @@
1313
*
1414
* @author Daniel Leech <daniel@dantleech.com>
1515
*/
16-
class TableHelper extends Helper
16+
class TableHelper extends OriginalTableHelper
1717
{
18+
private $numberOfRows = 0;
19+
1820
public function create()
1921
{
20-
return new OriginalTableHelper();
22+
return new self;
23+
}
24+
25+
public function addRow(array $row)
26+
{
27+
parent::addRow($row);
28+
$this->numberOfRows++;
2129
}
2230

23-
public function getName()
31+
public function getNumberOfRows()
2432
{
25-
return 'table';
33+
return $this->numberOfRows;
2634
}
2735
}

src/PHPCR/Shell/PhpcrSession.php

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,17 @@
77
use PHPCR\Util\PathHelper;
88
use PHPCR\Util\UUIDHelper;
99
use PHPCR\PathNotFoundException;
10+
use Symfony\Cmf\Component\Resource\Finder\PhpcrTraversalFinder;
1011

1112
class PhpcrSession implements SessionInterface
1213
{
1314
protected $session;
1415
protected $cwd = '/';
1516

16-
public function __construct(SessionInterface $session)
17+
public function __construct(SessionInterface $session, PhpcrTraversalFinder $finder = null)
1718
{
1819
$this->session = $session;
20+
$this->finder = $finder ? : new PhpcrTraversalFinder($session);
1921
}
2022

2123
/**
@@ -171,6 +173,16 @@ public function getNodeByPathOrIdentifier($pathOrId)
171173
return $this->getNode($pathOrId);
172174
}
173175

176+
public function findNodes($patternOrId)
177+
{
178+
if (true === UUIDHelper::isUUID($patternOrId)) {
179+
return $this->getNodeByIdentifier($patternOrId);
180+
}
181+
182+
$res = $this->finder->find($this->getAbsPath($patternOrId));
183+
184+
return $res;
185+
}
174186

175187
public function getRepository()
176188
{

0 commit comments

Comments
 (0)