michael-rubel /
laravel-auto-binder
| 1 | <?php |
||||
| 2 | |||||
| 3 | declare(strict_types=1); |
||||
| 4 | |||||
| 5 | namespace MichaelRubel\AutoBinder\Traits; |
||||
| 6 | |||||
| 7 | use Closure; |
||||
| 8 | use Psr\Container\ContainerExceptionInterface; |
||||
| 9 | use Psr\Container\NotFoundExceptionInterface; |
||||
| 10 | |||||
| 11 | trait CachesBindings |
||||
| 12 | { |
||||
| 13 | /** |
||||
| 14 | * Determines if the caching is enabled. |
||||
| 15 | */ |
||||
| 16 | public bool $caching = true; |
||||
| 17 | |||||
| 18 | /** |
||||
| 19 | * Determines if the caching is being used |
||||
| 20 | * by the automatic binder instance. |
||||
| 21 | */ |
||||
| 22 | public bool $usesCache = false; |
||||
| 23 | |||||
| 24 | /** |
||||
| 25 | * Disables the caching. |
||||
| 26 | */ |
||||
| 27 | 1 | public function withoutCaching(): static |
|||
| 28 | { |
||||
| 29 | 1 | $this->caching = false; |
|||
| 30 | |||||
| 31 | 1 | return $this; |
|||
| 32 | } |
||||
| 33 | |||||
| 34 | /** |
||||
| 35 | * Get the clue to access the cache. |
||||
| 36 | */ |
||||
| 37 | 22 | public function cacheClue(): string |
|||
| 38 | { |
||||
| 39 | 22 | return config('app.name') . static::CACHE_KEY . $this->classFolder; |
|||
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||||
| 40 | } |
||||
| 41 | |||||
| 42 | /** |
||||
| 43 | * Check if the caching is enabled. |
||||
| 44 | */ |
||||
| 45 | 23 | protected function cacheEnabled(): bool |
|||
| 46 | { |
||||
| 47 | 23 | return $this->caching && ! app()->isLocal(); |
|||
|
0 ignored issues
–
show
The method
isLocal() does not exist on Illuminate\Container\Container. Are you sure you never get this type here, but always one of the subclasses?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||
| 48 | } |
||||
| 49 | |||||
| 50 | /** |
||||
| 51 | * Check if the caching is enabled. |
||||
| 52 | */ |
||||
| 53 | 23 | protected function hasCache(): bool |
|||
| 54 | { |
||||
| 55 | 23 | return $this->cacheEnabled() && cache()->has($this->cacheClue()); |
|||
| 56 | } |
||||
| 57 | |||||
| 58 | /** |
||||
| 59 | * Use the bindings from the cache. |
||||
| 60 | * |
||||
| 61 | * @throws ContainerExceptionInterface |
||||
| 62 | * @throws NotFoundExceptionInterface |
||||
| 63 | */ |
||||
| 64 | 3 | protected function fromCache(): void |
|||
| 65 | { |
||||
| 66 | 3 | collect(cache()->get($this->cacheClue()))->each( |
|||
| 67 | 3 | fn ($concrete, $interface) => app()->{$this->bindingType}($interface, $concrete) |
|||
| 68 | 3 | ); |
|||
| 69 | } |
||||
| 70 | |||||
| 71 | /** |
||||
| 72 | * Cache the binding. |
||||
| 73 | * |
||||
| 74 | * @throws ContainerExceptionInterface |
||||
| 75 | * @throws NotFoundExceptionInterface |
||||
| 76 | */ |
||||
| 77 | 20 | protected function cacheBindingFor(string $interface, Closure|string $concrete): void |
|||
| 78 | { |
||||
| 79 | 20 | $clue = $this->cacheClue(); |
|||
| 80 | |||||
| 81 | 20 | $cache = cache()->get($clue); |
|||
| 82 | |||||
| 83 | 20 | $cache[$interface] = $concrete; |
|||
| 84 | |||||
| 85 | 20 | cache()->put($clue, $cache); |
|||
| 86 | } |
||||
| 87 | } |
||||
| 88 |