Skip to content

Commit a5ac217

Browse files
wachterjohannesdbu
authored andcommitted
added option to control the uuid-behavior
1 parent d876a5d commit a5ac217

File tree

2 files changed

+55
-7
lines changed

2 files changed

+55
-7
lines changed

src/PHPCR/Util/Console/Command/WorkspaceImportCommand.php

+28-5
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,13 @@
1717
*/
1818
class WorkspaceImportCommand extends BaseCommand
1919
{
20+
const UUID_BEHAVIOR = [
21+
'new' => ImportUUIDBehaviorInterface::IMPORT_UUID_CREATE_NEW,
22+
'remove' => ImportUUIDBehaviorInterface::IMPORT_UUID_COLLISION_REMOVE_EXISTING,
23+
'replace' => ImportUUIDBehaviorInterface::IMPORT_UUID_COLLISION_REPLACE_EXISTING,
24+
'throw' => ImportUUIDBehaviorInterface::IMPORT_UUID_COLLISION_THROW,
25+
];
26+
2027
/**
2128
* {@inheritdoc}
2229
*/
@@ -28,6 +35,7 @@ protected function configure()
2835
->setName('phpcr:workspace:import')
2936
->addArgument('filename', null, 'The xml file to import')
3037
->addOption('parentpath', 'p', InputOption::VALUE_OPTIONAL, 'Repository path to the parent where to import the file contents', '/')
38+
->addOption('uuid-behavior', null, InputOption::VALUE_REQUIRED, 'How to handle UUID collisions during the import', 'new')
3139
->setDescription('Import xml data into the repository, either in JCR system view format or arbitrary xml')
3240
->setHelp(<<<'EOF'
3341
The <info>import</info> command uses the PHPCR SessionInterface::importXml method
@@ -38,6 +46,17 @@ protected function configure()
3846
3947
If the <info>parentpath</info> option is set, the document is imported to that
4048
path. Otherwise the document is imported at the repository root.
49+
50+
The optional <info>uuid-behavior</info> option describes how UUIDs should be
51+
handled. The following options are available:
52+
53+
* <info>new</info> recreate a new uuid for each imported node;
54+
* <info>remove</info> on collision, remove the old node from the repository and
55+
put the imported data in the tree;
56+
* <info>replace</info> on collision, replace the existing node with the one being
57+
imported. All children of the imported node also go to the new path;
58+
* <info>throw</info> throw an exception on uuid collision.
59+
4160
EOF
4261
);
4362
}
@@ -58,11 +77,15 @@ protected function execute(InputInterface $input, OutputInterface $output)
5877
return 1;
5978
}
6079

61-
$session->importXML(
62-
$parentPath,
63-
$filename,
64-
ImportUUIDBehaviorInterface::IMPORT_UUID_CREATE_NEW
65-
);
80+
$uuidBehavior = $input->getOption('uuid-behavior');
81+
if (!array_key_exists($uuidBehavior, self::UUID_BEHAVIOR)) {
82+
$output->writeln(sprintf('<error>UUID-Behavior "%s" is not supported</error>', $uuidBehavior));
83+
$output->writeln(sprintf('Supported behaviors are %s', implode(', ', array_keys(self::UUID_BEHAVIOR))));
84+
85+
return 1;
86+
}
87+
88+
$session->importXML($parentPath, $filename, self::UUID_BEHAVIOR[$uuidBehavior]);
6689
$session->save();
6790

6891
$output->writeln(sprintf(

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

+27-2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace PHPCR\Tests\Util\Console\Command;
44

5+
use PHPCR\ImportUUIDBehaviorInterface;
56
use PHPCR\RepositoryInterface;
67
use PHPCR\Util\Console\Command\WorkspaceImportCommand;
78

@@ -14,7 +15,7 @@ public function setUp()
1415
$this->application->add(new WorkspaceImportCommand());
1516
}
1617

17-
public function testNodeTypeList()
18+
public function testImport()
1819
{
1920
$this->session->expects($this->once())
2021
->method('getRepository')
@@ -26,12 +27,36 @@ public function testNodeTypeList()
2627
->will($this->returnValue(true));
2728

2829
$this->session->expects($this->once())
29-
->method('importXml');
30+
->method('importXml')
31+
->with('/', 'test_import.xml', ImportUUIDBehaviorInterface::IMPORT_UUID_CREATE_NEW);
3032

3133
$ct = $this->executeCommand('phpcr:workspace:import', [
3234
'filename' => 'test_import.xml',
3335
]);
3436

3537
$this->assertContains('Successfully imported', $ct->getDisplay());
3638
}
39+
40+
public function testImportUuidBehaviorThrow()
41+
{
42+
$this->session->expects($this->once())
43+
->method('getRepository')
44+
->will($this->returnValue($this->repository));
45+
46+
$this->repository->expects($this->once())
47+
->method('getDescriptor')
48+
->with(RepositoryInterface::OPTION_XML_IMPORT_SUPPORTED)
49+
->will($this->returnValue(true));
50+
51+
$this->session->expects($this->once())
52+
->method('importXml')
53+
->with('/', 'test_import.xml', ImportUUIDBehaviorInterface::IMPORT_UUID_COLLISION_THROW);
54+
55+
$ct = $this->executeCommand('phpcr:workspace:import', [
56+
'filename' => 'test_import.xml',
57+
'--uuid-behavior' => 'throw',
58+
]);
59+
60+
$this->assertContains('Successfully imported', $ct->getDisplay());
61+
}
3762
}

0 commit comments

Comments
 (0)