Subversion-Projekte lars-tiefland.laravel_shop

Revision

Revision 148 | Details | Vergleich mit vorheriger | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
148 lars 1
<?php
2
 
3
namespace Illuminate\Console;
4
 
5
use Illuminate\Console\View\Components\Factory;
6
use Illuminate\Contracts\Console\Isolatable;
7
use Illuminate\Support\Traits\Macroable;
8
use Symfony\Component\Console\Command\Command as SymfonyCommand;
9
use Symfony\Component\Console\Input\InputInterface;
10
use Symfony\Component\Console\Input\InputOption;
11
use Symfony\Component\Console\Output\OutputInterface;
12
 
13
class Command extends SymfonyCommand
14
{
15
    use Concerns\CallsCommands,
16
        Concerns\HasParameters,
17
        Concerns\InteractsWithIO,
18
        Concerns\InteractsWithSignals,
399 lars 19
        Concerns\PromptsForMissingInput,
148 lars 20
        Macroable;
21
 
22
    /**
23
     * The Laravel application instance.
24
     *
25
     * @var \Illuminate\Contracts\Foundation\Application
26
     */
27
    protected $laravel;
28
 
29
    /**
30
     * The name and signature of the console command.
31
     *
32
     * @var string
33
     */
34
    protected $signature;
35
 
36
    /**
37
     * The console command name.
38
     *
39
     * @var string
40
     */
41
    protected $name;
42
 
43
    /**
44
     * The console command description.
45
     *
46
     * @var string|null
47
     */
48
    protected $description;
49
 
50
    /**
51
     * The console command help text.
52
     *
53
     * @var string
54
     */
55
    protected $help;
56
 
57
    /**
58
     * Indicates whether the command should be shown in the Artisan command list.
59
     *
60
     * @var bool
61
     */
62
    protected $hidden = false;
63
 
64
    /**
65
     * Create a new console command instance.
66
     *
67
     * @return void
68
     */
69
    public function __construct()
70
    {
71
        // We will go ahead and set the name, description, and parameters on console
72
        // commands just to make things a little easier on the developer. This is
73
        // so they don't have to all be manually specified in the constructors.
74
        if (isset($this->signature)) {
75
            $this->configureUsingFluentDefinition();
76
        } else {
77
            parent::__construct($this->name);
78
        }
79
 
80
        // Once we have constructed the command, we'll set the description and other
81
        // related properties of the command. If a signature wasn't used to build
82
        // the command we'll set the arguments and the options on this command.
83
        if (! isset($this->description)) {
84
            $this->setDescription((string) static::getDefaultDescription());
85
        } else {
86
            $this->setDescription((string) $this->description);
87
        }
88
 
89
        $this->setHelp((string) $this->help);
90
 
91
        $this->setHidden($this->isHidden());
92
 
93
        if (! isset($this->signature)) {
94
            $this->specifyParameters();
95
        }
96
 
97
        if ($this instanceof Isolatable) {
98
            $this->configureIsolation();
99
        }
100
    }
101
 
102
    /**
103
     * Configure the console command using a fluent definition.
104
     *
105
     * @return void
106
     */
107
    protected function configureUsingFluentDefinition()
108
    {
109
        [$name, $arguments, $options] = Parser::parse($this->signature);
110
 
111
        parent::__construct($this->name = $name);
112
 
113
        // After parsing the signature we will spin through the arguments and options
114
        // and set them on this command. These will already be changed into proper
115
        // instances of these "InputArgument" and "InputOption" Symfony classes.
116
        $this->getDefinition()->addArguments($arguments);
117
        $this->getDefinition()->addOptions($options);
118
    }
119
 
120
    /**
121
     * Configure the console command for isolation.
122
     *
123
     * @return void
124
     */
125
    protected function configureIsolation()
126
    {
127
        $this->getDefinition()->addOption(new InputOption(
128
            'isolated',
129
            null,
130
            InputOption::VALUE_OPTIONAL,
131
            'Do not run the command if another instance of the command is already running',
132
            false
133
        ));
134
    }
135
 
136
    /**
137
     * Run the console command.
138
     *
139
     * @param  \Symfony\Component\Console\Input\InputInterface  $input
140
     * @param  \Symfony\Component\Console\Output\OutputInterface  $output
141
     * @return int
142
     */
143
    public function run(InputInterface $input, OutputInterface $output): int
144
    {
145
        $this->output = $this->laravel->make(
146
            OutputStyle::class, ['input' => $input, 'output' => $output]
147
        );
148
 
149
        $this->components = $this->laravel->make(Factory::class, ['output' => $this->output]);
150
 
151
        try {
152
            return parent::run(
153
                $this->input = $input, $this->output
154
            );
155
        } finally {
156
            $this->untrap();
157
        }
158
    }
159
 
160
    /**
161
     * Execute the console command.
162
     *
163
     * @param  \Symfony\Component\Console\Input\InputInterface  $input
164
     * @param  \Symfony\Component\Console\Output\OutputInterface  $output
165
     * @return int
166
     */
167
    protected function execute(InputInterface $input, OutputInterface $output)
168
    {
169
        if ($this instanceof Isolatable && $this->option('isolated') !== false &&
170
            ! $this->commandIsolationMutex()->create($this)) {
171
            $this->comment(sprintf(
172
                'The [%s] command is already running.', $this->getName()
173
            ));
174
 
175
            return (int) (is_numeric($this->option('isolated'))
176
                        ? $this->option('isolated')
177
                        : self::SUCCESS);
178
        }
179
 
180
        $method = method_exists($this, 'handle') ? 'handle' : '__invoke';
181
 
182
        try {
183
            return (int) $this->laravel->call([$this, $method]);
184
        } finally {
185
            if ($this instanceof Isolatable && $this->option('isolated') !== false) {
186
                $this->commandIsolationMutex()->forget($this);
187
            }
188
        }
189
    }
190
 
191
    /**
192
     * Get a command isolation mutex instance for the command.
193
     *
194
     * @return \Illuminate\Console\CommandMutex
195
     */
196
    protected function commandIsolationMutex()
197
    {
198
        return $this->laravel->bound(CommandMutex::class)
199
            ? $this->laravel->make(CommandMutex::class)
200
            : $this->laravel->make(CacheCommandMutex::class);
201
    }
202
 
203
    /**
204
     * Resolve the console command instance for the given command.
205
     *
206
     * @param  \Symfony\Component\Console\Command\Command|string  $command
207
     * @return \Symfony\Component\Console\Command\Command
208
     */
209
    protected function resolveCommand($command)
210
    {
211
        if (! class_exists($command)) {
212
            return $this->getApplication()->find($command);
213
        }
214
 
215
        $command = $this->laravel->make($command);
216
 
217
        if ($command instanceof SymfonyCommand) {
218
            $command->setApplication($this->getApplication());
219
        }
220
 
221
        if ($command instanceof self) {
222
            $command->setLaravel($this->getLaravel());
223
        }
224
 
225
        return $command;
226
    }
227
 
228
    /**
229
     * {@inheritdoc}
230
     *
231
     * @return bool
232
     */
233
    public function isHidden(): bool
234
    {
235
        return $this->hidden;
236
    }
237
 
238
    /**
239
     * {@inheritdoc}
240
     */
241
    public function setHidden(bool $hidden = true): static
242
    {
243
        parent::setHidden($this->hidden = $hidden);
244
 
245
        return $this;
246
    }
247
 
248
    /**
249
     * Get the Laravel application instance.
250
     *
251
     * @return \Illuminate\Contracts\Foundation\Application
252
     */
253
    public function getLaravel()
254
    {
255
        return $this->laravel;
256
    }
257
 
258
    /**
259
     * Set the Laravel application instance.
260
     *
261
     * @param  \Illuminate\Contracts\Container\Container  $laravel
262
     * @return void
263
     */
264
    public function setLaravel($laravel)
265
    {
266
        $this->laravel = $laravel;
267
    }
268
}