Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions bin/phpcr
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ $cli->addCommands(array(
new \PHPCR\Util\Console\Command\PurgeCommand(),
new \PHPCR\Util\Console\Command\RegisterNodeTypesCommand(),
new \PHPCR\Util\Console\Command\QueryCommand(),
new \PHPCR\Util\Console\Command\MoveCommand(),
));
$cli->run();

78 changes: 78 additions & 0 deletions src/PHPCR/Util/Console/Command/MoveCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
<?php

/**
* This file is part of the PHPCR Utils
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* @license http://www.apache.org/licenses/LICENSE-2.0 Apache Software License 2.0
* @link http://phpcr.github.com/
*/

namespace PHPCR\Util\Console\Command;

use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

/**
* Command to move a node from one path to another
*
* @author Daniel Leech <daniel@dantleech.com>
*/
class MoveCommand extends Command
{
/**
* {@inheritDoc}
*/
protected function configure()
{
$this
->setName('phpcr:move')
->addArgument('source', InputArgument::REQUIRED, 'Path of node to move')
->addArgument('destination', InputArgument::REQUIRED, 'Destination for node')
->setDescription('Moves a node from one path to another')
->setHelp(<<<EOF
This command simply moves a node from one path (the source path)
to another (the destination path), it can also be considered
as a rename command.

$ php bin/phpcr phpcr:move /foobar /barfoo

Note that the parent node of the destination path must already exist.
EOF
)
;
}

/**
* {@inheritDoc}
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
$session = $this->getHelper('phpcr')->getSession();

$sourcePath = $input->getArgument('source');
$destPath = $input->getArgument('destination');

$output->writeln(sprintf(
'<info>Moving </info>%s<info> to </info>%s',
$sourcePath, $destPath
));

$session->move($sourcePath, $destPath);
$session->save();
}

}
6 changes: 5 additions & 1 deletion src/PHPCR/Util/PathHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -140,10 +140,14 @@ public static function assertValidLocalName($name, $throw = true)
*/
public static function normalizePath($path, $destination = false, $throw = true)
{
if (! is_string($path) || strlen($path) === 0) {
if (!is_string($path)) {
throw new RepositoryException('Expected string but got ' . gettype($path));
}

if (strlen($path) === 0) {
throw new RepositoryException('Path must not be of zero length');
}

if ('/' === $path) {

return '/';
Expand Down