Subversion-Projekte lars-tiefland.laravel_shop

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
148 lars 1
<?php
2
 
3
namespace Illuminate\Console;
4
 
5
use Closure;
6
use Illuminate\Console\Events\ArtisanStarting;
7
use Illuminate\Console\Events\CommandFinished;
8
use Illuminate\Console\Events\CommandStarting;
9
use Illuminate\Contracts\Console\Application as ApplicationContract;
10
use Illuminate\Contracts\Container\Container;
11
use Illuminate\Contracts\Events\Dispatcher;
12
use Illuminate\Support\ProcessUtils;
13
use Symfony\Component\Console\Application as SymfonyApplication;
14
use Symfony\Component\Console\Command\Command as SymfonyCommand;
15
use Symfony\Component\Console\Exception\CommandNotFoundException;
16
use Symfony\Component\Console\Input\ArgvInput;
17
use Symfony\Component\Console\Input\ArrayInput;
18
use Symfony\Component\Console\Input\InputDefinition;
19
use Symfony\Component\Console\Input\InputInterface;
20
use Symfony\Component\Console\Input\InputOption;
21
use Symfony\Component\Console\Input\StringInput;
22
use Symfony\Component\Console\Output\BufferedOutput;
23
use Symfony\Component\Console\Output\OutputInterface;
24
use Symfony\Component\Process\PhpExecutableFinder;
25
 
26
class Application extends SymfonyApplication implements ApplicationContract
27
{
28
    /**
29
     * The Laravel application instance.
30
     *
31
     * @var \Illuminate\Contracts\Container\Container
32
     */
33
    protected $laravel;
34
 
35
    /**
36
     * The event dispatcher instance.
37
     *
38
     * @var \Illuminate\Contracts\Events\Dispatcher
39
     */
40
    protected $events;
41
 
42
    /**
43
     * The output from the previous command.
44
     *
45
     * @var \Symfony\Component\Console\Output\BufferedOutput
46
     */
47
    protected $lastOutput;
48
 
49
    /**
50
     * The console application bootstrappers.
51
     *
52
     * @var array
53
     */
54
    protected static $bootstrappers = [];
55
 
56
    /**
57
     * A map of command names to classes.
58
     *
59
     * @var array
60
     */
61
    protected $commandMap = [];
62
 
63
    /**
64
     * Create a new Artisan console application.
65
     *
66
     * @param  \Illuminate\Contracts\Container\Container  $laravel
67
     * @param  \Illuminate\Contracts\Events\Dispatcher  $events
68
     * @param  string  $version
69
     * @return void
70
     */
71
    public function __construct(Container $laravel, Dispatcher $events, $version)
72
    {
73
        parent::__construct('Laravel Framework', $version);
74
 
75
        $this->laravel = $laravel;
76
        $this->events = $events;
77
        $this->setAutoExit(false);
78
        $this->setCatchExceptions(false);
79
 
80
        $this->events->dispatch(new ArtisanStarting($this));
81
 
82
        $this->bootstrap();
83
    }
84
 
85
    /**
86
     * {@inheritdoc}
87
     *
88
     * @return int
89
     */
90
    public function run(InputInterface $input = null, OutputInterface $output = null): int
91
    {
92
        $commandName = $this->getCommandName(
93
            $input = $input ?: new ArgvInput
94
        );
95
 
96
        $this->events->dispatch(
97
            new CommandStarting(
98
                $commandName, $input, $output = $output ?: new BufferedConsoleOutput
99
            )
100
        );
101
 
102
        $exitCode = parent::run($input, $output);
103
 
104
        $this->events->dispatch(
105
            new CommandFinished($commandName, $input, $output, $exitCode)
106
        );
107
 
108
        return $exitCode;
109
    }
110
 
111
    /**
112
     * Determine the proper PHP executable.
113
     *
114
     * @return string
115
     */
116
    public static function phpBinary()
117
    {
118
        return ProcessUtils::escapeArgument((new PhpExecutableFinder)->find(false));
119
    }
120
 
121
    /**
122
     * Determine the proper Artisan executable.
123
     *
124
     * @return string
125
     */
126
    public static function artisanBinary()
127
    {
128
        return ProcessUtils::escapeArgument(defined('ARTISAN_BINARY') ? ARTISAN_BINARY : 'artisan');
129
    }
130
 
131
    /**
132
     * Format the given command as a fully-qualified executable command.
133
     *
134
     * @param  string  $string
135
     * @return string
136
     */
137
    public static function formatCommandString($string)
138
    {
139
        return sprintf('%s %s %s', static::phpBinary(), static::artisanBinary(), $string);
140
    }
141
 
142
    /**
143
     * Register a console "starting" bootstrapper.
144
     *
145
     * @param  \Closure  $callback
146
     * @return void
147
     */
148
    public static function starting(Closure $callback)
149
    {
150
        static::$bootstrappers[] = $callback;
151
    }
152
 
153
    /**
154
     * Bootstrap the console application.
155
     *
156
     * @return void
157
     */
158
    protected function bootstrap()
159
    {
160
        foreach (static::$bootstrappers as $bootstrapper) {
161
            $bootstrapper($this);
162
        }
163
    }
164
 
165
    /**
166
     * Clear the console application bootstrappers.
167
     *
168
     * @return void
169
     */
170
    public static function forgetBootstrappers()
171
    {
172
        static::$bootstrappers = [];
173
    }
174
 
175
    /**
176
     * Run an Artisan console command by name.
177
     *
178
     * @param  string  $command
179
     * @param  array  $parameters
180
     * @param  \Symfony\Component\Console\Output\OutputInterface|null  $outputBuffer
181
     * @return int
182
     *
183
     * @throws \Symfony\Component\Console\Exception\CommandNotFoundException
184
     */
185
    public function call($command, array $parameters = [], $outputBuffer = null)
186
    {
187
        [$command, $input] = $this->parseCommand($command, $parameters);
188
 
189
        if (! $this->has($command)) {
190
            throw new CommandNotFoundException(sprintf('The command "%s" does not exist.', $command));
191
        }
192
 
193
        return $this->run(
194
            $input, $this->lastOutput = $outputBuffer ?: new BufferedOutput
195
        );
196
    }
197
 
198
    /**
199
     * Parse the incoming Artisan command and its input.
200
     *
201
     * @param  string  $command
202
     * @param  array  $parameters
203
     * @return array
204
     */
205
    protected function parseCommand($command, $parameters)
206
    {
207
        if (is_subclass_of($command, SymfonyCommand::class)) {
208
            $callingClass = true;
209
 
210
            $command = $this->laravel->make($command)->getName();
211
        }
212
 
213
        if (! isset($callingClass) && empty($parameters)) {
214
            $command = $this->getCommandName($input = new StringInput($command));
215
        } else {
216
            array_unshift($parameters, $command);
217
 
218
            $input = new ArrayInput($parameters);
219
        }
220
 
221
        return [$command, $input];
222
    }
223
 
224
    /**
225
     * Get the output for the last run command.
226
     *
227
     * @return string
228
     */
229
    public function output()
230
    {
231
        return $this->lastOutput && method_exists($this->lastOutput, 'fetch')
232
                        ? $this->lastOutput->fetch()
233
                        : '';
234
    }
235
 
236
    /**
237
     * Add a command to the console.
238
     *
239
     * @param  \Symfony\Component\Console\Command\Command  $command
240
     * @return \Symfony\Component\Console\Command\Command
241
     */
242
    public function add(SymfonyCommand $command)
243
    {
244
        if ($command instanceof Command) {
245
            $command->setLaravel($this->laravel);
246
        }
247
 
248
        return $this->addToParent($command);
249
    }
250
 
251
    /**
252
     * Add the command to the parent instance.
253
     *
254
     * @param  \Symfony\Component\Console\Command\Command  $command
255
     * @return \Symfony\Component\Console\Command\Command
256
     */
257
    protected function addToParent(SymfonyCommand $command)
258
    {
259
        return parent::add($command);
260
    }
261
 
262
    /**
263
     * Add a command, resolving through the application.
264
     *
265
     * @param  \Illuminate\Console\Command|string  $command
266
     * @return \Symfony\Component\Console\Command\Command|null
267
     */
268
    public function resolve($command)
269
    {
270
        if (is_subclass_of($command, SymfonyCommand::class) && ($commandName = $command::getDefaultName())) {
271
            $this->commandMap[$commandName] = $command;
272
 
273
            return null;
274
        }
275
 
276
        if ($command instanceof Command) {
277
            return $this->add($command);
278
        }
279
 
280
        return $this->add($this->laravel->make($command));
281
    }
282
 
283
    /**
284
     * Resolve an array of commands through the application.
285
     *
286
     * @param  array|mixed  $commands
287
     * @return $this
288
     */
289
    public function resolveCommands($commands)
290
    {
291
        $commands = is_array($commands) ? $commands : func_get_args();
292
 
293
        foreach ($commands as $command) {
294
            $this->resolve($command);
295
        }
296
 
297
        return $this;
298
    }
299
 
300
    /**
301
     * Set the container command loader for lazy resolution.
302
     *
303
     * @return $this
304
     */
305
    public function setContainerCommandLoader()
306
    {
307
        $this->setCommandLoader(new ContainerCommandLoader($this->laravel, $this->commandMap));
308
 
309
        return $this;
310
    }
311
 
312
    /**
313
     * Get the default input definition for the application.
314
     *
315
     * This is used to add the --env option to every available command.
316
     *
317
     * @return \Symfony\Component\Console\Input\InputDefinition
318
     */
319
    protected function getDefaultInputDefinition(): InputDefinition
320
    {
321
        return tap(parent::getDefaultInputDefinition(), function ($definition) {
322
            $definition->addOption($this->getEnvironmentOption());
323
        });
324
    }
325
 
326
    /**
327
     * Get the global environment option for the definition.
328
     *
329
     * @return \Symfony\Component\Console\Input\InputOption
330
     */
331
    protected function getEnvironmentOption()
332
    {
333
        $message = 'The environment the command should run under';
334
 
335
        return new InputOption('--env', null, InputOption::VALUE_OPTIONAL, $message);
336
    }
337
 
338
    /**
339
     * Get the Laravel application instance.
340
     *
341
     * @return \Illuminate\Contracts\Foundation\Application
342
     */
343
    public function getLaravel()
344
    {
345
        return $this->laravel;
346
    }
347
}