ConsoleShell::execute()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 27
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
cc 2
eloc 20
nc 2
nop 2
dl 0
loc 27
ccs 0
cts 21
cp 0
crap 6
rs 9.6
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
27
use function class_exists;
28
use function restore_error_handler;
29
use function restore_exception_handler;
30
31
/**
32
 * Simple console wrapper around Psy\Shell.
33
 */
34
class ConsoleShell extends Command
35
{
36
    protected const NAME = 'console';
37
38
    /**
39
     * The name of this command.
40
     *
41
     * @var string
42
     */
43
    protected $name = self::NAME;
44
45 21
    public static function defaultName(): string
46
    {
47 21
        return self::NAME;
48
    }
49
50
    protected function buildOptionParser(ConsoleOptionParser $parser): ConsoleOptionParser
51
    {
52
        return $parser
53
            ->setCommand($this->name)
54
            ->setDescription('Open an interactive console');
55
    }
56
57
    /**
58
     * Start the shell and interactive console.
59
     *
60
     * @return int|void
61
     */
62
    public function execute(Arguments $args, ConsoleIo $io)
63
    {
64
        if (! class_exists(PsyShell::class)) {
65
            $io->err('<error>Unable to load Psy\Shell.</error>');
66
            $io->err('');
67
            $io->err('Make sure you have installed psysh as a dependency,');
68
            $io->err('and that Psy\Shell is registered in your autoloader.');
69
            $io->err('');
70
            $io->err('If you are using composer run');
71
            $io->err('');
72
            $io->err('<info>$ php composer.phar require --dev psy/psysh</info>');
73
            $io->err('');
74
75
            return 1;
76
        }
77
78
        $io->out('You can exit with <info>`CTRL-C`</info> or <info>`exit`</info>');
79
        $io->out('');
80
81
        Log::drop('debug');
82
        Log::drop('error');
83
        $io->setLoggers(false);
84
        restore_error_handler();
85
        restore_exception_handler();
86
87
        $psy = new PsyShell();
88
        $psy->run();
89
    }
90
91
    /**
92
     * Display help for this console.
93
     *
94
     * @return ConsoleOptionParser The console option
95
     */
96
    public function getOptionParser(): ConsoleOptionParser
97
    {
98
        $parser = new ConsoleOptionParser($this->name, false);
99
        $parser->setDescription(
100
            'This shell provides a REPL that you can use to interact ' .
101
            'with your application in an interactive fashion. You can use ' .
102
            'it to run adhoc queries with your models, or experiment ' .
103
            'and explore the features of CakePHP and your application.' .
104
            "\n\n" .
105
            'You will need to have psysh installed for this Shell to work.'
106
        );
107
108
        return $parser;
109
    }
110
}
111