Subdirectories.php 1.15 KB
Newer Older
Thibaud Dauce's avatar
Thibaud Dauce committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
<?php declare(strict_types = 1);

namespace ThibaudDauce\LaravelRecursiveMigrations;

use Illuminate\Support\Collection;
use Symfony\Component\Finder\Finder;
use Symfony\Component\Finder\SplFileInfo;

trait Subdirectories
{
    /**
     * Fetch all subdirectories from a path.
     * The original path is included in the array.
     *
     * @param string $path
     * @return string[]
     */
    protected function subdirectories($path)
    {
        return Collection::make(Finder::create()->in($path)->directories())
            ->map(function (SplFileInfo $directory) {
                return $directory->getPathname();
            })
            ->prepend($path)
            ->values()
            ->toArray();
    }

    /**
     * Fetch all subdirectories from an array of base paths.
     * The original paths are included in the array.
     *
     * @param string[] $paths
     * @return string[]
     */
    protected function allSubdirectories($paths)
    {
        return Collection::make($paths)
            ->flatMap(function ($path) {
                return $this->subdirectories($path);
            })
Thibaud Dauce's avatar
Thibaud Dauce committed
42
            ->sort()
Thibaud Dauce's avatar
Thibaud Dauce committed
43
44
45
            ->toArray();
    }
}