1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/* |
6
|
|
|
* This file is part of the humbug/php-scoper package. |
7
|
|
|
* |
8
|
|
|
* Copyright (c) 2017 Théo FIDRY <[email protected]>, |
9
|
|
|
* Pádraic Brady <[email protected]> |
10
|
|
|
* |
11
|
|
|
* For the full copyright and license information, please view the LICENSE |
12
|
|
|
* file that was distributed with this source code. |
13
|
|
|
*/ |
14
|
|
|
|
15
|
|
|
namespace Humbug\PhpScoper\Logger; |
16
|
|
|
|
17
|
|
|
use Humbug\PhpScoper\Throwable\Exception\ParsingException; |
18
|
|
|
use Symfony\Component\Console\Application; |
19
|
|
|
use Symfony\Component\Console\Helper\ProgressBar; |
20
|
|
|
use Symfony\Component\Console\Output\NullOutput; |
21
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
22
|
|
|
use Symfony\Component\Console\Style\SymfonyStyle; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @private |
26
|
|
|
* @final |
27
|
|
|
*/ |
28
|
|
|
class ConsoleLogger |
29
|
|
|
{ |
30
|
|
|
private $application; |
31
|
|
|
private $io; |
32
|
|
|
private $startTime; |
33
|
|
|
private $progressBar; |
34
|
|
|
|
35
|
|
|
public function __construct(Application $application, SymfonyStyle $io) |
36
|
|
|
{ |
37
|
|
|
$this->io = $io; |
38
|
|
|
$this->application = $application; |
39
|
|
|
$this->startTime = microtime(true); |
40
|
|
|
$this->progressBar = new ProgressBar(new NullOutput()); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @param string $prefix |
45
|
|
|
* @param string[] $paths |
46
|
|
|
*/ |
47
|
|
|
public function outputScopingStart(string $prefix, array $paths): void |
48
|
|
|
{ |
49
|
|
|
$this->io->writeln($this->application->getHelp()); |
50
|
|
|
|
51
|
|
|
$newLine = 1; |
52
|
|
|
|
53
|
|
|
if ($this->io->getVerbosity() >= OutputInterface::VERBOSITY_DEBUG) { |
54
|
|
|
$this->io->section('Input'); |
55
|
|
|
|
56
|
|
|
$this->io->writeln( |
57
|
|
|
sprintf( |
58
|
|
|
'Prefix: %s', |
59
|
|
|
$prefix |
60
|
|
|
) |
61
|
|
|
); |
62
|
|
|
|
63
|
|
|
$this->io->write('Paths:'); |
64
|
|
|
|
65
|
|
|
if (0 === count($paths)) { |
66
|
|
|
$this->io->writeln(' Loaded from config'); |
67
|
|
|
} else { |
68
|
|
|
$this->io->writeln(''); |
69
|
|
|
$this->io->listing($paths); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
$this->io->section('Processing'); |
73
|
|
|
$newLine = 0; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
$this->io->newLine($newLine); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* Output file count message if relevant. |
81
|
|
|
* |
82
|
|
|
* @param int $count |
83
|
|
|
*/ |
84
|
|
|
public function outputFileCount(int $count): void |
85
|
|
|
{ |
86
|
|
|
if (OutputInterface::VERBOSITY_NORMAL === $this->io->getVerbosity()) { |
87
|
|
|
$this->progressBar = $this->io->createProgressBar($count); |
88
|
|
|
$this->progressBar->start(); |
89
|
|
|
} elseif ($this->io->getVerbosity() >= OutputInterface::VERBOSITY_VERBOSE) { |
90
|
|
|
$this->progressBar = new ProgressBar(new NullOutput()); |
91
|
|
|
} |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* Output scoping success message. |
96
|
|
|
* |
97
|
|
|
* @param string $path |
98
|
|
|
*/ |
99
|
|
View Code Duplication |
public function outputSuccess(string $path): void |
|
|
|
|
100
|
|
|
{ |
101
|
|
|
if ($this->io->getVerbosity() >= OutputInterface::VERBOSITY_VERBOSE) { |
102
|
|
|
$this->io->writeln( |
103
|
|
|
sprintf( |
104
|
|
|
' * [<info>OK</info>] %s', |
105
|
|
|
$path |
106
|
|
|
) |
107
|
|
|
); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
$this->progressBar->advance(); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
public function outputWarnOfFailure(string $path, ParsingException $exception): void |
114
|
|
|
{ |
115
|
|
|
if ($this->io->getVerbosity() >= OutputInterface::VERBOSITY_VERBOSE) { |
116
|
|
|
$this->io->writeln( |
117
|
|
|
sprintf( |
118
|
|
|
' * [<error>NO</error>] %s', |
119
|
|
|
$path |
120
|
|
|
) |
121
|
|
|
); |
122
|
|
|
} |
123
|
|
|
if ($this->io->getVerbosity() >= OutputInterface::VERBOSITY_VERY_VERBOSE) { |
124
|
|
|
$this->io->writeln( |
125
|
|
|
sprintf( |
126
|
|
|
"\t".'%s: %s', |
127
|
|
|
$exception->getMessage(), |
128
|
|
|
$exception->getPrevious() |
129
|
|
|
) |
130
|
|
|
); |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
$this->progressBar->advance(); |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* Output scoping failure message. |
138
|
|
|
* |
139
|
|
|
* @param string $path |
140
|
|
|
*/ |
141
|
|
View Code Duplication |
public function outputFail(string $path): void |
|
|
|
|
142
|
|
|
{ |
143
|
|
|
if ($this->io->getVerbosity() >= OutputInterface::VERBOSITY_VERBOSE) { |
144
|
|
|
$this->io->writeln( |
145
|
|
|
sprintf( |
146
|
|
|
' * [<error>FA</error>] %s', |
147
|
|
|
$path |
148
|
|
|
) |
149
|
|
|
); |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
$this->progressBar->advance(); |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
public function outputScopingEnd(): void |
156
|
|
|
{ |
157
|
|
|
$this->finish(false); |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
public function outputScopingEndWithFailure(): void |
161
|
|
|
{ |
162
|
|
|
$this->finish(true); |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
private function finish(bool $failed): void |
166
|
|
|
{ |
167
|
|
|
$this->progressBar->finish(); |
168
|
|
|
$this->io->newLine(2); |
169
|
|
|
|
170
|
|
|
if (false === $failed) { |
171
|
|
|
$this->io->success( |
172
|
|
|
sprintf( |
173
|
|
|
'Successfully prefixed %d files.', |
174
|
|
|
$this->progressBar->getMaxSteps() |
175
|
|
|
) |
176
|
|
|
); |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
if ($this->io->getVerbosity() >= OutputInterface::VERBOSITY_NORMAL) { |
180
|
|
|
$this->io->comment( |
181
|
|
|
sprintf( |
182
|
|
|
'<info>Memory usage: %.2fMB (peak: %.2fMB), time: %.2fs<info>', |
183
|
|
|
round(memory_get_usage() / 1024 / 1024, 2), |
184
|
|
|
round(memory_get_peak_usage() / 1024 / 1024, 2), |
185
|
|
|
round(microtime(true) - $this->startTime, 2) |
186
|
|
|
) |
187
|
|
|
); |
188
|
|
|
} |
189
|
|
|
} |
190
|
|
|
} |
191
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.