Completed
Pull Request — master (#1)
by Greg
02:25
created

SiteAliasFileDiscovery::find()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
1
<?php
2
namespace Consolidation\SiteAlias;
3
4
use Symfony\Component\Finder\Finder;
5
6
/**
7
 * Discover alias files named:
8
 *
9
 * - sitename.site.yml: contains multiple aliases, one for each of the
10
 *     environments of 'sitename'.
11
 *
12
 * Drush aliases that contain both a site name and an environment
13
 * (e.g. @site.env) will cause Drush to find the file named after
14
 * the respective site name and retrieve the specified environment
15
 * record.
16
 *
17
 * Sites may also define a special alias file self.site.yml, which
18
 * may be stored in the drush/sites directory relative to either
19
 * the Drupal root or the Composer root of the site. The environments
20
 * in this file will be merged with the available environments for
21
 * the element @self, however it is defined.
22
 */
23
class SiteAliasFileDiscovery
24
{
25
    protected $searchLocations;
26
    protected $locationFilter;
27
    protected $depth;
28
29
    public function __construct($searchLocations = [], $depth = '<= 1', $locationFilter = null)
30
    {
31
        $this->locationFilter = $locationFilter;
32
        $this->searchLocations = $searchLocations;
33
        $this->depth = $depth;
34
    }
35
36
    /**
37
     * Add a location that alias files may be found.
38
     *
39
     * @param string $path
40
     * @return $this
41
     */
42
    public function addSearchLocation($path)
43
    {
44
        if (is_dir($path)) {
45
            $this->searchLocations[] = $path;
46
        }
47
        return $this;
48
    }
49
50
    /**
51
     * Return all of the paths where alias files may be found.
52
     * @return string[]
53
     */
54
    public function searchLocations()
55
    {
56
        return $this->searchLocations;
57
    }
58
59
    public function locationFilter()
60
    {
61
        return $this->locationFilter;
62
    }
63
64
    /**
65
     * Set the search depth for finding alias files
66
     *
67
     * @param string|int $depth (@see \Symfony\Component\Finder\Finder::depth)
68
     * @return $this
69
     */
70
    public function depth($depth)
71
    {
72
        $this->depth = $depth;
73
        return $this;
74
    }
75
76
    /**
77
     * Only search for aliases that are in alias files stored in directories
78
     * whose basename or key matches the specified location.
79
     */
80
    public function filterByLocation($location)
81
    {
82
        if (empty($location)) {
83
            return $this;
84
        }
85
86
        return new SiteAliasFileDiscovery($this->searchLocations(), $this->depth, $location);
87
    }
88
89
    /**
90
     * Find an alias file SITENAME.site.yml in one
91
     * of the specified search locations.
92
     *
93
     * @param string $siteName
94
     * @return string[]
95
     */
96
    public function find($siteName)
97
    {
98
        return $this->searchForAliasFiles("$siteName.site.yml");
99
    }
100
101
    /**
102
     * Find an alias file SITENAME.site.yml in one
103
     * of the specified search locations.
104
     *
105
     * @param string $siteName
106
     * @return string|bool
107
     */
108
    public function findSingleSiteAliasFile($siteName)
109
    {
110
        $matches = $this->find($siteName);
111
        if (empty($matches)) {
112
            return false;
113
        }
114
        return reset($matches);
115
    }
116
117
    /**
118
     * Return a list of all SITENAME.site.yml files in any of
119
     * the search locations.
120
     *
121
     * @return string[]
122
     */
123
    public function findAllSingleAliasFiles()
124
    {
125
        return $this->searchForAliasFiles('*.site.yml');
126
    }
127
128
    /**
129
     * Return all of the legacy alias files used in previous Drush versions.
130
     *
131
     * @return string[]
132
     */
133
    public function findAllLegacyAliasFiles()
134
    {
135
        return array_merge(
136
            $this->searchForAliasFiles('*.alias.drushrc.php'),
137
            $this->searchForAliasFiles('*.aliases.drushrc.php')
138
        );
139
    }
140
141
    /**
142
     * Create a Symfony Finder object to search all available search locations
143
     * for the specified search pattern.
144
     *
145
     * @param string $searchPattern
146
     * @return Finder
147
     */
148
    protected function createFinder($searchPattern)
149
    {
150
        $finder = new Finder();
151
        $finder->files()
152
            ->name($searchPattern)
153
            ->in($this->searchLocations)
154
            ->depth($this->depth);
155
        return $finder;
156
    }
157
158
    /**
159
     * Return a list of all alias files matching the provided pattern.
160
     *
161
     * @param string $searchPattern
162
     * @return string[]
163
     */
164
    protected function searchForAliasFiles($searchPattern)
165
    {
166
        if (empty($this->searchLocations)) {
167
            return [];
168
        }
169
        $finder = $this->createFinder($searchPattern);
170
        $result = [];
171
        foreach ($finder as $file) {
172
            $path = $file->getRealPath();
173
            $result[] = $path;
174
        }
175
        // In theory we can use $finder->path() instead. That didn't work well,
176
        // in practice, though; had trouble correctly escaping the path separators.
177
        if (!empty($this->locationFilter)) {
178
            $result = array_filter($result, function ($path) {
179
                return SiteAliasName::locationFromPath($path) === $this->locationFilter;
180
            });
181
        }
182
183
        return $result;
184
    }
185
186
    // TODO: Seems like this could just be basename()
187
    protected function extractKey($basename, $filenameExensions)
188
    {
189
        return str_replace($filenameExensions, '', $basename);
190
    }
191
}
192