Skip to content

Commit 843d22f

Browse files
committed
Merge pull request #74 from phpcr/command-helpers
properly use the cli helper
2 parents 998c8c0 + 6d0b695 commit 843d22f

29 files changed

+353
-330
lines changed

cli-config.php.dist

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
<?php
22

3-
// adjust as needed
3+
/**
4+
* This is a sample bootstrap file for the console. All jackalope variants
5+
* provide their own bootstrap file. This file is only relevant for people
6+
* implementing PHPCR and using the phpcr-utils.
7+
*/
8+
49
$path_to_jackalope = __DIR__.'/../jackalope';
510

611
// $autoload was created in the autoload that is included before this.
@@ -12,12 +17,19 @@ $workspace = 'default';
1217
$user = 'admin';
1318
$pass = 'admin';
1419

15-
/* bootstrapping the repository implementation. for jackalope with jackrabbit, do this: */
16-
$factory = new \Jackalope\RepositoryFactoryJackrabbit;
17-
$repository = $factory->getRepository(array("jackalope.jackrabbit_uri" => $jackrabbit_url));
18-
$credentials = new \PHPCR\SimpleCredentials($user, $pass);
19-
$session = $repository->login($credentials, $workspace);
20+
if (isset($argv[1])
21+
&& $argv[1] != 'list'
22+
&& $argv[1] != 'help'
23+
) {
24+
/* bootstrapping the repository implementation. for jackalope with jackrabbit, do this: */
25+
$factory = new \Jackalope\RepositoryFactoryJackrabbit;
26+
$repository = $factory->getRepository(array("jackalope.jackrabbit_uri" => $jackrabbit_url));
27+
$credentials = new \PHPCR\SimpleCredentials($user, $pass);
28+
$session = $repository->login($credentials, $workspace);
2029

21-
$helperSet = new \Symfony\Component\Console\Helper\HelperSet(array(
22-
'session' => new \PHPCR\Util\Console\Helper\PhpcrHelper($session)
23-
));
30+
$helperSet = new \Symfony\Component\Console\Helper\HelperSet(array(
31+
'dialog' => new \Symfony\Component\Console\Helper\DialogHelper(),
32+
'phpcr' => new \PHPCR\Util\Console\Helper\PhpcrHelper($session),
33+
'phpcr_console_dumper' => new \PHPCR\Util\Console\Helper\PhpcrConsoleDumperHelper(),
34+
));
35+
}

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@
3838
},
3939
"extra": {
4040
"branch-alias": {
41-
"dev-master": "1.0-dev"
41+
"dev-master": "1.1-dev"
4242
}
4343
}
4444
}

src/PHPCR/Util/CND/Parser/CndParser.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
* @license http://opensource.org/licenses/MIT MIT License
3232
*
3333
* @author Daniel Barsotti <[email protected]>
34-
* @author David Buchmann <david@liip.ch>
34+
* @author David Buchmann <mail@davidbu.ch>
3535
*/
3636
class CndParser extends AbstractParser
3737
{

src/PHPCR/Util/CND/Writer/CndWriter.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
* @license http://www.apache.org/licenses Apache License Version 2.0, January 2004
2424
* @license http://opensource.org/licenses/MIT MIT License
2525
*
26-
* @author David Buchmann <david@liip.ch>
26+
* @author David Buchmann <mail@davidbu.ch>
2727
*/
2828
class CndWriter
2929
{

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

Lines changed: 10 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,13 @@
22

33
namespace PHPCR\Util\Console\Command;
44

5-
use PHPCR\SessionInterface;
65
use Symfony\Component\Console\Command\Command;
6+
use Symfony\Component\Console\Input\InputInterface;
77
use Symfony\Component\Console\Input\InputOption;
8-
use PHPCR\Util\Console\Helper\PhpcrCliHelper;
8+
use Symfony\Component\Console\Output\OutputInterface;
9+
10+
use PHPCR\SessionInterface;
11+
use PHPCR\Util\Console\Helper\PhpcrHelper;
912
use PHPCR\Util\Console\Helper\PhpcrConsoleDumperHelper;
1013

1114
/**
@@ -24,67 +27,22 @@ abstract class BaseCommand extends Command
2427
*/
2528
protected function getPhpcrSession()
2629
{
27-
return $this->getHelper('phpcr')->getSession();
30+
return $this->getPhpcrHelper()->getSession();
2831
}
2932

3033
/**
31-
* @return PhpcrCliHelper
34+
* @return PhpcrHelper
3235
*/
33-
protected function getPhpcrCliHelper()
36+
protected function getPhpcrHelper()
3437
{
35-
if (!$this->phpcrCliHelper) {
36-
$this->phpcrCliHelper = new PhpcrCliHelper($this->getPhpcrSession());
37-
}
38-
39-
return $this->phpcrCliHelper;
38+
return $this->getHelperSet()->get('phpcr');
4039
}
4140

4241
/**
4342
* @return PhpcrConsoleDumperHelper
4443
*/
4544
protected function getPhpcrConsoleDumperHelper()
4645
{
47-
if (!$this->phpcrConsoleDumperHelper) {
48-
$this->phpcrConsoleDumperHelper = new PhpcrConsoleDumperHelper();
49-
}
50-
51-
return $this->phpcrConsoleDumperHelper;
52-
}
53-
54-
public function setPhpcrConsoleDumperHelper($consoleDumperHelper)
55-
{
56-
$this->phpcrConsoleDumperHelper = $consoleDumperHelper;
57-
}
58-
59-
/**
60-
* Hack to enable overriding for unit tests.
61-
*/
62-
public function setPhpcrCliHelper(PhpcrCliHelper $helper)
63-
{
64-
$this->phpcrCliHelper = $helper;
65-
}
66-
67-
public function configureNodeManipulationInput()
68-
{
69-
$this->addOption('set-prop', 'p',
70-
InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY,
71-
'Set node property on nodes use foo=bar'
72-
);
73-
$this->addOption('remove-prop', 'r',
74-
InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY,
75-
'Remove property from nodes'
76-
);
77-
$this->addOption('add-mixin', null,
78-
InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY,
79-
'Add a mixin to the nodes'
80-
);
81-
$this->addOption('remove-mixin', null,
82-
InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY,
83-
'Remove mixin from the nodes'
84-
);
85-
$this->addOption('apply-closure', null,
86-
InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY,
87-
'Apply a closure to each node, closures are passed PHPCR\Session and PHPCR\NodeInterface'
88-
);
46+
return $this->getHelperSet()->get('phpcr_console_dumper');
8947
}
9048
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?php
2+
3+
namespace PHPCR\Util\Console\Command;
4+
5+
use Symfony\Component\Console\Input\InputOption;
6+
7+
/**
8+
* Base command for node manipulations, providing common setup.
9+
*
10+
* @license http://www.apache.org/licenses Apache License Version 2.0, January 2004
11+
* @license http://opensource.org/licenses/MIT MIT License
12+
*/
13+
abstract class BaseNodeManipulationCommand extends BaseCommand
14+
{
15+
/**
16+
* Set up the options to manipulate nodes.
17+
*/
18+
protected function configureNodeManipulationInput()
19+
{
20+
$this->addOption('set-prop', 'p',
21+
InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY,
22+
'Set node property on nodes use foo=bar'
23+
);
24+
$this->addOption('remove-prop', 'r',
25+
InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY,
26+
'Remove property from nodes'
27+
);
28+
$this->addOption('add-mixin', null,
29+
InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY,
30+
'Add a mixin to the nodes'
31+
);
32+
$this->addOption('remove-mixin', null,
33+
InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY,
34+
'Remove mixin from the nodes'
35+
);
36+
$this->addOption('apply-closure', null,
37+
InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY,
38+
'Apply a closure to each node, closures are passed PHPCR\Session and PHPCR\NodeInterface'
39+
);
40+
}
41+
}

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
*
1616
* @author Daniel Leech <[email protected]>
1717
*/
18-
class NodeMoveCommand extends Command
18+
class NodeMoveCommand extends BaseCommand
1919
{
2020
/**
2121
* {@inheritDoc}
@@ -45,7 +45,7 @@ protected function configure()
4545
*/
4646
protected function execute(InputInterface $input, OutputInterface $output)
4747
{
48-
$session = $this->getHelper('phpcr')->getSession();
48+
$session = $this->getPhpcrSession();
4949

5050
$sourcePath = $input->getArgument('source');
5151
$destPath = $input->getArgument('destination');
@@ -57,6 +57,8 @@ protected function execute(InputInterface $input, OutputInterface $output)
5757

5858
$session->move($sourcePath, $destPath);
5959
$session->save();
60+
61+
return 0;
6062
}
6163

6264
}

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
* @author Daniel Barsotti <[email protected]>
2222
* @author Daniel Leech <[email protected]>
2323
*/
24-
class NodeRemoveCommand extends Command
24+
class NodeRemoveCommand extends BaseCommand
2525
{
2626
/**
2727
* {@inheritDoc}
@@ -57,8 +57,7 @@ protected function configure()
5757
*/
5858
protected function execute(InputInterface $input, OutputInterface $output)
5959
{
60-
/** @var $session SessionInterface*/
61-
$session = $this->getHelper('phpcr')->getSession();
60+
$session = $this->getPhpcrSession();
6261

6362
$path = $input->getArgument('path');
6463
$force = $input->getOption('force');
@@ -74,7 +73,8 @@ protected function execute(InputInterface $input, OutputInterface $output)
7473
}
7574

7675
if (!$force) {
77-
$dialog = new DialogHelper();
76+
/** @var $dialog DialogHelper */
77+
$dialog = $this->getHelperSet()->get('dialog');
7878
$workspaceName = $session->getWorkspace()->getName();
7979

8080
if ($onlyChildren) {

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

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
*
2020
* @author Daniel Leech <[email protected]>
2121
*/
22-
class NodeTouchCommand extends BaseCommand
22+
class NodeTouchCommand extends BaseNodeManipulationCommand
2323
{
2424
/**
2525
* {@inheritDoc}
@@ -71,7 +71,7 @@ protected function configure()
7171
*/
7272
protected function execute(InputInterface $input, OutputInterface $output)
7373
{
74-
$helper = $this->getPhpcrCliHelper();
74+
$helper = $this->getPhpcrHelper();
7575
$session = $this->getPhpcrSession();
7676

7777
$path = $input->getArgument('path');
@@ -98,13 +98,14 @@ protected function execute(InputInterface $input, OutputInterface $output)
9898
));
9999

100100
if ($nodeType != $type) {
101-
throw new \Exception(sprintf(
102-
'You have specified node type "%s" but the existing node is of type "%s"',
101+
$output->writeln(sprintf(
102+
'<error>You have specified node type "%s" but the existing node is of type "%s"</error>',
103103
$type, $nodeType
104104
));
105+
106+
return 1;
105107
}
106108
} else {
107-
108109
$nodeName = PathHelper::getNodeName($path);
109110
$parentPath = PathHelper::getParentPath($path);
110111

@@ -116,7 +117,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
116117
$parentPath
117118
));
118119

119-
return;
120+
return 2;
120121
}
121122

122123
$output->writeln(sprintf(
@@ -135,5 +136,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
135136
));
136137

137138
$session->save();
139+
140+
return 0;
138141
}
139142
}

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

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
use Symfony\Component\Console\Input\InputInterface;
77
use Symfony\Component\Console\Output\OutputInterface;
88
use Symfony\Component\Console\Helper\DialogHelper;
9-
use PHPCR\Util\Console\Command\BaseCommand;
109

1110
/**
1211
* Command which can update the properties of nodes found
@@ -17,7 +16,7 @@
1716
*
1817
* @author Daniel Leech <[email protected]>
1918
*/
20-
class NodesUpdateCommand extends BaseCommand
19+
class NodesUpdateCommand extends BaseNodeManipulationCommand
2120
{
2221
/**
2322
* {@inheritDoc}
@@ -82,8 +81,6 @@ protected function configure()
8281
*/
8382
protected function execute(InputInterface $input, OutputInterface $output)
8483
{
85-
$this->dialog = new DialogHelper();
86-
8784
$query = $input->getOption('query');
8885
$queryLanguage = strtoupper($input->getOption('query-language'));
8986
$persistCounter = intval($input->getOption('persist-counter'));
@@ -93,7 +90,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
9390
$removeMixins = $input->getOption('remove-mixin');
9491
$applyClosures = $input->getOption('apply-closure');
9592
$noInteraction = $input->getOption('no-interaction');
96-
$helper = $this->getPhpcrCliHelper();
93+
$helper = $this->getPhpcrHelper();
9794
$session = $this->getPhpcrSession();
9895

9996
if (!$query) {
@@ -154,7 +151,9 @@ protected function execute(InputInterface $input, OutputInterface $output)
154151

155152
protected function getAction($output, $result)
156153
{
157-
$response = strtoupper($this->dialog->ask($output, sprintf(
154+
/** @var $dialog DialogHelper */
155+
$dialog = $this->getHelperSet()->get('dialog');
156+
$response = strtoupper($dialog->ask($output, sprintf(
158157
'<question>About to update %d nodes. Enter "Y" to continue, "N" to cancel or "L" to list.</question>',
159158
count($result->getRows())
160159
), false));

0 commit comments

Comments
 (0)