Skip to content

Commit 5531c7e

Browse files
committed
Added "touch" command
Enables: - Creation of nodes - Modification and removal of nodes
1 parent f402bf2 commit 5531c7e

File tree

2 files changed

+178
-0
lines changed

2 files changed

+178
-0
lines changed

bin/phpcr

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ $cli->addCommands(array(
4848
new \PHPCR\Util\Console\Command\ListNodeTypesCommand(),
4949
new \PHPCR\Util\Console\Command\QueryCommand(),
5050
new \PHPCR\Util\Console\Command\MoveCommand(),
51+
new \PHPCR\Util\Console\Command\TouchCommand(),
5152
));
5253
$cli->run();
5354

Lines changed: 177 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,177 @@
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+
->setDescription('Create or modify a node')
71+
->setHelp(<<<HERE
72+
This command allows you to create or modify a node at the specified path.
73+
74+
For example::
75+
76+
$ ./bin/phpcr phpcr:touch /foobar --type=my:nodetype --set=foo=bar
77+
78+
Will create the node "/foobar" and set (or create) the "foo" property
79+
with a value of "bar".
80+
81+
You can execute the command again to further modify the node. Here we add
82+
the property "bar" and remove the property "foo". We also add the dump option
83+
to output a string reperesentation of the node.
84+
85+
$ ./bin/phpcr phpcr:touch /foobar --type=my:nodetype --set=bar=myvalue --unset=foo --dump
86+
HERE
87+
);
88+
}
89+
90+
/**
91+
* {@inheritDoc}
92+
*/
93+
protected function execute(InputInterface $input, OutputInterface $output)
94+
{
95+
$session = $this->getHelper('phpcr')->getSession();
96+
$path = $input->getArgument('path');
97+
$type = $input->getOption('type');
98+
$sets = $input->getOption('set');
99+
$unsets = $input->getOption('unset');
100+
$dump = $input->getOption('dump');
101+
102+
try {
103+
$node = $session->getNode($path);
104+
} catch (PathNotFoundException $e) {
105+
$node = null;
106+
}
107+
108+
if ($node) {
109+
$nodeType = $node->getPrimaryNodeType()->getName();
110+
$output->writeln(sprintf(
111+
'<info>Node at path </info>%s <info>already exists and has primary type</info> %s.',
112+
$path,
113+
$nodeType
114+
));
115+
116+
if ($nodeType != $type) {
117+
throw new \Exception(sprintf(
118+
'You have specified node type "%s" but the existing node is of type "%s"',
119+
$type, $nodeType
120+
));
121+
}
122+
} else {
123+
124+
$nodeName = basename($path);
125+
$parentPath = dirname($path);
126+
127+
try {
128+
$parentNode = $session->getNode($parentPath);
129+
} catch (PathNotFoundException $e) {
130+
$output->writeln(sprintf(
131+
'<error>Parent path "%s" does not exist</error>',
132+
$parentPath
133+
));
134+
return;
135+
}
136+
137+
$output->writeln(sprintf(
138+
'<info>Creating node: </info> %s [%s]', $path, $type
139+
));
140+
141+
$node = $parentNode->addNode($nodeName, $type);
142+
}
143+
144+
foreach ($sets as $set) {
145+
$parts = explode('=', $set);
146+
$output->writeln(sprintf(
147+
'<comment> > Setting property </comment>%s<comment> to </comment>%s',
148+
$parts[0], $parts[1]
149+
));
150+
$node->setProperty($parts[0], $parts[1]);
151+
}
152+
153+
foreach ($unsets as $unset) {
154+
$output->writeln(sprintf(
155+
'<comment> > Unsetting property </comment>%s',
156+
$unset
157+
));
158+
$node->setProperty($unset, null);
159+
}
160+
161+
if ($dump) {
162+
$output->writeln('<info>Node dump: </info>');
163+
foreach ($node->getProperties() as $property) {
164+
$value = $property->getValue();
165+
if (!is_string($value)) {
166+
$value = print_r($value, true);
167+
}
168+
$output->writeln(sprintf('<comment> - %s = </comment>%s',
169+
$property->getName(),
170+
$value
171+
));
172+
}
173+
}
174+
175+
$session->save();
176+
}
177+
}

0 commit comments

Comments
 (0)