diff --git a/includes/output.inc b/includes/output.inc index f9ed04b0eb..66e609f8e8 100644 --- a/includes/output.inc +++ b/includes/output.inc @@ -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); diff --git a/lib/Drush/Boot/bootstrap.inc b/lib/Drush/Boot/bootstrap.inc index 9723cc0567..7b24c58aea 100644 --- a/lib/Drush/Boot/bootstrap.inc +++ b/lib/Drush/Boot/bootstrap.inc @@ -15,6 +15,9 @@ * Sequential Drupal bootstrapping phases. */ +use Symfony\Component\HttpFoundation\Request; +use Drupal\Core\DrupalKernel; + /** * No bootstrap. * @@ -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) { @@ -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(); } @@ -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; +}