leetom
leetom

Reputation: 773

How to add a new method to a laravel facade that the default adapter doesn't have

Let's say I have a specific Filesystem that has a multiple-deleting method that could delete a bunch of files in just one request. However, the Storage facade will iterate the paths and delete them one by one. Is there a way to add a new method like deleteMultipleFiles in the filesystem adapter that could be called with the facade, without changing the code of the laravel framework?

Update: I am trying to delete millions of files from cloud storage, but the default delete API will delete only one file in one request, so I need a new method to do this. The default cloud disk package is using League\Flysystem\Filesystem; which don't have macro method to extend it.

Update:

Here is how I am doing to achieve this:

Put the new method in the Adapter.php of the disk, and call it with this Storage::disk('xxx')->getAdapter()->newMethod()

Upvotes: 2

Views: 810

Answers (1)

jrcamatog
jrcamatog

Reputation: 1504

You can add macros to certain Illuminate classes like the Filesystem. Just add them in the boot method of AppServiceProvider. Note that you can adjust the arguments ($args and $ifAny on this case) however you like,

use Illuminate\Filesystem\Filesystem;

class AppServiceProvider
{
    public function boot()
    {
        Filesystem::macro('deleteMultipleFiles', function ($args, $ifAny) {
            // Do what you need to do.
        });
    }
}

Then, use it like this.

File::deleteMultipleFiles($args, $ifAny);

Upvotes: 2

Related Questions