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

97
June 16, 2021, at 01:50 AM

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()

Answer 1

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);
READ ALSO
iOS Smart Banner Causes Bottom Toolbar to Overlap Elements which are Fixed to Bottom of Screen

iOS Smart Banner Causes Bottom Toolbar to Overlap Elements which are Fixed to Bottom of Screen

I have been searching for the best part of a day in order to try and find a way around this but cantSo here I am

74
How do I add a review function to a site

How do I add a review function to a site

So I'm making a site for my sister's fashion companyShe wants to add a review feature so she can see

33
Why can't I use `AnimatedVisibility` in a `BoxScope`?

Why can't I use `AnimatedVisibility` in a `BoxScope`?

I have a layout which looks like this:

73
Compare Elements from Two Dataframe Lists

Compare Elements from Two Dataframe Lists

I need to compare two lists, each from a different data frameIt should be: if element in List 3 is not in List 1 AND element in List 5 is not in List 1 AND element in List 4 IS in List 2, then print the elements in List 3

60