From 38e00714db6ab7a757f9edf2abb4f0004f7fe0c1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sigui=20Kess=C3=A9=20Emmanuel?= Date: Wed, 7 Feb 2024 21:26:27 +0100 Subject: [PATCH] :art: Use the constructor instead of creating the instance yourself --- src/Application.php | 40 +++++++++++++++------------------ src/Contracts/IsApplication.php | 2 -- 2 files changed, 18 insertions(+), 24 deletions(-) diff --git a/src/Application.php b/src/Application.php index d276110..d8f72ed 100644 --- a/src/Application.php +++ b/src/Application.php @@ -4,6 +4,7 @@ namespace Sikessem; +use Closure; use Illuminate\Contracts\Config\Repository as ConfigContract; use Illuminate\Contracts\Console\Kernel as ConsoleKernel; use Illuminate\Contracts\Http\Kernel as HttpKernel; @@ -15,36 +16,31 @@ class Application extends BaseApplication implements IsApplication { - protected static self $INSTANCE; - /** * @var string */ protected $namespace = 'App\\'; - public static function create(string $basePath = null): IsApplication + public function __construct(?string $basePath = null) { - if (! isset(self::$INSTANCE)) { - if (! $basePath) { - $basePath = backtrace(limit: 1)->getDirectory(); - } - self::$INSTANCE = new self($basePath); - /** @var string */ - $langPath = value(static function (): string { - $directory = is_dir(self::$INSTANCE->resourcePath('locales')) - ? self::$INSTANCE->resourcePath('locales') - : self::$INSTANCE->resourcePath('i18n'); - - if (is_dir($directory)) { - return $directory; - } - - return self::$INSTANCE->basePath('lang'); - }); - self::$INSTANCE->useLangPath($langPath); + if (! $basePath) { + $basePath = backtrace(limit: 1)->getDirectory(); } - return self::$INSTANCE; + parent::__construct($basePath); + + /** @var string */ + $langPath = value(Closure::bind(function (): string { + $directory = is_dir($this->resourcePath('locales')) + ? $this->resourcePath('locales') + : $this->resourcePath('i18n'); + if (is_dir($directory)) { + return $directory; + } + + return $this->basePath('lang'); + }, $this)); + $this->useLangPath($langPath); } public function run(): void diff --git a/src/Contracts/IsApplication.php b/src/Contracts/IsApplication.php index 05186a7..ee64d80 100644 --- a/src/Contracts/IsApplication.php +++ b/src/Contracts/IsApplication.php @@ -18,8 +18,6 @@ interface IsApplication extends ApplicationContract, CachesConfiguration, CachesRoutes, HttpKernelInterface { - public static function create(string $basePath = null): self; - public function run(): void; public function handleCommand(): void;