Skip to content
Snippets Groups Projects

Initial commit of sime's work

5 files
+ 141
14
Compare changes
  • Side-by-side
  • Inline

Files

+ 56
12
@@ -2,21 +2,36 @@
namespace Drupal\project_browser;
use Drupal\Component\Datetime\TimeInterface;
use Drupal\Core\Cache\CacheBackendInterface;
use Drupal\package_manager\Event\PreApplyEvent;
use Drupal\package_manager\StatusCheckTrait;
use Drupal\package_manager\ValidationResult;
use Drupal\project_browser\ComposerInstaller\Installer;
use Drupal\system\SystemManager;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
/**
* Defines Installer service.
* Performs status checks before installing projects.
*/
class InstallReadiness {
class InstallReadiness implements EventSubscriberInterface {
use StatusCheckTrait;
/**
* The cache key for the status check results.
*
* @var string
*/
private const CACHE_KEY = 'status_check_results';
public function __construct(
private readonly Installer $installer,
private readonly EventDispatcherInterface $eventDispatcher,
private readonly TimeInterface $time,
private readonly CacheBackendInterface $cache,
private readonly int $cacheTimeToLive = 300,
) {}
/**
@@ -26,10 +41,24 @@ class InstallReadiness {
* errors - an array of messages with severity 2
* messages - all other messages below severity 2 (warnings)
*/
public function validatePackageManager() {
$errors = [];
$warnings = [];
foreach ($this->runStatusCheck($this->installer, $this->eventDispatcher) as $result) {
public function validatePackageManager(): array {
if ($cache = $this->cache->get(self::CACHE_KEY)) {
$validation_results = $cache->data;
}
else {
$validation_results = $this->runStatusCheck($this->installer, $this->eventDispatcher);
// Cache the validation results if there were no errors.
if (ValidationResult::getOverallSeverity($validation_results) !== SystemManager::REQUIREMENT_ERROR) {
$this->cache->set(self::CACHE_KEY, $validation_results, $this->time->getRequestTime() + $this->cacheTimeToLive);
}
}
$return = [
'errors' => [],
'warnings' => [],
];
foreach ($validation_results as $result) {
$messages = $result->messages;
$summary = $result->summary;
if ($summary) {
@@ -38,16 +67,14 @@ class InstallReadiness {
$text = implode("\n", $messages);
if ($result->severity === SystemManager::REQUIREMENT_ERROR) {
$errors[] = $text;
$return['errors'][] = $text;
}
else {
$warnings[] = $text;
$return['warnings'][] = $text;
}
}
return [
'errors' => $errors,
'warnings' => $warnings,
];
return $return;
}
/**
@@ -60,4 +87,21 @@ class InstallReadiness {
return $this->installer->isAvailable();
}
/**
* Invalidate the readiness check.
*/
public function clearReadinessCheckCache(): void {
// @todo Under what conditions should we clear the cache?
$this->cache->delete(self::CACHE_KEY);
}
/**
* {@inheritdoc}
*/
public static function getSubscribedEvents(): array {
return [
PreApplyEvent::class => ['clearReadinessCheckCache'],
];
}
}
Loading