Completed
Pull Request — master (#177)
by
unknown
04:33
created

ConsoleCommand::configure()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 7
rs 9.4285
c 1
b 0
f 0
cc 1
eloc 4
nc 1
nop 0
1
<?php
2
3
namespace N98\Magento\Command\Developer;
4
5
use Exception;
6
use Magento\Framework\App\ProductMetadataInterface;
7
use N98\Magento\Command\AbstractMagentoCommand;
8
use N98\Util\Unicode\Charset;
9
use PhpParser\Lexer;
10
use PhpParser\Parser;
11
use Psy\CodeCleaner;
12
use Psy\Command\ListCommand;
13
use Psy\Configuration;
14
use Psy\Output\ShellOutput;
15
use Psy\Shell;
16
use Symfony\Component\Console\Input\InputInterface;
17
use Symfony\Component\Console\Output\OutputInterface;
18
use Psy\ParserFactory;
19
20
class ConsoleCommand extends AbstractMagentoCommand
21
{
22
    /**
23
     * @var ProductMetadataInterface
24
     */
25
    private $productMeta;
26
27
    protected function configure()
28
    {
29
        $this
30
            ->setName('dev:console')
31
            ->setDescription('Opens PHP interactive shell with initialized Mage::app() <comment>(Experimental)</comment>')
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 122 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
32
        ;
33
    }
34
35
    /**
36
     * @param ProductMetadataInterface $productMetadata
37
     */
38
    public function inject(ProductMetadataInterface $productMetadata)
39
    {
40
        $this->productMeta = $productMetadata;
41
    }
42
43
    /**
44
     * @param InputInterface  $input
45
     * @param OutputInterface $output
46
     *
47
     * @return int|void
48
     */
49
    protected function execute(InputInterface $input, OutputInterface $output)
50
    {
51
        $initialized = false;
52
        try {
53
            $this->detectMagento($output);
54
            $initialized = $this->initMagento();
55
        } catch (Exception $e) {
56
            // do nothing
57
        }
58
59
        $parser = new Parser(new Lexer());
60
        $cleaner = new CodeCleaner($parser);
61
        $consoleOutput = new ShellOutput();
62
        $config = new Configuration();
63
        $config->setCodeCleaner($cleaner);
64
        $shell = new Shell($config);
65
        $shell->setScopeVariables([
66
            'di' => $this->getObjectManager(),
67
        ]);
68
69
        if ($initialized) {
70
            $ok = Charset::convertInteger(Charset::UNICODE_CHECKMARK_CHAR);
71
72
            $edition = $this->productMeta->getEdition();
73
            $magentoVersion = $this->productMeta->getVersion();
74
75
            $consoleOutput->writeln('<fg=black;bg=green>Magento ' . $magentoVersion . ' ' . $edition . ' initialized.</fg=black;bg=green> ' . $ok);
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 147 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
76
        } else {
77
            $consoleOutput->writeln('<fg=black;bg=yellow>Magento is not initialized.</fg=black;bg=yellow>');
78
        }
79
80
        $help = <<<'help'
81
At the prompt, type <comment>help</comment> for some help.
82
83
To exit the shell, type <comment>^D</comment>.
84
help;
85
86
        $consoleOutput->writeln($help);
87
88
        $shell->run($input, $consoleOutput);
89
    }
90
}