Issues (11)

src/Output/Output.php (4 issues)

Labels
Severity
1
<?php
2
3
namespace Tequilarapido\Consolify\Output;
4
5
use Tequilarapido\Consolify\Progress\Progress;
6
use Tequilarapido\Consolify\Progress\ProgressHelper;
7
use Symfony\Component\Console\Output\OutputInterface;
8
use Tequilarapido\Consolify\Progress\SleepModeState;
9
10
/**
11
 * Console trait helpers.
12
 *
13
 * Brings the console methods to any service worker class.
14
 */
15
trait Output
16
{
17
    /**
18
     * Output interface.
19
     *
20
     * @var OutputInterface
21
     */
22
    protected $output;
23
24
    /**
25
     * Progress handler.
26
     *
27
     * @var Progress
28
     */
29
    protected $progress;
30
31
    /**
32
     * Set output interface.
33
     *
34
     * @param OutputInterface $output
35
     *
36
     * @return $this
37
     */
38
    public function setOutput(OutputInterface $output)
39
    {
40
        $this->output = $output;
41
42
        return $this;
43
    }
44
45
    public function createProgress($max, $uid, $advance = 0)
46
    {
47
        $this->progress = ProgressHelper::newProgressInstance($max, $uid, $this->output);
48
49
        $this->progress->setFormat(' %current%/%max% [%bar%] %percent:3s%% %elapsed:6s%/%estimated:-6s% %memory:6s% -- %message%');
50
        $this->progress->setRedrawFrequency(1);
51
        $this->progress->setMessage('');
52
        $this->progress->start();
53
        $this->progress->advance($advance);
54
    }
55
56
    public function advanceProgress($step = 1)
57
    {
58
        if (!$this->progress) {
59
            throw new \Exception('No progress was created.');
60
        }
61
62
        $this->progress->advance($step);
63
    }
64
65
    public function setProgressMessage($message)
66
    {
67
        $this->progress->setMessage($message);
0 ignored issues
show
The method setMessage() does not exist on Tequilarapido\Consolify\Progress\Progress. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

67
        $this->progress->/** @scrutinizer ignore-call */ 
68
                         setMessage($message);
Loading history...
68
    }
69
70
    public function finishProgress()
71
    {
72
        $this->progress->finish();
0 ignored issues
show
The method finish() does not exist on Tequilarapido\Consolify\Progress\Progress. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

72
        $this->progress->/** @scrutinizer ignore-call */ 
73
                         finish();
Loading history...
73
    }
74
75
    /**
76
     * Write a string as information output.
77
     *
78
     * @param string $string
79
     */
80
    public function info($string)
81
    {
82
        if (!$this->output) {
83
            return;
84
        }
85
86
        $this->output->writeln("<info>$string</info>");
87
    }
88
89
    /**
90
     * Write a string as standard output.
91
     *
92
     * @param string $string
93
     */
94
    public function line($string)
95
    {
96
        if (!$this->output) {
97
            return;
98
        }
99
100
        $this->output->writeln($string);
101
    }
102
103
    /**
104
     * Write a string as comment output.
105
     *
106
     * @param string $string
107
     */
108
    public function comment($string)
109
    {
110
        if (!$this->output) {
111
            return;
112
        }
113
114
        $this->output->writeln("<comment>$string</comment>");
115
    }
116
117
    /**
118
     * Write a string as error output.
119
     *
120
     * @param string $string
121
     */
122
    public function error($string)
123
    {
124
        if (!$this->output) {
125
            return;
126
        }
127
128
        $this->output->writeln("<error>$string</error>");
129
    }
130
131
    /**
132
     * Write a string prefixed by a check mark.
133
     *
134
     * @param   $string
135
     */
136
    public function done($string)
137
    {
138
        if (!$this->output) {
139
            return;
140
        }
141
142
        $this->output->writeln("\n\n<info>✔</info> $string");
143
    }
144
145
    /**
146
     * Simple table.
147
     *
148
     * @param $headers
149
     * @param $rows
150
     */
151
    public function table($headers, $rows)
152
    {
153
        $this->output->table($headers, $rows);
0 ignored issues
show
The method table() does not exist on Symfony\Component\Console\Output\OutputInterface. It seems like you code against a sub-type of Symfony\Component\Console\Output\OutputInterface such as Symfony\Component\Console\Style\OutputStyle. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

153
        $this->output->/** @scrutinizer ignore-call */ 
154
                       table($headers, $rows);
Loading history...
154
    }
155
156
    /**
157
     * Overwrite previous line.
158
     *
159
     * @param $string
160
     */
161
    public function overwrite($string)
162
    {
163
        $this->overwriteLine(1);
164
        $this->line($string);
165
    }
166
167
    /**
168
     * Overwrite line (or lines).
169
     *
170
     * @param int $numberLines
171
     */
172
    public function overwriteLine($numberLines = 1)
173
    {
174
        $this->output->write(sprintf("\033[%dA", $numberLines));
175
    }
176
177
    protected function sleepFor($seconds, $cycle = 1)
178
    {
179
        $this->progress->setSleepModeState($state = SleepModeState::entering());
180
        $this->line($state->getMessage());
181
182
        $remaining = $seconds;
183
        while ($remaining > $cycle) {
184
            $remaining -= $cycle;
185
186
            $this->progress->setSleepModeState($state = SleepModeState::remaining($remaining));
187
            $this->line($state->getMessage());
188
189
            sleep($cycle);
190
        }
191
192
        $this->progress->setSleepModeState($state = SleepModeState::leaving());
193
        $this->line($state->getMessage());
194
    }
195
196
197
    /** @return Progress */
198
    protected function createProgressInstance($max, $uid)
199
    {
200
        if (!class_exists($progressClass = config('consolify.progress.concrete_class'))) {
201
            throw new \LogicException("Cannot find progress class [$progressClass]");
202
        }
203
204
        return new $progressClass($this->output->createProgressBar($max), $uid);
0 ignored issues
show
The method createProgressBar() does not exist on Symfony\Component\Console\Output\OutputInterface. It seems like you code against a sub-type of Symfony\Component\Console\Output\OutputInterface such as Symfony\Component\Console\Style\OutputStyle. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

204
        return new $progressClass($this->output->/** @scrutinizer ignore-call */ createProgressBar($max), $uid);
Loading history...
205
    }
206
}
207