Skip to content

Commit d28469f

Browse files
committed
Merge pull request #18 from phpcr/order-before-command
Added node order before command
2 parents 3765772 + 9421255 commit d28469f

File tree

4 files changed

+76
-8
lines changed

4 files changed

+76
-8
lines changed

features/bootstrap/FeatureContext.php

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -368,16 +368,30 @@ public function thereShouldExistANodeAt($arg1)
368368
public function thereShouldExistANodeAtBefore($arg1, $arg2)
369369
{
370370
$session = $this->getSession();
371+
371372
try {
372-
$node = $session->getNode($arg1);
373+
$node = $session->getNode($arg2);
373374
} catch (PathNotFoundException $e) {
374375
throw new \Exception('Node does at path ' . $arg1 . ' does not exist.');
375376
}
376-
$parent = $session->getNode(PathHelper::getParentPath($arg1));
377-
$index = $node->getIndex();
377+
378+
$parent = $session->getNode(PathHelper::getParentPath($arg2));
378379
$parentChildren = array_values((array) $parent->getNodes());
379-
$beforeNode = $parentChildren[$index];
380-
PHPUnit_Framework_Assert::assertEquals($arg2, $beforeNode->getName());
380+
$targetNode = null;
381+
382+
foreach ($parentChildren as $i => $parentChild) {
383+
if ($parentChild->getPath() == $arg1) {
384+
$targetNode = $parentChild;
385+
$afterNode = $parentChildren[$i + 1];
386+
break;
387+
}
388+
}
389+
390+
if (null === $targetNode) {
391+
throw new \Exception('Could not find child node ' . $arg1);
392+
}
393+
394+
PHPUnit_Framework_Assert::assertEquals($arg2, $afterNode->getPath());
381395
}
382396

383397
/**

features/node_reorder_before.feature

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@ Feature: Reorder a node
88
And the "session_data.xml" fixtures are loaded
99

1010
Scenario: Reorder a node
11-
Given the cnp is "/tests_general_base"
12-
And I execute the "node:order-before unversionable" command
11+
Given the current node is "/tests_general_base"
12+
And I execute the "node:order-before emptyExample idExample" command
1313
Then the command should not fail
14-
And there should exist a node at "/tests_general_base/simpleVersioned" before "/tests_general_base/unversionable"
14+
And I save the session
15+
And there should exist a node at "/tests_general_base/emptyExample" before "/tests_general_base/idExample"

src/PHPCR/Shell/Console/Application/ShellApplication.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@
7979
use PHPCR\Shell\Console\Command\NodeRenameCommand;
8080
use PHPCR\Shell\Console\Command\NodeMixinAddCommand;
8181
use PHPCR\Shell\Console\Command\NodeMixinRemoveCommand;
82+
use PHPCR\Shell\Console\Command\NodeOrderBeforeCommand;
8283
use PHPCR\Shell\Console\Command\NodeInfoCommand;
8384
use PHPCR\Shell\Console\Command\NodeLifecycleFollowCommand;
8485
use PHPCR\Shell\Console\Command\NodeLifecycleListCommand;
@@ -205,6 +206,7 @@ public function init()
205206
$this->add(new NodeRenameCommand());
206207
$this->add(new NodeMixinAddCommand());
207208
$this->add(new NodeMixinRemoveCommand());
209+
$this->add(new NodeOrderBeforeCommand());
208210
$this->add(new NodeInfoCommand());
209211
$this->add(new NodeLifecycleFollowCommand());
210212
$this->add(new NodeLifecycleListCommand());
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
<?php
2+
3+
namespace PHPCR\Shell\Console\Command;
4+
5+
use Symfony\Component\Console\Command\Command;
6+
use Symfony\Component\Console\Input\InputInterface;
7+
use Symfony\Component\Console\Output\OutputInterface;
8+
use Symfony\Component\Console\Input\InputArgument;
9+
use PHPCR\Util\CND\Writer\CndWriter;
10+
use PHPCR\NodeType\NoSuchNodeTypeException;
11+
use PHPCR\Util\CND\Parser\CndParser;
12+
use PHPCR\NamespaceException;
13+
14+
class NodeOrderBeforeCommand extends Command
15+
{
16+
protected function configure()
17+
{
18+
$this->setName('node:order-before');
19+
$this->setDescription('');
20+
$this->addArgument('srcChildRelPath', null, InputArgument::REQUIRED, 'The relative path to the child node to be moved in the ordering');
21+
$this->addArgument('destChildRelPath', null, InputArgument::REQUIRED, 'The relative path to the child before which the node srcChildRelPath will be placed');
22+
$this->setHelp(<<<HERE
23+
If this node supports child node ordering, this method inserts the child
24+
node at <info>srcChildRelPath</info> into the child node list at the position
25+
immediately before <info>destChildRelPath</info>
26+
27+
To place the node <info>srcChildRelPath</info> at the end of the list, a
28+
destChildRelPath of null is used.
29+
30+
Note that (apart from the case where <info>destChildRelPath</info> is null) both of
31+
these arguments must be relative paths of depth one, in other words they
32+
are the names of the child nodes, possibly suffixed with an index.
33+
34+
If <info>srcChildRelPath</info> and <info>destChildRelPath</info> are the same, then no change is
35+
made.
36+
37+
This is session-write method, meaning that a change made by this method
38+
is dispatched on save.
39+
HERE
40+
);
41+
}
42+
43+
public function execute(InputInterface $input, OutputInterface $output)
44+
{
45+
$session = $this->getHelper('phpcr')->getSession();
46+
$srcChildRelPath = $input->getArgument('srcChildRelPath');
47+
$destChildRelPath = $input->getArgument('destChildRelPath');
48+
$node = $session->getCurrentNode();
49+
$node->orderBefore($srcChildRelPath, $destChildRelPath);
50+
}
51+
}

0 commit comments

Comments
 (0)