Skip to content

Commit c84f234

Browse files
committed
Experimental node apply-closures option
1 parent 0aa6751 commit c84f234

File tree

5 files changed

+63
-15
lines changed

5 files changed

+63
-15
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.

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: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
8989
$removeProp = $input->getOption('remove-prop');
9090
$addMixins = $input->getOption('add-mixin');
9191
$removeMixins = $input->getOption('remove-mixin');
92+
$applyClosures = $input->getOption('apply-closure');
9293
$noInteraction = $input->getOption('no-interaction');
9394
$helper = $this->getPhpcrCliHelper();
9495
$session = $this->getPhpcrSession();
@@ -129,6 +130,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
129130
'removeProp' => $removeProp,
130131
'addMixins' => $addMixins,
131132
'removeMixins' => $removeMixins,
133+
'applyClosures' => $applyClosures,
132134
));
133135
}
134136

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

Lines changed: 7 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,12 @@ public function processNode(OutputInterface $output, $node, $options)
115116
$node->removeMixin($removeMixin);
116117
}
117118

119+
foreach ($options['applyClosures'] as $closureString) {
120+
$evalCode = sprintf('$closure = %s;', $closureString);
121+
eval($evalCode);
122+
$closure($this->session, $node);
123+
}
124+
118125
if ($options['dump']) {
119126
$output->writeln('<info>Node dump: </info>');
120127
/** @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+
'function ($session, $node) { $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)