1
|
|
|
<?php |
2
|
|
|
|
|
|
|
|
3
|
|
|
namespace Db3v4l\Command; |
4
|
|
|
|
5
|
|
|
use Symfony\Component\Console\Command\Command; |
|
|
|
|
6
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
|
|
|
|
7
|
|
|
use Symfony\Component\Console\Output\ConsoleOutputInterface; |
|
|
|
|
8
|
|
|
|
9
|
|
|
abstract class BaseCommand extends Command |
|
|
|
|
10
|
|
|
{ |
11
|
|
|
/** @var OutputInterface $output */ |
|
|
|
|
12
|
|
|
protected $output; |
13
|
|
|
/** @var OutputInterface $errorOutput */ |
|
|
|
|
14
|
|
|
protected $errorOutput; |
15
|
|
|
protected $verbosity = OutputInterface::VERBOSITY_NORMAL; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Small tricks to allow us to lower verbosity between NORMAL and QUIET and have a decent writeln API, even with old SF versions |
19
|
|
|
* @param $message |
|
|
|
|
20
|
|
|
* @param int $verbosity |
|
|
|
|
21
|
|
|
* @param int $type |
|
|
|
|
22
|
|
|
*/ |
|
|
|
|
23
|
|
|
protected function writeln($message, $verbosity = OutputInterface::VERBOSITY_NORMAL, $type = OutputInterface::OUTPUT_NORMAL) |
24
|
|
|
{ |
25
|
|
|
if ($this->verbosity >= $verbosity) { |
26
|
|
|
$this->output->writeln($message, $type); |
27
|
|
|
} |
28
|
|
|
} |
29
|
|
|
/** |
|
|
|
|
30
|
|
|
* @param string|array $message The message as an array of lines or a single string |
|
|
|
|
31
|
|
|
* @param int $verbosity |
|
|
|
|
32
|
|
|
* @param int $type |
|
|
|
|
33
|
|
|
*/ |
|
|
|
|
34
|
|
|
protected function writeErrorln($message, $verbosity = OutputInterface::VERBOSITY_QUIET, $type = OutputInterface::OUTPUT_NORMAL) |
35
|
|
|
{ |
36
|
|
|
if ($this->verbosity >= $verbosity) { |
37
|
|
|
|
38
|
|
|
// When verbosity is set to quiet, SF swallows the error message in the writeln call |
39
|
|
|
// (unlike for other verbosity levels, which are left for us to handle...) |
40
|
|
|
// We resort to a hackish workaround to _always_ print errors to stderr, even in quiet mode. |
41
|
|
|
// If the end user does not want any error echoed, she can just 2>/dev/null |
42
|
|
|
if ($this->errorOutput->getVerbosity() == OutputInterface::VERBOSITY_QUIET) { |
43
|
|
|
$this->errorOutput->setVerbosity(OutputInterface::VERBOSITY_NORMAL); |
44
|
|
|
$this->errorOutput->writeln($message, $type); |
45
|
|
|
$this->errorOutput->setVerbosity(OutputInterface::VERBOSITY_QUIET); |
46
|
|
|
} |
47
|
|
|
else |
|
|
|
|
48
|
|
|
{ |
49
|
|
|
$this->errorOutput->writeln($message, $type); |
50
|
|
|
} |
51
|
|
|
} |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
protected function setOutput(OutputInterface $output) |
|
|
|
|
55
|
|
|
{ |
56
|
|
|
$this->output = $output; |
57
|
|
|
$this->errorOutput = $output instanceof ConsoleOutputInterface ? $output->getErrorOutput() : $output; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
protected function setVerbosity($verbosity) |
|
|
|
|
61
|
|
|
{ |
62
|
|
|
$this->verbosity = $verbosity; |
63
|
|
|
} |
64
|
|
|
} |
65
|
|
|
|