Skip to content

Commit

Permalink
Merge pull request drush-ops#857 from alexpott/drupal8-remove-drupal-…
Browse files Browse the repository at this point in the history
…bootstrap

Remove dependency on Drupal core's drupal_bootstrap for Drupal 8
  • Loading branch information
weitzman committed Nov 21, 2014
2 parents c53f71d + bf506a2 commit 3a7ff99
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 5 deletions.
7 changes: 4 additions & 3 deletions includes/output.inc
Original file line number Diff line number Diff line change
Expand Up @@ -147,9 +147,10 @@ function dt($string, $args = array()) {
if (function_exists('t') && drush_drupal_major_version() == 7) {
$output = t($string, $args);
}
// The language system requires a working container which is built in DRUPAL_BOOTSTRAP_KERNEL phase.
else if (drush_drupal_major_version() >= 8 && function_exists('drupal_get_bootstrap_phase') && drupal_get_bootstrap_phase() >= DRUPAL_BOOTSTRAP_KERNEL) {
$output = t($string, $args);
// The language system requires a working container which has the string
// translation service.
else if (drush_drupal_major_version() >= 8 && \Drupal::hasService('string_translation')) {
$output = \Drupal::translation()->translate($string, $args);
}
else if (function_exists('t') && drush_drupal_major_version() <= 7 && function_exists('theme')) {
$output = t($string, $args);
Expand Down
72 changes: 70 additions & 2 deletions lib/Drush/Boot/bootstrap.inc
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@
* Sequential Drupal bootstrapping phases.
*/

use Symfony\Component\HttpFoundation\Request;
use Drupal\Core\DrupalKernel;

/**
* No bootstrap.
*
Expand Down Expand Up @@ -619,7 +622,12 @@ function _drush_bootstrap_drupal_site() {
function _drush_bootstrap_drupal_configuration() {
global $conf;

drupal_bootstrap(DRUPAL_BOOTSTRAP_CONFIGURATION);
if (drush_drupal_major_version() <= 7) {
drupal_bootstrap(DRUPAL_BOOTSTRAP_CONFIGURATION);
}
else {
drupal8_bootstrap(DRUPAL_BOOTSTRAP_CONFIGURATION);
}

// Unset drupal error handler and restore drush's one.
if (drush_drupal_major_version() >= 7) {
Expand Down Expand Up @@ -673,7 +681,12 @@ function _drush_bootstrap_drupal_full() {
if (!drush_get_context('DRUSH_QUIET', FALSE)) {
ob_start();
}
drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL);
if (drush_drupal_major_version() <= 7) {
drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL);
}
else {
drupal8_bootstrap(DRUPAL_BOOTSTRAP_FULL);
}
if (!drush_get_context('DRUSH_QUIET', FALSE)) {
ob_end_clean();
}
Expand Down Expand Up @@ -759,3 +772,58 @@ function drush_bootstrap_value($context, $value = null) {

return null;
}


/**
* Ensures Drupal 8 is bootstrapped to the specified phase.
*
* In order to bootstrap Drupal from another PHP script, you can use this code:
* @code
* require_once '/path/to/drupal/core/vendor/autoload.php';
* require_once '/path/to/drupal/core/includes/bootstrap.inc';
* drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL);
* @endcode
*
* @param $phase
* A constant telling which phase to bootstrap to. Possible values:
* - DRUPAL_BOOTSTRAP_CONFIGURATION: Initializes configuration.
* - DRUPAL_BOOTSTRAP_KERNEL: Initializes a kernel.
*
* @return int
* The most recently completed phase.
*/
function drupal8_bootstrap($phase = NULL) {
// Temporary variables used for booting later legacy phases.
/** @var \Drupal\Core\DrupalKernel $kernel */
static $kernel;
static $boot_level = 0;

if (isset($phase)) {
$request = Request::createFromGlobals();
for ($current_phase = $boot_level; $current_phase <= $phase; $current_phase++) {

switch ($current_phase) {
case DRUPAL_BOOTSTRAP_CONFIGURATION:
$classloader = require DRUSH_DRUPAL_CORE . '/vendor/autoload.php';
$kernel = DrupalKernel::createFromRequest($request, $classloader, 'prod');
break;

case DRUPAL_BOOTSTRAP_KERNEL:
$kernel->boot();
break;

case DRUPAL_BOOTSTRAP_PAGE_CACHE:
$kernel->handlePageCache($request);
break;

case DRUPAL_BOOTSTRAP_CODE:
case DRUPAL_BOOTSTRAP_FULL:
$kernel->prepareLegacyRequest($request);
break;
}
}
$boot_level = $phase;
}

return \Drupal::getContainer() ? DRUPAL_BOOTSTRAP_CODE : DRUPAL_BOOTSTRAP_CONFIGURATION;
}

0 comments on commit 3a7ff99

Please sign in to comment.