Skip to content

Commit d6a3659

Browse files
committed
Merge pull request #54 from dantleech/move
Added move command
2 parents a21f770 + 7b2a4dc commit d6a3659

File tree

3 files changed

+84
-1
lines changed

3 files changed

+84
-1
lines changed

bin/phpcr

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ $cli->addCommands(array(
4646
new \PHPCR\Util\Console\Command\PurgeCommand(),
4747
new \PHPCR\Util\Console\Command\RegisterNodeTypesCommand(),
4848
new \PHPCR\Util\Console\Command\QueryCommand(),
49+
new \PHPCR\Util\Console\Command\MoveCommand(),
4950
));
5051
$cli->run();
5152

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
<?php
2+
3+
/**
4+
* This file is part of the PHPCR Utils
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*
18+
* @license http://www.apache.org/licenses/LICENSE-2.0 Apache Software License 2.0
19+
* @link http://phpcr.github.com/
20+
*/
21+
22+
namespace PHPCR\Util\Console\Command;
23+
24+
use Symfony\Component\Console\Command\Command;
25+
use Symfony\Component\Console\Input\InputArgument;
26+
use Symfony\Component\Console\Input\InputInterface;
27+
use Symfony\Component\Console\Output\OutputInterface;
28+
29+
/**
30+
* Command to move a node from one path to another
31+
*
32+
* @author Daniel Leech <daniel@dantleech.com>
33+
*/
34+
class MoveCommand extends Command
35+
{
36+
/**
37+
* {@inheritDoc}
38+
*/
39+
protected function configure()
40+
{
41+
$this
42+
->setName('phpcr:move')
43+
->addArgument('source', InputArgument::REQUIRED, 'Path of node to move')
44+
->addArgument('destination', InputArgument::REQUIRED, 'Destination for node')
45+
->setDescription('Moves a node from one path to another')
46+
->setHelp(<<<EOF
47+
This command simply moves a node from one path (the source path)
48+
to another (the destination path), it can also be considered
49+
as a rename command.
50+
51+
$ php bin/phpcr phpcr:move /foobar /barfoo
52+
53+
Note that the parent node of the destination path must already exist.
54+
EOF
55+
)
56+
;
57+
}
58+
59+
/**
60+
* {@inheritDoc}
61+
*/
62+
protected function execute(InputInterface $input, OutputInterface $output)
63+
{
64+
$session = $this->getHelper('phpcr')->getSession();
65+
66+
$sourcePath = $input->getArgument('source');
67+
$destPath = $input->getArgument('destination');
68+
69+
$output->writeln(sprintf(
70+
'<info>Moving </info>%s<info> to </info>%s',
71+
$sourcePath, $destPath
72+
));
73+
74+
$session->move($sourcePath, $destPath);
75+
$session->save();
76+
}
77+
78+
}

src/PHPCR/Util/PathHelper.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,10 +140,14 @@ public static function assertValidLocalName($name, $throw = true)
140140
*/
141141
public static function normalizePath($path, $destination = false, $throw = true)
142142
{
143-
if (! is_string($path) || strlen($path) === 0) {
143+
if (!is_string($path)) {
144144
throw new RepositoryException('Expected string but got ' . gettype($path));
145145
}
146146

147+
if (strlen($path) === 0) {
148+
throw new RepositoryException('Path must not be of zero length');
149+
}
150+
147151
if ('/' === $path) {
148152

149153
return '/';

0 commit comments

Comments
 (0)