Skip to content

Instantly share code, notes, and snippets.

@DarkGhostHunter
Created April 10, 2021 06:15
Show Gist options
  • Save DarkGhostHunter/0865d5aa39b84662aa8317578978bb37 to your computer and use it in GitHub Desktop.
Save DarkGhostHunter/0865d5aa39b84662aa8317578978bb37 to your computer and use it in GitHub Desktop.
Don't fuck up your Laravel Octane app
<?php
namespace App\Services;
use App\Helpers\ProtectsBoot;
use Illuminate\Contracts\Foundation\Application;
class ExampleService
{
use ProtectsBoots;
protected Application $app;
public function __construct(Application $app)
{
// If the app is booting, and the developer resolves it, abort.
static::protectBoot();
$this->app = $app;
}
// ...
}
<?php
namespace App\Helpers;
use Illuminate\Foundation\Application;
use Laravel\Octane\Octane;
use RuntimeException;
trait ProtectsBoot
{
/**
* Protects the app from resolving a service at booting.
*
* @return void
* @throws \RuntimeException
*/
protected static function protectBoot(): void
{
if (class_exists(Octane::class) && ! Application::getInstance()->isBooted()) {
throw new RuntimeException(
'The [' . static::class . '] service should not be resolved while the application is booting'
);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment