Passed
Pull Request — master (#701)
by Stefano
03:22
created

ConsoleCommand::execute()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 29
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 21
c 1
b 0
f 0
dl 0
loc 29
rs 9.584
cc 2
nc 2
nop 2
1
<?php
2
declare(strict_types=1);
3
4
/**
5
 * CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
6
 * Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
7
 *
8
 * Licensed under The MIT License
9
 * For full copyright and license information, please see the LICENSE.txt
10
 * Redistributions of files must retain the above copyright notice.
11
 *
12
 * @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
13
 * @link      https://cakephp.org CakePHP(tm) Project
14
 * @since     3.0.0
15
 * @license   https://opensource.org/licenses/mit-license.php MIT License
16
 */
17
namespace App\Command;
18
19
use Cake\Console\Arguments;
20
use Cake\Console\Command;
21
use Cake\Console\ConsoleIo;
22
use Cake\Console\ConsoleOptionParser;
23
use Cake\Log\Log;
24
use Psy\Configuration as PsyConfiguration;
25
use Psy\Shell as PsyShell;
26
27
/**
28
 * Simple console wrapper around Psy\Shell.
29
 */
30
class ConsoleCommand extends Command
31
{
32
    /**
33
     * Start the Command and interactive console.
34
     *
35
     * @param \Cake\Console\Arguments $args The command arguments.
36
     * @param \Cake\Console\ConsoleIo $io The console io
37
     * @return int|null|void The exit code or null for success
38
     */
39
    public function execute(Arguments $args, ConsoleIo $io)
40
    {
41
        if (!class_exists('Psy\Shell')) {
42
            $io->err('<error>Unable to load Psy\Shell.</error>');
43
            $io->err('');
44
            $io->err('Make sure you have installed psysh as a dependency,');
45
            $io->err('and that Psy\Shell is registered in your autoloader.');
46
            $io->err('');
47
            $io->err('If you are using composer run');
48
            $io->err('');
49
            $io->err('<info>$ php composer.phar require --dev psy/psysh</info>');
50
            $io->err('');
51
52
            return static::CODE_ERROR;
53
        }
54
55
        $io->out('You can exit with <info>`CTRL-C`</info> or <info>`exit`</info>');
56
        $io->out('');
57
58
        Log::drop('debug');
59
        Log::drop('error');
60
        $io->setLoggers(false);
61
        restore_error_handler();
62
        restore_exception_handler();
63
64
        $psy = new PsyShell(new PsyConfiguration([
65
            'updateCheck' => 'never',
66
        ]));
67
        $psy->run();
68
    }
69
70
    /**
71
     * Display help for this console.
72
     *
73
     * @param \Cake\Console\ConsoleOptionParser $parser The parser to update
74
     * @return \Cake\Console\ConsoleOptionParser
75
     */
76
    public function buildOptionParser(ConsoleOptionParser $parser): ConsoleOptionParser
77
    {
78
        $parser->setDescription(
79
            'This shell provides a REPL that you can use to interact with ' .
80
            'your application in a command line designed to run PHP code. ' .
81
            'You can use it to run adhoc queries with your models, or ' .
82
            'explore the features of CakePHP and your application.' .
83
            "\n\n" .
84
            'You will need to have psysh installed for this Shell to work.'
85
        );
86
87
        return $parser;
88
    }
89
}
90