Skip to content

Commit 04f0b23

Browse files
committed
Merge pull request #72 from phpcr/clean-dump-command
cleaning up the dump command parameters
2 parents 0a54685 + 84da397 commit 04f0b23

File tree

8 files changed

+244
-132
lines changed

8 files changed

+244
-132
lines changed

README.md

Lines changed: 25 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -28,36 +28,35 @@ NOTE: If you are using PHPCR inside of Symfony, the DoctrinePHPCRBundle
2828
provides the commands inside the normal Symfony console and you don't need to
2929
prepare anything special.
3030

31-
* ``phpcr:workspace:create <name>``: Create the workspace name in the configured repository
32-
* ``phpcr:register-node-types --allow-update [cnd-file]``: Register namespaces and node types from a "Compact Node Type Definition" .cnd file
33-
* ``phpcr:dump [--sys_nodes[="..."]] [--props[="..."]] [path]``: Show the node names
34-
under the specified path. If you set sys_nodes=yes you will also see system nodes.
35-
If you set props=yes you will additionally see all properties of the dumped nodes.
36-
* ``phpcr:purge``: Remove all content from the configured repository in the
37-
configured workspace
38-
* ``phpcr:sql2``: Run a query in the JCR SQL2 language against the repository and dump
39-
the resulting rows to the console.
40-
41-
**TODO:**
42-
43-
* Implement commands for phpcr:import and phpcr:export to import and export the
44-
PHPCR document view and system view XML dumps.
45-
* Implement a simple .cnd parser in PHP and use it to make register-node-types
46-
work with all repositories
47-
31+
To get a list of the available commands, run `bin/phpcr` or set the commands up
32+
in your application. Running `bin/phpcr help <command-name>` outputs the
33+
documentation of that command.
4834

4935
## Helper Classes
5036

5137
The helper classes provide implementations for basic common tasks to help users
52-
and implementors of PHPCR. They are all in the namespace PHPCR\Util
38+
and implementers of PHPCR. They are all in the namespace PHPCR\Util
5339

40+
### PathHelper
5441

55-
### TraversingItemVisitor
42+
Used to manipulate paths. Implementations are recommended to use this, and
43+
applications also profit from it. Using `dirname` and similar file system
44+
operations on paths is not compatible with Microsoft Windows systems, thus you
45+
should always use the methods in PathHelper.
5646

57-
This ``ItemVisitorInterface`` implementation is a basic implementation of crawling
58-
a PHPCR tree. You can extend it to define what it should do while crawling the
59-
tree.
47+
### NodeHelper
48+
49+
This helper has some generally useful methods like one to generate empty
50+
`nt:unstructured` nodes to make sure a parent path exists. It also provides
51+
some useful helper methods for implementations.
52+
53+
### UUIDHelper
6054

55+
This little helper is mainly of interest for PHPCR implementers. It generates
56+
valid *Universally Unique IDs* and can determine whether a given string is a
57+
valid UUID.
58+
We recommend all implementations to use this implementation to guarantee
59+
consistent behaviour.
6160

6261
### QOM QueryBuilder
6362

@@ -66,25 +65,15 @@ The ``QueryBuilder`` is a fluent query builder with method names matching the
6665
on top of the QOM factory. It is the easiest way to programmatically build a
6766
PHPCR query.
6867

69-
7068
### Query Object Model Converter
7169

7270
In the PHPCR\Util\QOM namespace we provide, implementation-independant code to
7371
convert between SQL2 and QOM. ``Sql2ToQomQueryConverter`` parses SQL2 queries
7472
into QOM . ``QomToSql2QueryConverter`` generates SQL2 out of a QOM.
7573

74+
### TraversingItemVisitor
7675

77-
### UUIDHelper
78-
79-
This little helper is mainly of interest for PHPCR implementors. It generates
80-
valid *Universally Unique IDs* and can determine wheter a given string is a
81-
valid UUID.
82-
We recommend all implementations to use this implementation to guarantee
83-
constistent behaviour.
84-
85-
86-
# TODO
87-
88-
Move tests about the query converter from phpcr-api-tests tests/06_Query/QOM to
89-
the tests folder. How to do the tests without a QOM factory implementation?
76+
This ``ItemVisitorInterface`` implementation is a basic implementation of crawling
77+
a PHPCR tree. You can extend it to define what it should do while crawling the
78+
tree.
9079

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,26 +2,27 @@
22

33
namespace PHPCR\Util\Console\Command;
44

5+
use PHPCR\SessionInterface;
56
use Symfony\Component\Console\Command\Command;
67
use Symfony\Component\Console\Input\InputInterface;
8+
use Symfony\Component\Console\Input\InputOption;
79
use Symfony\Component\Console\Output\OutputInterface;
810
use PHPCR\Util\Console\Helper\PhpcrCliHelper;
9-
use Symfony\Component\Console\Input\InputOption;
1011

1112
abstract class BaseCommand extends Command
1213
{
1314
protected $phpcrCliHelper;
1415

1516
/**
16-
* @return PHPCR\SessionInterface
17+
* @return SessionInterface
1718
*/
1819
protected function getPhpcrSession()
1920
{
2021
return $this->getHelper('phpcr')->getSession();
2122
}
2223

2324
/**
24-
* @return PHPCR\Util\Console\Helper\PhpcrCliHelper
25+
* @return PhpcrCliHelper
2526
*/
2627
protected function getPhpcrCliHelper()
2728
{

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

Lines changed: 7 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -21,20 +21,18 @@
2121

2222
namespace PHPCR\Util\Console\Command;
2323

24-
use PHPCR\Util\UUIDHelper;
2524
use PHPCR\ItemNotFoundException;
2625
use PHPCR\RepositoryException;
2726
use PHPCR\PathNotFoundException;
27+
28+
use PHPCR\Util\UUIDHelper;
29+
use PHPCR\Util\Console\Helper\PhpcrConsoleDumperHelper;
30+
2831
use Symfony\Component\Console\Input\InputArgument;
2932
use Symfony\Component\Console\Input\InputOption;
3033
use Symfony\Component\Console\Input\InputInterface;
3134
use Symfony\Component\Console\Output\OutputInterface;
3235

33-
use PHPCR\Util\TreeWalker;
34-
use PHPCR\Util\Console\Helper\TreeDumper\ConsoleDumperNodeVisitor;
35-
use PHPCR\Util\Console\Helper\TreeDumper\ConsoleDumperPropertyVisitor;
36-
use PHPCR\Util\Console\Helper\TreeDumper\SystemNodeFilter;
37-
3836
/**
3937
* Command subtrees under a path to the console
4038
*
@@ -43,21 +41,14 @@
4341
*/
4442
class NodeDumpCommand extends BaseCommand
4543
{
46-
/**
47-
* Limit after which to cut lines when dumping properties
48-
*
49-
* @var int
50-
*/
51-
private $dump_max_line_length = 120;
52-
5344
/**
5445
* {@inheritDoc}
5546
*/
5647
protected function configure()
5748
{
5849
$this
5950
->setName('phpcr:node:dump')
60-
->addOption('sys_nodes', null, InputOption::VALUE_NONE, 'Also dump system nodes')
51+
->addOption('sys-nodes', null, InputOption::VALUE_NONE, 'Also dump system nodes (recommended to use with a depth limit)')
6152
->addOption('props', null, InputOption::VALUE_NONE, 'Also dump properties of the nodes')
6253
->addOption('identifiers', null, InputOption::VALUE_NONE, 'Also output node UUID')
6354
->addOption('depth', null, InputOption::VALUE_OPTIONAL, 'Limit how many level of children to show', "-1")
@@ -72,29 +63,20 @@ protected function configure()
7263
displayed as yaml arrays.
7364
7465
By default the command filters out system nodes and properties (i.e. nodes and
75-
properties with names starting with 'jcr:'), the <info>sys_nodes</info> option
66+
properties with names starting with 'jcr:'), the <info>--sys-nodes</info> option
7667
allows to turn this filter off.
7768
HERE
7869
)
7970
;
8071
}
8172

82-
/**
83-
* Change at which length lines in the dump get cut.
84-
*
85-
* @param int $length maximum line length after which to cut the output.
86-
*/
87-
public function setDumpMaxLineLength($length)
88-
{
89-
$this->dump_max_line_length = $length;
90-
}
91-
9273
/**
9374
* {@inheritDoc}
9475
*/
9576
protected function execute(InputInterface $input, OutputInterface $output)
9677
{
9778
$session = $this->getPhpcrSession();
79+
/** @var $dumperHelper PhpcrConsoleDumperHelper */
9880
$dumperHelper = $this->getHelper('phpcr_console_dumper');
9981

10082
// node to dump

src/PHPCR/Util/NodeHelper.php

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323

2424
use PHPCR\ItemInterface;
2525
use PHPCR\NodeInterface;
26+
use PHPCR\PropertyInterface;
2627
use PHPCR\PropertyType;
2728
use PHPCR\SessionInterface;
2829
use PHPCR\RepositoryException;
@@ -50,7 +51,7 @@ private function __construct()
5051
* @param SessionInterface $session the PHPCR session to create the path
5152
* @param string $path full path, like /content/jobs/data
5253
*
53-
* @return \PHPCR\NodeInterface the last node of the path, i.e. data
54+
* @return NodeInterface the last node of the path, i.e. data
5455
*/
5556
public static function createPath(SessionInterface $session, $path)
5657
{
@@ -87,12 +88,14 @@ public static function purgeWorkspace(SessionInterface $session)
8788
{
8889
$root = $session->getRootNode();
8990

91+
/** @var $property PropertyInterface */
9092
foreach ($root->getProperties() as $property) {
9193
if (! self::isSystemItem($property)) {
9294
$property->remove();
9395
}
9496
}
9597

98+
/** @var $node NodeInterface */
9699
foreach ($root->getNodes() as $node) {
97100
if (! self::isSystemItem($node)) {
98101
$node->remove();
@@ -111,13 +114,17 @@ public static function deleteAllNodes(SessionInterface $session)
111114
}
112115

113116
/**
114-
* Determine whether this item has a namespace that is to be considered
115-
* a system namespace
117+
* Determine whether this item is to be considered a system item that you
118+
* usually want to hide and that should not be removed when purging the
119+
* repository.
116120
*
117121
* @param ItemInterface $item
118122
*/
119123
public static function isSystemItem(ItemInterface $item)
120124
{
125+
if ($item->getDepth() > 1) {
126+
return false;
127+
}
121128
$name = $item->getName();
122129

123130
return strpos($name, 'jcr:') === 0 || strpos($name, 'rep:') === 0;

src/PHPCR/Util/PathHelper.php

Lines changed: 40 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,11 @@ class PathHelper
3636
/**
3737
* Do not create an instance of this class
3838
*/
39+
// @codeCoverageIgnoreStart
3940
private function __construct()
4041
{
4142
}
43+
// @codeCoverageIgnoreEnd
4244

4345
/**
4446
* Check whether this is a syntactically valid absolute path.
@@ -66,19 +68,10 @@ public static function assertValidAbsolutePath($path, $destination = false, $thr
6668
|| strlen($path) > 1 && '/' === $path[strlen($path) - 1]
6769
|| preg_match('-//|/\./|/\.\./-', $path)
6870
) {
69-
if ($throw) {
70-
throw new RepositoryException("Invalid path $path");
71-
}
72-
73-
return false;
71+
return self::error("Invalid path $path", $throw);
7472
}
7573
if ($destination && ']' === $path[strlen($path) - 1]) {
76-
if ($throw) {
77-
throw new RepositoryException("Destination path may not end with index $path");
78-
}
79-
80-
return false;
81-
74+
return self::error("Destination path may not end with index $path", $throw);
8275
}
8376

8477
return true;
@@ -95,7 +88,8 @@ public static function assertValidAbsolutePath($path, $destination = false, $thr
9588
* encode and decode characters that are not natively allowed by a storage
9689
* engine.
9790
*
98-
* @param string $name The name to check
91+
* @param string $name The name to check
92+
* @param boolean $throw whether to throw an exception on validation errors.
9993
*
10094
* @return bool true if valid, false if not valid and $throw was false
10195
*
@@ -106,11 +100,11 @@ public static function assertValidAbsolutePath($path, $destination = false, $thr
106100
public static function assertValidLocalName($name, $throw = true)
107101
{
108102
if ('.' == $name || '..' == $name) {
109-
throw new RepositoryException('Name may not be parent or self identifier: ' . $name);
103+
return self::error("Name may not be parent or self identifier: $name", $throw);
110104
}
111105

112106
if (preg_match('/\\/|:|\\[|\\]|\\||\\*/', $name)) {
113-
throw new RepositoryException('Name contains illegal characters: '.$name);
107+
return self::error("Name contains illegal characters: $name", $throw);
114108
}
115109

116110
return true;
@@ -141,24 +135,18 @@ public static function assertValidLocalName($name, $throw = true)
141135
public static function normalizePath($path, $destination = false, $throw = true)
142136
{
143137
if (!is_string($path)) {
144-
throw new RepositoryException('Expected string but got ' . gettype($path));
138+
return self::error('Expected string but got ' . gettype($path), $throw);
145139
}
146-
147140
if (strlen($path) === 0) {
148-
throw new RepositoryException('Path must not be of zero length');
141+
return self::error('Path must not be of zero length', $throw);
149142
}
150143

151144
if ('/' === $path) {
152-
153145
return '/';
154146
}
155147

156148
if ('/' !== $path[0]) {
157-
if ($throw) {
158-
throw new RepositoryException("Not an absolute path '$path'");
159-
}
160-
161-
return false;
149+
return self::error("Not an absolute path '$path'", $throw);
162150
}
163151

164152
$finalParts= array();
@@ -211,9 +199,16 @@ public static function normalizePath($path, $destination = false, $throw = true)
211199
*/
212200
public static function absolutizePath($path, $context, $destination = false, $throw = true)
213201
{
214-
if (! $path) {
215-
throw new RepositoryException('empty path');
202+
if (!is_string($path)) {
203+
return self::error('Expected string path but got ' . gettype($path), $throw);
204+
}
205+
if (!is_string($context)) {
206+
return self::error('Expected string context but got ' . gettype($context), $throw);
216207
}
208+
if (strlen($path) === 0) {
209+
return self::error('Path must not be of zero length', $throw);
210+
}
211+
217212
if ('/' !== $path[0]) {
218213
$path = ('/' === $context) ? "/$path" : "$context/$path";
219214
}
@@ -270,4 +265,24 @@ public static function getPathDepth($path)
270265
{
271266
return substr_count(rtrim($path, '/'), '/');
272267
}
268+
269+
/**
270+
* If $throw is true, throw a RepositoryException with $msg. Otherwise
271+
* return false.
272+
*
273+
* @param string $msg the exception message to use in case of throw being true
274+
* @param boolean $throw whether to throw the exception or return false
275+
*
276+
* @return boolean false
277+
*
278+
* @throws RepositoryException
279+
*/
280+
private static function error($msg, $throw)
281+
{
282+
if ($throw) {
283+
throw new RepositoryException($msg);
284+
}
285+
286+
return false;
287+
}
273288
}

0 commit comments

Comments
 (0)