Skip to content
Snippets Groups Projects
Commit 4c15e96d authored by omkar podey's avatar omkar podey Committed by Adam G-H
Browse files

Issue #3341469 by omkar.podey, Wim Leers: Create...

Issue #3341469 by omkar.podey, Wim Leers: Create StageBase::stageDirectoryExists() for improved DX to see if stage directory exists
parent e9acf9d4
No related branches found
No related tags found
No related merge requests found
......@@ -143,6 +143,12 @@
* post-destroy events. It is possible to destroy the stage without having
* claimed it first, but this shouldn't be done unless absolutely necessary.
*
* - \Drupal\package_manager\StageBase::stageDirectoryExists()
* Determines if the stage directory exists and returns a boolean accordingly.
* This allows validators to directly know if the stage directory exists
* without using \Drupal\package_manager\StageBase::getStageDirectory(), which
* throws an exception if the stage directory does not exist.
*
* @section sec_stage_exceptions Stage life cycle exceptions
* If problems occur during any point of the stage life cycle, a
* \Drupal\package_manager\Exception\StageException is thrown. If problems are
......
......@@ -738,6 +738,22 @@ abstract class StageBase implements LoggerAwareInterface {
return $dir;
}
/**
* Determines if the stage directory exists.
*
* @return bool
* TRUE if the directory exists, otherwise FALSE.
*/
public function stageDirectoryExists(): bool {
try {
$this->getStageDirectory();
return TRUE;
}
catch (\LogicException $e) {
return FALSE;
}
}
/**
* Checks if staged changes are being applied to the active directory.
*
......
......@@ -47,14 +47,10 @@ class StagedDBUpdateValidator implements EventSubscriberInterface {
* The event object.
*/
public function checkForStagedDatabaseUpdates(StatusCheckEvent $event): void {
try {
$stage_dir = $event->stage->getStageDirectory();
}
catch (\LogicException) {
// Stage directory can't be determined, so there's nothing to validate.
if (!$event->stage->stageDirectoryExists()) {
return;
}
$stage_dir = $event->stage->getStageDirectory();
$extensions_with_updates = $this->getExtensionsWithDatabaseUpdates($stage_dir);
if ($extensions_with_updates) {
$extensions_with_updates = array_map($this->t(...), $extensions_with_updates);
......
......@@ -53,12 +53,10 @@ class SymlinkValidator implements EventSubscriberInterface {
// that contains this file, which contains only a few files and no symlinks,
// as the stage directory. The precondition itself doesn't care if the
// directory actually exists or not.
try {
$stage_dir = __DIR__;
if ($event->stage->stageDirectoryExists()) {
$stage_dir = $event->stage->getStageDirectory();
}
catch (\LogicException) {
$stage_dir = __DIR__;
}
$stage_dir = $this->pathFactory->create($stage_dir);
$excluded_paths = $event->getExcludedPaths();
......
......@@ -651,6 +651,16 @@ class StageBaseTest extends PackageManagerKernelTestBase {
$stage->apply();
}
/**
* @covers ::stageDirectoryExists
*/
public function testStageDirectoryExists(): void {
$stage = $this->createStage();
$this->assertFalse($stage->stageDirectoryExists());
$stage->create();
$this->assertTrue($stage->stageDirectoryExists());
}
}
/**
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment