|
| 1 | +<?php |
| 2 | + |
| 3 | +/** |
| 4 | + * This file is part of the PHPCR Utils |
| 5 | + * |
| 6 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | + * you may not use this file except in compliance with the License. |
| 8 | + * You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, software |
| 13 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | + * See the License for the specific language governing permissions and |
| 16 | + * limitations under the License. |
| 17 | + * |
| 18 | + * @license http://www.apache.org/licenses/LICENSE-2.0 Apache Software License 2.0 |
| 19 | + * @link http://phpcr.github.com/ |
| 20 | + */ |
| 21 | + |
| 22 | +namespace PHPCR\Util\Console\Command; |
| 23 | + |
| 24 | +use Symfony\Component\Console\Command\Command; |
| 25 | +use Symfony\Component\Console\Input\InputOption; |
| 26 | +use Symfony\Component\Console\Input\InputArgument; |
| 27 | +use Symfony\Component\Console\Input\InputInterface; |
| 28 | +use Symfony\Component\Console\Output\OutputInterface; |
| 29 | +use PHPCR\PathNotFoundException; |
| 30 | + |
| 31 | +/** |
| 32 | + * Command to create a PHPCR node of a specified type from |
| 33 | + * the command line.. |
| 34 | + * |
| 35 | + * @author Daniel Leech <daniel@dantleech.com> |
| 36 | + */ |
| 37 | +class TouchCommand extends Command |
| 38 | +{ |
| 39 | + /** |
| 40 | + * {@inheritDoc} |
| 41 | + */ |
| 42 | + protected function configure() |
| 43 | + { |
| 44 | + parent::configure(); |
| 45 | + |
| 46 | + $this->setName('phpcr:touch') |
| 47 | + ->addArgument( |
| 48 | + 'path', |
| 49 | + InputArgument::REQUIRED, |
| 50 | + 'Path at which to create the new node' |
| 51 | + ) |
| 52 | + ->addOption( |
| 53 | + 'type', 't', |
| 54 | + InputOption::VALUE_OPTIONAL, |
| 55 | + 'Node type, default nt:unstructured', |
| 56 | + 'nt:unstructured' |
| 57 | + ) |
| 58 | + ->addOption('set', 's', |
| 59 | + InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY, |
| 60 | + 'Set node property, use foo=bar' |
| 61 | + ) |
| 62 | + ->addOption('unset', 'r', |
| 63 | + InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY, |
| 64 | + 'Remove node property' |
| 65 | + ) |
| 66 | + ->addOption('dump', 'd', |
| 67 | + InputOption::VALUE_NONE, |
| 68 | + 'Dump a string reperesentation of the created / modified node.' |
| 69 | + ) |
| 70 | + ->addOption('add-mixin', null, |
| 71 | + InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY, |
| 72 | + 'Add a mixin to the node' |
| 73 | + ) |
| 74 | + ->addOption('remove-mixin', null, |
| 75 | + InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY, |
| 76 | + 'Add a mixin to the node' |
| 77 | + ) |
| 78 | + ->setDescription('Create or modify a node') |
| 79 | + ->setHelp(<<<HERE |
| 80 | +This command allows you to create or modify a node at the specified path. |
| 81 | +
|
| 82 | +For example:: |
| 83 | +
|
| 84 | + $ ./bin/phpcr phpcr:touch /foobar --type=my:nodetype --set=foo=bar |
| 85 | +
|
| 86 | +Will create the node "/foobar" and set (or create) the "foo" property |
| 87 | +with a value of "bar". |
| 88 | +
|
| 89 | +You can execute the command again to further modify the node. Here we add |
| 90 | +the property "bar" and remove the property "foo". We also add the dump option |
| 91 | +to output a string reperesentation of the node. |
| 92 | +
|
| 93 | + $ ./bin/phpcr phpcr:touch /foobar --type=my:nodetype --set=bar=myvalue --unset=foo --dump |
| 94 | +HERE |
| 95 | +); |
| 96 | + } |
| 97 | + |
| 98 | + /** |
| 99 | + * {@inheritDoc} |
| 100 | + */ |
| 101 | + protected function execute(InputInterface $input, OutputInterface $output) |
| 102 | + { |
| 103 | + $session = $this->getHelper('phpcr')->getSession(); |
| 104 | + $path = $input->getArgument('path'); |
| 105 | + $type = $input->getOption('type'); |
| 106 | + $sets = $input->getOption('set'); |
| 107 | + $unsets = $input->getOption('unset'); |
| 108 | + $dump = $input->getOption('dump'); |
| 109 | + $addMixins = $input->getOption('add-mixin'); |
| 110 | + $removeMixins = $input->getOption('remove-mixin'); |
| 111 | + |
| 112 | + try { |
| 113 | + $node = $session->getNode($path); |
| 114 | + } catch (PathNotFoundException $e) { |
| 115 | + $node = null; |
| 116 | + } |
| 117 | + |
| 118 | + if ($node) { |
| 119 | + $nodeType = $node->getPrimaryNodeType()->getName(); |
| 120 | + $output->writeln(sprintf( |
| 121 | + '<info>Node at path </info>%s <info>already exists and has primary type</info> %s.', |
| 122 | + $path, |
| 123 | + $nodeType |
| 124 | + )); |
| 125 | + |
| 126 | + if ($nodeType != $type) { |
| 127 | + throw new \Exception(sprintf( |
| 128 | + 'You have specified node type "%s" but the existing node is of type "%s"', |
| 129 | + $type, $nodeType |
| 130 | + )); |
| 131 | + } |
| 132 | + } else { |
| 133 | + |
| 134 | + $nodeName = basename($path); |
| 135 | + $parentPath = dirname($path); |
| 136 | + |
| 137 | + try { |
| 138 | + $parentNode = $session->getNode($parentPath); |
| 139 | + } catch (PathNotFoundException $e) { |
| 140 | + $output->writeln(sprintf( |
| 141 | + '<error>Parent path "%s" does not exist</error>', |
| 142 | + $parentPath |
| 143 | + )); |
| 144 | + return; |
| 145 | + } |
| 146 | + |
| 147 | + $output->writeln(sprintf( |
| 148 | + '<info>Creating node: </info> %s [%s]', $path, $type |
| 149 | + )); |
| 150 | + |
| 151 | + $node = $parentNode->addNode($nodeName, $type); |
| 152 | + } |
| 153 | + |
| 154 | + foreach ($sets as $set) { |
| 155 | + $parts = explode('=', $set); |
| 156 | + $output->writeln(sprintf( |
| 157 | + '<comment> > Setting property </comment>%s<comment> to </comment>%s', |
| 158 | + $parts[0], $parts[1] |
| 159 | + )); |
| 160 | + $node->setProperty($parts[0], $parts[1]); |
| 161 | + } |
| 162 | + |
| 163 | + foreach ($unsets as $unset) { |
| 164 | + $output->writeln(sprintf( |
| 165 | + '<comment> > Unsetting property </comment>%s', |
| 166 | + $unset |
| 167 | + )); |
| 168 | + $node->setProperty($unset, null); |
| 169 | + } |
| 170 | + |
| 171 | + foreach ($addMixins as $addMixin) { |
| 172 | + $node->addMixin($addMixin); |
| 173 | + } |
| 174 | + |
| 175 | + foreach ($removeMixins as $removeMixin) { |
| 176 | + $node->removeMixin($removeMixin); |
| 177 | + } |
| 178 | + |
| 179 | + if ($dump) { |
| 180 | + $output->writeln('<info>Node dump: </info>'); |
| 181 | + foreach ($node->getProperties() as $property) { |
| 182 | + $value = $property->getValue(); |
| 183 | + if (!is_string($value)) { |
| 184 | + $value = print_r($value, true); |
| 185 | + } |
| 186 | + $output->writeln(sprintf('<comment> - %s = </comment>%s', |
| 187 | + $property->getName(), |
| 188 | + $value |
| 189 | + )); |
| 190 | + } |
| 191 | + } |
| 192 | + |
| 193 | + $session->save(); |
| 194 | + } |
| 195 | +} |
0 commit comments