Completed
Push — master ( 1bb63a...7d7bb8 )
by
unknown
05:45 queued 03:50
created

Bundle/SculpinBundle/Console/Application.php (1 issue)

Severity

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is a part of Sculpin.
7
 *
8
 * (c) Dragonfly Development Inc.
9
 *
10
 * For the full copyright and license information, please view the LICENSE
11
 * file that was distributed with this source code.
12
 */
13
14
namespace Sculpin\Bundle\SculpinBundle\Console;
15
16
use Sculpin\Core\Sculpin;
17
use Symfony\Component\Console\Application as BaseApplication;
18
use Symfony\Component\Console\Formatter\OutputFormatter;
19
use Symfony\Component\Console\Formatter\OutputFormatterStyle;
20
use Symfony\Component\Console\Input\InputInterface;
21
use Symfony\Component\Console\Input\InputOption;
22
use Symfony\Component\Console\Output\ConsoleOutput;
23
use Symfony\Component\Console\Output\ConsoleOutputInterface;
24
use Symfony\Component\Console\Output\OutputInterface;
25
use Symfony\Component\Console\Style\SymfonyStyle;
26
use Symfony\Component\Debug\Exception\FatalThrowableError;
27
use Symfony\Component\HttpKernel\Bundle\Bundle;
28
use Symfony\Component\HttpKernel\KernelInterface;
29
30
/**
31
 * @author Beau Simensen <[email protected]>
32
 */
33
final class Application extends BaseApplication
34
{
35
    /**
36
     * @var KernelInterface
37
     */
38
    private $kernel;
39
40
    /**
41
     * @var \Throwable[]
42
     */
43
    private $registrationErrors = [];
44
45
    public function __construct(KernelInterface $kernel)
46
    {
47
        $this->kernel = $kernel;
48
49
        if (function_exists('date_default_timezone_set') && function_exists('date_default_timezone_get')) {
50
            date_default_timezone_set(@date_default_timezone_get());
51
        }
52
53
        parent::__construct(
54
            'Sculpin',
55
            $kernel->getName() . '/' . $kernel->getEnvironment() . ($kernel->isDebug() ? '/debug' : '')
0 ignored issues
show
Deprecated Code introduced by
The method Symfony\Component\HttpKe...nelInterface::getName() has been deprecated with message: since Symfony 4.2

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
56
        );
57
58
        $this->getDefinition()->addOption(new InputOption(
59
            '--project-dir',
60
            null,
61
            InputOption::VALUE_REQUIRED,
62
            'The project directory.',
63
            '.'
64
        ));
65
        $this->getDefinition()->addOption(new InputOption(
66
            '--env',
67
            '-e',
68
            InputOption::VALUE_REQUIRED,
69
            'The Environment name.',
70
            $kernel->getEnvironment()
71
        ));
72
        $this->getDefinition()->addOption(new InputOption(
73
            '--no-debug',
74
            null,
75
            InputOption::VALUE_NONE,
76
            'Switches off debug mode.'
77
        ));
78
        $this->getDefinition()->addOption(new InputOption(
79
            '--safe',
80
            null,
81
            InputOption::VALUE_NONE,
82
            'Enable safe mode (no bundles loaded, no kernel booted)'
83
        ));
84
    }
85
86
    /**
87
     * {@inheritDoc}
88
     */
89
    public function run(InputInterface $input = null, OutputInterface $output = null): int
90
    {
91
        if (null === $output) {
92
            $styles = [
93
                'highlight' => new OutputFormatterStyle('red'),
94
                'warning' => new OutputFormatterStyle('black', 'yellow'),
95
            ];
96
            $formatter = new OutputFormatter(false, $styles);
97
            $output = new ConsoleOutput(ConsoleOutput::VERBOSITY_NORMAL, null, $formatter);
98
        }
99
100
        return parent::run($input, $output);
101
    }
102
103
    /**
104
     * {@inheritdoc}
105
     */
106
    public function doRun(InputInterface $input, OutputInterface $output): int
107
    {
108
        if (!$input->hasParameterOption('--safe')) {
109
            // In safe mode enable no commands
110
            $this->registerCommands();
111
112
            if ($this->registrationErrors) {
113
                $this->renderRegistrationErrors($input, $output);
114
            }
115
        }
116
117
        $exitCode = parent::doRun($input, $output);
118
119
        foreach ($this->getMissingSculpinBundlesMessages() as $message) {
120
            $output->writeln($message);
121
        }
122
123
        return $exitCode;
124
    }
125
126
    public function getMissingSculpinBundlesMessages(): array
127
    {
128
        $messages = [];
129
130
        // Display missing bundle to user.
131
        if ($missingBundles = $this->kernel->getMissingSculpinBundles()) {
132
            $messages[] = '';
133
            $messages[] = '<comment>Missing Sculpin Bundles:</comment>';
134
            foreach ($missingBundles as $bundle) {
135
                $messages[] = "  * <highlight>$bundle</highlight>";
136
            }
137
            $messages[] = '';
138
        }
139
140
        return $messages;
141
    }
142
143
    /**
144
     * Get Kernel
145
     *
146
     * @return KernelInterface
147
     */
148
    public function getKernel(): KernelInterface
149
    {
150
        return $this->kernel;
151
    }
152
153
    private function registerCommands(): void
154
    {
155
        $this->kernel->boot();
156
157
        foreach ($this->kernel->getBundles() as $bundle) {
158
            if ($bundle instanceof Bundle) {
159
                $bundle->registerCommands($this);
160
            }
161
        }
162
163
        $container = $this->kernel->getContainer();
164
165
        if ($container->has('console.command_loader')) {
166
            $this->setCommandLoader($container->get('console.command_loader'));
167
        }
168
169
        if (!$container->hasParameter('console.command.ids')) {
170
            return;
171
        }
172
173
        $lazyCommandIds = $container->hasParameter('console.lazy_command.ids')
174
            ? $container->getParameter('console.lazy_command.ids')
175
            : [];
176
177
        foreach ($container->getParameter('console.command.ids') as $id) {
178
            if (!isset($lazyCommandIds[$id])) {
179
                try {
180
                    $this->add($container->get($id));
181
                } catch (\Exception $e) {
182
                    $this->registrationErrors[] = $e;
183
                } catch (\Throwable $e) {
184
                    $this->registrationErrors[] = new FatalThrowableError($e);
185
                }
186
            }
187
        }
188
    }
189
190
    private function renderRegistrationErrors(InputInterface $input, OutputInterface $output): void
191
    {
192
        if ($output instanceof ConsoleOutputInterface) {
193
            $output = $output->getErrorOutput();
194
        }
195
196
        (new SymfonyStyle($input, $output))->warning('Some commands could not be registered:');
197
198
        foreach ($this->registrationErrors as $error) {
199
            $this->doRenderException($error, $output);
200
        }
201
    }
202
}
203