3 namespace Drush\Preflight;
5 use Webmozart\PathUtil\Path;
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.
14 class RedispatchToSiteLocal
18 * Determine if a local redispatch is needed, and do so if it is.
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.
26 * True if redispatch occurred, and was returned successfully.
28 public static function redispatchIfSiteLocalDrush($argv, $root, $vendor, PreflightLog $preflightLog)
30 // Special check for the SUT, which is always a site-local install.
31 // The symlink that Composer sets up can make it challenging to
32 // detect that the vendor directory is in the same place. Do not
33 // set DRUSH_AUTOLOAD_PHP unless you know what you are doing! This
34 // mechanism should be reserved for use with test fixtures.
35 if (getenv('DRUSH_AUTOLOAD_PHP')) {
39 // Try to find the site-local Drush. If there is none, we are done.
40 $siteLocalDrush = static::findSiteLocalDrush($root);
41 if (!$siteLocalDrush) {
45 // If the site-local Drush is us, then we do not need to redispatch.
46 if (Path::isBasePath($vendor, $siteLocalDrush)) {
50 // Do another special check to detect symlinked Drush folder similar
51 // to what the SUT sets up for Drush functional tests.
52 if (dirname($vendor) == dirname($siteLocalDrush)) {
57 $command = $siteLocalDrush;
58 $preflightLog->log(dt('Redispatch to site-local Drush: !cmd.', ['!cmd' => $command]));
62 return escapeshellarg($item);
66 $command .= ' ' . implode(' ', $args);
67 passthru($command, $status);
72 * Find a site-local Drush, if there is one in the selected site's
75 * @param string $root The selected site root
77 protected static function findSiteLocalDrush($root)
80 "$root/vendor/drush/drush/drush",
81 dirname($root) . '/vendor/drush/drush/drush',
83 foreach ($candidates as $candidate) {
84 if (file_exists($candidate)) {