Skip to content

Commit

Permalink
Improve Package status check from ouside the Package class
Browse files Browse the repository at this point in the history
- Introduced Package::hasContainer(), Package::hasFailed(), Package::hasReachedStatus() to ease status check from ouside the Package class
- Use the above methods to improve PackageProxyContainer access to the proxied container

Co-authored-by: Thorsten Frommen <info@tfrommen.de>
Signed-off-by: Giuseppe Mazzapica <giuseppe.mazzapica@gmail.com>
  • Loading branch information
gmazzap and tfrommen authored Aug 29, 2024
1 parent d4ea195 commit 8663163
Show file tree
Hide file tree
Showing 7 changed files with 72 additions and 8 deletions.
7 changes: 2 additions & 5 deletions src/Container/PackageProxyContainer.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,12 +53,9 @@ private function tryContainer(): bool
return true;
}

/** TODO: We need a better way to deal with status checking besides equality */
if (
$this->package->statusIs(Package::STATUS_INITIALIZED)
|| $this->package->statusIs(Package::STATUS_MODULES_ADDED)
|| $this->package->statusIs(Package::STATUS_READY)
|| $this->package->statusIs(Package::STATUS_BOOTED)
$this->package->hasContainer()
|| $this->package->hasReachedStatus(Package::STATUS_INITIALIZED)
) {
$this->container = $this->package->container();
}
Expand Down
41 changes: 39 additions & 2 deletions src/Package.php
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,14 @@ class Package
public const STATUS_BOOTED = 8;
public const STATUS_FAILED = -8;

private const SUCCESS_STATUSES = [
self::STATUS_IDLE => self::STATUS_IDLE,
self::STATUS_INITIALIZED => self::STATUS_INITIALIZED,
self::STATUS_BOOTING => self::STATUS_BOOTING,
self::STATUS_READY => self::STATUS_READY,
self::STATUS_BOOTED => self::STATUS_BOOTED,
];

private const OPERATORS = [
'<' => '<',
'<=' => '<=',
Expand Down Expand Up @@ -274,8 +282,8 @@ public function connect(Package $package): bool
}

// Don't connect, if already booted or boot failed
$failed = $this->statusIs(self::STATUS_FAILED);
if ($failed || $this->checkStatus(self::STATUS_INITIALIZED, '>=')) {
$failed = $this->hasFailed();
if ($failed || $this->hasReachedStatus(self::STATUS_INITIALIZED)) {
$reason = $failed ? 'an errored package' : 'a package with a built container';
$status = $failed ? 'failed' : 'built_container';
$error = "{$errorMessage} to {$reason}.";
Expand Down Expand Up @@ -606,6 +614,14 @@ public function container(): ContainerInterface
return $this->containerConfigurator->createReadOnlyContainer();
}

/**
* @return bool
*/
public function hasContainer(): bool
{
return $this->hasContainer;
}

/**
* @return string
*/
Expand All @@ -623,6 +639,27 @@ public function statusIs(int $status): bool
return $this->checkStatus($status);
}

/**
* @return bool
*/
public function hasFailed(): bool
{
return $this->status === self::STATUS_FAILED;
}

/**
* @param int $status
* @return bool
*/
public function hasReachedStatus(int $status): bool
{
if ($this->hasFailed()) {
return false;
}

return isset(self::SUCCESS_STATUSES[$status]) && $this->checkStatus($status, '>=');
}

/**
* @param int $status
* @param value-of<Package::OPERATORS> $operator
Expand Down
3 changes: 2 additions & 1 deletion src/Properties/BaseProperties.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,14 @@ class BaseProperties implements Properties
protected string $baseName;
protected string $basePath;
protected ?string $baseUrl;
/** @var array<string, mixed> */
protected array $properties;

/**
* @param string $baseName
* @param string $basePath
* @param string|null $baseUrl
* @param array $properties
* @param array<string, mixed> $properties
*/
protected function __construct(
string $baseName,
Expand Down
1 change: 1 addition & 0 deletions src/Properties/PluginProperties.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ protected function __construct(string $pluginMainFile)
$properties[$key] = $pluginData[$pluginDataKey] ?? '';
unset($pluginData[$pluginDataKey]);
}
/** @var array<string, mixed> $properties */
$properties = array_merge($properties, $pluginData);

$this->pluginMainFile = wp_normalize_path($pluginMainFile);
Expand Down
1 change: 1 addition & 0 deletions tests/unit/Container/ContainerConfiguratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -526,6 +526,7 @@ public function testCustomContainer(): void

$childContainer = new class ($expectedId, $expectedValue) implements ContainerInterface
{
/** @var array<string, object> */
private array $values = [];

public function __construct(string $expectedId, object $expectedValue)
Expand Down
1 change: 1 addition & 0 deletions tests/unit/Container/ReadOnlyContainerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ public function testHasGetServiceFromChildContainer(): void

$childContainer = new class ($expectedKey, $expectedValue) implements ContainerInterface
{
/** @var array<string, \stdClass> */
private array $data = [];

public function __construct(string $key, \stdClass $value)
Expand Down
26 changes: 26 additions & 0 deletions tests/unit/PackageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,28 @@ public function testBasic(): void
$package = Package::new($propertiesStub);

static::assertTrue($package->statusIs(Package::STATUS_IDLE));
static::assertTrue($package->hasReachedStatus(Package::STATUS_IDLE));
static::assertFalse($package->hasReachedStatus(Package::STATUS_INITIALIZED));
static::assertFalse($package->hasReachedStatus(Package::STATUS_BOOTING));
static::assertFalse($package->hasReachedStatus(Package::STATUS_BOOTED));

$package->build();

static::assertFalse($package->statusIs(Package::STATUS_IDLE));
static::assertTrue($package->hasReachedStatus(Package::STATUS_IDLE));
static::assertTrue($package->hasReachedStatus(Package::STATUS_INITIALIZED));
static::assertFalse($package->hasReachedStatus(Package::STATUS_BOOTING));
static::assertFalse($package->hasReachedStatus(Package::STATUS_BOOTED));

static::assertTrue($package->boot());

static::assertTrue($package->statusIs(Package::STATUS_BOOTED));
static::assertTrue($package->hasReachedStatus(Package::STATUS_IDLE));
static::assertTrue($package->hasReachedStatus(Package::STATUS_INITIALIZED));
static::assertTrue($package->hasReachedStatus(Package::STATUS_BOOTING));
static::assertTrue($package->hasReachedStatus(Package::STATUS_BOOTED));
static::assertFalse($package->hasReachedStatus(3));

static::assertSame($expectedName, $package->name());
static::assertInstanceOf(Properties::class, $package->properties());
static::assertInstanceOf(ContainerInterface::class, $package->container());
Expand Down Expand Up @@ -900,6 +920,8 @@ public function testFailureFlowWithFailureOnBootDebugModeOff(): void

static::assertFalse($package->boot());
static::assertTrue($package->statusIs(Package::STATUS_FAILED));
static::assertTrue($package->hasFailed());
static::assertFalse($package->hasReachedStatus(Package::STATUS_IDLE));
}

/**
Expand Down Expand Up @@ -971,6 +993,7 @@ function (\Throwable $throwable) use ($exception, $package): void {
);

static::assertFalse($package->addModule($module1)->addModule($module2)->build()->boot());
static::assertTrue($package->hasFailed());
static::assertTrue($package->statusIs(Package::STATUS_FAILED));
}

Expand Down Expand Up @@ -1025,6 +1048,7 @@ function (\Throwable $throwable) use ($exception, $package): void {
static::assertFalse($package->connect($connected));
static::assertFalse($package->boot());
static::assertTrue($package->statusIs(Package::STATUS_FAILED));
static::assertTrue($package->hasFailed());
}

/**
Expand Down Expand Up @@ -1066,6 +1090,7 @@ function (\Throwable $throwable) use ($exception, $package): void {

static::assertFalse($package->build()->boot());
static::assertTrue($package->statusIs(Package::STATUS_FAILED));
static::assertTrue($package->hasFailed());
}

/**
Expand All @@ -1089,6 +1114,7 @@ public function testFailureFlowWithFailureOnBuildDebugModeOn(): void
static function (\Throwable $throwable) use ($exception, $package): void {
static::assertSame($exception, $throwable);
static::assertTrue($package->statusIs(Package::STATUS_FAILED));
static::assertTrue($package->hasFailed());
}
);

Expand Down

0 comments on commit 8663163

Please sign in to comment.