F
F
Fireproof Cockroach2020-02-06 21:57:51
Laravel
Fireproof Cockroach, 2020-02-06 21:57:51

How to hang interfaces on Laravel disks through ioc?

Hey!

Laravel has disks. It's cool and convenient. But calling configs in code is not convenient and not beautiful.

There is an idea to make ioc work according to this principle: I throw in the constructor, for example, TemporaryDisk (interface or class, it doesn’t matter) and I will receive the desired, configured disk.

But when throwing, the designer swears that the adapter has arrived.

Service::__construct() must implement interface App\Disks\TemporaryDisk, instance of Illuminate\Filesystem\FilesystemAdapter given


Bindil as usual:
$this->app->bind(TemporaryDisk::class, static fn() => Storage::disk(
    (string) config('disks.tmp')
));


The doc says how to override the Filesystem, and it works.

How to throw it right?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alex Wells, 2020-02-06
@uonick

Crap idea. You resolve TemporaryDisk (your interface) and get Filesystem as a result? This is nonsense. Here's an exception for you, it's absolutely logical that he flew in.
A more adequate solution is to create a factory and resolve the disks there, inject the factory accordingly.
If you really want to use this crutch - extend TemporaryDisk from FilesystemAdapter.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question