-
Notifications
You must be signed in to change notification settings - Fork 0
/
Bootloader.php
393 lines (328 loc) · 10.3 KB
/
Bootloader.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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
<?php
namespace Roots\Acorn;
use Illuminate\Contracts\Foundation\Application as ApplicationContract;
use Illuminate\Support\Env;
use Illuminate\Support\Str;
use Roots\Acorn\Filesystem\Filesystem;
use function get_theme_file_path;
class Bootloader
{
/**
* Bootloader instance
*
* @var static
*/
protected static $instance;
/**
* Application instance
*
* @var \Illuminate\Contracts\Foundation\Application
*/
protected $app;
/**
* Base path for the application
*
* @var string
*/
protected $basePath;
/**
* The prefixes of absolute cache paths for use during normalization.
*
* @var string[]
*/
protected $absoluteApplicationPathPrefixes = ['/', '\\'];
/**
* Set the Bootloader instance
*
* @param Bootloader $bootloader
*/
public static function setInstance(?self $bootloader)
{
static::$instance = $bootloader;
}
/**
* Get the Bootloader instance
*
* @param \Illuminate\Contracts\Foundation\Application $app
* @return static
*/
public static function getInstance(?ApplicationContract $app = null)
{
return static::$instance ??= new static($app);
}
/**
* Create a new bootloader instance.
*
* @param \Illuminate\Contracts\Foundation\Application $app
*/
public function __construct(?ApplicationContract $app = null)
{
$this->app = $app;
static::$instance ??= $this;
}
/**
* Boot the Application.
*
* @return void
*/
public function __invoke()
{
$this->boot();
}
/**
* Boot the Application.
*
* @param callable $callback
* @return void
*/
public function boot($callback = null)
{
if (! defined('LARAVEL_START')) {
define('LARAVEL_START', microtime(true));
}
$app = $this->getApplication();
if ($callback) {
return $callback($app);
}
if ($app->hasBeenBootstrapped()) {
return;
}
if ($app->runningInConsole()) {
$this->enableHttpsInConsole();
return class_exists('WP_CLI') ? $this->bootWpCli($app) : $this->bootConsole($app);
}
if (Env::get('ACORN_ENABLE_EXPIRIMENTAL_ROUTER')) {
$app->singleton(
\Illuminate\Contracts\Http\Kernel::class,
\Roots\Acorn\Http\Kernel::class
);
return $this->bootHttp($app);
}
return $this->bootWordPress($app);
}
/**
* Enable $_SERVER[HTTPS] in a console environment.
*
* @return void
*/
protected function enableHttpsInConsole()
{
$enable = apply_filters('acorn/enable_https_in_console', parse_url(get_option('home'), PHP_URL_SCHEME) === 'https');
if ($enable) {
$_SERVER['HTTPS'] = 'on';
}
}
/**
* Boot the Application for console.
*
* @param ApplicationContract $app
* @return void
*/
protected function bootConsole(ApplicationContract $app)
{
$kernel = $app->make(\Illuminate\Contracts\Console\Kernel::class);
$status = $kernel->handle(
$input = new \Symfony\Component\Console\Input\ArgvInput(),
new \Symfony\Component\Console\Output\ConsoleOutput()
);
$kernel->terminate($input, $status);
exit($status);
}
/**
* Boot the Application for wp-cli.
*
* @param ApplicationContract $app
* @return void
*/
protected function bootWpCli(ApplicationContract $app)
{
\WP_CLI::add_command('acorn', function ($args, $assoc_args) use ($app) {
$kernel = $app->make(\Illuminate\Contracts\Console\Kernel::class);
$kernel->commands();
$command = implode(' ', $args);
foreach ($assoc_args as $key => $value) {
$command .= " --{$key}";
if ($value !== true) {
$command .= "='{$value}'";
}
}
$command = str_replace('\\', '\\\\', $command);
$status = $kernel->handle(
$input = new \Symfony\Component\Console\Input\StringInput($command),
new \Symfony\Component\Console\Output\ConsoleOutput()
);
$kernel->terminate($input, $status);
\WP_CLI::halt($status);
});
}
/**
* Boot the Application for HTTP requests.
*
* @param ApplicationContract $app
* @return void
*/
protected function bootHttp(ApplicationContract $app)
{
$kernel = $app->make(\Illuminate\Contracts\Http\Kernel::class);
$response = tap($kernel->handle(
$request = \Illuminate\Http\Request::capture()
))->send();
$kernel->terminate($request, $response);
}
/**
* Boot the Application for WordPress requests.
*
* @param ApplicationContract $app
* @return void
*/
protected function bootWordPress(ApplicationContract $app)
{
$app->make(\Illuminate\Contracts\Http\Kernel::class)
->handle(\Illuminate\Http\Request::capture());
}
/**
* Get Application instance.
*
* @param ApplicationContract $app
* @return \Illuminate\Contracts\Foundation\Application
*/
public function getApplication(): ApplicationContract
{
$this->app ??= new Application($this->basePath(), $this->usePaths());
$this->app->singleton(
\Illuminate\Contracts\Http\Kernel::class,
\Roots\Acorn\Kernel::class
);
$this->app->singleton(
\Illuminate\Contracts\Console\Kernel::class,
\Roots\Acorn\Console\Kernel::class
);
$this->app->singleton(
\Illuminate\Contracts\Debug\ExceptionHandler::class,
\Roots\Acorn\Exceptions\Handler::class
);
if (class_exists(\Whoops\Run::class)) {
$this->app->bind(
\Illuminate\Contracts\Foundation\ExceptionRenderer::class,
fn (\Illuminate\Contracts\Foundation\Application $app) =>
$app->make(\Roots\Acorn\Exceptions\Whoops\WhoopsExceptionRenderer::class)
);
}
return $this->app;
}
/**
* Get the application basepath
*
* @return string
*/
protected function basePath(): string
{
if ($this->basePath) {
return $this->basePath;
}
return $this->basePath = match (true) {
isset($_ENV['APP_BASE_PATH']) => $_ENV['APP_BASE_PATH'],
defined('ACORN_BASEPATH') => constant('ACORN_BASEPATH'),
is_file($composer_path = get_theme_file_path('composer.json')) => dirname($composer_path),
is_dir($app_path = get_theme_file_path('app')) => dirname($app_path),
$vendor_path = (new Filesystem())->closest(dirname(__DIR__, 4), 'composer.json') => dirname($vendor_path),
default => dirname(__DIR__, 3)
};
}
/**
* Use paths that are configurable by the developer.
*
* @return array
*/
protected function usePaths(): array
{
$paths = [];
foreach (['app', 'config', 'storage', 'resources', 'public'] as $path) {
$paths[$path] = $this->normalizeApplicationPath($path, null);
}
$paths['bootstrap'] = $this->normalizeApplicationPath($path, "{$paths['storage']}/framework");
return $paths;
}
/**
* Normalize a relative or absolute path to an application directory.
*
* @param string $path
* @param string|null $default
* @return string
*/
protected function normalizeApplicationPath($path, $default = null)
{
$key = strtoupper($path);
if (is_null($env = Env::get("ACORN_{$key}_PATH"))) {
return $default
?? (defined("ACORN_{$key}_PATH") ? constant("ACORN_{$key}_PATH") : $this->findPath($path));
}
return Str::startsWith($env, $this->absoluteApplicationPathPrefixes)
? $env
: $this->basePath($env);
}
/**
* Add new prefix to list of absolute path prefixes.
*
* @param string $prefix
* @return $this
*/
public function addAbsoluteApplicationPathPrefix($prefix)
{
$this->absoluteApplicationPathPrefixes[] = $prefix;
return $this;
}
/**
* Find a path that is configurable by the developer.
*
* @param string $path
* @return string
*/
protected function findPath($path): string
{
$path = trim($path, '\\/');
$searchPaths = [
$this->basePath() . DIRECTORY_SEPARATOR . $path,
get_theme_file_path($path),
];
return collect($searchPaths)
->map(function ($path) {
return (is_string($path) && is_dir($path)) ? $path : null;
})
->filter()
->whenEmpty(function ($paths) use ($path) {
return $paths->add($this->fallbackPath($path));
})
->unique()
->first();
}
/**
* Fallbacks for path types.
*
* @param string $path
* @return string
*/
protected function fallbackPath(string $path): string
{
return match ($path) {
'storage' => $this->fallbackStoragePath(),
'app' => $this->basePath() . DIRECTORY_SEPARATOR . 'app',
'public' => $this->basePath() . DIRECTORY_SEPARATOR . 'public',
default => dirname(__DIR__, 3) . DIRECTORY_SEPARATOR . $path,
};
}
/**
* Ensure that all of the storage directories exist.
*
* @return string
*/
protected function fallbackStoragePath()
{
$files = new Filesystem();
$path = WP_CONTENT_DIR . DIRECTORY_SEPARATOR . 'cache' . DIRECTORY_SEPARATOR . 'acorn';
$files->ensureDirectoryExists($path . DIRECTORY_SEPARATOR . 'framework' . DIRECTORY_SEPARATOR . 'cache' . DIRECTORY_SEPARATOR . 'data', 0755, true);
$files->ensureDirectoryExists($path . DIRECTORY_SEPARATOR . 'framework' . DIRECTORY_SEPARATOR . 'views', 0755, true);
$files->ensureDirectoryExists($path . DIRECTORY_SEPARATOR . 'logs', 0755, true);
return $path;
}
}