Skip to content

Commit

Permalink
added option to control the uuid-behavior
Browse files Browse the repository at this point in the history
  • Loading branch information
wachterjohannes committed Feb 9, 2018
1 parent 9f51c3e commit a5bf108
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 13 deletions.
33 changes: 28 additions & 5 deletions src/PHPCR/Util/Console/Command/WorkspaceImportCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,13 @@
*/
class WorkspaceImportCommand extends BaseCommand
{
private static $uuidBehavior = array(
'new' => ImportUUIDBehaviorInterface::IMPORT_UUID_CREATE_NEW,
'remove' => ImportUUIDBehaviorInterface::IMPORT_UUID_COLLISION_REMOVE_EXISTING,
'replace' => ImportUUIDBehaviorInterface::IMPORT_UUID_COLLISION_REPLACE_EXISTING,
'throw' => ImportUUIDBehaviorInterface::IMPORT_UUID_COLLISION_THROW,
);

/**
* {@inheritDoc}
*/
Expand All @@ -30,6 +37,7 @@ protected function configure()
->setName('phpcr:workspace:import')
->addArgument('filename', null, 'The xml file to import')
->addOption('parentpath', 'p', InputOption::VALUE_OPTIONAL, 'Repository path to the parent where to import the file contents', '/')
->addOption('uuid-behavior', null, InputOption::VALUE_REQUIRED, 'How to handle UUID collisions during the import', 'new')
->setDescription('Import xml data into the repository, either in JCR system view format or arbitrary xml')
->setHelp(<<<EOF
The <info>import</info> command uses the PHPCR SessionInterface::importXml method
Expand All @@ -40,6 +48,17 @@ protected function configure()
If the <info>parentpath</info> option is set, the document is imported to that
path. Otherwise the document is imported at the repository root.
The optional <info>uuid-behavior</info> option describes how UUIDs should be
handled. The following options are available:
* <info>new</info> recreate a new uuid for each imported node;
* <info>remove</info> on collision, remove the old node from the repository and
put the imported data in the tree;
* <info>replace</info> on collision, replace the existing node with the one being
imported. All children of the imported node also go to the new path;
* <info>throw</info> throw an exception on uuid collision.
EOF
)
;
Expand All @@ -61,11 +80,15 @@ protected function execute(InputInterface $input, OutputInterface $output)
return 1;
}

$session->importXml(
$parentPath,
$filename,
ImportUUIDBehaviorInterface::IMPORT_UUID_CREATE_NEW
);
$uuidBehavior = $input->getOption('uuid-behavior');
if (!array_key_exists($uuidBehavior, self::$uuidBehavior)) {
$output->writeln(sprintf('<error>UUID-Behavior "%s" is not supported</error>', $uuidBehavior));
$output->writeln(sprintf('Supported behaviors are %s', implode(', ', array_keys(self::$uuidBehavior))));

return 1;
}

$session->importXML($parentPath, $filename, self::$uuidBehavior[$uuidBehavior]);
$session->save();

$output->writeln(sprintf(
Expand Down
8 changes: 4 additions & 4 deletions src/PHPCR/Util/NodeHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -154,10 +154,10 @@ public static function generateAutoNodeName($usedNames, $namespaces, $defaultNam
return self::generateWithPrefix($usedNames, '');
}

/*
* "<i>somePrefix</i>:" where <i>somePrefix</i> is a syntactically
* valid namespace prefix
*/
/*
* "<i>somePrefix</i>:" where <i>somePrefix</i> is a syntactically
* valid namespace prefix
*/
if (':' == $nameHint[strlen($nameHint)-1]
&& substr_count($nameHint, ':') === 1
&& preg_match('#^[a-zA-Z][a-zA-Z0-9]*:$#', $nameHint)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

namespace PHPCR\Tests\Util\Console\Command;

use Symfony\Component\Console\Application;
use PHPCR\Util\Console\Command\WorkspaceImportCommand;
use PHPCR\ImportUUIDBehaviorInterface;
use PHPCR\RepositoryInterface;
use PHPCR\Util\Console\Command\WorkspaceImportCommand;

class WorkspaceImportCommandTest extends BaseCommandTest
{
Expand All @@ -14,7 +14,7 @@ public function setUp()
$this->application->add(new WorkspaceImportCommand());
}

public function testNodeTypeList()
public function testImport()
{
$this->session->expects($this->once())
->method('getRepository')
Expand All @@ -24,12 +24,34 @@ public function testNodeTypeList()
->with(RepositoryInterface::OPTION_XML_IMPORT_SUPPORTED)
->will($this->returnValue(true));
$this->session->expects($this->once())
->method('importXml');
->method('importXml')
->with('/', 'test_import.xml', ImportUUIDBehaviorInterface::IMPORT_UUID_CREATE_NEW);

$ct = $this->executeCommand('phpcr:workspace:import', array(
'filename' => 'test_import.xml'
));

$this->assertContains('Successfully imported', $ct->getDisplay());
}

public function testImportUuidBehaviorThrow()
{
$this->session->expects($this->once())
->method('getRepository')
->will($this->returnValue($this->repository));
$this->repository->expects($this->once())
->method('getDescriptor')
->with(RepositoryInterface::OPTION_XML_IMPORT_SUPPORTED)
->will($this->returnValue(true));
$this->session->expects($this->once())
->method('importXml')
->with('/', 'test_import.xml', ImportUUIDBehaviorInterface::IMPORT_UUID_COLLISION_THROW);

$ct = $this->executeCommand('phpcr:workspace:import', array(
'filename' => 'test_import.xml',
'--uuid-behavior' => 'throw',
));

$this->assertContains('Successfully imported', $ct->getDisplay());
}
}

0 comments on commit a5bf108

Please sign in to comment.