22
22
* @license http://opensource.org/licenses/MIT MIT License
23
23
*
24
24
* @author Uwe Jäger <[email protected] >
25
+ * @author Daniel Leech <[email protected] >
25
26
*/
26
27
class NodeTypeRegisterCommand extends BaseCommand
27
28
{
@@ -33,23 +34,27 @@ protected function configure()
33
34
$ this
34
35
->setName ('phpcr:node-type:register ' )
35
36
->setDescription ('Register node types in the PHPCR repository ' )
36
- ->setDefinition (array (
37
- new InputArgument (
38
- 'cnd-file ' , InputArgument::REQUIRED , 'Register namespaces and node types from a "Compact Node Type Definition" .cnd file '
39
- ),
40
- new InputOption ('allow-update ' , '' , InputOption::VALUE_NONE , 'Overwrite existig node type ' ),
41
- ))
37
+ ->addArgument ('cnd-file ' , InputArgument::IS_ARRAY , 'Register namespaces and node types from a "Compact Node Type Definition" .cnd file(s) ' )
38
+ ->addOption ('allow-update ' , null , InputOption::VALUE_NONE , 'Overwrite existig node type ' )
42
39
->setHelp (<<<EOT
43
40
Register node types in the PHPCR repository.
44
41
45
42
This command allows to register node types in the repository that are defined
46
- in a CND (Compact Namespace and Node Type Definition) file as used by jackrabbit.
43
+ in a CND (Compact Namespace and Node Type Definition) file as defined in the JCR-283
44
+ specification.
47
45
48
46
Custom node types can be used to define the structure of content repository
49
47
nodes, like allowed properties and child nodes together with the namespaces
50
48
and their prefix used for the names of node types and properties.
51
49
52
- If you use --allow-update existing node type definitions will be overwritten
50
+ This command allows you to specify multiple files and/or folders:
51
+
52
+ $ php app/console phpcr:node-type:register /path/to/nodetype1.cnd /path/to/a/folder
53
+
54
+ When a folder is specified all files within the folder that have the <comment>.cnd</comment>
55
+ extension will be treated as node definition files.
56
+
57
+ If you use <info>--allow-update</info> existing node type definitions will be overwritten
53
58
in the repository.
54
59
EOT
55
60
)
@@ -61,25 +66,28 @@ protected function configure()
61
66
*/
62
67
protected function execute (InputInterface $ input , OutputInterface $ output )
63
68
{
64
- $ cnd_file = realpath ( $ input ->getArgument ('cnd-file ' ) );
69
+ $ definitions = $ input ->getArgument ('cnd-file ' );
65
70
66
- if (!file_exists ($ cnd_file )) {
67
- throw new \InvalidArgumentException (
68
- sprintf ("Node type definition file '<info>%s</info>' does not exist. " , $ cnd_file )
69
- );
70
- } elseif (!is_readable ($ cnd_file )) {
71
+ if (count ($ definitions ) == 0 ) {
71
72
throw new \InvalidArgumentException (
72
- sprintf ( " Node type definition file '<info>%s</info>' does not have read permissions. " , $ cnd_file )
73
+ ' At least one definition (i.e. file or folder) must be specified '
73
74
);
74
75
}
75
76
76
- $ cnd = file_get_contents ($ cnd_file );
77
77
$ allowUpdate = $ input ->getOption ('allow-update ' );
78
78
$ session = $ this ->getPhpcrSession ();
79
79
80
- $ this ->updateFromCnd ($ output , $ session , $ cnd , $ allowUpdate );
80
+ $ filePaths = $ this ->getFilePaths ($ definitions );
81
+
82
+ $ count = 0 ;
83
+ foreach ($ filePaths as $ filePath ) {
84
+ $ cnd = file_get_contents ($ filePath );
85
+ $ this ->updateFromCnd ($ output , $ session , $ cnd , $ allowUpdate );
86
+ $ output ->writeln (sprintf ('Registered: <info>%s</info> ' , $ filePath ));
87
+ $ count ++;
88
+ }
81
89
82
- $ output ->write ( PHP_EOL . sprintf ('Successfully registered node types from "<info>%s</info>" ' , $ cnd_file ) . PHP_EOL );
90
+ $ output ->writeln ( sprintf ('%d node definition file(s) ' , $ count ) );
83
91
84
92
return 0 ;
85
93
}
@@ -109,4 +117,60 @@ protected function updateFromCnd(OutputInterface $output, SessionInterface $sess
109
117
throw $ e ;
110
118
}
111
119
}
120
+
121
+ /**
122
+ * Return a list of node type definition file paths from
123
+ * the given definition files or folders.
124
+ *
125
+ * @param array $definitions List of files of folders
126
+ *
127
+ * @return array Array of full paths to all the type node definition files.
128
+ */
129
+ protected function getFilePaths ($ definitions )
130
+ {
131
+ $ filePaths = array ();
132
+
133
+ foreach ($ definitions as $ definition ) {
134
+ $ definition = realpath ($ definition );
135
+
136
+ if (is_dir ($ definition )) {
137
+ $ dirHandle = opendir ($ definition );
138
+
139
+ while ($ file = readdir ($ dirHandle )) {
140
+ if (false === $ this ->fileIsNodeType ($ file )) {
141
+ continue ;
142
+ }
143
+
144
+ $ filePath = sprintf ('%s/%s ' , $ definition , $ file );
145
+
146
+ if (!is_readable ($ filePath )) {
147
+ throw new \InvalidArgumentException (
148
+ sprintf ("Node type definition file '<info>%s</info>' does not have read permissions. " , $ file )
149
+ );
150
+ }
151
+
152
+ $ filePaths [] = $ filePath ;
153
+ }
154
+ } else {
155
+ if (!file_exists ($ definition )) {
156
+ throw new \InvalidArgumentException (
157
+ sprintf ("Node type definition file '<info>%s</info>' does not exist. " , $ definition )
158
+ );
159
+ }
160
+
161
+ $ filePaths [] = $ definition ;
162
+ }
163
+ }
164
+
165
+ return $ filePaths ;
166
+ }
167
+
168
+ protected function fileIsNodeType ($ filename )
169
+ {
170
+ if (substr ($ filename , -4 ) == '.cnd ' ) {
171
+ return true ;
172
+ }
173
+
174
+ return false ;
175
+ }
112
176
}
0 commit comments