-
Notifications
You must be signed in to change notification settings - Fork 29
Refactored node type register command to be more flexible re. loading #108
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -22,6 +22,7 @@ | |
| * @license http://opensource.org/licenses/MIT MIT License | ||
| * | ||
| * @author Uwe Jäger <uwej711@googlemail.com> | ||
| * @author Daniel Leech <daniel@dantleech.com> | ||
| */ | ||
| class NodeTypeRegisterCommand extends BaseCommand | ||
| { | ||
|
|
@@ -33,23 +34,27 @@ protected function configure() | |
| $this | ||
| ->setName('phpcr:node-type:register') | ||
| ->setDescription('Register node types in the PHPCR repository') | ||
| ->setDefinition(array( | ||
| new InputArgument( | ||
| 'cnd-file', InputArgument::REQUIRED, 'Register namespaces and node types from a "Compact Node Type Definition" .cnd file' | ||
| ), | ||
| new InputOption('allow-update', '', InputOption::VALUE_NONE, 'Overwrite existig node type'), | ||
| )) | ||
| ->addArgument('cnd-file', InputArgument::IS_ARRAY, 'Register namespaces and node types from a "Compact Node Type Definition" .cnd file(s)') | ||
| ->addOption('allow-update', null, InputOption::VALUE_NONE, 'Overwrite existig node type') | ||
| ->setHelp(<<<EOT | ||
| Register node types in the PHPCR repository. | ||
|
|
||
| This command allows to register node types in the repository that are defined | ||
| in a CND (Compact Namespace and Node Type Definition) file as used by jackrabbit. | ||
| in a CND (Compact Namespace and Node Type Definition) file as defined in the JCR-283 | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. i was going to say this is jackrabbit specific but then i found http://www.day.com/maven/jcr/2.0/25_Appendix.html#25.2%20Compact%20Node%20Type%20Definition%20Notation which indicates its now somehow part of jcr. did not find it in the chapter on node type management... |
||
| specification. | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. i think we should say here that it can also be folders and that you can specify several files / folders. (btw, is it a deep recursion or just the folder itself?) folder itself is fine, but we should say it. |
||
|
|
||
| Custom node types can be used to define the structure of content repository | ||
| nodes, like allowed properties and child nodes together with the namespaces | ||
| and their prefix used for the names of node types and properties. | ||
|
|
||
| If you use --allow-update existing node type definitions will be overwritten | ||
| This command allows you to specify multiple files and/or folders: | ||
|
|
||
| $ php app/console phpcr:node-type:register /path/to/nodetype1.cnd /path/to/a/folder | ||
|
|
||
| When a folder is specified all files within the folder that have the <comment>.cnd</comment> | ||
| extension will be treated as node definition files. | ||
|
|
||
| If you use <info>--allow-update</info> existing node type definitions will be overwritten | ||
| in the repository. | ||
| EOT | ||
| ) | ||
|
|
@@ -61,25 +66,28 @@ protected function configure() | |
| */ | ||
| protected function execute(InputInterface $input, OutputInterface $output) | ||
| { | ||
| $cnd_file = realpath($input->getArgument('cnd-file')); | ||
| $definitions = $input->getArgument('cnd-file'); | ||
|
|
||
| if (!file_exists($cnd_file)) { | ||
| throw new \InvalidArgumentException( | ||
| sprintf("Node type definition file '<info>%s</info>' does not exist.", $cnd_file) | ||
| ); | ||
| } elseif (!is_readable($cnd_file)) { | ||
| if (count($definitions) == 0) { | ||
| throw new \InvalidArgumentException( | ||
| sprintf("Node type definition file '<info>%s</info>' does not have read permissions.", $cnd_file) | ||
| 'At least one definition (i.e. file or folder) must be specified' | ||
| ); | ||
| } | ||
|
|
||
| $cnd = file_get_contents($cnd_file); | ||
| $allowUpdate = $input->getOption('allow-update'); | ||
| $session = $this->getPhpcrSession(); | ||
|
|
||
| $this->updateFromCnd($output, $session, $cnd, $allowUpdate); | ||
| $filePaths = $this->getFilePaths($definitions); | ||
|
|
||
| $count = 0; | ||
| foreach ($filePaths as $filePath) { | ||
| $cnd = file_get_contents($filePath); | ||
| $this->updateFromCnd($output, $session, $cnd, $allowUpdate); | ||
| $output->writeln(sprintf('Registered: <info>%s</info>', $filePath)); | ||
| $count++; | ||
| } | ||
|
|
||
| $output->write(PHP_EOL.sprintf('Successfully registered node types from "<info>%s</info>"', $cnd_file) . PHP_EOL); | ||
| $output->writeln(sprintf('%d node definition file(s)', $count)); | ||
|
|
||
| return 0; | ||
| } | ||
|
|
@@ -109,4 +117,60 @@ protected function updateFromCnd(OutputInterface $output, SessionInterface $sess | |
| throw $e; | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Return a list of node type definition file paths from | ||
| * the given definition files or folders. | ||
| * | ||
| * @param array $definitions List of files of folders | ||
| * | ||
| * @return array Array of full paths to all the type node definition files. | ||
| */ | ||
| protected function getFilePaths($definitions) | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. we could depend on the symfony file component, would be much nicer. but i would say its not worth the additional dependency.
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. yeah |
||
| { | ||
| $filePaths = array(); | ||
|
|
||
| foreach ($definitions as $definition) { | ||
| $definition = realpath($definition); | ||
|
|
||
| if (is_dir($definition)) { | ||
| $dirHandle = opendir($definition); | ||
|
|
||
| while ($file = readdir($dirHandle)) { | ||
| if (false === $this->fileIsNodeType($file)) { | ||
| continue; | ||
| } | ||
|
|
||
| $filePath = sprintf('%s/%s', $definition, $file); | ||
|
|
||
| if (!is_readable($filePath)) { | ||
| throw new \InvalidArgumentException( | ||
| sprintf("Node type definition file '<info>%s</info>' does not have read permissions.", $file) | ||
| ); | ||
| } | ||
|
|
||
| $filePaths[] = $filePath; | ||
| } | ||
| } else { | ||
| if (!file_exists($definition)) { | ||
| throw new \InvalidArgumentException( | ||
| sprintf("Node type definition file '<info>%s</info>' does not exist.", $definition) | ||
| ); | ||
| } | ||
|
|
||
| $filePaths[] = $definition; | ||
| } | ||
| } | ||
|
|
||
| return $filePaths; | ||
| } | ||
|
|
||
| protected function fileIsNodeType($filename) | ||
| { | ||
| if (substr($filename, -4) == '.cnd') { | ||
| return true; | ||
| } | ||
|
|
||
| return false; | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
imo its not an option to BC break this command, its used in too many places. i think we still should alternatively accept a filename as argument. i guess nothing can then be required anymore but the check if we have something would be in the command itself.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ok, do you think it better to deprecate the first argument in favor of multiple arguments? And eventually do a BC break on the next major version?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
we could also keep using the argument instead of an option, then you could just specify one or several things after the command name that are file or folder paths. then only the naming of the argument for use inside the command is a bit off.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
hmm, but how can you specify multiple paths with an argument?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can't you simply repeat the argument when its an array argument?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh yeah, didn't know you could do that. Maybe thats the way to do it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
+1