Skip to content

Commit 9d7c531

Browse files
committed
fix commands and cleanup the help messages
1 parent 43e06b7 commit 9d7c531

File tree

4 files changed

+82
-63
lines changed

4 files changed

+82
-63
lines changed

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

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,10 @@
3737
use PHPCR\Util\Console\Helper\TreeDumper\SystemNodeFilter;
3838

3939
/**
40-
* Command to dump all nodes under a path to the console
40+
* Command subtrees under a path to the console
4141
*
4242
* @author Daniel Barsotti <daniel.barsotti@liip.ch>
43+
* @author Daniel Leech <daniel@dantleech.com>
4344
*/
4445
class NodeDumpCommand extends Command
4546
{
@@ -57,13 +58,13 @@ protected function configure()
5758
{
5859
$this
5960
->setName('phpcr:node:dump')
60-
->addOption('sys_nodes', null, InputOption::VALUE_NONE, 'Use to dump the system nodes')
61-
->addOption('props', null, InputOption::VALUE_NONE, 'Use to dump the node properties')
62-
->addOption('depth', null, InputOption::VALUE_OPTIONAL, 'Set to a number to limit how deep into the tree to recurse', "-1")
63-
->addOption('identifiers', null, InputOption::VALUE_NONE, 'Use to also output node UUID')
61+
->addOption('sys_nodes', null, InputOption::VALUE_NONE, 'Also dump system nodes')
62+
->addOption('props', null, InputOption::VALUE_NONE, 'Also dump properties of the nodes')
63+
->addOption('identifiers', null, InputOption::VALUE_NONE, 'Also output node UUID')
64+
->addOption('depth', null, InputOption::VALUE_OPTIONAL, 'Limit how many level of children to show', "-1")
6465
->addOption('ref-format', 'uuid', InputOption::VALUE_REQUIRED, 'Set the way references should be displayed when dumping reference properties - either "uuid" (default) or "path"')
65-
->addArgument('identifier', InputArgument::OPTIONAL, 'Path of the node to dump', '/')
66-
->setDescription('Dump the content repository')
66+
->addArgument('identifier', InputArgument::OPTIONAL, 'Root path to dump', '/')
67+
->setDescription('Dump subtrees of the content repository')
6768
->setHelp(<<<EOF
6869
The <info>dump</info> command recursively outputs the name of the node specified
6970
by the <info>identifier</info> argument and its subnodes in a yaml-like style.

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

Lines changed: 35 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121

2222
namespace PHPCR\Util\Console\Command;
2323

24+
use PHPCR\NodeInterface;
25+
use PHPCR\SessionInterface;
2426
use Symfony\Component\Console\Command\Command;
2527
use Symfony\Component\Console\Input\InputArgument;
2628
use Symfony\Component\Console\Input\InputOption;
@@ -35,6 +37,7 @@
3537
* session.
3638
*
3739
* @author Daniel Barsotti <daniel.barsotti@liip.ch>
40+
* @author Daniel Leech <daniel@dantleech.com>
3841
*/
3942
class NodeRemoveCommand extends Command
4043
{
@@ -48,11 +51,11 @@ protected function configure()
4851
$this
4952
->setName('phpcr:node:remove')
5053
->setDescription('Remove content from the repository')
51-
->addArgument('path', InputArgument::OPTIONAL, 'Path of the node to purge', '/')
54+
->addArgument('path', InputArgument::REQUIRED, 'Path of the node to purge')
5255
->addOption('force', null, InputOption::VALUE_NONE, 'Use to bypass the confirmation dialog')
5356
->addOption('only-children', null, InputOption::VALUE_NONE, 'Use to only purge children of specified path')
5457
->setHelp(<<<EOF
55-
The <info>phpcr:node:remove</info> command will remove the given node or the
58+
The <info>phpcr:node:remove</info> command will remove the given node or the
5659
children of the given node according to the options given.
5760
5861
Remove specified node and its children:
@@ -72,22 +75,33 @@ protected function configure()
7275
*/
7376
protected function execute(InputInterface $input, OutputInterface $output)
7477
{
78+
/** @var $session SessionInterface*/
7579
$session = $this->getHelper('phpcr')->getSession();
7680

7781
$path = $input->getArgument('path');
7882
$force = $input->getOption('force');
7983
$onlyChildren = $input->getOption('only-children');
8084

85+
86+
if ('/' === $path) {
87+
// even if we have only children, this will not work as we would
88+
// try to remove system nodes.
89+
throw new \InvalidArgumentException(
90+
'Can not delete root node (path "/"), please use the '.
91+
'workspace:purge command instead to purge the whole workspace.'
92+
);
93+
}
94+
8195
if (!$force) {
8296
$dialog = new DialogHelper();
8397
$workspaceName = $session->getWorkspace()->getName();
8498

8599
if ($onlyChildren) {
86-
$question =
100+
$question =
87101
'Are you sure you want to recursively delete the children of path "%s" '.
88102
'from workspace "%s"';
89103
} else {
90-
$question =
104+
$question =
91105
'Are you sure you want to recursively delete the path "%s" '.
92106
'from workspace "%s"';
93107
}
@@ -97,34 +111,29 @@ protected function execute(InputInterface $input, OutputInterface $output)
97111
));
98112
}
99113

100-
if ($force) {
101-
$message = '<comment>></comment> <info>Purging: </info>%s';
114+
if (!$force) {
115+
$output->writeln('<error>Aborted</error>');
102116

103-
if ($onlyChildren) {
104-
$baseNode = $session->getNode($path, 0);
117+
return 1;
118+
}
105119

106-
foreach ($baseNode->getNodes() as $childNode) {
107-
$output->writeln(sprintf($message, $childNode->getPath()));
108-
$childNode->remove();
109-
}
110-
} else {
111-
$output->writeln(sprintf($message, $path));
112-
113-
if ('/' === $path) {
114-
throw new \Exception(
115-
'Will not purge path entire workspace ("/"), use the '.
116-
'workspace:purge method instead.'
117-
);
118-
} else {
119-
$session->removeItem($path);
120-
}
121-
}
120+
$message = '<comment>></comment> <info>Removing: </info>%s';
121+
122+
if ($onlyChildren) {
123+
$baseNode = $session->getNode($path, 0);
122124

123-
$session->save();
125+
/** @var $childNode NodeInterface */
126+
foreach ($baseNode->getNodes() as $childNode) {
127+
$childNode->remove();
128+
$output->writeln(sprintf($message, $childNode->getPath()));
129+
}
124130
} else {
125-
$output->writeln('<error>Aborted</error>');
131+
$session->removeItem($path);
132+
$output->writeln(sprintf($message, $path));
126133
}
127134

135+
$session->save();
136+
128137
return 0;
129138
}
130139
}

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

Lines changed: 24 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121

2222
namespace PHPCR\Util\Console\Command;
2323

24+
use PHPCR\PropertyInterface;
25+
use PHPCR\SessionInterface;
2426
use Symfony\Component\Console\Command\Command;
2527
use Symfony\Component\Console\Input\InputOption;
2628
use Symfony\Component\Console\Input\InputArgument;
@@ -45,26 +47,26 @@ protected function configure()
4547

4648
$this->setName('phpcr:node:touch')
4749
->addArgument(
48-
'path',
49-
InputArgument::REQUIRED,
50+
'path',
51+
InputArgument::REQUIRED,
5052
'Path at which to create the new node'
5153
)
5254
->addOption(
53-
'type', 't',
54-
InputOption::VALUE_OPTIONAL,
55-
'Node type, default nt:unstructured',
55+
'type', 't',
56+
InputOption::VALUE_OPTIONAL,
57+
'Node type, default nt:unstructured',
5658
'nt:unstructured'
5759
)
58-
->addOption('set-prop', 'p',
59-
InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY,
60+
->addOption('set-prop', 'p',
61+
InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY,
6062
'Set node property, use foo=bar'
6163
)
62-
->addOption('remove-prop', 'r',
63-
InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY,
64+
->addOption('remove-prop', 'r',
65+
InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY,
6466
'Remove node property'
6567
)
66-
->addOption('dump', 'd',
67-
InputOption::VALUE_NONE,
68+
->addOption('dump', 'd',
69+
InputOption::VALUE_NONE,
6870
'Dump a string reperesentation of the created / modified node.'
6971
)
7072
->addOption('add-mixin', null,
@@ -81,7 +83,7 @@ protected function configure()
8183
8284
For example::
8385
84-
$ ./bin/phpcr phpcr:touch /foobar --type=my:nodetype --set=foo=bar
86+
$ ./bin/phpcr phpcr:touch /foobar --type=my:nodetype --set-prop=foo=bar
8587
8688
Will create the node "/foobar" and set (or create) the "foo" property
8789
with a value of "bar".
@@ -90,7 +92,7 @@ protected function configure()
9092
the property "bar" and remove the property "foo". We also add the dump option
9193
to output a string reperesentation of the node.
9294
93-
$ ./bin/phpcr phpcr:touch /foobar --type=my:nodetype --set=bar=myvalue --unset=foo --dump
95+
$ ./bin/phpcr phpcr:touch /foobar --type=my:nodetype --set-prop=bar=myvalue --remove-prop=foo --dump
9496
HERE
9597
);
9698
}
@@ -100,11 +102,13 @@ protected function configure()
100102
*/
101103
protected function execute(InputInterface $input, OutputInterface $output)
102104
{
105+
/** @var $session SessionInterface */
103106
$session = $this->getHelper('phpcr')->getSession();
107+
104108
$path = $input->getArgument('path');
105109
$type = $input->getOption('type');
106-
$sets = $input->getOption('set');
107-
$unsets = $input->getOption('unset');
110+
$setProp = $input->getOption('set-prop');
111+
$removeProp = $input->getOption('remove-prop');
108112
$dump = $input->getOption('dump');
109113
$addMixins = $input->getOption('add-mixin');
110114
$removeMixins = $input->getOption('remove-mixin');
@@ -118,7 +122,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
118122
if ($node) {
119123
$nodeType = $node->getPrimaryNodeType()->getName();
120124
$output->writeln(sprintf(
121-
'<info>Node at path </info>%s <info>already exists and has primary type</info> %s.',
125+
'<info>Node at path </info>%s <info>already exists and has primary type</info> %s.',
122126
$path,
123127
$nodeType
124128
));
@@ -148,10 +152,10 @@ protected function execute(InputInterface $input, OutputInterface $output)
148152
'<info>Creating node: </info> %s [%s]', $path, $type
149153
));
150154

151-
$node = $parentNode->addNode($nodeName, $type);
155+
$node = $parentNode->addNode($nodeName, $type);
152156
}
153157

154-
foreach ($sets as $set) {
158+
foreach ($setProp as $set) {
155159
$parts = explode('=', $set);
156160
$output->writeln(sprintf(
157161
'<comment> > Setting property </comment>%s<comment> to </comment>%s',
@@ -160,7 +164,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
160164
$node->setProperty($parts[0], $parts[1]);
161165
}
162166

163-
foreach ($unsets as $unset) {
167+
foreach ($removeProp as $unset) {
164168
$output->writeln(sprintf(
165169
'<comment> > Unsetting property </comment>%s',
166170
$unset
@@ -178,6 +182,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
178182

179183
if ($dump) {
180184
$output->writeln('<info>Node dump: </info>');
185+
/** @var $property PropertyInterface */
181186
foreach ($node->getProperties() as $property) {
182187
$value = $property->getValue();
183188
if (!is_string($value)) {

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

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121

2222
namespace PHPCR\Util\Console\Command;
2323

24+
use PHPCR\SessionInterface;
2425
use Symfony\Component\Console\Command\Command;
2526
use Symfony\Component\Console\Input\InputArgument;
2627
use Symfony\Component\Console\Input\InputOption;
@@ -31,8 +32,8 @@
3132
use PHPCR\Util\NodeHelper;
3233

3334
/**
34-
* Command to remove all nodes from a path in the workspace of the configured
35-
* session.
35+
* Command to remove all non-system nodes and properties in the workspace of
36+
* the configured session.
3637
*
3738
* @author Daniel Leech <daniel@dantleech.com>
3839
*/
@@ -50,7 +51,8 @@ protected function configure()
5051
->setDescription('Remove all nodes from a workspace')
5152
->addOption('force', null, InputOption::VALUE_NONE, 'Use to bypass the confirmation dialog')
5253
->setHelp(<<<EOF
53-
The <info>phpcr:purge</info> command remove all the non-standard nodes from the workspace.
54+
The <info>phpcr:workspace:purge</info> command removes all nodes except the
55+
system nodes and all non-system properties of the root node from the workspace.
5456
EOF
5557
)
5658
;
@@ -61,27 +63,29 @@ protected function configure()
6163
*/
6264
protected function execute(InputInterface $input, OutputInterface $output)
6365
{
66+
/** @var $session SessionInterface */
6467
$session = $this->getHelper('phpcr')->getSession();
6568
$force = $input->getOption('force');
6669

70+
$workspaceName = $session->getWorkspace()->getName();
6771
if (!$force) {
6872
$dialog = new DialogHelper();
69-
$workspaceName = $session->getWorkspace()->getName();
7073
$force = $dialog->askConfirmation($output, sprintf(
71-
'<question>Are you sure you want to purge workspace "%s" Y/N ?</question>',
74+
'<question>Are you sure you want to purge workspace "%s" Y/N ?</question>',
7275
$workspaceName
7376
), false);
7477
}
7578

76-
if ($force) {
77-
$message = '';
78-
$output->writeln(sprintf('<info>Purging workspace:</info> %s', $path));
79-
NodeHelper::purgeWorkspace($session);
80-
$session->save();
81-
} else {
79+
if (!$force) {
8280
$output->writeln('<error>Aborted</error>');
81+
82+
return 1;
8383
}
8484

85+
$output->writeln(sprintf('<info>Purging workspace:</info> %s', $workspaceName));
86+
NodeHelper::purgeWorkspace($session);
87+
$session->save();
88+
8589
return 0;
8690
}
8791
}

0 commit comments

Comments
 (0)