Skip to content

Commit 0fe627d

Browse files
committed
Refactored input configuration for node manipulation
1 parent a7cc636 commit 0fe627d

File tree

5 files changed

+54
-89
lines changed

5 files changed

+54
-89
lines changed

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

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use Symfony\Component\Console\Input\InputInterface;
77
use Symfony\Component\Console\Output\OutputInterface;
88
use PHPCR\Util\Console\Helper\PhpcrCliHelper;
9+
use Symfony\Component\Console\Input\InputOption;
910

1011
abstract class BaseCommand extends Command
1112
{
@@ -25,4 +26,24 @@ protected function getPhpcrCliHelper()
2526
$phpcrCliHelper = new PhpcrCliHelper($this->getPhpcrSession());
2627
return $phpcrCliHelper;
2728
}
29+
30+
public function configureNodeManipulationInput()
31+
{
32+
$this->addOption('set-prop', 'p',
33+
InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY,
34+
'Set node property on nodes use foo=bar'
35+
);
36+
$this->addOption('remove-prop', 'r',
37+
InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY,
38+
'Remove property from nodes'
39+
);
40+
$this->addOption('add-mixin', null,
41+
InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY,
42+
'Add a mixin to the nodes'
43+
);
44+
$this->addOption('remove-mixin', null,
45+
InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY,
46+
'Remove mixin from the nodes'
47+
);
48+
}
2849
}

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

Lines changed: 4 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@ protected function configure()
4545
{
4646
parent::configure();
4747

48+
$this->configureNodeManipulationInput();
49+
4850
$this->setName('phpcr:node:touch')
4951
->addArgument(
5052
'path',
@@ -57,26 +59,10 @@ protected function configure()
5759
'Node type, default nt:unstructured',
5860
'nt:unstructured'
5961
)
60-
->addOption('set-prop', 'p',
61-
InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY,
62-
'Set node property, use foo=bar'
63-
)
64-
->addOption('remove-prop', 'r',
65-
InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY,
66-
'Remove node property'
67-
)
6862
->addOption('dump', 'd',
6963
InputOption::VALUE_NONE,
7064
'Dump a string reperesentation of the created / modified node.'
7165
)
72-
->addOption('add-mixin', null,
73-
InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY,
74-
'Add a mixin to the node'
75-
)
76-
->addOption('remove-mixin', null,
77-
InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY,
78-
'Add a mixin to the node'
79-
)
8066
->setDescription('Create or modify a node')
8167
->setHelp(<<<HERE
8268
This command allows you to create or modify a node at the specified path.
@@ -107,9 +93,10 @@ protected function execute(InputInterface $input, OutputInterface $output)
10793

10894
$path = $input->getArgument('path');
10995
$type = $input->getOption('type');
96+
$dump = $input->getOption('dump');
97+
11098
$setProp = $input->getOption('set-prop');
11199
$removeProp = $input->getOption('remove-prop');
112-
$dump = $input->getOption('dump');
113100
$addMixins = $input->getOption('add-mixin');
114101
$removeMixins = $input->getOption('remove-mixin');
115102

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

Lines changed: 19 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -42,44 +42,20 @@ protected function configure()
4242
{
4343
parent::configure();
4444

45+
$this->configureNodeManipulationInput($this);
46+
4547
$this->setName('phpcr:nodes:update')
4648
->addOption(
47-
'type', 't',
49+
'query', null,
4850
InputOption::VALUE_REQUIRED,
49-
'Update nodes of given node type, e.g. nt:unstructured'
50-
)
51-
->addOption(
52-
'where', 'w',
53-
InputOption::VALUE_OPTIONAL,
54-
'Specify the update criteria in SQL, e.g. "foobar = \'foo\' AND barfoo = \'bar\'"'
51+
'Query used to select the nodes'
5552
)
5653
->addOption(
5754
'query-language', 'l',
5855
InputOption::VALUE_OPTIONAL,
59-
'The query language (sql, jcr_sql2',
56+
'The query language (e.g. sql, jcr_sql2)',
6057
'jcr_sql2'
6158
)
62-
63-
->addOption('set-prop', 'p',
64-
InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY,
65-
'Set node property on nodes use foo=bar'
66-
)
67-
->addOption('remove-prop', 'r',
68-
InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY,
69-
'Remove property from nodes'
70-
)
71-
->addOption('add-mixin', null,
72-
InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY,
73-
'Add a mixin to the nodes'
74-
)
75-
->addOption('remove-mixin', null,
76-
InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY,
77-
'Remove mixin from the nodes'
78-
)
79-
->addOption('force', null,
80-
InputOption::VALUE_NONE,
81-
'Use to bypass the confirmation dialog'
82-
)
8359
->setDescription('Command to manipulate the nodes in the workspace.')
8460
->setHelp(<<<HERE
8561
The <info>nodes:update</info> command updates properties of nodes of type x matching
@@ -101,8 +77,7 @@ protected function configure()
10177
*/
10278
protected function execute(InputInterface $input, OutputInterface $output)
10379
{
104-
$type = $input->getOption('type');
105-
$where = $input->getOption('where');
80+
$query = $input->getOption('query');
10681
$queryLanguage = strtoupper($input->getOption('query-language'));
10782
$setProp = $input->getOption('set-prop');
10883
$removeProp = $input->getOption('remove-prop');
@@ -114,22 +89,21 @@ protected function execute(InputInterface $input, OutputInterface $output)
11489

11590
$this->dialog = new DialogHelper();
11691

117-
if (!$type) {
118-
throw new \InvalidArgumentException('You must provide the "type" option, to select all nodes specify the type "nt:base"');
92+
if (!$query) {
93+
throw new \InvalidArgumentException(
94+
'You must provide a SELECT query, e.g. --select="SELECT * FROM [nt:unstructured]"'
95+
);
11996
}
12097

121-
if ($where) {
122-
$sql = sprintf('SELECT * FROM [%s] WHERE %s', $type, $where);
123-
} else {
124-
$sql = sprintf('SELECT * FROM [%s]', $type);
98+
if (strtoupper(substr($query, 0, 6) != 'SELECT')) {
99+
throw new \InvalidArgumentException(sprintf(
100+
'Query doesn\'t look like a SELECT query: "%s"',
101+
$query
102+
));
125103
}
126104

127-
$output->writeln($sql);
128-
$query = $helper->createQuery($queryLanguage, $sql);
129-
130-
$start = microtime(true);
105+
$query = $helper->createQuery($queryLanguage, $query);
131106
$result = $query->execute();
132-
$elapsed = microtime(true) - $start;
133107

134108
if (!$noInteraction) {
135109
if (false === $this->getAction($output, $result)) {
@@ -139,7 +113,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
139113

140114
foreach ($result as $i => $row) {
141115
$output->writeln(sprintf(
142-
"<info>Updating node:</info>[%d] %s.",
116+
"<info>Updating node:</info> [%d] %s.",
143117
$i,
144118
$row->getPath()
145119
));
@@ -154,9 +128,9 @@ protected function execute(InputInterface $input, OutputInterface $output)
154128
));
155129
}
156130

131+
$output->writeln('<info>Saving session...</info>');
157132
$session->save();
158-
159-
$output->writeln(sprintf('<info>%.2f seconds</info>', $elapsed));
133+
$output->writeln('<info>Done.</info>');
160134

161135
return 0;
162136
}

src/PHPCR/Util/Console/Helper/PhpcrCliHelper.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ public function processNode(OutputInterface $output, $node, $options)
9999

100100
foreach ($options['addMixins'] as $addMixin) {
101101
$output->writeln(sprintf(
102-
'<comment> > Adding mixin </comment>%s<comment>',
102+
'<comment> > Adding mixin </comment>%s',
103103
$addMixin
104104
));
105105

@@ -108,7 +108,7 @@ public function processNode(OutputInterface $output, $node, $options)
108108

109109
foreach ($options['removeMixins'] as $removeMixin) {
110110
$output->writeln(sprintf(
111-
'<comment> > Removing mixin </comment>%s<comment>',
111+
'<comment> > Removing mixin </comment>%s',
112112
$removeMixin
113113
));
114114

@@ -159,4 +159,3 @@ public function createQuery($language, $sql)
159159
return $query;
160160
}
161161
}
162-

tests/PHPCR/Tests/Util/Console/Command/NodesUpdateCommandTest.php

Lines changed: 8 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -22,33 +22,23 @@ public function provideNodeUpdate()
2222
{
2323
return array(
2424

25-
// no type specified
25+
// no query specified
2626
array(array(
2727
'exception' => 'InvalidArgumentException',
2828
)),
2929

30-
// select with no WHERE
30+
// specify query
3131
array(array(
32-
'type' => 'nt:unstructured',
33-
'expectedSql' => 'SELECT * FROM nt:unstructured',
34-
)),
35-
36-
// select with WHERE
37-
array(array(
38-
'type' => 'nt:unstructured',
39-
'where' => 'foo="bar"',
40-
'expectedSql' => 'SELECT * FROM nt:unstructured WHERE foo="bar"',
32+
'query' => 'SELECT * FROM nt:unstructured WHERE foo="bar"',
4133
)),
4234

4335
// set, remote properties and mixins
4436
array(array(
45-
'type' => 'nt:unstructured',
4637
'setProp' => array(array('foo', 'bar')),
4738
'removeProp' => array('bar'),
4839
'addMixin' => array('mixin1'),
4940
'removeMixin' => array('mixin1'),
50-
51-
'expectedSql' => 'SELECT * FROM nt:unstructured',
41+
'query' => 'SELECT * FROM nt:unstructured',
5242
)),
5343
);
5444
}
@@ -59,13 +49,11 @@ public function provideNodeUpdate()
5949
public function testNodeUpdate($options)
6050
{
6151
$options = array_merge(array(
62-
'type' => null,
63-
'where' => null,
52+
'query' => null,
6453
'setProp' => array(),
6554
'removeProp' => array(),
6655
'addMixin' => array(),
6756
'removeMixin' => array(),
68-
'expectedSql' => null,
6957
'exception' => null,
7058
), $options);
7159

@@ -82,7 +70,7 @@ public function testNodeUpdate($options)
8270

8371
$this->queryManager->expects($this->any())
8472
->method('createQuery')
85-
->with($options['expectedSql'], 'sql')
73+
->with($options['query'], 'JCR-SQL2')
8674
->will($this->returnValue($this->query));
8775

8876
$this->query->expects($this->any())
@@ -96,18 +84,14 @@ public function testNodeUpdate($options)
9684

9785
$args = array(
9886
'--query-language' => null,
99-
'--where' => $options['where'],
100-
'--force' => true,
87+
'--query' => $options['query'],
88+
'--no-interaction' => true,
10189
'--set-prop' => array(),
10290
'--remove-prop' => array(),
10391
'--add-mixin' => array(),
10492
'--remove-mixin' => array(),
10593
);
10694

107-
if ($options['type']) {
108-
$args['--type'] = $options['type'];
109-
}
110-
11195
foreach ($options['setProp'] as $setProp)
11296
{
11397
list($prop, $value) = $setProp;

0 commit comments

Comments
 (0)