1818 */
1919class WorkspaceImportCommand extends BaseCommand
2020{
21+ const UUID_BEHAVIOR = [
22+ 'new ' => ImportUUIDBehaviorInterface::IMPORT_UUID_CREATE_NEW ,
23+ 'remove ' => ImportUUIDBehaviorInterface::IMPORT_UUID_COLLISION_REMOVE_EXISTING ,
24+ 'replace ' => ImportUUIDBehaviorInterface::IMPORT_UUID_COLLISION_REPLACE_EXISTING ,
25+ 'throw ' => ImportUUIDBehaviorInterface::IMPORT_UUID_COLLISION_THROW ,
26+ ];
27+
2128 /**
2229 * {@inheritDoc}
2330 */
@@ -29,6 +36,7 @@ protected function configure()
2936 ->setName ('phpcr:workspace:import ' )
3037 ->addArgument ('filename ' , null , 'The xml file to import ' )
3138 ->addOption ('parentpath ' , 'p ' , InputOption::VALUE_OPTIONAL , 'Repository path to the parent where to import the file contents ' , '/ ' )
39+ ->addOption ('uuid-behavior ' , null , InputOption::VALUE_REQUIRED , 'How to handle UUID collisions during the import ' , 'new ' )
3240 ->setDescription ('Import xml data into the repository, either in JCR system view format or arbitrary xml ' )
3341 ->setHelp (<<<EOF
3442The <info>import</info> command uses the PHPCR SessionInterface::importXml method
@@ -39,6 +47,17 @@ protected function configure()
3947
4048If the <info>parentpath</info> option is set, the document is imported to that
4149path. Otherwise the document is imported at the repository root.
50+
51+ The optional <info>uuid-behavior</info> option describes how UUIDs should be
52+ handled. The following options are available:
53+
54+ * <info>new</info> recreate a new uuid for each imported node;
55+ * <info>remove</info> on collision, remove the old node from the repository and
56+ put the imported data in the tree;
57+ * <info>replace</info> on collision, replace the existing node with the one being
58+ imported. All children of the imported node also go to the new path;
59+ * <info>throw</info> throw an exception on uuid collision.
60+
4261EOF
4362 )
4463 ;
@@ -60,11 +79,15 @@ protected function execute(InputInterface $input, OutputInterface $output)
6079 return 1 ;
6180 }
6281
63- $ session ->importXML (
64- $ parentPath ,
65- $ filename ,
66- ImportUUIDBehaviorInterface::IMPORT_UUID_CREATE_NEW
67- );
82+ $ uuidBehavior = $ input ->getOption ('uuid-behavior ' );
83+ if (!array_key_exists ($ uuidBehavior , self ::UUID_BEHAVIOR )) {
84+ $ output ->writeln (sprintf ('<error>UUID-Behavior "%s" is not supported</error> ' , $ uuidBehavior ));
85+ $ output ->writeln (sprintf ('Supported behaviors are %s ' , implode (', ' , array_keys (self ::UUID_BEHAVIOR ))));
86+
87+ return 1 ;
88+ }
89+
90+ $ session ->importXML ($ parentPath , $ filename , self ::UUID_BEHAVIOR [$ uuidBehavior ]);
6891 $ session ->save ();
6992
7093 $ output ->writeln (sprintf (
0 commit comments