4 use Drush\Config\Environment;
5 use Drush\Preflight\Preflight;
6 use Drush\Runtime\Runtime;
7 use Webmozart\PathUtil\Path;
10 * This script runs Drush.
12 * Responsibilities of this script:
13 * - Locate and include the Composer autoload file for Drush.
14 * - Set up the environment (record user home directory, cwd, etc.).
15 * - Call the Preflight object to do all necessary setup and execution.
16 * - Exit with status code returned
18 * It is our goal to put all $_SERVER access and other constructs that are
19 * difficult to test in this script to reduce the burden on the unit tests.
20 * This script will only be tested via the functional tests.
22 * The Drush bootstrap goes through the following steps:
23 * - (ArgsPreprocessor) Preprocess the commandline arguments, considering only:
24 * - The named alias `@sitealias` (removed from arguments if present)
25 * - The --root option (read and retained)
26 * - The --config option (read and retained)
27 * - The --alias-path option (read and retained)
28 * - Load the Drush configuration and alias files from the standard
29 * global locations (including --config and --alias-path)
30 * - Determine the local Drupal site targeted, if any
31 * - Include the Composer autoload for Drupal (if different)
32 * - Extend configuration and alias files to include files in target Drupal site.
33 * - Create the Robo DI container and Symfony Application et. al.
34 * - Run the Symfony Application
35 * - Predispatch: call a remote Drush command if applicable
36 * - Bootstrap Drupal via @bootstrap command hook
37 * - Run commands and command hooks via annotated commands library
38 * - Catch 'command not found' exception, bootstrap Drupal and run again
39 * - Return status code
42 // We use PWD if available because getcwd() resolves symlinks, which
43 // could take us outside of the Drupal root, making it impossible to find.
44 $cwd = empty($_SERVER['PWD']) ? getcwd() : $_SERVER['PWD'];
48 if (file_exists($autoloadFile = __DIR__ . '/vendor/autoload.php')
49 || file_exists($autoloadFile = __DIR__ . '/../autoload.php')
50 || file_exists($autoloadFile = __DIR__ . '/../../autoload.php')
52 $loader = include_once($autoloadFile);
54 throw new \Exception("Could not locate autoload.php. cwd is $cwd; __DIR__ is " . __DIR__);
58 $environment = new Environment(Path::getHomeDirectory(), $cwd, $autoloadFile);
59 $environment->setConfigFileVariant(Drush::getMajorVersion());
60 $environment->setLoader($loader);
61 $environment->applyEnvironment();
64 $preflight = new Preflight($environment);
65 $runtime = new Runtime($preflight);
66 $status_code = $runtime->run($_SERVER['argv']);