bootstrap.inc

  1. 8.0.x tests/bootstrap.inc
  2. 8.0.x includes/bootstrap.inc
  3. 6.x includes/bootstrap.inc
  4. 7.x tests/bootstrap.inc
  5. 7.x includes/bootstrap.inc
  6. 5.x includes/bootstrap.inc
  7. master includes/bootstrap.inc
  8. master tests/bootstrap.inc

Drush bootstrapping code.

Functions here are used to bootstrap drush and then maybe Drupal to a level sufficient so that we can run commands. This is also where third party extensions can bootstrap drush without going through drush.php (this is used in the unit tests for example).

Functions

Namesort descending Description
drush_bootstrap Bootstrap Drush to the desired phase.
drush_bootstrap_error Helper function to collect any errors that occur during the bootstrap process. Always returns FALSE, for convenience.
drush_bootstrap_finish Cleanup our bootstrap.
drush_bootstrap_max Bootstrap to the highest level possible, without triggering any errors.
drush_bootstrap_max_to_sitealias Bootstrap the specified site alias. The site alias must be a valid alias to a local site.
drush_bootstrap_prepare Prepare drush for bootstrap
drush_bootstrap_to_phase Bootstrap to the specified phase.
drush_bootstrap_validate Validate whether a bootstrap phase can be reached.
drush_bootstrap_value Helper function to store any context settings that are being validated.
drush_has_boostrapped Determine whether a given bootstrap phase has been completed
drush_return_status
drush_shutdown Shutdown function for use while Drupal is bootstrapping and to return any registered errors.
_drush_bootstrap_base_environment Sets up basic environment that controls where Drush looks for files on a system-wide basis. Important to call for "early" functions that need to work with unit tests.
_drush_bootstrap_do_drupal_site Called by _drush_bootstrap_drupal_site to do the main work of the drush drupal site bootstrap.
_drush_bootstrap_drupal_configuration Initialize and load the Drupal configuration files.
_drush_bootstrap_drupal_database Boostrap the Drupal database.
_drush_bootstrap_drupal_database_validate Validate the DRUSH_BOOTSTRAP_DRUPAL_DATABASE phase
_drush_bootstrap_drupal_full Attempt to load the full Drupal system.
_drush_bootstrap_drupal_login Log into the bootstrapped Drupal site with a specific username or user id.
_drush_bootstrap_drupal_root Bootstrap Drush with a valid Drupal Directory.
_drush_bootstrap_drupal_root_validate Validate the DRUSH_BOOTSTRAP_DRUPAL_ROOT phase.
_drush_bootstrap_drupal_site Initialize a site on the Drupal root.
_drush_bootstrap_drupal_site_validate VALIDATE the DRUSH_BOOTSTRAP_DRUPAL_SITE phase.
_drush_bootstrap_drush Initial Drush bootstrap phase.
_drush_bootstrap_drush_validate Validate that Drush is running in a suitable environment.
_drush_bootstrap_global_options
_drush_bootstrap_output_prepare
_drush_bootstrap_phases Helper function listing phases.
_drush_bootstrap_selected_uri Find the URI that has been selected by the --uri / -l option or the cwd.
_drush_bootstrap_select_drupal_site Determine which Drupal site will be selected.

Constants

Namesort descending Description
DRUSH_BOOTSTRAP_DRUPAL_CONFIGURATION Load the settings from the Drupal sites directory.
DRUSH_BOOTSTRAP_DRUPAL_DATABASE Connect to the Drupal database using the database credentials loaded during the previous bootstrap phase.
DRUSH_BOOTSTRAP_DRUPAL_FULL Fully initialize Drupal.
DRUSH_BOOTSTRAP_DRUPAL_LOGIN Log in to the initialiased Drupal site.
DRUSH_BOOTSTRAP_DRUPAL_ROOT Set up and test for a valid drupal root, either through the -r/--root options, or evaluated based on the current working directory.
DRUSH_BOOTSTRAP_DRUPAL_SITE Set up a Drupal site directory and the correct environment variables to allow Drupal to find the configuration file.
DRUSH_BOOTSTRAP_DRUSH Only bootstrap Drush, without any Drupal specific code.
DRUSH_BOOTSTRAP_MAX Use drush_bootstrap_max instead of drush_bootstrap_to_phase
DRUSH_BOOTSTRAP_NONE No bootstrap.

File

includes/bootstrap.inc
View source
  1. <?php
  2. /**
  3. * @file
  4. *
  5. * Drush bootstrapping code.
  6. *
  7. * Functions here are used to bootstrap drush and then maybe Drupal to
  8. * a level sufficient so that we can run commands. This is also where
  9. * third party extensions can bootstrap drush without going through
  10. * drush.php (this is used in the unit tests for example).
  11. */
  12. /**
  13. * @name Drush bootstrap phases
  14. * @{
  15. * Sequential Drush bootstrapping phases.
  16. */
  17. /**
  18. * No bootstrap.
  19. *
  20. * This constant is only used to indicate that the bootstrap process has
  21. * not started yet. It is not possible to have no bootstrap.
  22. */
  23. define('DRUSH_BOOTSTRAP_NONE', -1);
  24. /**
  25. * Use drush_bootstrap_max instead of drush_bootstrap_to_phase
  26. *
  27. * This constant is only usable as the value of the 'bootstrap'
  28. * item of a command object, or as the parameter to
  29. * drush_bootstrap_to_phase. It is not a real bootstrap state.
  30. */
  31. define('DRUSH_BOOTSTRAP_MAX', -2);
  32. /**
  33. * Only bootstrap Drush, without any Drupal specific code.
  34. *
  35. * Any code that operates on the Drush installation, and not specifically
  36. * any Drupal directory, should bootstrap to this phase.
  37. */
  38. define('DRUSH_BOOTSTRAP_DRUSH', 0);
  39. /**
  40. * Set up and test for a valid drupal root, either through the -r/--root options,
  41. * or evaluated based on the current working directory.
  42. *
  43. * Any code that interacts with an entire Drupal installation, and not a specific
  44. * site on the Drupal installation should use this bootstrap phase.
  45. */
  46. define('DRUSH_BOOTSTRAP_DRUPAL_ROOT', 1);
  47. /**
  48. * Set up a Drupal site directory and the correct environment variables to
  49. * allow Drupal to find the configuration file.
  50. *
  51. * If no site is specified with the -l / --uri options, Drush will assume the
  52. * site is 'default', which mimics Drupal's behaviour.
  53. *
  54. * If you want to avoid this behaviour, it is recommended that you use the
  55. * DRUSH_BOOTSTRAP_DRUPAL_ROOT bootstrap phase instead.
  56. *
  57. * Any code that needs to modify or interact with a specific Drupal site's
  58. * settings.php file should bootstrap to this phase.
  59. */
  60. define('DRUSH_BOOTSTRAP_DRUPAL_SITE', 2);
  61. /**
  62. * Load the settings from the Drupal sites directory.
  63. *
  64. * This phase is analagous to the DRUPAL_BOOTSTRAP_CONFIGURATION bootstrap phase in Drupal
  65. * itself, and this is also the first step where Drupal specific code is included.
  66. *
  67. * This phase is commonly used for code that interacts with the Drupal install API,
  68. * as both install.php and update.php start at this phase.
  69. */
  70. define('DRUSH_BOOTSTRAP_DRUPAL_CONFIGURATION', 3);
  71. /**
  72. * Connect to the Drupal database using the database credentials loaded
  73. * during the previous bootstrap phase.
  74. *
  75. * This phase is analogous to the DRUPAL_BOOTSTRAP_DATABASE bootstrap phase in
  76. * Drupal.
  77. *
  78. * Any code that needs to interact with the Drupal database API needs to
  79. * be bootstrapped to at least this phase.
  80. */
  81. define('DRUSH_BOOTSTRAP_DRUPAL_DATABASE', 4);
  82. /**
  83. * Fully initialize Drupal.
  84. *
  85. * This is analogous to the DRUPAL_BOOTSTRAP_FULL bootstrap phase in
  86. * Drupal.
  87. *
  88. * Any code that interacts with the general Drupal API should be
  89. * bootstrapped to this phase.
  90. */
  91. define('DRUSH_BOOTSTRAP_DRUPAL_FULL', 5);
  92. /**
  93. * Log in to the initialiased Drupal site.
  94. *
  95. * This is the default bootstrap phase all commands will try to reach,
  96. * unless otherwise specified.
  97. *
  98. * This bootstrap phase is used after the site has been
  99. * fully bootstrapped.
  100. *
  101. * This phase will log you in to the drupal site with the username
  102. * or user ID specified by the --user/ -u option.
  103. *
  104. * Use this bootstrap phase for your command if you need to have access
  105. * to information for a specific user, such as listing nodes that might
  106. * be different based on who is logged in.
  107. */
  108. define('DRUSH_BOOTSTRAP_DRUPAL_LOGIN', 6);
  109. /**
  110. * Helper function listing phases.
  111. *
  112. * For commands that need to iterate through the phases, such as help
  113. */
  114. function _drush_bootstrap_phases($function_names = FALSE, $init_phases_only = FALSE) {
  115. static $functions = array(
  116. DRUSH_BOOTSTRAP_DRUSH => '_drush_bootstrap_drush',
  117. DRUSH_BOOTSTRAP_DRUPAL_ROOT => '_drush_bootstrap_drupal_root',
  118. DRUSH_BOOTSTRAP_DRUPAL_SITE => '_drush_bootstrap_drupal_site',
  119. DRUSH_BOOTSTRAP_DRUPAL_CONFIGURATION => '_drush_bootstrap_drupal_configuration',
  120. DRUSH_BOOTSTRAP_DRUPAL_DATABASE => '_drush_bootstrap_drupal_database',
  121. DRUSH_BOOTSTRAP_DRUPAL_FULL => '_drush_bootstrap_drupal_full',
  122. DRUSH_BOOTSTRAP_DRUPAL_LOGIN => '_drush_bootstrap_drupal_login');
  123. $result = array();
  124. if ($init_phases_only) {
  125. foreach (array(DRUSH_BOOTSTRAP_DRUSH, DRUSH_BOOTSTRAP_DRUPAL_FULL) as $phase) {
  126. $result[$phase] = $functions[$phase];
  127. }
  128. }
  129. else {
  130. $result = $functions;
  131. }
  132. if (!$function_names) {
  133. $result = array_keys($result);
  134. }
  135. return $result;
  136. }
  137. /**
  138. * @} End of Drush bootstrap phases.
  139. */
  140. /**
  141. * Bootstrap Drush to the desired phase.
  142. *
  143. * This function will sequentially bootstrap each
  144. * lower phase up to the phase that has been requested.
  145. *
  146. * @param phase
  147. * The bootstrap phase to bootstrap to.
  148. * Any of the following constants :
  149. * DRUSH_BOOTSTRAP_DRUSH = Only Drush.
  150. * DRUSH_BOOTSTRAP_DRUPAL_ROOT = Find a valid Drupal root.
  151. * DRUSH_BOOTSTRAP_DRUPAL_SITE = Find a valid Drupal site.
  152. * DRUSH_BOOTSTRAP_DRUPAL_CONFIGURATION = Load the site's settings.
  153. * DRUSH_BOOTSTRAP_DRUPAL_DATABASE = Initialize the database.
  154. * DRUSH_BOOTSTRAP_DRUPAL_FULL = Initialize Drupal fully.
  155. * DRUSH_BOOTSTRAP_DRUPAL_LOGIN = Log into Drupal with a valid user.
  156. */
  157. function drush_bootstrap($phase, $phase_max = FALSE) {
  158. static $phases;
  159. if (!$phases) {
  160. $phases = _drush_bootstrap_phases(TRUE);
  161. }
  162. static $phase_index = 0;
  163. drush_set_context('DRUSH_BOOTSTRAPPING', TRUE);
  164. while ($phase >= $phase_index && isset($phases[$phase_index])) {
  165. if (drush_bootstrap_validate($phase_index)) {
  166. $current_phase = $phases[$phase_index];
  167. if (function_exists($current_phase) && !drush_get_error()) {
  168. drush_log(dt("Drush bootstrap phase : !function()", array('!function' => $current_phase)), 'bootstrap');
  169. $current_phase();
  170. // Find any command files that are available during this bootstrap phase.
  171. _drush_find_commandfiles($phase_index, $phase_max);
  172. }
  173. drush_set_context('DRUSH_BOOTSTRAP_PHASE', $phase_index);
  174. }
  175. else {
  176. $errors = drush_get_context('DRUSH_BOOTSTRAP_ERRORS', array());
  177. foreach ($errors as $code => $message) {
  178. drush_set_error($code, $message);
  179. }
  180. }
  181. unset($phases[$phase_index++]);
  182. }
  183. drush_set_context('DRUSH_BOOTSTRAPPING', FALSE);
  184. return !drush_get_error();
  185. }
  186. /**
  187. * Determine whether a given bootstrap phase has been completed
  188. *
  189. * This function name has a typo which makes me laugh so we choose not to
  190. * fix it. Take a deep breath, and smile. See
  191. * http://en.wikipedia.org/wiki/HTTP_referer
  192. *
  193. *
  194. * @param phase
  195. * The bootstrap phase to test
  196. *
  197. * @returns
  198. * TRUE if the specified bootstrap phase has completed.
  199. */
  200. function drush_has_boostrapped($phase) {
  201. $phase_index = drush_get_context('DRUSH_BOOTSTRAP_PHASE');
  202. return isset($phase_index) && ($phase_index >= $phase);
  203. }
  204. /**
  205. * Validate whether a bootstrap phase can be reached.
  206. *
  207. * This function will validate the settings that will be used
  208. * during the actual bootstrap process, and allow commands to
  209. * progressively bootstrap to the highest level that can be reached.
  210. *
  211. * This function will only run the validation function once, and
  212. * store the result from that execution in a local static. This avoids
  213. * validating phases multiple times.
  214. *
  215. * @param phase
  216. * The bootstrap phase to validate to.
  217. * Any of the following constants :
  218. * DRUSH_BOOTSTRAP_DRUSH = Only Drush.
  219. * DRUSH_BOOTSTRAP_DRUPAL_ROOT = Find a valid Drupal root.
  220. * DRUSH_BOOTSTRAP_DRUPAL_SITE = Find a valid Drupal site.
  221. * DRUSH_BOOTSTRAP_DRUPAL_CONFIGURATION = Load the site's settings.
  222. * DRUSH_BOOTSTRAP_DRUPAL_DATABASE = Initialize the database.
  223. * DRUSH_BOOTSTRAP_DRUPAL_FULL = Initialize Drupal fully.
  224. * DRUSH_BOOTSTRAP_DRUPAL_LOGIN = Log into Drupal with a valid user.
  225. *
  226. * @return
  227. * True if bootstrap is possible, False if the validation failed.
  228. *
  229. */
  230. function drush_bootstrap_validate($phase) {
  231. static $phases;
  232. static $result_cache = array();
  233. if (!$phases) {
  234. $phases = _drush_bootstrap_phases(TRUE);
  235. }
  236. static $phase_index = 0;
  237. // Check to see if anyone has changed --root or --uri
  238. _drush_bootstrap_select_drupal_site();
  239. if (!array_key_exists($phase, $result_cache)) {
  240. drush_set_context('DRUSH_BOOTSTRAP_ERRORS', array());
  241. drush_set_context('DRUSH_BOOTSTRAP_VALUES', array());
  242. while ($phase >= $phase_index && isset($phases[$phase_index])) {
  243. $current_phase = $phases[$phase_index] . '_validate';
  244. if (function_exists($current_phase)) {
  245. $result_cache[$phase_index] = $current_phase();
  246. }
  247. else {
  248. $result_cache[$phase_index] = TRUE;
  249. }
  250. drush_set_context('DRUSH_BOOTSTRAP_VALIDATION_PHASE', $phase_index);
  251. unset($phases[$phase_index++]);
  252. }
  253. }
  254. return $result_cache[$phase];
  255. }
  256. /**
  257. * Bootstrap to the specified phase.
  258. *
  259. * @param $max_phase_index
  260. * Only attempt bootstrap to the specified level.
  261. */
  262. function drush_bootstrap_to_phase($max_phase_index) {
  263. // If $max_phase_index is DRUSH_BOOTSTRAP_MAX, then
  264. // we will bootstrap as far as we can. drush_bootstrap_max
  265. // is different than drush_bootstrap_to_phase in that
  266. // it is not an error if DRUSH_BOOTSTRAP_LOGIN is not reached.
  267. if ($max_phase_index == DRUSH_BOOTSTRAP_MAX) {
  268. drush_bootstrap_max();
  269. return TRUE;
  270. }
  271. drush_log(dt("Bootstrap to phase !phase.", array('!phase' => $max_phase_index)), 'bootstrap');
  272. $phases = _drush_bootstrap_phases();
  273. $result = TRUE;
  274. // Try to bootstrap to the maximum possible level, without generating errors
  275. foreach ($phases as $phase_index) {
  276. if ($phase_index > $max_phase_index) {
  277. // Stop trying, since we achieved what was specified.
  278. break;
  279. }
  280. if (drush_bootstrap_validate($phase_index)) {
  281. if ($phase_index > drush_get_context('DRUSH_BOOTSTRAP_PHASE')) {
  282. $result = drush_bootstrap($phase_index, $max_phase_index);
  283. }
  284. }
  285. else {
  286. $result = FALSE;
  287. break;
  288. }
  289. }
  290. return $result;
  291. }
  292. /**
  293. * Bootstrap to the highest level possible, without triggering any errors.
  294. *
  295. * @param $max_phase_index
  296. * Only attempt bootstrap to the specified level.
  297. *
  298. * @return int
  299. * The maximum phase to which we bootstrapped.
  300. */
  301. function drush_bootstrap_max($max_phase_index = FALSE) {
  302. $phases = _drush_bootstrap_phases();
  303. $phase_index = DRUSH_BOOTSTRAP_DRUSH;
  304. if (!$max_phase_index) {
  305. $max_phase_index = count($phases);
  306. }
  307. // Try to bootstrap to the maximum possible level, without generating errors
  308. foreach ($phases as $phase_index) {
  309. if ($phase_index > $max_phase_index) {
  310. // Stop trying, since we achieved what was specified.
  311. break;
  312. }
  313. if (drush_bootstrap_validate($phase_index)) {
  314. if ($phase_index > drush_get_context('DRUSH_BOOTSTRAP_PHASE')) {
  315. drush_bootstrap($phase_index, $max_phase_index);
  316. }
  317. }
  318. else {
  319. break;
  320. }
  321. }
  322. return drush_get_context('DRUSH_BOOTSTRAP_PHASE');
  323. }
  324. /**
  325. * Bootstrap the specified site alias. The site alias must
  326. * be a valid alias to a local site.
  327. *
  328. * @param $site_record
  329. * The alias record for the given site alias.
  330. * @see drush_sitealias_get_record().
  331. * @param $max_phase_index
  332. * Only attempt bootstrap to the specified level.
  333. * @returns TRUE if attempted to bootstrap, or FALSE
  334. * if no bootstrap attempt was made.
  335. */
  336. function drush_bootstrap_max_to_sitealias($site_record, $max_phase_index = NULL) {
  337. if ((array_key_exists('root', $site_record) && !array_key_exists('remote-host', $site_record))) {
  338. drush_sitealias_set_alias_context($site_record);
  339. drush_bootstrap_max($max_phase_index);
  340. return TRUE;
  341. }
  342. return FALSE;
  343. }
  344. /**
  345. * Helper function to collect any errors that occur during the bootstrap process.
  346. * Always returns FALSE, for convenience.
  347. */
  348. function drush_bootstrap_error($code, $message = null) {
  349. $errors = drush_get_context('DRUSH_BOOTSTRAP_ERRORS');
  350. $errors[$code] = $message;
  351. drush_set_context('DRUSH_BOOTSTRAP_ERRORS', $errors);
  352. return FALSE;
  353. }
  354. /**
  355. * Validate that Drush is running in a suitable environment.
  356. */
  357. function _drush_bootstrap_drush_validate() {
  358. // @todo _drush_environment_php_ini_checks() always returns TRUE.
  359. $return = _drush_environment_check_php_ini();
  360. if ($return !== TRUE) {
  361. return $return;
  362. }
  363. if (drush_environment_lib() === FALSE) {
  364. return FALSE;
  365. }
  366. if (drush_environment_table_lib() === FALSE) {
  367. return FALSE;
  368. }
  369. return TRUE;
  370. }
  371. /**
  372. * Initial Drush bootstrap phase.
  373. *
  374. * During the initialization of Drush,
  375. * this is the first step where all we are
  376. * aware of is Drush itself.
  377. *
  378. * In this step we will register the shutdown function,
  379. * parse the command line arguments and store them in their
  380. * related contexts.
  381. *
  382. * Configuration files (drushrc.php) that are
  383. * a) Specified on the command line
  384. * b) Stored in the root directory of drush.php
  385. * c) Stored in the home directory of the system user.
  386. *
  387. * Additionally the DRUSH_QUIET and DRUSH_BACKEND contexts,
  388. * will be evaluated now, as they need to be set very early in
  389. * the execution flow to be able to take affect/
  390. */
  391. function _drush_bootstrap_drush() {
  392. // Create an alias '@none' to represent no Drupal site
  393. _drush_sitealias_cache_alias('none', array('root' => '', 'uri' => ''));
  394. // Set the terminal width, used for wrapping table output.
  395. // Normally this is exported using tput in the drush script.
  396. // If this is not present we do an additional check using stty here.
  397. // On Windows in CMD and PowerShell is this exported using mode con.
  398. // PHP version prior to 5.3.0 is not supporting this, they always
  399. // return 80 columns
  400. if (!($columns = getenv('COLUMNS'))) {
  401. // Trying to export the columns using stty.
  402. exec('stty size 2>&1', $columns_output, $columns_status);
  403. if (!$columns_status) $columns = preg_replace('/\d+\s(\d+)/', '$1', $columns_output[0], -1, $columns_count);
  404. // If stty fails and Drush us running on Windows are we trying with mode con.
  405. if (($columns_status || !$columns_count) && drush_is_windows() && version_compare(phpversion(), '5.3.0') > -1) {
  406. $columns_output = array();
  407. exec('mode con', $columns_output, $columns_status);
  408. if (!$columns_status && is_array($columns_output)) {
  409. $columns = (int)preg_replace('/\D/', '', $columns_output[4], -1, $columns_count);
  410. }
  411. else {
  412. drush_log(dt('Drush could not detect the console window width. Set a Windows Environment Variable of COLUMNS to the desired width.'), 'warning');
  413. }
  414. }
  415. // Failling back to default columns value
  416. if (empty($columns)) {
  417. $columns = 80;
  418. }
  419. }
  420. // If a caller wants to reserve some room to add additional
  421. // information to the drush output via post-processing, the
  422. // --reserve-margin flag can be used to declare how much
  423. // space to leave out. This only affects drush functions
  424. // such as drush_print_table that wrap the output.
  425. $columns -= drush_get_option('reserve-margin', 0);
  426. drush_set_context('DRUSH_COLUMNS', $columns);
  427. // Statically define a way to call drush again.
  428. define('DRUSH_COMMAND', drush_find_drush());
  429. // prime the CWD cache
  430. drush_cwd();
  431. // Set up base environment for system-wide file locations.
  432. _drush_bootstrap_base_environment();
  433. // Load a drushrc.php file in the drush.php's directory.
  434. drush_load_config('drush');
  435. // Load a drushrc.php file in the $ETC_PREFIX/etc/drush directory.
  436. drush_load_config('system');
  437. // Load a drushrc.php file at ~/.drushrc.php.
  438. drush_load_config('user');
  439. // Load a drushrc.php file in the ~/.drush directory.
  440. drush_load_config('home.drush');
  441. // Load a custom config specified with the --config option.
  442. drush_load_config('custom');
  443. // Process the site alias that specifies which instance
  444. // of drush (local or remote) this command will operate on.
  445. // We must do this after we load our config files (so that
  446. // site aliases are available), but before the rest
  447. // of the drush and drupal root bootstrap phases are
  448. // done, since site aliases may set option values that
  449. // affect these phases.
  450. drush_sitealias_check_arg();
  451. // Check to see if we 'use'd a site alias with site-set
  452. drush_sitealias_check_site_env();
  453. // Load the config options from Drupal's /drush and sites/all/drush directories,
  454. // even prior to bootstrapping the root.
  455. drush_load_config('drupal');
  456. // Similarly, load the Drupal site configuration options upfront.
  457. drush_load_config('site');
  458. // If applicable swaps in shell alias value (or executes it).
  459. drush_shell_alias_replace();
  460. // If drush_load_config defined a site alias that did not
  461. // exist before, then sitealias check arg might now match
  462. // against one of those aliases.
  463. if ((drush_sitealias_check_arg() === TRUE) || (drush_sitealias_check_site_env() === TRUE)) {
  464. $remote_host = drush_get_option('remote-host');
  465. if (!isset($remote_host)) {
  466. // Load the config files for the "new" site.
  467. drush_load_config('drupal');
  468. drush_load_config('site');
  469. }
  470. }
  471. _drush_bootstrap_global_options();
  472. }
  473. function _drush_bootstrap_output_prepare() {
  474. $backend = drush_set_context('DRUSH_BACKEND', drush_get_option('backend'));
  475. // Pipe implies quiet.
  476. $quiet = drush_set_context('DRUSH_QUIET', drush_get_option(array('quiet', 'pipe')));
  477. drush_set_context('DRUSH_PIPE', drush_get_option('pipe'));
  478. if ($backend) {
  479. // Load options passed as a JSON encoded string through STDIN.
  480. $stdin_options = _drush_backend_get_stdin();
  481. if (is_array($stdin_options)) {
  482. drush_set_context('stdin', $stdin_options);
  483. }
  484. // Add an output buffer handler to collect output/pass through backend
  485. // packets. Using a chunksize of 2 ensures that each line is flushed
  486. // straight away.
  487. if ($quiet) {
  488. // Pass through of backend packets, discard regular output.
  489. ob_start('drush_backend_output_discard', 2);
  490. }
  491. else {
  492. // Collect output.
  493. ob_start('drush_backend_output_collect', 2);
  494. }
  495. }
  496. // In non-backend quiet mode we start buffering and discards it on command
  497. // completion.
  498. if ($quiet && !$backend) {
  499. ob_start();
  500. }
  501. }
  502. /**
  503. * Determine which Drupal site will be selected.
  504. *
  505. * The Drupal site itself will be bootstrapped at a later
  506. * phase; at this time, we set context variables to
  507. * point to the drupal root, site URI and site configuration
  508. * path that will be used when needed.
  509. *
  510. * These early context variables are used to find
  511. * drush configuration and alias files stored with the
  512. * site to be bootstrapped.
  513. */
  514. function _drush_bootstrap_select_drupal_site() {
  515. $drupal_root = drush_get_option('root');
  516. if (!isset($drupal_root)) {
  517. $drupal_root = drush_locate_root();
  518. }
  519. drush_set_context('DRUSH_SELECTED_DRUPAL_ROOT', $drupal_root);
  520. drush_set_context('DRUSH_SELECTED_DRUPAL_SITES_ALL_DRUSH', $drupal_root . '/sites/all/drush');
  521. $uri = _drush_bootstrap_selected_uri();
  522. drush_set_context('DRUSH_SELECTED_URI', $uri);
  523. drush_set_context('DRUSH_SELECTED_DRUPAL_SITE_CONF_PATH', drush_conf_path($uri));
  524. if (!empty($drupal_root) && !empty($uri)) {
  525. // Create an alias '@self'
  526. _drush_sitealias_cache_alias('self', array('root' => $drupal_root, 'uri' => $uri));
  527. }
  528. }
  529. /**
  530. * Sets up basic environment that controls where Drush looks for files on a
  531. * system-wide basis. Important to call for "early" functions that need to
  532. * work with unit tests.
  533. */
  534. function _drush_bootstrap_base_environment() {
  535. // Copy ETC_PREFIX and SHARE_PREFIX from environment variables if available.
  536. // This alters where we check for server-wide config and alias files.
  537. // Used by unit test suite to provide a clean environment.
  538. if (getenv('ETC_PREFIX')) drush_set_context('ETC_PREFIX', getenv('ETC_PREFIX'));
  539. if (getenv('SHARE_PREFIX')) drush_set_context('SHARE_PREFIX', getenv('SHARE_PREFIX'));
  540. drush_set_context('DOC_PREFIX', DRUSH_BASE_PATH);
  541. if (!file_exists(DRUSH_BASE_PATH . '/README.md') && file_exists(drush_get_context('SHARE_PREFIX', '/usr') . '/share/doc/drush' . '/README.md')) {
  542. drush_set_context('DOC_PREFIX', drush_get_context('SHARE_PREFIX', '/usr') . '/share/doc/drush');
  543. }
  544. $alias_path =& drush_get_context('ALIAS_PATH');
  545. $default_prefix_configuration = drush_is_windows() ? getenv('ALLUSERSPROFILE') . '/Drush' : '';
  546. $default_prefix_commandfile = drush_is_windows() ? getenv('ALLUSERSPROFILE') . '/Drush' : '/usr';
  547. $site_wide_configuration_dir = drush_get_context('ETC_PREFIX', $default_prefix_configuration) . '/etc/drush';
  548. $site_wide_commandfile_dir = drush_get_context('SHARE_PREFIX', $default_prefix_commandfile) . '/share/drush/commands';
  549. $alias_path[] = $site_wide_configuration_dir;
  550. drush_set_context('DRUSH_SITE_WIDE_CONFIGURATION', $site_wide_configuration_dir);
  551. drush_set_context('DRUSH_SITE_WIDE_COMMANDFILES', $site_wide_commandfile_dir);
  552. $alias_path[] = dirname(__FILE__) . '/..';
  553. if(!is_null(drush_server_home())) {
  554. $alias_path[] = drush_server_home() . '/.drush';
  555. drush_set_context('DRUSH_PER_USER_CONFIGURATION', drush_server_home() . '/.drush');
  556. }
  557. }
  558. function _drush_bootstrap_global_options() {
  559. // Debug implies verbose
  560. drush_set_context('DRUSH_VERBOSE', drush_get_option(array('verbose', 'debug'), FALSE));
  561. drush_set_context('DRUSH_DEBUG', drush_get_option('debug'));
  562. // Backend implies affirmative unless negative is explicitly specified
  563. drush_set_context('DRUSH_NEGATIVE', drush_get_option('no', FALSE));
  564. drush_set_context('DRUSH_AFFIRMATIVE', drush_get_option(array('yes', 'pipe'), FALSE) || (drush_get_context('DRUSH_BACKEND') && !drush_get_context('DRUSH_NEGATIVE')));
  565. drush_set_context('DRUSH_SIMULATE', drush_get_option('simulate', FALSE));
  566. // Suppress colored logging if --nocolor option is explicitly given or if
  567. // terminal does not support it.
  568. $nocolor = (drush_get_option('nocolor', FALSE));
  569. if (!$nocolor) {
  570. // Check for colorless terminal. If there is no terminal, then
  571. // 'tput colors 2>&1' will return "tput: No value for $TERM and no -T specified",
  572. // which is not numeric and therefore will put us in no-color mode.
  573. $colors = exec('tput colors 2>&1');
  574. $nocolor = !($colors === FALSE || (is_numeric($colors) && $colors >= 3));
  575. }
  576. drush_set_context('DRUSH_NOCOLOR', $nocolor);
  577. }
  578. /**
  579. * Validate the DRUSH_BOOTSTRAP_DRUPAL_ROOT phase.
  580. *
  581. * In this function, we will check if a valid Drupal directory is available.
  582. * We also determine the value that will be stored in the DRUSH_DRUPAL_ROOT
  583. * context and DRUPAL_ROOT constant if it is considered a valid option.
  584. */
  585. function _drush_bootstrap_drupal_root_validate() {
  586. $drupal_root = drush_get_context('DRUSH_SELECTED_DRUPAL_ROOT');
  587. if (empty($drupal_root)) {
  588. return drush_bootstrap_error('DRUSH_NO_DRUPAL_ROOT', dt("A Drupal installation directory could not be found"));
  589. }
  590. if (!$signature = drush_valid_drupal_root($drupal_root)) {
  591. return drush_bootstrap_error('DRUSH_INVALID_DRUPAL_ROOT', dt("The directory !drupal_root does not contain a valid Drupal installation", array('!drupal_root' => $drupal_root)));
  592. }
  593. drush_bootstrap_value('drupal_root', $drupal_root);
  594. define('DRUSH_DRUPAL_SIGNATURE', $signature);
  595. return TRUE;
  596. }
  597. /**
  598. * Bootstrap Drush with a valid Drupal Directory.
  599. *
  600. * In this function, the pwd will be moved to the root
  601. * of the Drupal installation.
  602. *
  603. * The DRUSH_DRUPAL_ROOT context, DRUSH_DRUPAL_CORE context, DRUPAL_ROOT, and the
  604. * DRUSH_DRUPAL_CORE constants are populated from the value that we determined during
  605. * the validation phase.
  606. *
  607. * We also now load the drushrc.php for this specific platform.
  608. * We can now include files from the Drupal Tree, and figure
  609. * out more context about the platform, such as the version of Drupal.
  610. */
  611. function _drush_bootstrap_drupal_root() {
  612. $drupal_root = drush_set_context('DRUSH_DRUPAL_ROOT', drush_bootstrap_value('drupal_root'));
  613. define('DRUPAL_ROOT', $drupal_root);
  614. chdir($drupal_root);
  615. // Beware the poison pill: prefer drush_drupal_version() and
  616. // drush_drupal_major_version() to 'DRUSH_DRUPAL_VERSION' and
  617. // 'DRUSH_DRUPAL_MAJOR_VERSION', respectively. See http://drupal.org/node/1816192
  618. $version = drush_set_context('DRUSH_DRUPAL_VERSION', drush_drupal_version());
  619. $major_version = drush_set_context('DRUSH_DRUPAL_MAJOR_VERSION', drush_drupal_major_version());
  620. if (($major_version < 6) || ($major_version > 7)) {
  621. if ($major_version < 6) {
  622. $recommended_version = 4;
  623. }
  624. else {
  625. $recommended_version = 6;
  626. }
  627. drush_set_error('DRUSH_DRUPAL_VERSION_UNSUPPORTED', dt('Drush !drush_version does not support Drupal !major_version. Use Drush !recommended_version instead.', array('!drush_version' => DRUSH_VERSION, '!major_version' => $major_version, '!recommended_version' => $recommended_version)));
  628. }
  629. drush_set_context('DRUSH_DRUPAL_CORE', DRUPAL_ROOT);
  630. define('DRUSH_DRUPAL_CORE', DRUPAL_ROOT);
  631. _drush_bootstrap_global_options();
  632. drush_log(dt("Initialized Drupal !version root directory at !drupal_root", array("!version" => $version, '!drupal_root' => $drupal_root)));
  633. }
  634. /**
  635. * Find the URI that has been selected by the --uri / -l option
  636. * or the cwd.
  637. */
  638. function _drush_bootstrap_selected_uri() {
  639. $uri = drush_get_option('uri');
  640. if (!isset($uri)) {
  641. $site_path = drush_site_path();
  642. $elements = explode('/', $site_path);
  643. $current = array_pop($elements);
  644. if (!$current) {
  645. $current = 'default';
  646. }
  647. $uri = 'http://'. $current;
  648. }
  649. return $uri;
  650. }
  651. /**
  652. * VALIDATE the DRUSH_BOOTSTRAP_DRUPAL_SITE phase.
  653. *
  654. * In this function we determine the URL used for the command,
  655. * and check for a valid settings.php file.
  656. *
  657. * To do this, we need to set up the $_SERVER environment variable,
  658. * to allow us to use conf_path to determine what Drupal will load
  659. * as a configuration file.
  660. */
  661. function _drush_bootstrap_drupal_site_validate() {
  662. $drush_uri = drush_get_context('DRUSH_SELECTED_URI');
  663. // Fake the necessary HTTP headers that Drupal needs:
  664. if ($drush_uri) {
  665. $drupal_base_url = parse_url($drush_uri);
  666. // If there's no url scheme set, add http:// and re-parse the url
  667. // so the host and path values are set accurately.
  668. if (!array_key_exists('scheme', $drupal_base_url)) {
  669. $drush_uri = 'http://' . $drush_uri;
  670. $drupal_base_url = parse_url($drush_uri);
  671. }
  672. // Fill in defaults.
  673. $drupal_base_url += array(
  674. 'path' => NULL,
  675. 'host' => NULL,
  676. 'port' => NULL,
  677. );
  678. $_SERVER['HTTP_HOST'] = $drupal_base_url['host'];
  679. if ($drupal_base_url['scheme'] == 'https') {
  680. $_SERVER['HTTPS'] = 'on';
  681. }
  682. if ($drupal_base_url['port']) {
  683. $_SERVER['HTTP_HOST'] .= ':' . $drupal_base_url['port'];
  684. }
  685. $_SERVER['SERVER_PORT'] = $drupal_base_url['port'];
  686. if (array_key_exists('path', $drupal_base_url)) {
  687. $_SERVER['PHP_SELF'] = $drupal_base_url['path'] . '/index.php';
  688. }
  689. else {
  690. $_SERVER['PHP_SELF'] = '/index.php';
  691. }
  692. }
  693. else {
  694. $_SERVER['HTTP_HOST'] = 'default';
  695. $_SERVER['PHP_SELF'] = '/index.php';
  696. }
  697. $_SERVER['REQUEST_URI'] = $_SERVER['SCRIPT_NAME'] = $_SERVER['PHP_SELF'];
  698. $_SERVER['REMOTE_ADDR'] = '127.0.0.1';
  699. $_SERVER['REQUEST_METHOD'] = NULL;
  700. $_SERVER['SERVER_SOFTWARE'] = NULL;
  701. $_SERVER['HTTP_USER_AGENT'] = NULL;
  702. $site = drush_bootstrap_value('site', $_SERVER['HTTP_HOST']);
  703. $conf_path = drush_bootstrap_value('conf_path', conf_path(TRUE, TRUE));
  704. $conf_file = "./$conf_path/settings.php";
  705. if (!file_exists($conf_file)) {
  706. return drush_bootstrap_error('DRUPAL_SITE_SETTINGS_NOT_FOUND', dt("Could not find a Drupal settings.php file at !file.",
  707. array('!file' => $conf_file)));
  708. }
  709. return TRUE;
  710. }
  711. /**
  712. * Called by _drush_bootstrap_drupal_site to do the main work
  713. * of the drush drupal site bootstrap.
  714. */
  715. function _drush_bootstrap_do_drupal_site() {
  716. $drush_uri = drush_get_context('DRUSH_SELECTED_URI');
  717. drush_set_context('DRUSH_URI', $drush_uri);
  718. $site = drush_set_context('DRUSH_DRUPAL_SITE', drush_bootstrap_value('site'));
  719. $conf_path = drush_set_context('DRUSH_DRUPAL_SITE_ROOT', drush_bootstrap_value('conf_path'));
  720. drush_log(dt("Initialized Drupal site !site at !site_root", array('!site' => $site, '!site_root' => $conf_path)));
  721. _drush_bootstrap_global_options();
  722. }
  723. /**
  724. * Initialize a site on the Drupal root.
  725. *
  726. * We now set various contexts that we determined and confirmed to be valid.
  727. * Additionally we load an optional drushrc.php file in the site directory.
  728. */
  729. function _drush_bootstrap_drupal_site() {
  730. _drush_bootstrap_do_drupal_site();
  731. }
  732. /**
  733. * Initialize and load the Drupal configuration files.
  734. *
  735. * We process and store a normalized set of database credentials
  736. * from the loaded configuration file, so we can validate them
  737. * and access them easily in the future.
  738. *
  739. * Also override Drupal variables as per --variables option.
  740. */
  741. function _drush_bootstrap_drupal_configuration() {
  742. global $conf;
  743. drupal_bootstrap(DRUPAL_BOOTSTRAP_CONFIGURATION);
  744. // Unset drupal error handler and restore drush's one.
  745. if (drush_drupal_major_version() >= 7) {
  746. restore_error_handler();
  747. }
  748. // Force Drupal6 not to store queries since we are not outputting them.
  749. // Don't run poormanscron during Drush request (D7+).
  750. $override = array(
  751. 'dev_query' => FALSE,
  752. 'cron_safe_threshold' => 0,
  753. );
  754. $current_override = drush_get_option_list('variables');
  755. foreach ($current_override as $name => $value) {
  756. if (is_numeric($name) && (strpos($value, '=') !== FALSE)) {
  757. list($name, $value) = explode('=', $value, 2);
  758. }
  759. $override[$name] = $value;
  760. }
  761. $conf = is_array($conf) ? array_merge($conf, $override) : $conf;
  762. // Populate the DRUSH_DB_CREDENTIALS with the fields loaded from the configuration.
  763. $creds = array();
  764. switch (drush_drupal_major_version()) {
  765. case 6:
  766. if (!empty($GLOBALS['db_url'])) {
  767. $url = $GLOBALS['db_url'];
  768. if (is_array($url)) {
  769. $url = $url['default'];
  770. }
  771. $parts = parse_url($url);
  772. $parts += array('pass' => '', 'port' => '');
  773. $creds['driver'] = $parts['scheme'];
  774. $creds['user'] = urldecode($parts['user']);
  775. $creds['host'] = $parts['host'];
  776. $creds['port'] = $parts['port'];
  777. $creds['pass'] = urldecode($parts['pass']);
  778. $creds['name'] = trim($parts['path'], '/');
  779. }
  780. break;
  781. case 7:
  782. default:
  783. if (!empty($GLOBALS['databases']['default']['default'])) {
  784. $conn = $GLOBALS['databases']['default']['default'];
  785. // Fill in defaults to prevent notices.
  786. $conn += array(
  787. 'username' => NULL,
  788. 'host' => NULL,
  789. 'port' => NULL,
  790. 'password' => NULL,
  791. 'database' => NULL,
  792. 'unix_socket' => NULL,
  793. );
  794. $creds['driver'] = $conn['driver'];
  795. $creds['user'] = $conn['username'];
  796. $creds['unix_socket'] = $conn['unix_socket'];
  797. $creds['host'] = $conn['host'];
  798. $creds['port'] = $conn['port'];
  799. $creds['name'] = $conn['database'];
  800. $creds['pass'] = $conn['password'];
  801. }
  802. break;
  803. }
  804. drush_set_context('DRUSH_DB_CREDENTIALS', $creds);
  805. }
  806. /**
  807. * Validate the DRUSH_BOOTSTRAP_DRUPAL_DATABASE phase
  808. *
  809. * Attempt to making a working database connection using the
  810. * database credentials that were loaded during the previous
  811. * phase.
  812. */
  813. function _drush_bootstrap_drupal_database_validate() {
  814. if (!drush_valid_db_credentials()) {
  815. return drush_bootstrap_error('DRUSH_DRUPAL_DB_ERROR');
  816. }
  817. return TRUE;
  818. }
  819. /**
  820. * Boostrap the Drupal database.
  821. */
  822. function _drush_bootstrap_drupal_database() {
  823. drush_log(dt("Successfully connected to the Drupal database."), 'bootstrap');
  824. drupal_bootstrap(DRUPAL_BOOTSTRAP_DATABASE);
  825. }
  826. /**
  827. * Attempt to load the full Drupal system.
  828. */
  829. function _drush_bootstrap_drupal_full() {
  830. if (!drush_get_context('DRUSH_QUIET', FALSE)) {
  831. ob_start();
  832. }
  833. drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL);
  834. if (!drush_get_context('DRUSH_QUIET', FALSE)) {
  835. ob_end_clean();
  836. }
  837. // Unset drupal error handler and restore drush's one.
  838. if (drush_drupal_major_version() == 6) {
  839. restore_error_handler();
  840. }
  841. // If needed, prod module_implements() to recognize our system_watchdog() implementation.
  842. $dogs = module_implements('watchdog');
  843. if (!in_array('system', $dogs)) {
  844. // Note that this resets module_implements cache.
  845. module_implements('watchdog', FALSE, TRUE);
  846. }
  847. _drush_log_drupal_messages();
  848. }
  849. /**
  850. * Log into the bootstrapped Drupal site with a specific
  851. * username or user id.
  852. */
  853. function _drush_bootstrap_drupal_login() {
  854. $drush_user = drush_set_context('DRUSH_USER', drush_get_option('user', 0));
  855. drush_drupal_login($drush_user);
  856. _drush_log_drupal_messages();
  857. }
  858. /**
  859. * Helper function to store any context settings that are being validated.
  860. */
  861. function drush_bootstrap_value($context, $value = null) {
  862. $values =& drush_get_context('DRUSH_BOOTSTRAP_VALUES', array());
  863. if (!is_null($value)) {
  864. $values[$context] = $value;
  865. }
  866. if (array_key_exists($context, $values)) {
  867. return $values[$context];
  868. }
  869. return null;
  870. }
  871. /**
  872. * Prepare drush for bootstrap
  873. *
  874. * All pre-flight checks and includes necessary to begin the bootstrap
  875. * process.
  876. *
  877. * Ran before drush_main().
  878. *
  879. * @see drush_main()
  880. * @see drush.php
  881. */
  882. function drush_bootstrap_prepare() {
  883. define('DRUSH_BASE_PATH', dirname(dirname(__FILE__)));
  884. require_once DRUSH_BASE_PATH . '/includes/environment.inc';
  885. require_once DRUSH_BASE_PATH . '/includes/command.inc';
  886. require_once DRUSH_BASE_PATH . '/includes/drush.inc';
  887. require_once DRUSH_BASE_PATH . '/includes/backend.inc';
  888. require_once DRUSH_BASE_PATH . '/includes/batch.inc';
  889. require_once DRUSH_BASE_PATH . '/includes/context.inc';
  890. require_once DRUSH_BASE_PATH . '/includes/sitealias.inc';
  891. require_once DRUSH_BASE_PATH . '/includes/exec.inc';
  892. require_once DRUSH_BASE_PATH . '/includes/drupal.inc';
  893. require_once DRUSH_BASE_PATH . '/includes/output.inc';
  894. require_once DRUSH_BASE_PATH . '/includes/cache.inc';
  895. require_once DRUSH_BASE_PATH . '/includes/filesystem.inc';
  896. require_once DRUSH_BASE_PATH . '/includes/dbtng.inc';
  897. // Terminate immediately unless invoked as a command line script
  898. if (!drush_verify_cli()) {
  899. return drush_set_error('DRUSH_REQUIREMENTS_ERROR', dt('Drush is designed to run via the command line.'));
  900. }
  901. // Check supported version of PHP.
  902. define('DRUSH_MINIMUM_PHP', '5.2.0');
  903. if (version_compare(phpversion(), DRUSH_MINIMUM_PHP) < 0) {
  904. return drush_set_error('DRUSH_REQUIREMENTS_ERROR', dt('Your command line PHP installation is too old. Drush requires at least PHP !version.', array('!version' => DRUSH_MINIMUM_PHP)));
  905. }
  906. $drush_info = drush_read_drush_info();
  907. define('DRUSH_VERSION', $drush_info['drush_version']);
  908. $version_parts = explode('.', DRUSH_VERSION);
  909. define('DRUSH_MAJOR_VERSION', $version_parts[0]);
  910. define('DRUSH_MINOR_VERSION', $version_parts[1]);
  911. define('DRUSH_REQUEST_TIME', microtime(TRUE));
  912. drush_set_context('argc', $GLOBALS['argc']);
  913. drush_set_context('argv', $GLOBALS['argv']);
  914. // Set an error handler and a shutdown function
  915. set_error_handler('drush_error_handler');
  916. register_shutdown_function('drush_shutdown');
  917. drush_set_context('DRUSH_BOOTSTRAP_PHASE', DRUSH_BOOTSTRAP_NONE);
  918. // We need some global options/arguments processed at this early stage.
  919. drush_parse_args();
  920. }
  921. /**
  922. * Cleanup our bootstrap.
  923. *
  924. * This is ran after we have bootstrapped and dispatched properly in
  925. * drush_main().
  926. *
  927. * @see drush_main()
  928. */
  929. function drush_bootstrap_finish() {
  930. // We set this context to let the shutdown function know we reached the end of drush_main();
  931. drush_set_context("DRUSH_EXECUTION_COMPLETED", TRUE);
  932. }
  933. /**
  934. * Shutdown function for use while Drupal is bootstrapping and to return any
  935. * registered errors.
  936. *
  937. * The shutdown command checks whether certain options are set to reliably
  938. * detect and log some common Drupal initialization errors.
  939. *
  940. * If the command is being executed with the --backend option, the script
  941. * will return a json string containing the options and log information
  942. * used by the script.
  943. *
  944. * The command will exit with '1' if it was successfully executed, and the
  945. * result of drush_get_error() if it wasn't.
  946. */
  947. function drush_shutdown() {
  948. // Mysteriously make $user available during sess_write(). Avoids a NOTICE.
  949. global $user;
  950. if (!drush_get_context('DRUSH_EXECUTION_COMPLETED', FALSE) && !drush_get_context('DRUSH_USER_ABORT', FALSE)) {
  951. $php_error_message = '';
  952. if ($error = error_get_last()) {
  953. $php_error_message = "\n" . dt('Error: !message in !file, line !line', array('!message' => $error['message'], '!file' => $error['file'], '!line' => $error['line']));
  954. }
  955. // We did not reach the end of the drush_main function,
  956. // this generally means somewhere in the code a call to exit(),
  957. // was made. We catch this, so that we can trigger an error in
  958. // those cases.
  959. drush_set_error("DRUSH_NOT_COMPLETED", dt("Drush command terminated abnormally due to an unrecoverable error.!message", array('!message' => $php_error_message)));
  960. // Attempt to give the user some advice about how to fix the problem
  961. _drush_postmortem();
  962. }
  963. $phase = drush_get_context('DRUSH_BOOTSTRAP_PHASE');
  964. if (drush_get_context('DRUSH_BOOTSTRAPPING')) {
  965. switch ($phase) {
  966. case DRUSH_BOOTSTRAP_DRUPAL_FULL :
  967. ob_end_clean();
  968. _drush_log_drupal_messages();
  969. drush_set_error('DRUSH_DRUPAL_BOOTSTRAP_ERROR');
  970. break;
  971. }
  972. }
  973. if (drush_get_context('DRUSH_BACKEND', FALSE)) {
  974. drush_backend_output();
  975. }
  976. elseif (drush_get_context('DRUSH_QUIET', FALSE)) {
  977. ob_end_clean();
  978. // If we are in pipe mode, emit the compact representation of the command, if available.
  979. if (drush_get_context('DRUSH_PIPE')) {
  980. drush_pipe_output();
  981. }
  982. }
  983. /**
  984. * For now, drush skips end of page processing on D7. Doing so could write
  985. * cache entries to module_implements and lookup_cache that don't match web requests.
  986. */
  987. // if (drush_drupal_major_version() >= 7 && function_exists('drupal_page_footer')) {
  988. // drupal_page_footer();
  989. // }
  990. // this way drush_return_status will always be the last shutdown function (unless other shutdown functions register shutdown functions...)
  991. // and won't prevent other registered shutdown functions (IE from numerous cron methods) from running by calling exit() before they get a chance.
  992. register_shutdown_function('drush_return_status');
  993. }
  994. function drush_return_status() {
  995. exit((drush_get_error()) ? DRUSH_FRAMEWORK_ERROR : DRUSH_SUCCESS);
  996. }