-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(core): Octane compatible resolvers for
Application
, `Container…
…` and `Config`.
- Loading branch information
1 parent
5f3b1ab
commit e12355a
Showing
7 changed files
with
107 additions
and
0 deletions.
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -62,6 +62,11 @@ | |
] | ||
} | ||
} | ||
}, | ||
"laravel": { | ||
"providers": [ | ||
"LastDragon_ru\\LaraASP\\Core\\Provider" | ||
] | ||
} | ||
}, | ||
"config": { | ||
|
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace LastDragon_ru\LaraASP\Core\Application; | ||
|
||
use Illuminate\Contracts\Foundation\Application; | ||
|
||
/** | ||
* @extends Resolver<Application> | ||
*/ | ||
class ApplicationResolver extends Resolver { | ||
// empty | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace LastDragon_ru\LaraASP\Core\Application; | ||
|
||
use Illuminate\Contracts\Config\Repository; | ||
|
||
/** | ||
* @extends Resolver<Repository> | ||
*/ | ||
class ConfigResolver extends Resolver { | ||
// empty | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace LastDragon_ru\LaraASP\Core\Application; | ||
|
||
use Illuminate\Contracts\Container\Container; | ||
|
||
/** | ||
* @extends Resolver<Container> | ||
*/ | ||
class ContainerResolver extends Resolver { | ||
// empty | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace LastDragon_ru\LaraASP\Core\Application; | ||
|
||
use Closure; | ||
|
||
/** | ||
* Laravel Octane does not recommend injecting container/config/etc because it | ||
* may lead to using stale versions of them. The {@see Resolver} and its | ||
* subclasses designed specially to fix the problem. | ||
* | ||
* @see https://laravel.com/docs/octane#dependency-injection-and-octane | ||
* | ||
* @template TInstance of object | ||
*/ | ||
abstract class Resolver { | ||
/** | ||
* @param Closure(): TInstance $resolver | ||
*/ | ||
public function __construct( | ||
protected readonly Closure $resolver, | ||
) { | ||
// empty | ||
} | ||
|
||
/** | ||
* @return TInstance | ||
*/ | ||
public function getInstance(): object { | ||
return ($this->resolver)(); | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace LastDragon_ru\LaraASP\Core; | ||
|
||
use Illuminate\Container\Container; | ||
use Illuminate\Contracts\Config\Repository; | ||
use Illuminate\Contracts\Foundation\Application; | ||
use Illuminate\Support\ServiceProvider; | ||
use LastDragon_ru\LaraASP\Core\Application\ApplicationResolver; | ||
use LastDragon_ru\LaraASP\Core\Application\ConfigResolver; | ||
use LastDragon_ru\LaraASP\Core\Application\ContainerResolver; | ||
use Override; | ||
|
||
class Provider extends ServiceProvider { | ||
#[Override] | ||
public function register(): void { | ||
parent::register(); | ||
|
||
$this->registerResolvers(); | ||
} | ||
|
||
protected function registerResolvers(): void { | ||
$this->app->singletonIf(ContainerResolver::class, static function (): ContainerResolver { | ||
return new ContainerResolver(static fn () => Container::getInstance()); | ||
}); | ||
$this->app->singletonIf(ApplicationResolver::class, static function (): ApplicationResolver { | ||
return new ApplicationResolver(static fn () => Container::getInstance()->make(Application::class)); | ||
}); | ||
$this->app->singletonIf(ConfigResolver::class, static function (): ConfigResolver { | ||
return new ConfigResolver(static fn () => Container::getInstance()->make(Repository::class)); | ||
}); | ||
} | ||
} |