Passed
Push — master ( 083ff7...c84473 )
by William
03:02
created

ConsoleShell::buildOptionParser()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
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 5
ccs 0
cts 3
cp 0
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
5
 * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org).
6
 *
7
 * Licensed under The MIT License
8
 * For full copyright and license information, please see the LICENSE.txt
9
 * Redistributions of files must retain the above copyright notice.
10
 *
11
 * @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
12
 * @license   https://opensource.org/licenses/mit-license.php MIT License
13
 *
14
 * @see      http://cakephp.org CakePHP(tm) Project
15
 * @since     3.0.0
16
 */
17
18
namespace App\Shell;
19
20
use Cake\Command\Command;
21
use Cake\Console\Arguments;
22
use Cake\Console\ConsoleIo;
23
use Cake\Console\ConsoleOptionParser;
24
use Cake\Log\Log;
25
use Psy\Shell as PsyShell;
26
use function class_exists;
27
use function restore_error_handler;
28
use function restore_exception_handler;
29
30
/**
31
 * Simple console wrapper around Psy\Shell.
32
 */
33
class ConsoleShell extends Command
34
{
35
    protected const NAME = 'console';
36
37
    /**
38
     * The name of this command.
39
     *
40
     * @var string
41
     */
42
    protected $name = self::NAME;
43
44 21
    public static function defaultName(): string
45
    {
46 21
        return self::NAME;
47
    }
48
49
    protected function buildOptionParser(ConsoleOptionParser $parser): ConsoleOptionParser
50
    {
51
        return $parser
52
            ->setCommand($this->name)
53
            ->setDescription('Open an interactive console');
54
    }
55
56
    /**
57
     * Start the shell and interactive console.
58
     *
59
     * @return int|void
60
     */
61
    public function execute(Arguments $args, ConsoleIo $io)
62
    {
63
        if (! class_exists(PsyShell::class)) {
64
            $io->err('<error>Unable to load Psy\Shell.</error>');
65
            $io->err('');
66
            $io->err('Make sure you have installed psysh as a dependency,');
67
            $io->err('and that Psy\Shell is registered in your autoloader.');
68
            $io->err('');
69
            $io->err('If you are using composer run');
70
            $io->err('');
71
            $io->err('<info>$ php composer.phar require --dev psy/psysh</info>');
72
            $io->err('');
73
74
            return 1;
75
        }
76
77
        $io->out('You can exit with <info>`CTRL-C`</info> or <info>`exit`</info>');
78
        $io->out('');
79
80
        Log::drop('debug');
81
        Log::drop('error');
82
        $io->setLoggers(false);
83
        restore_error_handler();
84
        restore_exception_handler();
85
86
        $psy = new PsyShell();
87
        $psy->run();
88
    }
89
90
    /**
91
     * Display help for this console.
92
     *
93
     * @return ConsoleOptionParser The console option
94
     */
95
    public function getOptionParser(): ConsoleOptionParser
96
    {
97
        $parser = new ConsoleOptionParser($this->name, false);
98
        $parser->setDescription(
99
            'This shell provides a REPL that you can use to interact ' .
100
            'with your application in an interactive fashion. You can use ' .
101
            'it to run adhoc queries with your models, or experiment ' .
102
            'and explore the features of CakePHP and your application.' .
103
            "\n\n" .
104
            'You will need to have psysh installed for this Shell to work.'
105
        );
106
107
        return $parser;
108
    }
109
}
110