Further Drupal 8.6.4 changes. Some core files were not committed before a commit...
[yaffs-website] / vendor / drush / drush / src / Preflight / RedispatchToSiteLocal.php
1 <?php
2
3 namespace Drush\Preflight;
4
5 use Webmozart\PathUtil\Path;
6
7 /**
8  * RedispatchToSiteLocal forces an `exec` to the site-local Drush if it
9  * exist.  We must do this super-early, before loading Drupal's autoload
10  * file.  If we do not, we will crash unless the site-local Drush and the
11  * global Drush are using the exact same versions of all dependencies, which
12  * will rarely line up sufficiently to prevent problems.
13  */
14 class RedispatchToSiteLocal
15 {
16
17     /**
18      * Determine if a local redispatch is needed, and do so if it is.
19      *
20      * @param array $argv The commandline arguments
21      * @param string $root The selected site root or false if none
22      * @param string $vendor The path to the vendor directory
23      * @param PreflightLog $preflightLog A basic logger.
24      *
25      * @return bool
26      *   True if redispatch occurred, and was returned successfully.
27      */
28     public static function redispatchIfSiteLocalDrush($argv, $root, $vendor, PreflightLog $preflightLog)
29     {
30
31         // Try to find the site-local Drush. If there is none, we are done.
32         $siteLocalDrush = static::findSiteLocalDrush($root);
33         if (!$siteLocalDrush) {
34             return false;
35         }
36
37         // If the site-local Drush is us, then we do not need to redispatch.
38         if (Path::isBasePath($vendor, $siteLocalDrush)) {
39             return false;
40         }
41
42         // Do another special check to detect symlinked Drush folder similar
43         // to what the SUT sets up for Drush functional tests.
44         if (dirname($vendor) == dirname($siteLocalDrush)) {
45             return false;
46         }
47
48         // Redispatch!
49         $command = $siteLocalDrush;
50         $preflightLog->log(dt('Redispatch to site-local Drush: !cmd.', ['!cmd' => $command]));
51         array_shift($argv);
52         $args = array_map(
53             function ($item) {
54                 return escapeshellarg($item);
55             },
56             $argv
57         );
58         $command .= ' ' . implode(' ', $args);
59         passthru($command, $status);
60         return $status;
61     }
62
63     /**
64      * Find a site-local Drush, if there is one in the selected site's
65      * vendor directory.
66      *
67      * @param string $root The selected site root
68      */
69     protected static function findSiteLocalDrush($root)
70     {
71         $candidates = [
72             "$root/vendor/drush/drush/drush",
73             dirname($root) . '/vendor/drush/drush/drush',
74         ];
75         foreach ($candidates as $candidate) {
76             if (file_exists($candidate)) {
77                 return $candidate;
78             }
79         }
80     }
81 }