Completed
Push — master ( 0f4882...0b6865 )
by Roberto
03:52
created

TinkerServerCommand::getCasters()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
nc 4
nop 0
dl 0
loc 14
rs 9.7998
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 Laravel\Tinker\ClassAliasAutoloader;
9
use RedMoon\TinkerServer\Server;
10
use Symfony\Component\Console\Output\BufferedOutput;
11
use Symfony\Component\Console\Formatter\OutputFormatterStyle;
12
13
class TinkerServerCommand extends Command
14
{
15
    protected $signature = 'tinker-server';
16
17
    public function handle()
18
    {
19
        $output = $this->createWarningFormatter();
20
21
        $server = new Server(config('tinker-server.host'), $this->createPsyShell(), $output);
22
23
        $server->start();
24
    }
25
26
    protected function createWarningFormatter(): BufferedOutput
27
    {
28
        $output = new BufferedOutput();
29
30
        if (! $output->getFormatter()->hasStyle('warning')) {
31
            $style = new OutputFormatterStyle('yellow');
32
33
            $output->getFormatter()->setStyle('warning', $style);
34
        }
35
36
        return $output;
37
    }
38
39
    protected function createPsyShell()
40
    {
41
        $config = new Configuration([
42
            'updateCheck' => 'never',
43
        ]);
44
45
        $config->getPresenter()->addCasters(
46
            $this->getCasters()
47
        );
48
49
        $shell = new Shell($config);
50
51
        $path = $this->getLaravel()->basePath().DIRECTORY_SEPARATOR.'vendor/composer/autoload_classmap.php';
52
53
        ClassAliasAutoloader::register($shell, $path);
54
55
        return $shell;
56
    }
57
58
    protected function getCasters()
59
    {
60
        $casters = [
61
            'Illuminate\Support\Collection' => 'Laravel\Tinker\TinkerCaster::castCollection',
62
        ];
63
        if (class_exists('Illuminate\Database\Eloquent\Model')) {
64
            $casters['Illuminate\Database\Eloquent\Model'] = 'Laravel\Tinker\TinkerCaster::castModel';
65
        }
66
        if (class_exists('Illuminate\Foundation\Application')) {
67
            $casters['Illuminate\Foundation\Application'] = 'Laravel\Tinker\TinkerCaster::castApplication';
68
        }
69
70
        return $casters;
71
    }
72
}
73