1<?php
2
3/*
4 * This file is part of Component Installer.
5 *
6 * (c) Rob Loach (http://robloach.net)
7 *
8 * For the full copyright and license information, please view the LICENSE.md
9 * file that was distributed with this source code.
10 */
11
12namespace ComponentInstaller\Util;
13
14use Composer\Util\Filesystem as BaseFilesystem;
15
16/**
17 * Provides basic file system operations.
18 */
19class Filesystem extends BaseFilesystem
20{
21    /**
22     * Performs a recursive-enabled glob search with the given pattern.
23     *
24     * @param string $pattern
25     *   The pattern passed to glob(). If the pattern contains "**", then it
26     *   a recursive search will be used.
27     * @param int $flags
28     *   Flags to pass into glob().
29     *
30     * @return mixed
31     *  An array of files that match the recursive pattern given.
32     */
33    public function recursiveGlob($pattern, $flags = 0)
34    {
35        // Perform the glob search.
36        $files = glob($pattern, $flags);
37
38        // Check if this is to be recursive.
39        if (strpos($pattern, '**') !== FALSE) {
40            $dirs = glob(dirname($pattern).DIRECTORY_SEPARATOR.'*', GLOB_ONLYDIR|GLOB_NOSORT);
41            if ($dirs) {
42                foreach ($dirs as $dir) {
43                    $files = array_merge($files, $this->recursiveGlob($dir.DIRECTORY_SEPARATOR.basename($pattern), $flags));
44                }
45            }
46        }
47
48        return $files;
49    }
50
51    /**
52     * Performs a recursive glob search for files with the given pattern.
53     *
54     * @param string $pattern
55     *   The pattern passed to glob().
56     * @param int $flags
57     *   Flags to pass into glob().
58     *
59     * @return mixed
60     *  An array of files that match the recursive pattern given.
61     */
62    public function recursiveGlobFiles($pattern, $flags = 0)
63    {
64        $files = $this->recursiveGlob($pattern, $flags);
65
66        return array_filter($files, 'is_file');
67    }
68}
69