|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of the Blast Project package. |
|
5
|
|
|
* |
|
6
|
|
|
* Copyright (C) 2015-2017 Libre Informatique |
|
7
|
|
|
* |
|
8
|
|
|
* This file is licenced under the GNU LGPL v3. |
|
9
|
|
|
* For the full copyright and license information, please view the LICENSE.md |
|
10
|
|
|
* file that was distributed with this source code. |
|
11
|
|
|
*/ |
|
12
|
|
|
|
|
13
|
|
|
namespace Blast\Bundle\CoreBundle\Command\Traits; |
|
14
|
|
|
|
|
15
|
|
|
use Sensio\Bundle\GeneratorBundle\Command\Helper\DialogHelper; |
|
16
|
|
|
use Sensio\Bundle\GeneratorBundle\Command\Helper\QuestionHelper; |
|
17
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
|
18
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
|
19
|
|
|
use Symfony\Component\Console\Question\ConfirmationQuestion; |
|
20
|
|
|
use Symfony\Component\Console\Question\Question; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* This trait is the perfect copy of the Blast\Bundle\OuterExtensionBundle\Command\Traits\Interaction |
|
24
|
|
|
* They are still distinct because of the few amount of code to factorize, but it will be done |
|
25
|
|
|
* as soon as we have a bit more to make generic. |
|
26
|
|
|
**/ |
|
27
|
|
|
trait Interaction |
|
28
|
|
|
{ |
|
29
|
|
|
/** |
|
30
|
|
|
* @param OutputInterface $output |
|
31
|
|
|
* @param string $message |
|
32
|
|
|
*/ |
|
33
|
|
|
private function writeError(OutputInterface $output, $message) |
|
|
|
|
|
|
34
|
|
|
{ |
|
35
|
|
|
$output->writeln(sprintf("\n<error>%s</error>", $message)); |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* @param InputInterface $input |
|
40
|
|
|
* @param OutputInterface $output |
|
41
|
|
|
* @param string $questionText |
|
42
|
|
|
* @param mixed $default |
|
43
|
|
|
* @param callable $validator |
|
44
|
|
|
* |
|
45
|
|
|
* @return mixed |
|
46
|
|
|
*/ |
|
47
|
|
|
private function askAndValidate(InputInterface $input, OutputInterface $output, $questionText, $default = null, $validator = null) |
|
|
|
|
|
|
48
|
|
|
{ |
|
49
|
|
|
$questionHelper = $this->getQuestionHelper(); |
|
50
|
|
|
|
|
51
|
|
|
// NEXT_MAJOR: Remove this BC code for SensioGeneratorBundle 2.3/2.4 after dropping support for Symfony 2.3 |
|
52
|
|
View Code Duplication |
if ($questionHelper instanceof DialogHelper) { |
|
|
|
|
|
|
53
|
|
|
return $questionHelper->askAndValidate($output, $questionHelper->getQuestion($questionText, $default), $validator, false, $default); |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
$question = new Question($questionHelper->getQuestion($questionText, $default), $default); |
|
57
|
|
|
|
|
58
|
|
|
if (null !== $validator) { |
|
59
|
|
|
$question->setValidator($validator); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
return $questionHelper->ask($input, $output, $question); |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* @param InputInterface $input |
|
67
|
|
|
* @param OutputInterface $output |
|
68
|
|
|
* @param string $questionText |
|
69
|
|
|
* @param string $default |
|
70
|
|
|
* @param string $separator |
|
71
|
|
|
* |
|
72
|
|
|
* @return string |
|
73
|
|
|
*/ |
|
74
|
|
View Code Duplication |
private function askConfirmation(InputInterface $input, OutputInterface $output, $questionText, $default, $separator) |
|
|
|
|
|
|
75
|
|
|
{ |
|
76
|
|
|
$questionHelper = $this->getQuestionHelper(); |
|
77
|
|
|
|
|
78
|
|
|
// NEXT_MAJOR: Remove this BC code for SensioGeneratorBundle 2.3/2.4 after dropping support for Symfony 2.3 |
|
79
|
|
|
if ($questionHelper instanceof DialogHelper) { |
|
|
|
|
|
|
80
|
|
|
$question = $questionHelper->getQuestion($questionText, $default, $separator); |
|
81
|
|
|
|
|
82
|
|
|
return $questionHelper->askConfirmation($output, $question, ($default === 'no' ? false : true)); |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
$question = new ConfirmationQuestion($questionHelper->getQuestion( |
|
86
|
|
|
$questionText, |
|
87
|
|
|
$default, |
|
88
|
|
|
$separator |
|
89
|
|
|
), ($default === 'no' ? false : true)); |
|
90
|
|
|
|
|
91
|
|
|
return $questionHelper->ask($input, $output, $question); |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
/** |
|
95
|
|
|
* @return QuestionHelper|DialogHelper |
|
96
|
|
|
*/ |
|
97
|
|
View Code Duplication |
private function getQuestionHelper() |
|
|
|
|
|
|
98
|
|
|
{ |
|
99
|
|
|
// NEXT_MAJOR: Remove this BC code for SensioGeneratorBundle 2.3/2.4 after dropping support for Symfony 2.3 |
|
100
|
|
|
if (class_exists('Sensio\Bundle\GeneratorBundle\Command\Helper\DialogHelper')) { |
|
101
|
|
|
$questionHelper = $this->getHelper('dialog'); |
|
|
|
|
|
|
102
|
|
|
|
|
103
|
|
|
if (!$questionHelper instanceof DialogHelper) { |
|
|
|
|
|
|
104
|
|
|
$questionHelper = new DialogHelper(); |
|
105
|
|
|
$this->getHelperSet()->set($questionHelper); |
|
|
|
|
|
|
106
|
|
|
} |
|
107
|
|
|
} else { |
|
108
|
|
|
$questionHelper = $this->getHelper('question'); |
|
|
|
|
|
|
109
|
|
|
|
|
110
|
|
|
if (!$questionHelper instanceof QuestionHelper) { |
|
111
|
|
|
$questionHelper = new QuestionHelper(); |
|
112
|
|
|
$this->getHelperSet()->set($questionHelper); |
|
|
|
|
|
|
113
|
|
|
} |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
return $questionHelper; |
|
117
|
|
|
} |
|
118
|
|
|
} |
|
119
|
|
|
|