TinkerServerCommand::getCasters()   A
last analyzed

Complexity

Conditions 3
Paths 4

Size

Total Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
nc 4
nop 0
dl 0
loc 16
rs 9.7333
c 0
b 0
f 0
1
<?php
2
3
namespace RedMoon\TinkerServer\Console;
4
5
use Psy\Shell;
6
use Psy\Configuration;
7
use Illuminate\Console\Command;
8
use RedMoon\TinkerServer\Server;
9
use Laravel\Tinker\ClassAliasAutoloader;
10
use Symfony\Component\Console\Output\BufferedOutput;
11
use Symfony\Component\Console\Formatter\OutputFormatterStyle;
12
13
class TinkerServerCommand extends Command
14
{
15
    /** @var string */
16
    protected $signature = 'tinker-server';
17
18
    /**
19
     * Handle Command.
20
     *
21
     * @return void
22
     */
23
    public function handle()
24
    {
25
        $output = $this->createWarningFormatter();
26
        $server = new Server(config('tinker-server.host'), $this->createPsyShell(), $output);
27
        $server->start();
28
    }
29
30
    /**
31
     * Create Warning Formatter.
32
     *
33
     * @return BufferedOutput
34
     */
35
    protected function createWarningFormatter() : BufferedOutput
36
    {
37
        $output = new BufferedOutput();
38
39
        if (! $output->getFormatter()->hasStyle('warning')) {
40
            $style = new OutputFormatterStyle('yellow');
41
            $output->getFormatter()->setStyle('warning', $style);
42
        }
43
44
        return $output;
45
    }
46
47
    /**
48
     * Create PsyShell.
49
     *
50
     * @return Shell
51
     */
52
    protected function createPsyShell() : Shell
53
    {
54
        $config = new Configuration([
55
            'updateCheck' => 'never',
56
        ]);
57
58
        $config->getPresenter()->addCasters(
59
            $this->getCasters()
60
        );
61
62
        $shell = new Shell($config);
63
        $path = $this->getLaravel()->basePath().DIRECTORY_SEPARATOR.'vendor/composer/autoload_classmap.php';
64
65
        ClassAliasAutoloader::register($shell, $path);
66
67
        return $shell;
68
    }
69
70
    /**
71
     * Get Casters.
72
     *
73
     * @return array
74
     */
75
    protected function getCasters() : array
76
    {
77
        $casters = [
78
            'Illuminate\Support\Collection' => 'Laravel\Tinker\TinkerCaster::castCollection',
79
        ];
80
81
        if (class_exists('Illuminate\Database\Eloquent\Model')) {
82
            $casters['Illuminate\Database\Eloquent\Model'] = 'Laravel\Tinker\TinkerCaster::castModel';
83
        }
84
85
        if (class_exists('Illuminate\Foundation\Application')) {
86
            $casters['Illuminate\Foundation\Application'] = 'Laravel\Tinker\TinkerCaster::castApplication';
87
        }
88
89
        return $casters;
90
    }
91
}
92