Last active
January 21, 2022 13:07
-
-
Save AronNovak/86db5f931b4c9d83ef4337ab3a8f5ba7 to your computer and use it in GitHub Desktop.
RoboFile with Drupal 8 / 9 bootstrapped inside
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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