99use PHPCR \Util \CND \Writer \CndWriter ;
1010use PHPCR \NodeType \NoSuchNodeTypeException ;
1111use PHPCR \Util \CND \Parser \CndParser ;
12+ use PHPCR \NamespaceException ;
1213
1314class NodeTypeEditCommand extends Command
1415{
1516 protected function configure ()
1617 {
1718 $ this ->setName ('node-type:edit ' );
18- $ this ->setDescription ('Edit the current node ' );
19- $ this ->addArgument ('nodeTypeName ' , null , InputArgument::REQUIRED , 'The name of the node type to edit ' );
19+ $ this ->setDescription ('Edit or create a node type ' );
20+ $ this ->addArgument ('nodeTypeName ' , null , InputArgument::REQUIRED , 'The name of the node type to edit or create ' );
2021 $ this ->setHelp (<<<HERE
2122Edit the given node type name with the editor defined in the EDITOR environment variable.
23+
24+ If the node type does not exist, it will be created. All node types must be prefixed with
25+ a namespace prefix as shown in the <info>session:namespace:list</info> command
26+
27+ $ node-type:edit nt:examplenode
28+
29+ Will open an editor with a new node type.
2230HERE
2331 );
2432 }
@@ -35,22 +43,49 @@ public function execute(InputInterface $input, OutputInterface $output)
3543
3644 try {
3745 $ nodeType = $ nodeTypeManager ->getNodeType ($ nodeTypeName );
46+ $ cndWriter = new CndWriter ($ namespaceRegistry );
47+ $ out = $ cndWriter ->writeString (array ($ nodeType ));
48+ $ message = null ;
3849 } catch (NoSuchNodeTypeException $ e ) {
39- throw new \Exception (sprintf (
40- 'The node type "%s" does not exist '
41- , $ nodeTypeName ));
50+ $ parts = explode (': ' , $ nodeTypeName );
51+
52+ if (count ($ parts ) != 2 ) {
53+ throw new \InvalidArgumentException (
54+ 'Node type names must be prefixed with a namespace, e.g. ns:foobar '
55+ );
56+ }
57+ list ($ namespace , $ name ) = $ parts ;
58+ $ uri = $ session ->getNamespaceURI ($ namespace );
59+
60+ // so we will create one ..
61+ $ out = <<<EOT
62+ < $ namespace =' $ uri'>
63+ [ $ namespace: $ name] > nt:unstructured
64+
65+ EOT
66+ ;
67+ $ message = <<<EOT
68+ Creating a new node type: $ nodeTypeName
69+ EOT
70+ ;
4271 }
43- $ cndWriter = new CndWriter ($ namespaceRegistry );
44- $ out = $ cndWriter ->writeString (array ($ nodeType ));
45- $ res = $ editor ->fromString ($ out );
72+
4673
4774 $ valid = false ;
48- while (false === $ valid ) {
75+ $ prefix = '# ' ;
76+ do {
77+ $ res = $ editor ->fromStringWithMessage ($ out , $ message );
78+
79+ if (empty ($ res )) {
80+ $ output ->writeln ('<info>Editor emptied the CND file, doing nothing. Use node-type:delete to remove node types.</info> ' );
81+ return 0 ;
82+ }
83+
4984 try {
5085 $ cndParser = new CndParser ($ nodeTypeManager );
51- $ res = $ cndParser ->parseString ($ res );
86+ $ namespacesAndNodeTypes = $ cndParser ->parseString ($ res );
5287
53- foreach ($ res ['nodeTypes ' ] as $ nodeType ) {
88+ foreach ($ namespacesAndNodeTypes ['nodeTypes ' ] as $ nodeType ) {
5489 $ nodeTypeManager ->registerNodeType ($ nodeType , true );
5590 }
5691 $ valid = true ;
@@ -66,13 +101,12 @@ public function execute(InputInterface $input, OutputInterface $output)
66101 return 1 ;
67102 }
68103
69- $ prefix = '# ' ;
70104 $ message = 'The following errors were encountered (all lines starting with ' . $ prefix . ' will be ignored): ' ;
71105 $ message .= PHP_EOL ;
72106 $ message .= PHP_EOL ;
73107 $ message .= $ e ->getMessage ();
74- $ editor -> fromStringWithMessage ( $ res , $ message , $ prefix ) ;
108+ $ out = $ res ;
75109 }
76- }
110+ } while ( false === $ valid );
77111 }
78112}
0 commit comments