Skip to content

Commit c4041d0

Browse files
committed
Merge pull request #67 from dantleech/workspace-node-update
[WIP] Workspace Node Update Command and fixes
2 parents 9360d81 + dabbb3a commit c4041d0

18 files changed

+584
-95
lines changed
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
<?php
2+
3+
namespace PHPCR\Util\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 PHPCR\Util\Console\Helper\PhpcrCliHelper;
9+
use Symfony\Component\Console\Input\InputOption;
10+
11+
abstract class BaseCommand extends Command
12+
{
13+
/**
14+
* @return PHPCR\SessionInterface
15+
*/
16+
protected function getPhpcrSession()
17+
{
18+
return $this->getHelper('phpcr')->getSession();
19+
}
20+
21+
/**
22+
* @return PHPCR\Util\Console\Helper\PhpcrCliHelper
23+
*/
24+
protected function getPhpcrCliHelper()
25+
{
26+
$phpcrCliHelper = new PhpcrCliHelper($this->getPhpcrSession());
27+
return $phpcrCliHelper;
28+
}
29+
30+
public function configureNodeManipulationInput()
31+
{
32+
$this->addOption('set-prop', 'p',
33+
InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY,
34+
'Set node property on nodes use foo=bar'
35+
);
36+
$this->addOption('remove-prop', 'r',
37+
InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY,
38+
'Remove property from nodes'
39+
);
40+
$this->addOption('add-mixin', null,
41+
InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY,
42+
'Add a mixin to the nodes'
43+
);
44+
$this->addOption('remove-mixin', null,
45+
InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY,
46+
'Remove mixin from the nodes'
47+
);
48+
}
49+
}

src/PHPCR/Util/Console/Command/NodeDumpCommand.php

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
namespace PHPCR\Util\Console\Command;
2323

2424
use PHPCR\Util\UUIDHelper;
25-
use Symfony\Component\Console\Command\Command;
2625
use PHPCR\ItemNotFoundException;
2726
use PHPCR\RepositoryException;
2827
use PHPCR\PathNotFoundException;
@@ -42,7 +41,7 @@
4241
* @author Daniel Barsotti <daniel.barsotti@liip.ch>
4342
* @author Daniel Leech <daniel@dantleech.com>
4443
*/
45-
class NodeDumpCommand extends Command
44+
class NodeDumpCommand extends BaseCommand
4645
{
4746
/**
4847
* Limit after which to cut lines when dumping properties
@@ -65,7 +64,7 @@ protected function configure()
6564
->addOption('ref-format', 'uuid', InputOption::VALUE_REQUIRED, 'Set the way references should be displayed when dumping reference properties - either "uuid" (default) or "path"')
6665
->addArgument('identifier', InputArgument::OPTIONAL, 'Root path to dump', '/')
6766
->setDescription('Dump subtrees of the content repository')
68-
->setHelp(<<<EOF
67+
->setHelp(<<<HERE
6968
The <info>dump</info> command recursively outputs the name of the node specified
7069
by the <info>identifier</info> argument and its subnodes in a yaml-like style.
7170
@@ -75,7 +74,7 @@ protected function configure()
7574
By default the command filters out system nodes and properties (i.e. nodes and
7675
properties with names starting with 'jcr:'), the <info>sys_nodes</info> option
7776
allows to turn this filter off.
78-
EOF
77+
HERE
7978
)
8079
;
8180
}
@@ -95,7 +94,7 @@ public function setDumpMaxLineLength($length)
9594
*/
9695
protected function execute(InputInterface $input, OutputInterface $output)
9796
{
98-
$session = $this->getHelper('phpcr')->getSession();
97+
$session = $this->getPhpcrSession();
9998
$dumperHelper = $this->getHelper('phpcr_console_dumper');
10099

101100
// node to dump

src/PHPCR/Util/Console/Command/NodeTouchCommand.php

Lines changed: 14 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636
*
3737
* @author Daniel Leech <daniel@dantleech.com>
3838
*/
39-
class NodeTouchCommand extends Command
39+
class NodeTouchCommand extends BaseCommand
4040
{
4141
/**
4242
* {@inheritDoc}
@@ -45,6 +45,8 @@ protected function configure()
4545
{
4646
parent::configure();
4747

48+
$this->configureNodeManipulationInput();
49+
4850
$this->setName('phpcr:node:touch')
4951
->addArgument(
5052
'path',
@@ -57,26 +59,10 @@ protected function configure()
5759
'Node type, default nt:unstructured',
5860
'nt:unstructured'
5961
)
60-
->addOption('set-prop', 'p',
61-
InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY,
62-
'Set node property, use foo=bar'
63-
)
64-
->addOption('remove-prop', 'r',
65-
InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY,
66-
'Remove node property'
67-
)
6862
->addOption('dump', 'd',
6963
InputOption::VALUE_NONE,
7064
'Dump a string reperesentation of the created / modified node.'
7165
)
72-
->addOption('add-mixin', null,
73-
InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY,
74-
'Add a mixin to the node'
75-
)
76-
->addOption('remove-mixin', null,
77-
InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY,
78-
'Add a mixin to the node'
79-
)
8066
->setDescription('Create or modify a node')
8167
->setHelp(<<<HERE
8268
This command allows you to create or modify a node at the specified path.
@@ -102,14 +88,15 @@ protected function configure()
10288
*/
10389
protected function execute(InputInterface $input, OutputInterface $output)
10490
{
105-
/** @var $session SessionInterface */
106-
$session = $this->getHelper('phpcr')->getSession();
91+
$helper = $this->getPhpcrCliHelper();
92+
$session = $this->getPhpcrSession();
10793

10894
$path = $input->getArgument('path');
10995
$type = $input->getOption('type');
96+
$dump = $input->getOption('dump');
97+
11098
$setProp = $input->getOption('set-prop');
11199
$removeProp = $input->getOption('remove-prop');
112-
$dump = $input->getOption('dump');
113100
$addMixins = $input->getOption('add-mixin');
114101
$removeMixins = $input->getOption('remove-mixin');
115102

@@ -155,45 +142,13 @@ protected function execute(InputInterface $input, OutputInterface $output)
155142
$node = $parentNode->addNode($nodeName, $type);
156143
}
157144

158-
foreach ($setProp as $set) {
159-
$parts = explode('=', $set);
160-
$output->writeln(sprintf(
161-
'<comment> > Setting property </comment>%s<comment> to </comment>%s',
162-
$parts[0], $parts[1]
163-
));
164-
$node->setProperty($parts[0], $parts[1]);
165-
}
166-
167-
foreach ($removeProp as $unset) {
168-
$output->writeln(sprintf(
169-
'<comment> > Unsetting property </comment>%s',
170-
$unset
171-
));
172-
$node->setProperty($unset, null);
173-
}
174-
175-
foreach ($addMixins as $addMixin) {
176-
$node->addMixin($addMixin);
177-
}
178-
179-
foreach ($removeMixins as $removeMixin) {
180-
$node->removeMixin($removeMixin);
181-
}
182-
183-
if ($dump) {
184-
$output->writeln('<info>Node dump: </info>');
185-
/** @var $property PropertyInterface */
186-
foreach ($node->getProperties() as $property) {
187-
$value = $property->getValue();
188-
if (!is_string($value)) {
189-
$value = print_r($value, true);
190-
}
191-
$output->writeln(sprintf('<comment> - %s = </comment>%s',
192-
$property->getName(),
193-
$value
194-
));
195-
}
196-
}
145+
$helper->processNode($output, $node, array(
146+
'setProps' => $setProp,
147+
'removeProps' => $removeProp,
148+
'addMixins' => $addMixins,
149+
'removeMixins' => $removeMixins,
150+
'dump' => $dump,
151+
));
197152

198153
$session->save();
199154
}

src/PHPCR/Util/Console/Command/NodeTypeListCommand.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
*
3131
* @author Daniel Leech <daniel@dantleech.com>
3232
*/
33-
class NodeTypeListCommand extends Command
33+
class NodeTypeListCommand extends BaseCommand
3434
{
3535
/**
3636
* {@inheritDoc}
@@ -53,7 +53,7 @@ protected function configure()
5353
*/
5454
protected function execute(InputInterface $input, OutputInterface $output)
5555
{
56-
$session = $this->getHelper('phpcr')->getSession();
56+
$session = $this->getPhpcrSession();
5757
$ntm = $session->getWorkspace()->getNodeTypeManager();
5858

5959
$nodeTypes = $ntm->getAllNodeTypes();

src/PHPCR/Util/Console/Command/NodeTypeRegisterCommand.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@
3939
*
4040
* @author Uwe Jäger <uwej711@googlemail.com>
4141
*/
42-
class NodeTypeRegisterCommand extends Command
42+
class NodeTypeRegisterCommand extends BaseCommand
4343
{
4444
/**
4545
* {@inheritDoc}
@@ -91,7 +91,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
9191

9292
$cnd = file_get_contents($cnd_file);
9393
$allowUpdate = $input->getOption('allow-update');
94-
$session = $this->getHelper('phpcr')->getSession();
94+
$session = $this->getPhpcrSession();
9595

9696
try {
9797
$this->updateFromCnd($output, $session, $cnd, $allowUpdate);

0 commit comments

Comments
 (0)