-
-
Notifications
You must be signed in to change notification settings - Fork 41
/
Commander.php
188 lines (160 loc) · 4.92 KB
/
Commander.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
<?php
namespace Orchestra\Testbench\Console;
use Illuminate\Contracts\Console\Kernel as ConsoleKernel;
use Illuminate\Contracts\Debug\ExceptionHandler;
use Illuminate\Foundation\Application as LaravelApplication;
use Orchestra\Testbench\Foundation\Application;
use Orchestra\Testbench\Foundation\Bootstrap\LoadMigrationsFromArray;
use Orchestra\Testbench\Foundation\Config;
use Orchestra\Testbench\Foundation\TestbenchServiceProvider;
use Orchestra\Testbench\Workbench\Workbench;
use Symfony\Component\Console\Application as ConsoleApplication;
use Symfony\Component\Console\Input\ArgvInput;
use Symfony\Component\Console\Output\ConsoleOutput;
use Symfony\Component\Console\Output\OutputInterface;
use Throwable;
use function Orchestra\Testbench\transform_relative_path;
class Commander
{
/**
* Application instance.
*
* @var \Illuminate\Foundation\Application
*/
protected $app;
/**
* List of configurations.
*
* @var \Orchestra\Testbench\Foundation\Config
*/
protected $config;
/**
* Working path.
*
* @var string
*/
protected $workingPath;
/**
* The environment file name.
*
* @var string
*/
protected $environmentFile = '.env';
/**
* Construct a new Commander.
*
* @param array|\Orchestra\Testbench\Foundation\Config $config
* @param string $workingPath
*/
public function __construct($config, string $workingPath)
{
$this->config = $config instanceof Config ? $config : new Config($config);
$this->workingPath = $workingPath;
}
/**
* Handle the command.
*
* @return void
*/
public function handle()
{
$input = new ArgvInput();
$output = new ConsoleOutput();
try {
$laravel = $this->laravel();
$kernel = $laravel->make(ConsoleKernel::class);
$status = $kernel->handle($input, $output);
$kernel->terminate($input, $status);
} catch (Throwable $error) {
$status = $this->handleException($output, $error);
} finally {
Workbench::flush();
}
exit($status);
}
/**
* Create Laravel application.
*
* @return \Illuminate\Foundation\Application
*/
public function laravel()
{
if (! $this->app instanceof LaravelApplication) {
$laravelBasePath = $this->getBasePath();
Application::createVendorSymlink($laravelBasePath, $this->workingPath.'/vendor');
$hasEnvironmentFile = file_exists("{$laravelBasePath}/.env");
$options = array_filter([
'load_environment_variables' => $hasEnvironmentFile,
'extra' => $this->config->getExtraAttributes(),
]);
$this->app = Application::create(
$this->getBasePath(),
function ($app) {
Workbench::start($app, $this->config);
Workbench::discoverRoutes($app, $this->config);
(new LoadMigrationsFromArray(
$this->config['migrations'] ?? [],
$this->config['seeders'] ?? false,
))->bootstrap($app);
\call_user_func($this->resolveApplicationCallback(), $app);
},
$options
);
}
return $this->app;
}
/**
* Resolve application implementation.
*
* @return \Closure
*/
protected function resolveApplicationCallback()
{
return static function ($app) {
$app->register(TestbenchServiceProvider::class);
};
}
/**
* Get base path.
*
* @return string
*/
protected function getBasePath()
{
$path = $this->config['laravel'] ?? null;
if (! \is_null($path)) {
return tap(transform_relative_path($path, $this->workingPath), static function ($path) {
$_ENV['APP_BASE_PATH'] = $path;
});
}
return static::applicationBasePath();
}
/**
* Get Application base path.
*
* @return string
*/
public static function applicationBasePath()
{
return Application::applicationBasePath();
}
/**
* Render an exception to the console.
*
* @param \Symfony\Component\Console\Output\OutputInterface $output
* @param \Throwable $error
* @return int
*/
protected function handleException(OutputInterface $output, Throwable $error)
{
if ($this->app instanceof LaravelApplication) {
tap($this->app->make(ExceptionHandler::class), static function ($handler) use ($error, $output) {
$handler->report($error);
$handler->renderForConsole($output, $error);
});
} else {
(new ConsoleApplication)->renderThrowable($error, $output);
}
return 1;
}
}