|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace PHPCR\Util\Console\Command; |
| 4 | + |
| 5 | +use Symfony\Component\Console\Input\InputArgument; |
| 6 | +use Symfony\Component\Console\Input\InputOption; |
| 7 | +use Symfony\Component\Console\Command\Command; |
| 8 | +use Symfony\Component\Console\Input\InputInterface; |
| 9 | +use Symfony\Component\Console\Output\OutputInterface; |
| 10 | + |
| 11 | +/** |
| 12 | + * Command to load and register a node type defined in a CND file. |
| 13 | + * |
| 14 | + * See the link below for the cnd definition. |
| 15 | + * @link http://jackrabbit.apache.org/node-type-notation.html |
| 16 | + * |
| 17 | + * @author Uwe Jäger <uwej711@googlemail.com> |
| 18 | + */ |
| 19 | +class RegisterNodeTypesCommand extends Command |
| 20 | +{ |
| 21 | + /** |
| 22 | + * @see Command |
| 23 | + */ |
| 24 | + protected function configure() |
| 25 | + { |
| 26 | + $this |
| 27 | + ->setName('phpcr:register-node-types') |
| 28 | + ->setDescription('Register node types in the PHPCR repository') |
| 29 | + ->setDefinition(array( |
| 30 | + new InputArgument( |
| 31 | + 'cnd-file', InputArgument::REQUIRED, 'Register namespaces and node types from a "Compact Node Type Definition" .cnd file' |
| 32 | + ), |
| 33 | + new InputOption('allow-update', '', InputOption::VALUE_NONE, 'Overwrite existig node type'), |
| 34 | + )) |
| 35 | + ->setHelp(<<<EOT |
| 36 | +Register node types in the PHPCR repository. |
| 37 | +
|
| 38 | +This command allows to register node types in the repository that are defined |
| 39 | +in a CND (Compact Namespace and Node Type Definition) file as used by jackrabbit. |
| 40 | +
|
| 41 | +Custom node types can be used to define the structure of content repository |
| 42 | +nodes, like allowed properties and child nodes together with the namespaces |
| 43 | +and their prefix used for the names of node types and properties. |
| 44 | +
|
| 45 | +If you use --allow-update existing node type definitions will be overwritten |
| 46 | +in the repository. |
| 47 | +EOT |
| 48 | + ); |
| 49 | + } |
| 50 | + |
| 51 | + /** |
| 52 | + * @see Command |
| 53 | + */ |
| 54 | + protected function execute(InputInterface $input, OutputInterface $output) |
| 55 | + { |
| 56 | + $cnd_file = realpath($input->getArgument('cnd-file')); |
| 57 | + |
| 58 | + if (!file_exists($cnd_file)) { |
| 59 | + throw new \InvalidArgumentException( |
| 60 | + sprintf("Node type definition file '<info>%s</info>' does not exist.", $cnd_file) |
| 61 | + ); |
| 62 | + } elseif (!is_readable($cnd_file)) { |
| 63 | + throw new \InvalidArgumentException( |
| 64 | + sprintf("Node type definition file '<info>%s</info>' does not have read permissions.", $cnd_file) |
| 65 | + ); |
| 66 | + } |
| 67 | + |
| 68 | + $cnd = file_get_contents($cnd_file); |
| 69 | + $allowUpdate = $input->getOption('allow-update'); |
| 70 | + |
| 71 | + $session = $this->getHelper('phpcr')->getSession(); |
| 72 | + if (! $session instanceof \Jackalope\Session) { |
| 73 | + throw new \Exception('PHPCR only provides an API to register node types. Your implementation is not Jackalope (which provides a method for .cnd). TODO: parse the file and do the necessary API calls'); |
| 74 | + } |
| 75 | + $ntm = $session->getWorkspace()->getNodeTypeManager(); |
| 76 | + |
| 77 | + try { |
| 78 | + $ntm->registerNodeTypesCnd($cnd, $allowUpdate); |
| 79 | + } catch (\PHPCR\NodeType\NodeTypeExistsException $e) { |
| 80 | + if (!$allowUpdate) { |
| 81 | + $output->write(PHP_EOL.'<error>The node type(s) you tried to register already exist.</error>'.PHP_EOL); |
| 82 | + $output->write(PHP_EOL.'If you want to override the existing definition call this command with the '); |
| 83 | + $output->write('<info>--allow-update</info> option.'.PHP_EOL); |
| 84 | + } |
| 85 | + throw $e; |
| 86 | + } |
| 87 | + $output->write(PHP_EOL.sprintf('Successfully registered node types from "<info>%s</info>"', $cnd_file) . PHP_EOL); |
| 88 | + } |
| 89 | +} |
0 commit comments