|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace PHPCR\Shell\Console\Command; |
| 4 | + |
| 5 | +use Symfony\Component\Console\Command\Command; |
| 6 | +use Symfony\Component\Console\Input\InputInterface; |
| 7 | +use Symfony\Component\Console\Output\OutputInterface; |
| 8 | +use Symfony\Component\Console\Input\InputArgument; |
| 9 | +use PHPCR\Util\CND\Writer\CndWriter; |
| 10 | +use PHPCR\NodeType\NoSuchNodeTypeException; |
| 11 | +use PHPCR\Util\CND\Parser\CndParser; |
| 12 | +use PHPCR\NamespaceException; |
| 13 | +use Symfony\Component\Console\Input\InputOption; |
| 14 | + |
| 15 | +class NodeTypeLoadCommand extends Command |
| 16 | +{ |
| 17 | + protected function configure() |
| 18 | + { |
| 19 | + $this->setName('node-type:load'); |
| 20 | + $this->setDescription('Load or create a node type'); |
| 21 | + $this->addOption('update', null, InputOption::VALUE_NONE, 'Update existing node type'); |
| 22 | + $this->addArgument('cndFile', InputArgument::REQUIRED, 'The name file containing the CND data'); |
| 23 | + $this->setHelp(<<<HERE |
| 24 | +This command allows to register node types in the repository that are defined |
| 25 | +in a CND (Compact Namespace and Node Type Definition) file as used by jackrabbit. |
| 26 | +
|
| 27 | +Custom node types can be used to define the structure of content repository |
| 28 | +nodes, like allowed properties and child nodes together with the namespaces |
| 29 | +and their prefix used for the names of node types and properties. |
| 30 | +
|
| 31 | +If you use <info>--update</info> existing node type definitions will be overwritten |
| 32 | +in the repository. |
| 33 | +HERE |
| 34 | + ); |
| 35 | + } |
| 36 | + |
| 37 | + public function execute(InputInterface $input, OutputInterface $output) |
| 38 | + { |
| 39 | + $session = $this->getHelper('phpcr')->getSession(); |
| 40 | + $cndFile = $input->getArgument('cndFile'); |
| 41 | + $update = $input->getOption('update'); |
| 42 | + $workspace = $session->getWorkspace(); |
| 43 | + $namespaceRegistry = $workspace->getNamespaceRegistry(); |
| 44 | + $nodeTypeManager = $workspace->getNodeTypeManager(); |
| 45 | + |
| 46 | + if (!file_exists($cndFile)) { |
| 47 | + throw new \InvalidArgumentException(sprintf( |
| 48 | + 'The CND file "%s" does not exist.', $cndFile |
| 49 | + )); |
| 50 | + } |
| 51 | + |
| 52 | + $cndData = file_get_contents($cndFile); |
| 53 | + $nodeTypeManager->registerNodeTypesCnd($cndData, $update); |
| 54 | + } |
| 55 | +} |
0 commit comments