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?

Marcin Nabiałek
  • 98,126
  • 37
  • 219
  • 261
Réjôme
  • 1,454
  • 3
  • 14
  • 25
  • If don't you just use `Filesystem::glob()`? – lukasgeiter Apr 25 '15 at 15:10
  • 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... – Réjôme Apr 25 '15 at 22:37
  • Cant you do `Filesystem->disk('local')->glob()`? – Laurence Apr 28 '15 at 13:22
  • I think the only way to achieve this is extending the FileststemManager, using extend function (you can see this in laravel API: http://laravel.com/api/5.0/Illuminate/Filesystem/FilesystemManager.html ($this)). – Santiago Mendoza Ramirez Apr 28 '15 at 16:42
  • 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. – bishop Apr 28 '15 at 19:08

1 Answers1

12

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

Marcin Nabiałek
  • 98,126
  • 37
  • 219
  • 261
  • Maybe `allFiles()` if recursive matching is desired. – bishop Apr 28 '15 at 19:05
  • 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 :) – Réjôme Apr 28 '15 at 21:32
  • 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. ;-) – Réjôme Apr 29 '15 at 20:51