Skip to content

Commit

Permalink
feat(core): Octane compatible resolvers for Application, `Container…
Browse files Browse the repository at this point in the history
…` and `Config`.
  • Loading branch information
LastDragon-ru committed Apr 29, 2024
1 parent 5f3b1ab commit e12355a
Show file tree
Hide file tree
Showing 7 changed files with 107 additions and 0 deletions.
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,7 @@
},
"laravel": {
"providers": [
"LastDragon_ru\\LaraASP\\Core\\Provider",
"LastDragon_ru\\LaraASP\\Documentator\\Provider",
"LastDragon_ru\\LaraASP\\Eloquent\\Provider",
"LastDragon_ru\\LaraASP\\Formatter\\Provider",
Expand Down
5 changes: 5 additions & 0 deletions packages/core/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,11 @@
]
}
}
},
"laravel": {
"providers": [
"LastDragon_ru\\LaraASP\\Core\\Provider"
]
}
},
"config": {
Expand Down
12 changes: 12 additions & 0 deletions packages/core/src/Application/ApplicationResolver.php
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
}
12 changes: 12 additions & 0 deletions packages/core/src/Application/ConfigResolver.php
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
}
12 changes: 12 additions & 0 deletions packages/core/src/Application/ContainerResolver.php
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
}
32 changes: 32 additions & 0 deletions packages/core/src/Application/Resolver.php
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)();
}
}
33 changes: 33 additions & 0 deletions packages/core/src/Provider.php
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));
});
}
}

0 comments on commit e12355a

Please sign in to comment.