1717 */
1818class 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'
3341The <info>import</info> command uses the PHPCR SessionInterface::importXml method
@@ -38,6 +46,17 @@ protected function configure()
3846
3947If the <info>parentpath</info> option is set, the document is imported to that
4048path. 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+
4160EOF
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 (
0 commit comments