Skip to content

Commit

Permalink
NEW DatabaselessKernel to support operation without DB
Browse files Browse the repository at this point in the history
This is required for GraphQL code generation in CI (without a working runtime database/webserver environment).
Context: silverstripe/silverstripe-graphql#388
  • Loading branch information
chillu committed Jul 12, 2021
1 parent ae61be3 commit 625e724
Show file tree
Hide file tree
Showing 4 changed files with 385 additions and 0 deletions.
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
"psr/container": "1.0.0",
"silverstripe/config": "^1@dev",
"silverstripe/assets": "^1@dev",
"silverstripe/event-dispatcher": "^0.1.0",
"silverstripe/vendor-plugin": "^1.4",
"sminnee/callbacklist": "^0.1",
"swiftmailer/swiftmailer": "~5.4",
Expand Down
53 changes: 53 additions & 0 deletions src/Core/DatabaselessKernel.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?php

namespace SilverStripe\Core;

use SilverStripe\EventDispatcher\Dispatch\Dispatcher;
use SilverStripe\EventDispatcher\Event\EventContextInterface;
use SilverStripe\EventDispatcher\Event\EventHandlerInterface;
use SilverStripe\ORM\Connect\NullDatabase;
use SilverStripe\ORM\DB;

/**
* Boot a kernel without requiring a database connection.
* This is a workaround for the lack of composition in the boot stages
* of CoreKernel, as well as for the framework's misguided assumptions
* around the availability of a database for every execution path.
*
* @internal
*/
class DatabaselessKernel extends CoreKernel
{
protected $queryErrorMessage = 'Booted with DatabaseLessKernel, cannot execute query: %s';

/**
* Allows disabling of the configured error handling.
* This can be useful to ensure the execution context (e.g. composer)
* can consistently use its own error handling.
*
* @var boolean
*/
protected $bootErrorHandling = true;

public function setBootErrorHandling(bool $bool)
{
$this->bootErrorHandling = $bool;
return $this;
}

public function boot($flush = false)
{
$this->flush = $flush;

$this->bootPHP();
$this->bootManifests($flush);

if ($this->bootErrorHandling) {
$this->bootErrorHandling();
}

$this->bootConfigs();

$this->booted = true;
}
}
17 changes: 17 additions & 0 deletions src/ORM/Connect/Database.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
use SilverStripe\ORM\PaginatedList;
use SilverStripe\ORM\Queries\SQLUpdate;
use SilverStripe\ORM\Queries\SQLInsert;
use SilverStripe\EventDispatcher\Dispatch\Dispatcher;
use SilverStripe\EventDispatcher\Symfony\Event;
use BadMethodCallException;
use Exception;
use SilverStripe\Dev\Backtrace;
Expand Down Expand Up @@ -151,6 +153,13 @@ public function query($sql, $errorLevel = E_USER_ERROR)
return null;
}

Dispatcher::singleton()->trigger('database.query', Event::create(
null,
[
'sql' => $sql
]
));

// Benchmark query
$connector = $this->connector;
return $this->benchmarkQuery(
Expand All @@ -177,6 +186,14 @@ public function preparedQuery($sql, $parameters, $errorLevel = E_USER_ERROR)
return null;
}

Dispatcher::singleton()->trigger('database.preparedQuery', Event::create(
null,
[
'sql' => $sql,
'parameters' => $parameters
]
));

// Benchmark query
$connector = $this->connector;
return $this->benchmarkQuery(
Expand Down
Loading

0 comments on commit 625e724

Please sign in to comment.