Created
April 10, 2021 06:15
-
-
Save DarkGhostHunter/0865d5aa39b84662aa8317578978bb37 to your computer and use it in GitHub Desktop.
Don't fuck up your Laravel Octane app
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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; | |
} | |
// ... | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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