Passed
Pull Request — master (#248)
by Théo
03:11
created

Process::logCompactors()   B

Complexity

Conditions 5
Paths 6

Size

Total Lines 39
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 39
rs 8.439
c 0
b 0
f 0
cc 5
eloc 20
nc 6
nop 2
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the box project.
7
 *
8
 * (c) Kevin Herrera <[email protected]>
9
 *     Théo Fidry <[email protected]>
10
 *
11
 * This source file is subject to the MIT license that is bundled
12
 * with this source code in the file LICENSE.
13
 */
14
15
namespace KevinGH\Box\Console\Command;
16
17
use KevinGH\Box\Box;
18
use KevinGH\Box\Compactor;
19
use KevinGH\Box\Compactor\Placeholder;
20
use KevinGH\Box\Compactors;
21
use KevinGH\Box\Configuration;
22
use KevinGH\Box\PhpSettingsHandler;
23
use stdClass;
24
use Symfony\Component\Console\Input\InputArgument;
25
use Symfony\Component\Console\Input\InputInterface;
26
use Symfony\Component\Console\Input\InputOption;
27
use Symfony\Component\Console\Logger\ConsoleLogger;
28
use Symfony\Component\Console\Output\OutputInterface;
29
use Symfony\Component\Console\Style\SymfonyStyle;
30
use const KevinGH\Box\BOX_ALLOW_XDEBUG;
1 ignored issue
show
Bug introduced by
The constant KevinGH\Box\BOX_ALLOW_XDEBUG was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
31
use function array_shift;
32
use function array_unshift;
33
use function explode;
34
use function get_class;
35
use function getcwd;
36
use function implode;
37
use function KevinGH\Box\FileSystem\file_contents;
38
use function KevinGH\Box\FileSystem\make_path_absolute;
39
use function putenv;
40
use function sprintf;
41
use function strlen;
42
use function substr;
43
44
final class Process extends Configurable
45
{
46
    use ChangeableWorkingDirectory;
47
48
    private const FILE_ARGUMENT = 'file';
49
50
    private const NO_RESTART_OPTION = 'no-restart';
51
    private const NO_CONFIG_OPTION = 'no-config';
52
53
    /**
54
     * {@inheritdoc}
55
     */
56
    protected function configure(): void
57
    {
58
        parent::configure();
59
60
        $this->setName('process');
61
        $this->setDescription('Apply the registered compactors and replacement values on a file');
62
        $this->setHelp(
63
            'The <info>%command.name%</info> command will apply the registered compactors and replacement values '
64
            .'on the the given file. This is useful to debug the scoping of a specific file for example.'
65
        );
66
67
        $this->addArgument(
68
            self::FILE_ARGUMENT,
69
            InputArgument::REQUIRED,
70
            'Path to the file to process'
71
        );
72
        $this->addOption(
73
            self::NO_RESTART_OPTION,
74
            null,
75
            InputOption::VALUE_NONE,
76
            'Do not restart the PHP process. Box restarts the process by default to disable xdebug'
77
        );
78
        $this->addOption(
79
            self::NO_CONFIG_OPTION,
80
            null,
81
            InputOption::VALUE_NONE,
82
            'Ignore the config file even when one is specified with the --config option'
83
        );
84
85
        $this->configureWorkingDirOption();
86
    }
87
88
    /**
89
     * {@inheritdoc}
90
     */
91
    protected function execute(InputInterface $input, OutputInterface $output): void
92
    {
93
        $io = new SymfonyStyle($input, $output);
94
95
        if ($input->getOption(self::NO_RESTART_OPTION)) {
96
            putenv(BOX_ALLOW_XDEBUG.'=1');
97
        }
98
99
        (new PhpSettingsHandler(new ConsoleLogger($output)))->check();
100
101
        $this->changeWorkingDirectory($input);
102
103
        $io->writeln('');
104
105
        $config = $input->getOption(self::NO_CONFIG_OPTION)
106
            ? Configuration::create(null, new stdClass())
107
            : $this->getConfig($input, $output, true)
108
        ;
109
110
        $path = make_path_absolute($input->getArgument(self::FILE_ARGUMENT), getcwd());
111
112
        $compactors = $this->retrieveCompactors($config);
113
114
        $fileContents = file_contents($path);
115
116
        $io->writeln([
117
            sprintf(
118
                'Processing the contents of the file <info>%s</info>',
119
                $path
120
            ),
121
            '',
122
        ]);
123
124
        $this->logPlaceholders($io, $config);
125
        $this->logCompactors($io, $compactors);
126
127
        $fileProcessedContents = $compactors->compactContents($path, $fileContents);
128
129
        $io->writeln([
130
            'Processed contents:',
131
            '',
132
            '<comment>"""</comment>',
133
            $fileProcessedContents,
134
            '<comment>"""</comment>',
135
        ]);
136
    }
137
138
    private function retrieveCompactors(Configuration $config): Compactors
139
    {
140
        $compactors = $config->getCompactors();
141
142
        array_unshift(
143
            $compactors,
144
            new Placeholder($config->getReplacements())
145
        );
146
147
        return new Compactors(...$compactors);
148
    }
149
150
    private function logPlaceholders(SymfonyStyle $io, Configuration $config): void
151
    {
152
        if ([] === $config->getReplacements()) {
153
            $io->writeln([
154
                'No replacement values registered',
155
                '',
156
            ]);
157
158
            return;
159
        }
160
161
        $io->writeln('Registered replacement values:');
162
163
        foreach ($config->getReplacements() as $key => $value) {
164
            $io->writeln(
165
                sprintf(
166
                    '  <comment>+</comment> %s: %s',
167
                    $key,
168
                    $value
169
                )
170
            );
171
        }
172
173
        $io->writeln('');
174
    }
175
176
    private function logCompactors(SymfonyStyle $io, Compactors $compactors): void
177
    {
178
        $compactors = $compactors->toArray();
179
180
        foreach ($compactors as $index => $compactor) {
181
            if ($compactor instanceof Placeholder) {
182
                unset($compactors[$index]);
183
            }
184
        }
185
186
        if ([] === $compactors) {
187
            $io->writeln([
188
                'No compactor registered',
189
                '',
190
            ]);
191
192
            return;
193
        }
194
195
        $io->writeln('Registered compactors:');
196
197
        $logCompactors = function (Compactor $compactor) use ($io): void {
198
            $compactorClassParts = explode('\\', get_class($compactor));
199
200
            if ('_HumbugBox' === substr($compactorClassParts[0], 0, strlen('_HumbugBox'))) {
201
                // Keep the non prefixed class name for the user
202
                array_shift($compactorClassParts);
203
            }
204
205
            $io->writeln(
206
                sprintf(
207
                    '  <comment>+</comment> %s',
208
                    implode('\\', $compactorClassParts)
209
                )
210
            );
211
        };
212
213
        array_map($logCompactors, $compactors);
214
        $io->writeln('');
215
    }
216
}
217