forked from drush-ops/drush
-
Notifications
You must be signed in to change notification settings - Fork 0
/
DrushCommands.php
132 lines (118 loc) · 3.82 KB
/
DrushCommands.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
<?php
namespace Drush\Commands;
use Drush\Log\DrushLoggerManager;
use Drush\Log\Logger;
use Symfony\Component\Console\Style\SymfonyStyle;
use Consolidation\AnnotatedCommand\CommandData;
use Drush\Drush;
use Drush\Style\DrushStyle;
use GuzzleHttp\HandlerStack;
use GuzzleHttp\MessageFormatter;
use GuzzleHttp\Middleware;
use Psr\Log\LoggerAwareInterface;
use Psr\Log\LoggerAwareTrait;
use Drush\Config\ConfigAwareTrait;
use Drush\Exec\ExecTrait;
use Robo\Contract\ConfigAwareInterface;
use Robo\Contract\IOAwareInterface;
use Robo\Common\IO;
use Symfony\Component\Console\Input\InputOption;
use Consolidation\SiteProcess\ProcessManagerAwareTrait;
use Consolidation\SiteProcess\ProcessManagerAwareInterface;
use Webmozart\PathUtil\Path;
abstract class DrushCommands implements IOAwareInterface, LoggerAwareInterface, ConfigAwareInterface, ProcessManagerAwareInterface
{
use ProcessManagerAwareTrait;
use ExecTrait;
use ConfigAwareTrait;
use LoggerAwareTrait;
use IO {
io as roboIo;
}
// This is more readable.
const REQ = InputOption::VALUE_REQUIRED;
const OPT = InputOption::VALUE_OPTIONAL;
// Common exit codes.
const EXIT_SUCCESS = 0;
const EXIT_FAILURE = 1;
// Used to signal that the command completed successfully, but we still want to indicate a failure to the caller.
const EXIT_FAILURE_WITH_CLARITY = 3;
protected CommandData $commandData;
public function __construct()
{
}
/**
* Override Robo's IO function with our custom style.
*/
protected function io(): SymfonyStyle
{
if (!$this->io) {
// Specify our own Style class when needed.
$this->io = new DrushStyle($this->input(), $this->output());
}
return $this->io;
}
/**
* Returns a logger object.
*/
protected function logger(): ?DrushLoggerManager
{
return $this->logger;
}
/**
* Print the contents of a file.
*
* @param string $file
* Full path to a file.
*/
protected function printFile(string $file): void
{
if (str_ends_with($file, ".htm") || str_ends_with($file, ".html")) {
$tmp_file = drush_tempnam(basename($file));
file_put_contents($tmp_file, drush_html_to_text(file_get_contents($file)));
$file = $tmp_file;
}
if (self::input()->isInteractive()) {
if (self::programExists('less')) {
$process = $this->processManager()->process(['less', $file])->setTty(true);
if ($process->run() === 0) {
return;
}
}
}
$this->output()->writeln(file_get_contents($file));
}
/**
* Persist commandData for use in primary command callback. Used by 'topic' commands.
*
* @hook pre-command *
*
* @param CommandData $commandData
*/
public function preHook(CommandData $commandData)
{
$this->commandData = $commandData;
}
/**
* Print the contents of a file. The path comes from the @topic annotation.
*
* @param CommandData $commandData
* Full path to a file.
*/
protected function printFileTopic(CommandData $commandData)
{
$file = $commandData->annotationData()->get('topic');
$this->printFile(Path::makeAbsolute($file, dirname($commandData->annotationData()->get('_path'))));
}
/**
* Get a Guzzle handler stack that uses the Drush logger.
*
* @see https://stackoverflow.com/questions/32681165/how-do-you-log-all-api-calls-using-guzzle-6.
*/
protected function getStack(): HandlerStack
{
$stack = HandlerStack::create();
$stack->push(Middleware::log($this->logger(), new MessageFormatter(Drush::debug() ? MessageFormatter::DEBUG : MessageFormatter::SHORT)));
return $stack;
}
}