Skip to content

Commit d87a737

Browse files
committed
Made apply-closure use create_function instead of eval
- Also polished up documentation etc.
1 parent c84f234 commit d87a737

File tree

4 files changed

+21
-6
lines changed

4 files changed

+21
-6
lines changed

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/NodesUpdateCommand.php

Lines changed: 14 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
}

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

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -117,8 +117,11 @@ public function processNode(OutputInterface $output, $node, $options)
117117
}
118118

119119
foreach ($options['applyClosures'] as $closureString) {
120-
$evalCode = sprintf('$closure = %s;', $closureString);
121-
eval($evalCode);
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+
));
122125
$closure($this->session, $node);
123126
}
124127

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ public function testApplyClosure()
143143
'--query' => "SELECT foo FROM bar",
144144
'--no-interaction' => true,
145145
'--apply-closure' => array(
146-
'function ($session, $node) { $session->getNodeByIdentifier("/foo"); $node->setProperty("foo", "bar"); }'
146+
'$session->getNodeByIdentifier("/foo"); $node->setProperty("foo", "bar");'
147147
),
148148
);
149149

0 commit comments

Comments
 (0)