Skip to content

Commit 56af352

Browse files
committed
Merge pull request #71 from dantleech/nodes-update-closure
[RFC] Added `--apply-closure` option
2 parents 0aa6751 + d87a737 commit 56af352

File tree

6 files changed

+81
-18
lines changed

6 files changed

+81
-18
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
Changelog
2+
=========
3+
4+
* **2013-06-15**: [Command] Added `--apply-closure` option to `phpcr:nodes:update` command.

bin/phpcr

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ $cli->addCommands(array(
4444
new \PHPCR\Util\Console\Command\NodeMoveCommand(),
4545
new \PHPCR\Util\Console\Command\NodeRemoveCommand(),
4646
new \PHPCR\Util\Console\Command\NodeTouchCommand(),
47+
new \PHPCR\Util\Console\Command\NodesUpdateCommand(),
4748
new \PHPCR\Util\Console\Command\NodeTypeListCommand(),
4849
new \PHPCR\Util\Console\Command\NodeTypeRegisterCommand(),
4950
new \PHPCR\Util\Console\Command\WorkspaceCreateCommand(),

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,5 +78,9 @@ public function configureNodeManipulationInput()
7878
InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY,
7979
'Remove mixin from the nodes'
8080
);
81+
$this->addOption('apply-closure', null,
82+
InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY,
83+
'Apply a closure to each node, closures are passed PHPCR\Session and PHPCR\NodeInterface'
84+
);
8185
}
8286
}

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

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,17 +61,28 @@ protected function configure()
6161
The <info>phpcr:nodes:update</info> can manipulate the properties of nodes
6262
found using the given query.
6363
64-
For example, to set the property "foo" to "bar" on all unstructured nodes:
64+
For example, to set the property <comment>foo</comment> to <comment>bar</comment> on all unstructured nodes:
6565
66-
php bin/phpcr phpcr:nodes:update --query="SELECT * FROM [nt:unstructured]" --set-prop=foo=bar
66+
<info>php bin/phpcr phpcr:nodes:update --query="SELECT * FROM [nt:unstructured]" --set-prop=foo=bar</info>
6767
6868
Or to update only nodes matching a certain criteria:
6969
70-
php bin/phpcr nodes:update --query="SELECT * FROM [nt:unstructured] WHERE [phpcr:class]=\"Some\\Class\\Here\" --add-mixin=mix:mimetype
70+
<info>php bin/phpcr phpcr:nodes:update \
71+
--query="SELECT * FROM [nt:unstructured] WHERE [phpcr:class]=\"Some\\Class\\Here\"" \
72+
--add-mixin=mix:mimetype</info>
7173
7274
The options for manipulating nodes are the same as with the
7375
<info>node:touch</info> command and
7476
can be repeated to update multiple properties.
77+
78+
If you have an advanced use case you can use the <comment>--apply-closure</comment> option:
79+
80+
<info>php bin/phpcr phpcr:nodes:update \
81+
--query="SELECT * FROM [nt:unstructured] WHERE [phpcr:class]=\"Some\\Class\\Here\"" \
82+
--apply-closure="\\\$session->doSomething(); \\\$node->setProperty('foo', 'bar');"</info>
83+
84+
For each node in the result set, the closure will be passed the current
85+
<comment>PHPCR\SessionInterface</comment> implementation and the node (<comment>PHPCR\NodeInterface</comment>) as <comment>\$session</comment> and <comment>\$node</comment>.
7586
HERE
7687
);
7788
}
@@ -89,6 +100,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
89100
$removeProp = $input->getOption('remove-prop');
90101
$addMixins = $input->getOption('add-mixin');
91102
$removeMixins = $input->getOption('remove-mixin');
103+
$applyClosures = $input->getOption('apply-closure');
92104
$noInteraction = $input->getOption('no-interaction');
93105
$helper = $this->getPhpcrCliHelper();
94106
$session = $this->getPhpcrSession();
@@ -129,6 +141,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
129141
'removeProp' => $removeProp,
130142
'addMixins' => $addMixins,
131143
'removeMixins' => $removeMixins,
144+
'applyClosures' => $applyClosures,
132145
));
133146
}
134147

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ public function processNode(OutputInterface $output, $node, $options)
7777
'removeProp' => array(),
7878
'addMixins' => array(),
7979
'removeMixins' => array(),
80+
'applyClosures' => array(),
8081
'dump' => false,
8182
), $options);
8283

@@ -115,6 +116,15 @@ public function processNode(OutputInterface $output, $node, $options)
115116
$node->removeMixin($removeMixin);
116117
}
117118

119+
foreach ($options['applyClosures'] as $closureString) {
120+
$closure = create_function('$session, $node', $closureString);
121+
$output->writeln(sprintf(
122+
'<comment> > Applying closure: %s</comment>',
123+
strlen($closureString) > 75 ? substr($closureString, 0, 72).'...' : $closureString
124+
));
125+
$closure($this->session, $node);
126+
}
127+
118128
if ($options['dump']) {
119129
$output->writeln('<info>Node dump: </info>');
120130
/** @var $property PropertyInterface */

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

Lines changed: 46 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -40,24 +40,12 @@ public function provideNodeUpdate()
4040
);
4141
}
4242

43-
/**
44-
* @dataProvider provideNodeUpdate
45-
*/
46-
public function testNodeUpdate($options)
43+
protected function setupQueryManager($options)
4744
{
4845
$options = array_merge(array(
49-
'query' => null,
50-
'setProp' => array(),
51-
'removeProp' => array(),
52-
'addMixin' => array(),
53-
'removeMixin' => array(),
54-
'exception' => null,
46+
'query' => '',
5547
), $options);
5648

57-
if ($options['exception']) {
58-
$this->setExpectedException($options['exception']);
59-
}
60-
6149
$this->session->expects($this->any())
6250
->method('getWorkspace')
6351
->will($this->returnValue($this->workspace));
@@ -69,7 +57,6 @@ public function testNodeUpdate($options)
6957
->method('createQuery')
7058
->with($options['query'], 'JCR-SQL2')
7159
->will($this->returnValue($this->query));
72-
7360
$this->query->expects($this->any())
7461
->method('execute')
7562
->will($this->returnValue(array(
@@ -78,6 +65,27 @@ public function testNodeUpdate($options)
7865
$this->row1->expects($this->any())
7966
->method('getNode')
8067
->will($this->returnValue($this->node1));
68+
}
69+
70+
/**
71+
* @dataProvider provideNodeUpdate
72+
*/
73+
public function testNodeUpdate($options)
74+
{
75+
$options = array_merge(array(
76+
'query' => null,
77+
'setProp' => array(),
78+
'removeProp' => array(),
79+
'addMixin' => array(),
80+
'removeMixin' => array(),
81+
'exception' => null,
82+
), $options);
83+
84+
if ($options['exception']) {
85+
$this->setExpectedException($options['exception']);
86+
}
87+
88+
$this->setupQueryManager($options);
8189

8290
$args = array(
8391
'--query-language' => null,
@@ -128,4 +136,27 @@ public function testNodeUpdate($options)
128136

129137
$ct = $this->executeCommand('phpcr:nodes:update', $args);
130138
}
139+
140+
public function testApplyClosure()
141+
{
142+
$args = array(
143+
'--query' => "SELECT foo FROM bar",
144+
'--no-interaction' => true,
145+
'--apply-closure' => array(
146+
'$session->getNodeByIdentifier("/foo"); $node->setProperty("foo", "bar");'
147+
),
148+
);
149+
150+
$this->setupQueryManager(array('query' => 'SELECT foo FROM bar'));
151+
152+
$this->node1->expects($this->once())
153+
->method('setProperty')
154+
->with('foo', 'bar');
155+
156+
$this->session->expects($this->once())
157+
->method('getNodeByIdentifier')
158+
->with('/foo');
159+
160+
$ct = $this->executeCommand('phpcr:nodes:update', $args);
161+
}
131162
}

0 commit comments

Comments
 (0)