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