Skip to content

Instantly share code, notes, and snippets.

@AronNovak
Last active January 21, 2022 13:07
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AronNovak/86db5f931b4c9d83ef4337ab3a8f5ba7 to your computer and use it in GitHub Desktop.
Save AronNovak/86db5f931b4c9d83ef4337ab3a8f5ba7 to your computer and use it in GitHub Desktop.
RoboFile with Drupal 8 / 9 bootstrapped inside
<?php
use Robo\Tasks;
use Drupal\Core\DrupalKernel;
use Symfony\Component\HttpFoundation\Request;
$GLOBALS['drupal_autoloader'] = require_once 'web/autoload.php';
/**
* Sample RoboFile.
*/
class RoboFile extends Tasks {
/**
* Bootstraps Drupal 8/9 in addition to Robo.
*
* @throws \Exception
*/
public function __construct() {
if (!file_exists('web/sites/default/settings.php')) {
// Drupal is not yet initialized, do not attempt to boot.
return;
}
try {
chdir(__DIR__ . '/web');
$request = Request::createFromGlobals();
$kernel = DrupalKernel::createFromRequest($request, $GLOBALS['drupal_autoloader'], 'prod');
$kernel->handle($request);
chdir(__DIR__);
}
catch (\Exception $e) {
// Do not fail, there are several commands that do not need Drupal.
$this->yell($e->getMessage());
}
}
/**
* Utilize Drupal.
*/
public function invokeDrupal() {
$configFactory = \Drupal::service('config.factory');
$this->say('it works');
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment