5 * Functions used when Drush is starting up.
7 * This file is included and used early in the Drush
8 * startup process, before any other Drush APIs are
9 * available, and before the autoloader has been included.
13 * Get the current enviornment.
15 function drush_env() {
16 // Fetch the current environment. To ensure that
17 // $_ENV is correctly populated, make sure that
18 // the value of 'variables-order' in your php.ini
19 // contains "E" ("Environment"). See:
20 // http://us.php.net/manual/en/ini.core.php#ini.variables-order
23 // If PHP is not configured correctly, $_ENV will be
24 // empty. Drush counts on the fact that environment
25 // variables will always be available, though, so we
26 // need to repair this situation. We can always access
27 // individual environmnet values via getenv(); however,
28 // there is no PHP API that will tell us all of the
29 // available values, so we will get the environment
30 // variable values using 'printenv'.
32 exec('printenv', $env_items);
33 foreach ($env_items as $item) {
34 // Each $item is 'key=value' or just 'key'.
35 // If $item has no value, then explode will return
36 // a single array, [0 => 'key']. We add a default
37 // value of [1 => 'value'] to cover this case. If
38 // explode returns two items, the default value is ignored.
39 list($key, $value) = explode('=', $item, 2) + array(1 => '');
48 * Checks the provided location and return the appropriate
49 * Drush wrapper or Drush launcher script, if found.
51 * If the provided location looks like it might be a web
52 * root (i.e., it contains an index.php), then we will search
53 * in a number of locations in the general vicinity of the
54 * web root for a Drush executable.
56 * For other locations, we will look only in that specific
57 * directory, or in vendor/bin.
59 function find_wrapper_or_launcher($location) {
60 if (file_exists($location. DIRECTORY_SEPARATOR. 'index.php')) {
61 return find_wrapper_or_launcher_in_vicinity($location);
63 return find_wrapper_or_launcher_in_specific_locations($location, ["", 'vendor'. DIRECTORY_SEPARATOR. 'bin']);
67 * We look for a "Drush wrapper" script that might
68 * be stored in the root of a site. If there is
69 * no wrapper script, then we look for the
70 * drush.launcher script in vendor/bin. We try just a
71 * few of the most common locations; if the user relocates
72 * their vendor directory anywhere else, then they must
73 * use a wrapper script to locate it. See the comment in
74 * 'examples/drush' for details.
76 function find_wrapper_or_launcher_in_vicinity($location) {
77 $sep = DIRECTORY_SEPARATOR;
81 "..{$sep}vendor{$sep}bin{$sep}",
82 "sites{$sep}all{$sep}vendor{$sep}bin{$sep}",
83 "sites{$sep}all{$sep}vendor{$sep}drush{$sep}drush{$sep}",
84 "sites{$sep}all{$sep}drush{$sep}drush{$sep}",
85 "drush{$sep}drush{$sep}",
88 return find_wrapper_or_launcher_in_specific_locations($location, $drush_locations);
91 function find_wrapper_or_launcher_in_specific_locations($location, $drush_locations) {
92 $sep = DIRECTORY_SEPARATOR;
93 foreach ($drush_locations as $d) {
94 $found_script = find_wrapper_or_launcher_at_location("$location$sep$d");
95 if (!empty($found_script)) {
103 * We are somewhat "loose" about whether we are looking
104 * for "drush" or "drush.launcher", because in old versions
105 * of Drush, the "drush launcher" was named "drush".
106 * Otherwise, there wouldn't be any point in looking for
107 * "drush.launcher" at the root, or "drush" in a vendor directory.
108 * We also allow users to rename their drush wrapper to
109 * 'drush.wrapper' to avoid conflicting with a directory named
110 * 'drush' at the site root.
112 function find_wrapper_or_launcher_at_location($location) {
113 $sep = DIRECTORY_SEPARATOR;
114 // Sanity-check: empty $location means that we should search
115 // at the cwd, not at the root of the filesystem.
116 if (empty($location)) {
119 foreach (array('.launcher', '.wrapper', '') as $suffix) {
120 $check_location = "$location{$sep}drush$suffix";
121 if (is_file($check_location)) {
122 return $check_location;
129 * Determine whether current OS is a Windows variant.
131 function drush_is_windows($os = NULL) {
132 return strtoupper(substr($os ?: PHP_OS, 0, 3)) === 'WIN';
135 function drush_escapeshellarg($arg, $os = NULL, $raw = FALSE) {
136 // Short-circuit escaping for simple params (keep stuff readable)
137 if (preg_match('|^[a-zA-Z0-9.:/_-]*$|', $arg)) {
140 elseif (drush_is_windows($os)) {
141 return _drush_escapeshellarg_windows($arg, $raw);
144 return _drush_escapeshellarg_linux($arg, $raw);
149 * Linux version of escapeshellarg().
151 * This is intended to work the same way that escapeshellarg() does on
152 * Linux. If we need to escape a string that will be used remotely on
153 * a Linux system, then we need our own implementation of escapeshellarg,
154 * because the Windows version behaves differently.
156 function _drush_escapeshellarg_linux($arg, $raw = FALSE) {
157 // For single quotes existing in the string, we will "exit"
158 // single-quote mode, add a \' and then "re-enter"
159 // single-quote mode. The result of this is that
160 // 'quote' becomes '\''quote'\''
161 $arg = preg_replace('/\'/', '\'\\\'\'', $arg);
163 // Replace "\t", "\n", "\r", "\0", "\x0B" with a whitespace.
164 // Note that this replacement makes Drush's escapeshellarg work differently
165 // than the built-in escapeshellarg in PHP on Linux, as these characters
166 // usually are NOT replaced. However, this was done deliberately to be more
167 // conservative when running _drush_escapeshellarg_linux on Windows
168 // (this can happen when generating a command to run on a remote Linux server.)
169 $arg = str_replace(array("\t", "\n", "\r", "\0", "\x0B"), ' ', $arg);
171 // Only wrap with quotes when needed.
173 // Add surrounding quotes.
174 $arg = "'" . $arg . "'";
181 * Windows version of escapeshellarg().
183 function _drush_escapeshellarg_windows($arg, $raw = FALSE) {
184 // Double up existing backslashes
185 $arg = preg_replace('/\\\/', '\\\\\\\\', $arg);
187 // Double up double quotes
188 $arg = preg_replace('/"/', '""', $arg);
190 // Double up percents.
191 // $arg = preg_replace('/%/', '%%', $arg);
193 // Only wrap with quotes when needed.
195 // Add surrounding quotes.
196 $arg = '"' . $arg . '"';
203 * drush_startup is called once, by the Drush "finder"
204 * script -- the "drush" script at the Drush root.
205 * It finds the correct Drush "wrapper" or "launcher"
206 * script to use, and executes it with process replacement.
208 function drush_startup($argv) {
209 $sep = DIRECTORY_SEPARATOR;
212 $home = getenv("HOME");
213 $use_dir = "$home{$sep}.drush{$sep}use";
215 // Get the arguments for the command. Shift off argv[0],
216 // which contains the name of this script.
218 array_shift($arguments);
220 // We need to do at least a partial parsing of the options,
221 // so that we can find --root / -r and so on.
229 foreach ($arguments as $arg) {
230 // If a variable to set was indicated on the
231 // previous iteration, then set the value of
232 // the named variable (e.g. "ROOT") to "$arg".
257 if (!$COMMAND && !$ALIAS && ($arg[0] == '@')) {
260 elseif (!$COMMAND && ($arg[0] != '-')) {
263 if (substr($arg, 0, 7) == "--root=") {
264 $ROOT = substr($arg, 7);
269 $NONE=($ALIAS == "@none");
271 // If we have found the site-local Drush script, then
272 // do not search for it again; use the environment value
274 $found_script = getenv('DRUSH_FINDER_SCRIPT');
276 // If the @none alias is used, then we skip the Drush wrapper,
277 // and call the Drush launcher directly.
279 // In this instance, we are assuming that the 'drush' that is being
282 // a) The global 'drush', or
283 // b) A site-local 'drush' in a vendor/bin directory.
285 // In either event, the appropriate 'drush.launcher' should be right next
286 // to this script (stored in the same directory).
287 if (empty($found_script) && $NONE) {
288 if (is_file(dirname(__DIR__) . "{$sep}drush.launcher")) {
289 $found_script = dirname(__DIR__) . "{$sep}drush.launcher";
292 fwrite(STDERR, "Could not find drush.launcher in " . dirname(__DIR__) . ". Check your installation.\n");
297 // Check for a root option:
299 // drush --root=/path
301 // If the site root is specified via a commandline option, then we
302 // should always use the Drush stored at this root, if there is one.
303 // We will first check for a "wrapper" script at the root, and then
304 // we will look for a "launcher" script in vendor/bin.
305 if (empty($found_script) && !empty($ROOT)) {
306 $found_script = find_wrapper_or_launcher($ROOT);
307 if (!empty($found_script)) {
312 // If there is a .drush-use file, then its contents will
313 // contain the path to the Drush to use.
314 if (empty($found_script)) {
315 if (is_file(".drush-use")) {
316 $found_script = trim(file_get_contents(".drush-use"));
320 // Look for a 'drush' wrapper or launcher at the cwd,
321 // and in each of the directories above the cwd. If
322 // we find one, use it.
323 if (empty($found_script)) {
325 // Windows can give us lots of different strings to represent the root
326 // directory as it often includes the drive letter. If we get the same
327 // result from dirname() twice in a row, then we know we're at the root.
329 while (!empty($c) && ($c != $last)) {
330 $found_script = find_wrapper_or_launcher($c);
340 if (!empty($found_script)) {
341 $found_script = realpath($found_script);
343 // Guard against errors: if we have found a "drush" script
344 // (that is, theoretically a drush wrapper script), and
345 // there is a "drush.launcher" script in the same directory,
346 // then we will skip the "drush" script and use the drush launcher
347 // instead. This is because drush "wrapper" scripts should
348 // only ever exist at the root of a site, and there should
349 // never be a drush "launcher" at the root of a site.
350 // Therefore, if we find a "drush.launcher" next to a script
351 // called "drush", we have probably found a Drush install directory,
352 // not a site root. Adjust appropriately. Note that this
353 // also catches the case where a drush "finder" script finds itself.
354 if (is_file(dirname($found_script) . "{$sep}drush.launcher")) {
355 $found_script = dirname($found_script) . "{$sep}drush.launcher";
359 // Didn't find any site-local Drush, or @use'd Drush.
360 // Skip the Bash niceties of the launcher and proceed to drush_main() in either case:
361 // - No script was found and we are running a Phar
362 // - The found script *is* the Phar https://github.com/drush-ops/drush/pull/2246.
363 $phar_path = class_exists('Phar') ? Phar::running(FALSE) : '';
364 if ((empty($found_script) && $phar_path) || !empty($found_script) && $found_script == $phar_path) {
365 drush_run_main($DEBUG, $sep, "Phar detected. Proceeding to drush_main().");
368 // Didn't find any site-local Drush, or @use'd Drush, or Phar.
369 // There should be a drush.launcher in same directory as this script.
370 if (empty($found_script)) {
371 $found_script = dirname(__DIR__) . "{$sep}drush.launcher";
374 if (drush_is_windows()) {
375 // Sometimes we found launcher in /bin, and sometimes not. Adjust accordingly.
376 if (strpos($found_script, 'bin')) {
377 $found_script = dirname($found_script). $sep. 'drush.php.bat';
380 array_unshift($arguments, dirname($found_script). $sep. 'drush.php');
381 $found_script = 'php';
385 // Always use pcntl_exec if it exists.
386 $use_pcntl_exec = function_exists("pcntl_exec");
388 // If we have posix_getppid, then pass in the shell pid so
389 // that 'site-set' et. al. can work correctly.
390 if (function_exists('posix_getppid')) {
391 putenv("DRUSH_SHELL_PID=" . posix_getppid());
394 // Set an environment variable indicating which script
395 // the Drush finder found. If we end up re-entrantly calling
396 // another Drush finder, then we will skip searching for
397 // a site-local Drush, and always use the drush.launcher
398 // found previously. This environment variable typically should
399 // not be set by clients.
400 putenv("DRUSH_FINDER_SCRIPT=$found_script");
402 // Emit a message in debug mode advertising the location of the
405 $launch_method = $use_pcntl_exec ? 'pcntl_exec' : 'proc_open';
406 fwrite(STDERR, "Using the Drush script found at $found_script using $launch_method\n");
409 if ($use_pcntl_exec) {
410 // Get the current environment for pnctl_exec.
413 // Launch the new script in the same process.
414 // If the launch succeeds, then it will not return.
415 $error = pcntl_exec($found_script, $arguments, $env);
417 $errno = pcntl_get_last_error();
418 $strerror = pcntl_strerror($errno);
419 fwrite(STDERR, "Error has occurred executing the Drush script found at $found_script\n");
420 fwrite(STDERR, "(errno {$errno}) $strerror\n");
425 $escaped_args = array_map(function($item) { return drush_escapeshellarg($item); }, $arguments);
426 // Double quotes around $found_script as it can contain spaces.
427 $cmd = drush_escapeshellarg($found_script). ' '. implode(' ', $escaped_args);
428 if (drush_is_windows()) {
429 // Windows requires double quotes around whole command.
430 // @see https://bugs.php.net/bug.php?id=49139
431 // @see https://bugs.php.net/bug.php?id=60181
432 $cmd = '"'. $cmd. '"';
434 $process = proc_open($cmd, array(0 => STDIN, 1 => STDOUT, 2 => STDERR), $pipes, $cwd);
435 $proc_status = proc_get_status($process);
436 $exit_code = proc_close($process);
437 exit($proc_status["running"] ? $exit_code : $proc_status["exitcode"] );
442 * Run drush_main() and then exit. Used when we cannot hand over execution to
446 * Are we in debug mode
448 * Directory separator
450 * Debug message to log before running drush_main()
452 function drush_run_main($DEBUG, $sep, $msg) {
453 // Emit a message in debug mode advertising how we proceeded.
455 fwrite(STDERR, $msg. "\n");
457 require __DIR__ . "{$sep}preflight.inc";