Updated Drupal to 8.6. This goes with the following updates because it's possible...
[yaffs-website] / vendor / consolidation / site-alias / src / SiteAliasFileDiscovery.php
similarity index 59%
rename from vendor/drush/drush/src/SiteAlias/SiteAliasFileDiscovery.php
rename to vendor/consolidation/site-alias/src/SiteAliasFileDiscovery.php
index d41e2109ffeb2823eeed3b4d849cdb0ac4ca19c5..3902c6c357c03e70e9a28b7062b3806ac946cb38 100644 (file)
@@ -1,5 +1,5 @@
 <?php
-namespace Drush\SiteAlias;
+namespace Consolidation\SiteAlias;
 
 use Symfony\Component\Finder\Finder;
 
@@ -22,8 +22,16 @@ use Symfony\Component\Finder\Finder;
  */
 class SiteAliasFileDiscovery
 {
-    protected $searchLocations = [];
-    protected $depth = '<= 1';
+    protected $searchLocations;
+    protected $locationFilter;
+    protected $depth;
+
+    public function __construct($searchLocations = [], $depth = '<= 1', $locationFilter = null)
+    {
+        $this->locationFilter = $locationFilter;
+        $this->searchLocations = $searchLocations;
+        $this->depth = $depth;
+    }
 
     /**
      * Add a location that alias files may be found.
@@ -31,10 +39,12 @@ class SiteAliasFileDiscovery
      * @param string $path
      * @return $this
      */
-    public function addSearchLocation($path)
+    public function addSearchLocation($paths)
     {
-        if (is_dir($path)) {
-            $this->searchLocations[] = $path;
+        foreach ((array)$paths as $path) {
+            if (is_dir($path)) {
+                $this->searchLocations[] = $path;
+            }
         }
         return $this;
     }
@@ -48,6 +58,10 @@ class SiteAliasFileDiscovery
         return $this->searchLocations;
     }
 
+    public function locationFilter()
+    {
+        return $this->locationFilter;
+    }
 
     /**
      * Set the search depth for finding alias files
@@ -61,6 +75,31 @@ class SiteAliasFileDiscovery
         return $this;
     }
 
+    /**
+     * Only search for aliases that are in alias files stored in directories
+     * whose basename or key matches the specified location.
+     */
+    public function filterByLocation($location)
+    {
+        if (empty($location)) {
+            return $this;
+        }
+
+        return new SiteAliasFileDiscovery($this->searchLocations(), $this->depth, $location);
+    }
+
+    /**
+     * Find an alias file SITENAME.site.yml in one
+     * of the specified search locations.
+     *
+     * @param string $siteName
+     * @return string[]
+     */
+    public function find($siteName)
+    {
+        return $this->searchForAliasFiles("$siteName.site.yml");
+    }
+
     /**
      * Find an alias file SITENAME.site.yml in one
      * of the specified search locations.
@@ -70,7 +109,7 @@ class SiteAliasFileDiscovery
      */
     public function findSingleSiteAliasFile($siteName)
     {
-        $matches = $this->searchForAliasFiles("$siteName.site.yml");
+        $matches = $this->find($siteName);
         if (empty($matches)) {
             return false;
         }
@@ -97,7 +136,8 @@ class SiteAliasFileDiscovery
     {
         return array_merge(
             $this->searchForAliasFiles('*.alias.drushrc.php'),
-            $this->searchForAliasFiles('*.aliases.drushrc.php')
+            $this->searchForAliasFiles('*.aliases.drushrc.php'),
+            $this->searchForAliasFiles('aliases.drushrc.php')
         );
     }
 
@@ -129,37 +169,42 @@ class SiteAliasFileDiscovery
         if (empty($this->searchLocations)) {
             return [];
         }
+        list($match, $site) = $this->splitLocationFromSite($this->locationFilter);
+        if (!empty($site)) {
+            $searchPattern = str_replace('*', $site, $searchPattern);
+        }
         $finder = $this->createFinder($searchPattern);
         $result = [];
         foreach ($finder as $file) {
             $path = $file->getRealPath();
             $result[] = $path;
         }
+        // Find every location where the parent directory name matches
+        // with the first part of the search pattern.
+        // In theory we can use $finder->path() instead. That didn't work well,
+        // in practice, though; had trouble correctly escaping the path separators.
+        if (!empty($this->locationFilter)) {
+            $result = array_filter($result, function ($path) use ($match) {
+                return SiteAliasName::locationFromPath($path) === $match;
+            });
+        }
+
         return $result;
     }
 
     /**
-     * Return a list of all alias files with the specified extension.
-     *
-     * @param string $filenameExensions
-     * @return string[]
+     * splitLocationFromSite returns the part of 'site' before the first
+     * '.' as the "path match" component, and the part after the first
+     * '.' as the "site" component.
      */
-    protected function searchForAliasFilesKeyedByBasenamePrefix($filenameExensions)
+    protected function splitLocationFromSite($site)
     {
-        if (empty($this->searchLocations)) {
-            return [];
-        }
-        $searchPattern = '*' . $filenameExensions;
-        $finder = $this->createFinder($searchPattern);
-        $result = [];
-        foreach ($finder as $file) {
-            $path = $file->getRealPath();
-            $key = $this->extractKey($file->getBasename(), $filenameExensions);
-            $result[$key] = $path;
-        }
-        return $result;
+        $parts = explode('.', $site, 3) + ['', '', ''];
+
+        return array_slice($parts, 0, 2);
     }
 
+
     // TODO: Seems like this could just be basename()
     protected function extractKey($basename, $filenameExensions)
     {