Skip to content

Commit

Permalink
🎨 Use the constructor instead of creating the instance yourself
Browse files Browse the repository at this point in the history
  • Loading branch information
siguici committed Feb 7, 2024
1 parent cefb19a commit 38e0071
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 24 deletions.
40 changes: 18 additions & 22 deletions src/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
Expand Down
2 changes: 0 additions & 2 deletions src/Contracts/IsApplication.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit 38e0071

Please sign in to comment.