How to leverage the glob method of Filesystem class with StorageFacade?
Asked Answered
O

1

18

This is concerning Laravel 5.

I can see in Illuminate\Filesystem\Filesystem a method called glob($pattern, $flags = 0)

Unfortunately, this method is not reflected in the default FilesystemAdapter shipped with Laravel 5.

This would be great, due to the fact that I would need to do something like Storage::disk('local')->glob([_]*[.blade.php]); (in order to get all stored blade files starting with an underscore.

What is the cleanest way to achieve this?

Oxford answered 19/4, 2015 at 12:29 Comment(5)
If don't you just use Filesystem::glob()?Estreat
Using directly Filesystem::glob() would not allow using Storage and the config that goes with it... I guess I would need to add a new File Driver (or extend the "local" driver) but this is way too complex...Deciliter
Cant you do Filesystem->disk('local')->glob()?Transportation
I think the only way to achieve this is extending the FileststemManager, using extend function (you can see this in laravel API: laravel.com/api/5.0/Illuminate/Filesystem/… ($this)).Buddha
Handy yes, but glob is excluded from the interface because it's either expensive or untenable on certain filesystems. That said, @Marcin Nabiałek is likely the best approach.Where
H
16

I think you cannot run glob here, but you could get all files and then filter them, for example:

$files = array_filter(Storage::disk('local')->files(), function ($file)
{
    return preg_match('/_(.*)\.blade\.php$/U', $file);
});

Of course you need to decide to use files or allFiles (recursively) depending on your needs. Probably it's not the best solution if you have thousands of files but if you don't it should be enough

Hifi answered 28/4, 2015 at 18:51 Comment(3)
Maybe allFiles() if recursive matching is desired.Where
As you said this is not the most elegant function. I'd rather use the native php glob() function. Thanks anyway Marcin for taking the time to propose :)Deciliter
I ended up building a custom file helper class that does not make use of Laravel Dependency Injection Container but that will do the trick for me... @Marcin: anyway, you earned the bounty. ;-)Deciliter

© 2022 - 2024 — McMap. All rights reserved.