Subversion-Projekte lars-tiefland.laravel_shop

Revision

Zur aktuellen Revision | Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
148 lars 1
<?php
2
 
3
namespace Laravel\Tinker\Console;
4
 
5
use Illuminate\Console\Command;
6
use Illuminate\Support\Env;
7
use Laravel\Tinker\ClassAliasAutoloader;
8
use Psy\Configuration;
9
use Psy\Shell;
10
use Psy\VersionUpdater\Checker;
11
use Symfony\Component\Console\Input\InputArgument;
12
use Symfony\Component\Console\Input\InputOption;
13
 
14
class TinkerCommand extends Command
15
{
16
    /**
17
     * Artisan commands to include in the tinker shell.
18
     *
19
     * @var array
20
     */
21
    protected $commandWhitelist = [
22
        'clear-compiled', 'down', 'env', 'inspire', 'migrate', 'migrate:install', 'optimize', 'up',
23
    ];
24
 
25
    /**
26
     * The console command name.
27
     *
28
     * @var string
29
     */
30
    protected $name = 'tinker';
31
 
32
    /**
33
     * The console command description.
34
     *
35
     * @var string
36
     */
37
    protected $description = 'Interact with your application';
38
 
39
    /**
40
     * Execute the console command.
41
     *
42
     * @return int
43
     */
44
    public function handle()
45
    {
46
        $this->getApplication()->setCatchExceptions(false);
47
 
48
        $config = Configuration::fromInput($this->input);
49
        $config->setUpdateCheck(Checker::NEVER);
50
 
51
        $config->getPresenter()->addCasters(
52
            $this->getCasters()
53
        );
54
 
55
        if ($this->option('execute')) {
56
            $config->setRawOutput(true);
57
        }
58
 
59
        $shell = new Shell($config);
60
        $shell->addCommands($this->getCommands());
61
        $shell->setIncludes($this->argument('include'));
62
 
63
        $path = Env::get('COMPOSER_VENDOR_DIR', $this->getLaravel()->basePath().DIRECTORY_SEPARATOR.'vendor');
64
 
65
        $path .= '/composer/autoload_classmap.php';
66
 
67
        $config = $this->getLaravel()->make('config');
68
 
69
        $loader = ClassAliasAutoloader::register(
70
            $shell, $path, $config->get('tinker.alias', []), $config->get('tinker.dont_alias', [])
71
        );
72
 
73
        if ($code = $this->option('execute')) {
74
            try {
75
                $shell->setOutput($this->output);
76
                $shell->execute($code);
77
            } finally {
78
                $loader->unregister();
79
            }
80
 
81
            return 0;
82
        }
83
 
84
        try {
85
            return $shell->run();
86
        } finally {
87
            $loader->unregister();
88
        }
89
    }
90
 
91
    /**
92
     * Get artisan commands to pass through to PsySH.
93
     *
94
     * @return array
95
     */
96
    protected function getCommands()
97
    {
98
        $commands = [];
99
 
100
        foreach ($this->getApplication()->all() as $name => $command) {
101
            if (in_array($name, $this->commandWhitelist)) {
102
                $commands[] = $command;
103
            }
104
        }
105
 
106
        $config = $this->getLaravel()->make('config');
107
 
108
        foreach ($config->get('tinker.commands', []) as $command) {
109
            $commands[] = $this->getApplication()->add(
110
                $this->getLaravel()->make($command)
111
            );
112
        }
113
 
114
        return $commands;
115
    }
116
 
117
    /**
118
     * Get an array of Laravel tailored casters.
119
     *
120
     * @return array
121
     */
122
    protected function getCasters()
123
    {
124
        $casters = [
125
            'Illuminate\Support\Collection' => 'Laravel\Tinker\TinkerCaster::castCollection',
126
            'Illuminate\Support\HtmlString' => 'Laravel\Tinker\TinkerCaster::castHtmlString',
127
            'Illuminate\Support\Stringable' => 'Laravel\Tinker\TinkerCaster::castStringable',
128
        ];
129
 
130
        if (class_exists('Illuminate\Database\Eloquent\Model')) {
131
            $casters['Illuminate\Database\Eloquent\Model'] = 'Laravel\Tinker\TinkerCaster::castModel';
132
        }
133
 
134
        if (class_exists('Illuminate\Foundation\Application')) {
135
            $casters['Illuminate\Foundation\Application'] = 'Laravel\Tinker\TinkerCaster::castApplication';
136
        }
137
 
138
        $config = $this->getLaravel()->make('config');
139
 
140
        return array_merge($casters, (array) $config->get('tinker.casters', []));
141
    }
142
 
143
    /**
144
     * Get the console command arguments.
145
     *
146
     * @return array
147
     */
148
    protected function getArguments()
149
    {
150
        return [
151
            ['include', InputArgument::IS_ARRAY, 'Include file(s) before starting tinker'],
152
        ];
153
    }
154
 
155
    /**
156
     * Get the console command options.
157
     *
158
     * @return array
159
     */
160
    protected function getOptions()
161
    {
162
        return [
163
            ['execute', null, InputOption::VALUE_OPTIONAL, 'Execute the given code using Tinker'],
164
        ];
165
    }
166
}