Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Autoload bootstrap #1165

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 7 additions & 10 deletions drush.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,17 +38,15 @@ function drush_main() {
register_shutdown_function('drush_coverage_shutdown');
}

// Load the Drush configuration files.
// Load the global Drush configuration files, and global Drush commands.
drush_preflight();

/* Set up bootstrap object, so that
* - 'early' files can bootstrap when needed.
* - bootstrap constants are available.
*/
$bootstrap_class = drush_get_option('bootstrap_class', 'Drush\Boot\DrupalBoot');
$bootstrap = new $bootstrap_class;
drush_set_context('DRUSH_BOOTSTRAP_OBJECT', $bootstrap);
$bootstrap->preflight();
// Find the selected site based on --root, --uri or cwd, and
// return the bootstrap object that goes with it.
$bootstrap = _drush_preflight_root();
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the part of the PR that is most important to me. This and the corresponding code re-org that goes with it makes the Drush bootstrap much cleaner, and I'd rather not go keep the existing code around for all of the Drush 7 stable branch's life.


// Preflight the selected site, and load any configuration and commandfiles associated with it.
drush_preflight_site();

$return = '';
if (!drush_get_error()) {
Expand All @@ -70,7 +68,6 @@ function drush_main() {
// perhaps handling immediately.
$command_handled = drush_preflight_command_dispatch();
if (!$command_handled) {
$bootstrap = drush_get_context('DRUSH_BOOTSTRAP_OBJECT');
$return = $bootstrap->bootstrap_and_dispatch();
}
}
Expand Down
130 changes: 130 additions & 0 deletions includes/bootstrap.inc
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
<?php


/**
* No bootstrap.
*
* This constant is only used to indicate that the bootstrap process has
* not started yet. It is not possible to have no bootstrap.
*/
define('DRUSH_BOOTSTRAP_NONE', -1);

/**
* Use drush_bootstrap_max instead of drush_bootstrap_to_phase
*
* This constant is only usable as the value of the 'bootstrap'
* item of a command object, or as the parameter to
* drush_bootstrap_to_phase. It is not a real bootstrap state.
*/
define('DRUSH_BOOTSTRAP_MAX', -2);

/**
* @deprecated
*
* No longer used, but 0 remains reserved. Drush always runs preflight.
* Commands may alternatively use DRUSH_BOOTSTRAP_NONE.
*/
define('DRUSH_BOOTSTRAP_DRUSH', 0);

/**
* Set up and test for a valid drupal root, either through the -r/--root options,
* or evaluated based on the current working directory.
*
* Any code that interacts with an entire Drupal installation, and not a specific
* site on the Drupal installation should use this bootstrap phase.
*/
define('DRUSH_BOOTSTRAP_DRUPAL_ROOT', 1);

/**
* Set up a Drupal site directory and the correct environment variables to
* allow Drupal to find the configuration file.
*
* If no site is specified with the -l / --uri options, Drush will assume the
* site is 'default', which mimics Drupal's behaviour.
*
* If you want to avoid this behaviour, it is recommended that you use the
* DRUSH_BOOTSTRAP_DRUPAL_ROOT bootstrap phase instead.
*
* Any code that needs to modify or interact with a specific Drupal site's
* settings.php file should bootstrap to this phase.
*/
define('DRUSH_BOOTSTRAP_DRUPAL_SITE', 2);

/**
* Load the settings from the Drupal sites directory.
*
* This phase is analagous to the DRUPAL_BOOTSTRAP_CONFIGURATION bootstrap phase in Drupal
* itself, and this is also the first step where Drupal specific code is included.
*
* This phase is commonly used for code that interacts with the Drupal install API,
* as both install.php and update.php start at this phase.
*/
define('DRUSH_BOOTSTRAP_DRUPAL_CONFIGURATION', 3);

/**
* Connect to the Drupal database using the database credentials loaded
* during the previous bootstrap phase.
*
* This phase is analogous to the DRUPAL_BOOTSTRAP_DATABASE bootstrap phase in
* Drupal.
*
* Any code that needs to interact with the Drupal database API needs to
* be bootstrapped to at least this phase.
*/
define('DRUSH_BOOTSTRAP_DRUPAL_DATABASE', 4);

/**
* Fully initialize Drupal.
*
* This is analogous to the DRUPAL_BOOTSTRAP_FULL bootstrap phase in
* Drupal.
*
* Any code that interacts with the general Drupal API should be
* bootstrapped to this phase.
*/
define('DRUSH_BOOTSTRAP_DRUPAL_FULL', 5);

/**
* Log in to the initialiased Drupal site.
*
* This is the default bootstrap phase all commands will try to reach,
* unless otherwise specified.
*
* This bootstrap phase is used after the site has been
* fully bootstrapped.
*
* This phase will log you in to the drupal site with the username
* or user ID specified by the --user/ -u option.
*
* Use this bootstrap phase for your command if you need to have access
* to information for a specific user, such as listing nodes that might
* be different based on who is logged in.
*/
define('DRUSH_BOOTSTRAP_DRUPAL_LOGIN', 6);

/**
* Used by a Drush extension to request that its Composer autoload
* files be loaded by Drush, if they have not already been.
*
* Usage:
*
* function myextension_init() {
* drush_autoload(__FILE__)
* }
*/
function drush_autoload($commandfile) {
$already_added = commandfiles_cache()->add($commandfile);

if (!$already_added) {
$dir = dirname($commandfile);
$candidates = array("vendor/autoload.php", "../../../vendor/autoload.php");
$drush_autoload_file = drush_get_context('DRUSH_VENDOR_PATH', '');

foreach ($candidates as $candidate) {
$autoload = $dir . '/' . $candidate;
if (file_exists($autoload) && (realpath($autoload) != $drush_autoload_file)) {
include $autoload;
}
}
}
}
61 changes: 24 additions & 37 deletions includes/command.inc
Original file line number Diff line number Diff line change
Expand Up @@ -1365,14 +1365,12 @@ function drush_command_normalize_name($command_name) {
* command files.
*/
function drush_commandfile_list() {
return drush_get_context('DRUSH_COMMAND_FILES', array());
return commandfiles_cache()->get();
}

function _drush_add_commandfiles($searchpath, $phase = NULL, $reset = FALSE) {
static $evaluated = array();
static $deferred = array();

$cache =& drush_get_context('DRUSH_COMMAND_FILES', array());
$needs_sort = FALSE;

if (count($searchpath)) {
if (!$reset) {
Expand All @@ -1394,7 +1392,7 @@ function _drush_add_commandfiles($searchpath, $phase = NULL, $reset = FALSE) {

// Build a list of all of the modules to attempt to load.
// Start with any modules deferred from a previous phase.
$list = $deferred;
$list = commandfiles_cache()->deferred();
if (isset($cached_list)) {
$list = array_merge($list, $cached_list);
}
Expand Down Expand Up @@ -1423,43 +1421,23 @@ function _drush_add_commandfiles($searchpath, $phase = NULL, $reset = FALSE) {
// Check to see if the commandfile is valid for this version of Drupal
// and is still present on filesystem (in case of cached commandfile list).
foreach ($list as $module => $filename) {
$load_command = TRUE;
$module_versionless = preg_replace('/\.d([0-9]+)$/', '', $module);
if (!isset($cache[$module_versionless])) {
$drupal_version = '';
if (preg_match('/\.d([0-9]+)$/', $module, $matches)) {
$drupal_version = $matches[1];
}
if (!empty($drupal_version) && ($drupal_version != drush_drupal_major_version())) {
$load_command = FALSE;
}
// Only try to require if the file exists. If not, a file from the
// command file cache may not be available anymore, in which case
// we rebuild the cache for this phase.
if ($filepath = realpath($filename)) {
$load_command = commandfiles_cache()->add($filepath);
if ($load_command) {
// Only try to require if the file exists. If not, a file from the
// command file cache may not be available anymore, in which case
// we rebuild the cache for this phase.
if ($filepath = realpath($filename)) {
$cache[$module_versionless] = $filename;
require_once $filepath;
unset($deferred[$module]);
}
elseif (!$reset) {
_drush_add_commandfiles($searchpath, $phase, TRUE);
}
}
else {
unset($list[$module]);
// Signal that we should try again on
// the next bootstrap phase. We set
// the flag to the filename of the first
// module we find so that only that one
// will be retried.
$deferred[$module] = $filename;
$needs_sort = TRUE;
}
}
elseif (!$reset) {
_drush_add_commandfiles($searchpath, $phase, TRUE);
$needs_sort = FALSE;
}
}

if (count($list)) {
ksort($cache);
if ($needs_sort) {
commandfiles_cache()->sort();
}
}
}
Expand Down Expand Up @@ -1794,3 +1772,12 @@ function drush_shell_alias_replace() {
_drush_preflight_global_options();
}
}

function commandfiles_cache() {
static $commandfiles_cache = NULL;

if (!isset($commandfiles_cache)) {
$commandfiles_cache = new Drush\Command\Commandfiles();
}
return $commandfiles_cache;
}
1 change: 1 addition & 0 deletions includes/drupal.inc
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ function drush_drupal_load_autoloader($drupal_root, $new_autoloader = NULL) {
$autoloader = $new_autoloader;
}
if (!$autoloader) {
// Should this just be 'require'? See drush_preflight_prepare()
$autoloader = require_once $drupal_root .'/core/vendor/autoload.php';
}
return $autoloader;
Expand Down
20 changes: 9 additions & 11 deletions includes/environment.inc
Original file line number Diff line number Diff line change
Expand Up @@ -352,20 +352,18 @@ function drush_locate_root($start_path = NULL) {
* a Drupal root.
*/
function drush_valid_drupal_root($path) {
if (!empty($path) && is_dir($path) && file_exists($path . '/index.php')) {
// Drupal 8 root. Additional check for the presence of core/composer.json to
// grant it is not a Drupal 7 site with a base folder named "core".
$candidate = 'core/includes/common.inc';
if (file_exists($path . '/' . $candidate) && file_exists($path . '/core/misc/drupal.js') && file_exists($path . '/core/core.services.yml')) {
return $candidate;
}
// Drupal 7 root.
$candidate = 'includes/common.inc';
if (file_exists($path . '/' . $candidate) && file_exists($path . '/misc/drupal.js')) {
$bootstrap_class = drush_bootstrap_class_for_root($path);
return $bootstrap_class != NULL;
}

function drush_bootstrap_class_for_root($path) {
$candidates = drush_preflight_get_bootstrap_candidates();
foreach ($candidates as $candidate) {
if ($candidate->valid_root($path)) {
return $candidate;
}
}
return FALSE;
return NULL;
}

/**
Expand Down
Loading