Projekt

Obecné

Profil

Stáhnout (4.45 KB) Statistiky
| Větev: | Revize:
1 cb15593b Cajova-Houba
<?php
2
3
namespace Illuminate\Console;
4
5
use Illuminate\Contracts\Events\Dispatcher;
6
use Illuminate\Contracts\Container\Container;
7
use Symfony\Component\Console\Input\ArrayInput;
8
use Symfony\Component\Console\Input\InputOption;
9
use Symfony\Component\Console\Output\BufferedOutput;
10
use Symfony\Component\Console\Application as SymfonyApplication;
11
use Symfony\Component\Console\Command\Command as SymfonyCommand;
12
use Illuminate\Contracts\Console\Application as ApplicationContract;
13
14
class Application extends SymfonyApplication implements ApplicationContract
15
{
16
    /**
17
     * The Laravel application instance.
18
     *
19
     * @var \Illuminate\Contracts\Container\Container
20
     */
21
    protected $laravel;
22
23
    /**
24
     * The output from the previous command.
25
     *
26
     * @var \Symfony\Component\Console\Output\BufferedOutput
27
     */
28
    protected $lastOutput;
29
30
    /**
31
     * Create a new Artisan console application.
32
     *
33
     * @param  \Illuminate\Contracts\Container\Container  $laravel
34
     * @param  \Illuminate\Contracts\Events\Dispatcher  $events
35
     * @param  string  $version
36
     * @return void
37
     */
38
    public function __construct(Container $laravel, Dispatcher $events, $version)
39
    {
40
        parent::__construct('Laravel Framework', $version);
41
42
        $this->laravel = $laravel;
43
        $this->setAutoExit(false);
44
        $this->setCatchExceptions(false);
45
46
        $events->fire(new Events\ArtisanStarting($this));
47
    }
48
49
    /**
50
     * Run an Artisan console command by name.
51
     *
52
     * @param  string  $command
53
     * @param  array  $parameters
54
     * @return int
55
     */
56
    public function call($command, array $parameters = [])
57
    {
58
        $parameters = collect($parameters)->prepend($command);
59
60
        $this->lastOutput = new BufferedOutput;
61
62
        $this->setCatchExceptions(false);
63
64
        $result = $this->run(new ArrayInput($parameters->toArray()), $this->lastOutput);
65
66
        $this->setCatchExceptions(true);
67
68
        return $result;
69
    }
70
71
    /**
72
     * Get the output for the last run command.
73
     *
74
     * @return string
75
     */
76
    public function output()
77
    {
78
        return $this->lastOutput ? $this->lastOutput->fetch() : '';
79
    }
80
81
    /**
82
     * Add a command to the console.
83
     *
84
     * @param  \Symfony\Component\Console\Command\Command  $command
85
     * @return \Symfony\Component\Console\Command\Command
86
     */
87
    public function add(SymfonyCommand $command)
88
    {
89
        if ($command instanceof Command) {
90
            $command->setLaravel($this->laravel);
91
        }
92
93
        return $this->addToParent($command);
94
    }
95
96
    /**
97
     * Add the command to the parent instance.
98
     *
99
     * @param  \Symfony\Component\Console\Command\Command  $command
100
     * @return \Symfony\Component\Console\Command\Command
101
     */
102
    protected function addToParent(SymfonyCommand $command)
103
    {
104
        return parent::add($command);
105
    }
106
107
    /**
108
     * Add a command, resolving through the application.
109
     *
110
     * @param  string  $command
111
     * @return \Symfony\Component\Console\Command\Command
112
     */
113
    public function resolve($command)
114
    {
115
        return $this->add($this->laravel->make($command));
116
    }
117
118
    /**
119
     * Resolve an array of commands through the application.
120
     *
121
     * @param  array|mixed  $commands
122
     * @return $this
123
     */
124
    public function resolveCommands($commands)
125
    {
126
        $commands = is_array($commands) ? $commands : func_get_args();
127
128
        foreach ($commands as $command) {
129
            $this->resolve($command);
130
        }
131
132
        return $this;
133
    }
134
135
    /**
136
     * Get the default input definitions for the applications.
137
     *
138
     * This is used to add the --env option to every available command.
139
     *
140
     * @return \Symfony\Component\Console\Input\InputDefinition
141
     */
142
    protected function getDefaultInputDefinition()
143
    {
144
        $definition = parent::getDefaultInputDefinition();
145
146
        $definition->addOption($this->getEnvironmentOption());
147
148
        return $definition;
149
    }
150
151
    /**
152
     * Get the global environment option for the definition.
153
     *
154
     * @return \Symfony\Component\Console\Input\InputOption
155
     */
156
    protected function getEnvironmentOption()
157
    {
158
        $message = 'The environment the command should run under.';
159
160
        return new InputOption('--env', null, InputOption::VALUE_OPTIONAL, $message);
161
    }
162
163
    /**
164
     * Get the Laravel application instance.
165
     *
166
     * @return \Illuminate\Contracts\Foundation\Application
167
     */
168
    public function getLaravel()
169
    {
170
        return $this->laravel;
171
    }
172
}