Completed
Push — master ( e93a4a...079c5b )
by Théo
02:06
created

InitCommand::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 1
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
ccs 0
cts 3
cp 0
crap 2
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\Console\Command;
16
17
use Symfony\Component\Console\Helper\FormatterHelper;
18
use Symfony\Component\Console\Input\InputInterface;
19
use Symfony\Component\Console\Input\InputOption;
20
use Symfony\Component\Console\Output\OutputInterface;
21
use Symfony\Component\Console\Style\OutputStyle;
22
use Symfony\Component\Console\Style\SymfonyStyle;
23
use Symfony\Component\Filesystem\Filesystem;
24
25
final class InitCommand extends BaseCommand
26
{
27
    private const CONFIG_FILE_OPT = 'config';
28
    private const CONFIG_FILE_TEMPLATE = __DIR__.'/../../scoper.inc.php.tpl';
29
    private const CONFIG_FILE_DEFAULT = 'scoper.inc.php';
30
31
    private $fileSystem;
32
33
    public function __construct($name = null)
34
    {
35
        parent::__construct($name);
36
37
        $this->fileSystem = new Filesystem();
38
    }
39
40
    /**
41
     * @inheritdoc
42
     */
43
    protected function configure(): void
44
    {
45
        parent::configure();
46
47
        $this
48
            ->setName('init')
49
            ->setDescription('Generates a configuration file.')
50
            ->addOption(
51
                self::CONFIG_FILE_OPT,
52
                'c',
53
                InputOption::VALUE_REQUIRED,
54
                sprintf(
55
                    'Configuration file. Will use "%s" if found by default.',
56
                    self::CONFIG_FILE_DEFAULT
57
                ),
58
                null
59
            )
60
        ;
61
    }
62
63
    /**
64
     * @inheritdoc
65
     */
66
    protected function execute(InputInterface $input, OutputInterface $output): int
67
    {
68
        $this->changeWorkingDirectory($input);
69
70
        $io = new SymfonyStyle($input, $output);
71
        $io->writeln('');
72
73
        /** @var FormatterHelper $formatter */
74
        $formatter = $this->getHelper('formatter');
75
76
        $io->writeln(
77
            $formatter->formatSection(
78
                'PHP-Scoper configuration generate',
79
                'Welcome!'
80
            )
81
        );
82
83
        $configFile = $this->retrieveConfig($input, $io);
84
85
        $this->fileSystem->copy(self::CONFIG_FILE_TEMPLATE, $configFile);
86
87
        $io->writeln([
88
            '',
89
            sprintf(
90
                'Generated the configuration file "<comment>%s</comment>".',
91
                $configFile
92
            ),
93
            '',
94
        ]);
95
96
        return 0;
97
    }
98
99
    private function retrieveConfig(InputInterface $input, OutputStyle $io): ?string
100
    {
101
        $configFile = $input->getOption(self::CONFIG_FILE_OPT);
102
103
        $configFile = (null === $configFile)
104
            ? $this->makeAbsolutePath(self::CONFIG_FILE_DEFAULT)
105
            : $this->makeAbsolutePath($configFile)
106
        ;
107
108
        if (file_exists($configFile)) {
109
            $canDeleteFile = $io->confirm(
110
                sprintf(
111
                    'The configuration file "<comment>%s</comment>" already exists. Are you sure you want to '
112
                    .'replace it?',
113
                    $configFile
114
                ),
115
                false
116
            );
117
118
            if (false === $canDeleteFile) {
119
                $io->writeln('Skipped file generation.');
120
121
                return $configFile;
122
            }
123
124
            $this->fileSystem->remove($configFile);
125
        } else {
126
            $createConfig = $io->confirm('No configuration file found. Do you want to create one?');
127
128
            if (false === $createConfig) {
129
                return null;
130
            }
131
        }
132
133
        return $configFile;
134
    }
135
136
    private function makeAbsolutePath(string $path): string
137
    {
138
        if (false === $this->fileSystem->isAbsolutePath($path)) {
139
            $path = getcwd().DIRECTORY_SEPARATOR.$path;
140
        }
141
142
        return $path;
143
    }
144
}
145