Skip to content

Commit 647eb89

Browse files
committed
Merge pull request #154 from phpcr/sf3-compat
adjust commands for symfony 3 compatibility
2 parents 6d87ca3 + 5bc63c2 commit 647eb89

8 files changed

+79
-32
lines changed

CHANGELOG.md

+7
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,13 @@
11
Changelog
22
=========
33

4+
1.2.7
5+
-----
6+
7+
* **2015-07-13**: Added Symfony 3 compatibility for the console commands. If you use
8+
the commands, update your `cli-config.php` according to `cli-config.php.dist` to set
9+
the question helper if it is available.
10+
411
1.2.0
512
-----
613

cli-config.php.dist

+6-1
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,13 @@ if (isset($argv[1])
2828
$session = $repository->login($credentials, $workspace);
2929

3030
$helperSet = new \Symfony\Component\Console\Helper\HelperSet(array(
31-
'dialog' => new \Symfony\Component\Console\Helper\DialogHelper(),
3231
'phpcr' => new \PHPCR\Util\Console\Helper\PhpcrHelper($session),
3332
'phpcr_console_dumper' => new \PHPCR\Util\Console\Helper\PhpcrConsoleDumperHelper(),
3433
));
34+
35+
if (class_exists('Symfony\Component\Console\Helper\QuestionHelper')) {
36+
$helperSet->set(new \Symfony\Component\Console\Helper\QuestionHelper(), 'question');
37+
} else {
38+
$helperSet->set(new \Symfony\Component\Console\Helper\DialogHelper(), 'dialog');
39+
}
3540
}

composer.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
"require": {
3030
"php": ">=5.3.3",
3131
"phpcr/phpcr": "~2.1.0",
32-
"symfony/console": "2.*,>=2.0.5"
32+
"symfony/console": "~2.3|~3.0"
3333
},
3434
"conflict": {
3535
"jackalope/jackalope-jackrabbit": "<1.2.1"

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

+44-1
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,13 @@
44

55
use Symfony\Component\Console\Command\Command;
66
use Symfony\Component\Console\Input\InputInterface;
7-
use Symfony\Component\Console\Input\InputOption;
87
use Symfony\Component\Console\Output\OutputInterface;
98

109
use PHPCR\SessionInterface;
1110
use PHPCR\Util\Console\Helper\PhpcrHelper;
1211
use PHPCR\Util\Console\Helper\PhpcrConsoleDumperHelper;
12+
use Symfony\Component\Console\Question\ConfirmationQuestion;
13+
use Symfony\Component\Console\Question\Question;
1314

1415
/**
1516
* Common base class to help with the helpers.
@@ -45,4 +46,46 @@ protected function getPhpcrConsoleDumperHelper()
4546
{
4647
return $this->getHelperSet()->get('phpcr_console_dumper');
4748
}
49+
50+
/**
51+
* Ask a question with the question helper or the dialog helper for symfony < 2.5 compatibility.
52+
*
53+
* @param InputInterface $input
54+
* @param OutputInterface $output
55+
* @param string $question
56+
* @param string $default
57+
*
58+
* @return string
59+
*/
60+
protected function ask(InputInterface $input, OutputInterface $output, $question, $default = null)
61+
{
62+
if ($this->getHelperSet()->has('question')) {
63+
$question = new Question($question, $default);
64+
65+
return $this->getHelper('question')->ask($input, $output, $question);
66+
}
67+
68+
return $this->getHelper('dialog')->ask($output, $question, $default);
69+
}
70+
71+
/**
72+
* Ask for confirmation with the question helper or the dialog helper for symfony < 2.5 compatibility.
73+
*
74+
* @param InputInterface $input
75+
* @param OutputInterface $output
76+
* @param string $question
77+
* @param boolean $default
78+
*
79+
* @return string
80+
*/
81+
protected function askConfirmation(InputInterface $input, OutputInterface $output, $question, $default = true)
82+
{
83+
if ($this->getHelperSet()->has('question')) {
84+
$question = new ConfirmationQuestion($question, $default);
85+
86+
return $this->getHelper('question')->ask($input, $output, $question);
87+
}
88+
89+
return $this->getHelper('dialog')->askConfirmation($output, $question, $default);
90+
}
4891
}

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

+6-8
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,10 @@
33
namespace PHPCR\Util\Console\Command;
44

55
use PHPCR\NodeInterface;
6-
use PHPCR\SessionInterface;
7-
use Symfony\Component\Console\Command\Command;
86
use Symfony\Component\Console\Input\InputArgument;
97
use Symfony\Component\Console\Input\InputOption;
108
use Symfony\Component\Console\Input\InputInterface;
119
use Symfony\Component\Console\Output\OutputInterface;
12-
use Symfony\Component\Console\Helper\DialogHelper;
1310

1411
/**
1512
* Command to remove all nodes from a path in the workspace of the configured
@@ -73,8 +70,6 @@ protected function execute(InputInterface $input, OutputInterface $output)
7370
}
7471

7572
if (!$force) {
76-
/** @var $dialog DialogHelper */
77-
$dialog = $this->getHelperSet()->get('dialog');
7873
$workspaceName = $session->getWorkspace()->getName();
7974

8075
if ($onlyChildren) {
@@ -87,9 +82,12 @@ protected function execute(InputInterface $input, OutputInterface $output)
8782
'from workspace "%s"';
8883
}
8984

90-
$force = $dialog->askConfirmation($output, sprintf(
91-
'<question>'.$question.' Y/N ?</question>', $path, $workspaceName, false
92-
));
85+
$force = $this->askConfirmation(
86+
$input,
87+
$output,
88+
sprintf('<question>'.$question.' Y/N ?</question>', $path, $workspaceName),
89+
false
90+
);
9391
}
9492

9593
if (!$force) {

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

+13-9
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,11 @@
22

33
namespace PHPCR\Util\Console\Command;
44

5+
use PHPCR\Query\QueryResultInterface;
6+
use PHPCR\Query\RowInterface;
57
use Symfony\Component\Console\Input\InputOption;
68
use Symfony\Component\Console\Input\InputInterface;
79
use Symfony\Component\Console\Output\OutputInterface;
8-
use Symfony\Component\Console\Helper\DialogHelper;
910

1011
/**
1112
* Command which can update the properties of nodes found
@@ -110,13 +111,14 @@ protected function execute(InputInterface $input, OutputInterface $output)
110111
$result = $query->execute();
111112

112113
if (!$noInteraction) {
113-
if (false === $this->getAction($output, $result)) {
114+
if (false === $this->shouldExecute($input, $output, $result)) {
114115
return 0;
115116
}
116117
}
117118

118119
$persistIn = $persistCounter;
119120

121+
/** @var $row RowInterface */
120122
foreach ($result as $i => $row) {
121123
$output->writeln(sprintf(
122124
"<info>Updating node:</info> [%d] %s.",
@@ -149,21 +151,23 @@ protected function execute(InputInterface $input, OutputInterface $output)
149151
return 0;
150152
}
151153

152-
protected function getAction($output, $result)
154+
/**
155+
* @return bool Whether to execute the action or not.
156+
*/
157+
private function shouldExecute(InputInterface $input, OutputInterface $output, QueryResultInterface $result)
153158
{
154-
/** @var $dialog DialogHelper */
155-
$dialog = $this->getHelperSet()->get('dialog');
156-
$response = strtoupper($dialog->ask($output, sprintf(
159+
$response = strtoupper($this->ask($input, $output, sprintf(
157160
'<question>About to update %d nodes. Enter "Y" to continue, "N" to cancel or "L" to list.</question>',
158161
count($result->getRows())
159-
), false));
162+
)));
160163

161164
if ($response == 'L') {
165+
/** @var $row RowInterface */
162166
foreach ($result as $i => $row) {
163167
$output->writeln(sprintf(' - [%d] %s', $i, $row->getPath()));
164168
}
165169

166-
return $this->getAction($output, $result);
170+
return $this->shouldExecute($input, $output, $result);
167171
}
168172

169173
if ($response == 'N') {
@@ -174,6 +178,6 @@ protected function getAction($output, $result)
174178
return true;
175179
}
176180

177-
return $this->getAction($output, $result);
181+
return $this->shouldExecute($input, $output, $result);
178182
}
179183
}

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

+1-6
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,6 @@
33
namespace PHPCR\Util\Console\Command;
44

55
use PHPCR\RepositoryInterface;
6-
use PHPCR\SessionInterface;
7-
use Symfony\Component\Console\Command\Command;
8-
use Symfony\Component\Console\Helper\DialogHelper;
96
use Symfony\Component\Console\Input\InputArgument;
107
use Symfony\Component\Console\Input\InputInterface;
118
use Symfony\Component\Console\Input\InputOption;
@@ -71,9 +68,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
7168

7269
$force = $input->getOption('force');
7370
if (!$force) {
74-
/** @var $dialog DialogHelper */
75-
$dialog = $this->getHelperSet()->get('dialog');
76-
$force = $dialog->askConfirmation($output, sprintf(
71+
$force = $this->askConfirmation($input, $output, sprintf(
7772
'<question>Are you sure you want to delete workspace "%s" Y/N ?</question>',
7873
$workspaceName
7974
), false);

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

+1-6
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,9 @@
22

33
namespace PHPCR\Util\Console\Command;
44

5-
use PHPCR\SessionInterface;
6-
use Symfony\Component\Console\Command\Command;
75
use Symfony\Component\Console\Input\InputOption;
86
use Symfony\Component\Console\Input\InputInterface;
97
use Symfony\Component\Console\Output\OutputInterface;
10-
use Symfony\Component\Console\Helper\DialogHelper;
118

129
use PHPCR\Util\NodeHelper;
1310

@@ -51,9 +48,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
5148

5249
$workspaceName = $session->getWorkspace()->getName();
5350
if (!$force) {
54-
/** @var $dialog DialogHelper */
55-
$dialog = $this->getHelperSet()->get('dialog');
56-
$force = $dialog->askConfirmation($output, sprintf(
51+
$force = $this->askConfirmation($input, $output, sprintf(
5752
'<question>Are you sure you want to purge workspace "%s" Y/N ?</question>',
5853
$workspaceName
5954
), false);

0 commit comments

Comments
 (0)