Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Define missing laravel app constants when instantiate Laravel app #338

Merged
merged 1 commit into from
Mar 3, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 20 additions & 17 deletions src/Providers/ApplicationProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,33 +6,34 @@

use Barryvdh\LaravelIdeHelper\IdeHelperServiceProvider;
use Illuminate\Contracts\Console\Kernel;
use Illuminate\Contracts\Foundation\Application;
use Illuminate\Foundation\Application as LaravelApplication;
use Laravel\Lumen\Application as LumenApplication;
use Orchestra\Testbench\Concerns\CreatesApplication;

use function define;
use function defined;
use function dirname;
use function file_exists;
use function get_class;
use function getcwd;
use function microtime;

final class ApplicationProvider
{
use CreatesApplication;

/**
* @var LaravelApplication|LumenApplication|null
*/
private static $app;
/** @var LaravelApplication|LumenApplication|null */
private static $app = null;

public static function bootApp(): void
{
$app = self::getApp();

if ($app instanceof Application) {
if ($app instanceof LaravelApplication) {
/** @var \Illuminate\Contracts\Console\Kernel $consoleApp */
$consoleApp = $app->make(Kernel::class);
$consoleApp->bootstrap();
} else {
} else { // LumenApplication
$app->boot();
}

Expand All @@ -41,26 +42,28 @@ public static function bootApp(): void

public static function getApp(): LaravelApplication | LumenApplication
{
if (self::$app) {
if (self::$app instanceof \Illuminate\Container\Container) {
return self::$app;
}

if (file_exists($applicationPath = __DIR__ . '/../../../../bootstrap/app.php')) { // plugin installed to vendor
if (! defined('LARAVEL_START')) {
define('LARAVEL_START', microtime(true));
}

if (file_exists($applicationPath = getcwd() . '/bootstrap/app.php')) { // Applications and Local Dev
/** @psalm-suppress MixedAssignment */
$app = require $applicationPath;
if (! $app instanceof LaravelApplication && ! $app instanceof LumenApplication) {
throw new \RuntimeException('Could not instantiate Application: unknown path.');
}
} elseif (file_exists($applicationPath = getcwd() . '/bootstrap/app.php')) { // Local Dev
} elseif (file_exists($applicationPath = dirname(__DIR__, 5) . '/bootstrap/app.php')) { // plugin installed to vendor
/** @psalm-suppress MixedAssignment */
$app = require $applicationPath;
if (! $app instanceof LaravelApplication && ! $app instanceof LumenApplication) {
throw new \RuntimeException('Could not instantiate Application: unknown path.');
}
} else { // Packages
} else { // Laravel Packages
$app = (new self())->createApplication(); // Orchestra\Testbench
}

if (! $app instanceof LaravelApplication && ! $app instanceof LumenApplication) {
throw new \RuntimeException('Could not find Laravel/Lumen bootstrap file.');
}

self::$app = $app;

return $app;
Expand Down