Skip to content

Commit

Permalink
Merge pull request #49 from inpsyde/fix/status-check-in-package-proxy…
Browse files Browse the repository at this point in the history
…-container

Fix status check in `PackageProxyContainer`
  • Loading branch information
gmazzap authored Aug 27, 2024
2 parents bd65297 + 5c6b990 commit d4ea195
Show file tree
Hide file tree
Showing 4 changed files with 133 additions and 152 deletions.
4 changes: 2 additions & 2 deletions docs/Package.md
Original file line number Diff line number Diff line change
Expand Up @@ -235,8 +235,8 @@ $theme->boot();

To note:

- `Package::connect()` must be called **before** boot. If called later, no connections happen and it returns `false`
- The package to be connected might be already booted or not. In the second case the connection will happen, but before accessing its services it has to be booted, or an exception will happen.
- `Package::connect()` must be called **before** the package enters the "initialized" status, that is, before calling `Package::boot()` or `Package::build()`. If called later, no connections happen and it returns `false`
- The package to be connected might be already booted or not. In the second case the connection will happen, but before accessing its services it has to be at least built, or an exception will happen.

Package connection is a great way to create reusable libraries and services that can be used by many plugins. For example, it might be possible to have a *library* that has something like this:

Expand Down
4 changes: 3 additions & 1 deletion src/Container/PackageProxyContainer.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,9 @@ private function tryContainer(): bool

/** TODO: We need a better way to deal with status checking besides equality */
if (
$this->package->statusIs(Package::STATUS_READY)
$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->container = $this->package->container();
Expand Down
12 changes: 8 additions & 4 deletions src/Package.php
Original file line number Diff line number Diff line change
Expand Up @@ -275,9 +275,10 @@ public function connect(Package $package): bool

// Don't connect, if already booted or boot failed
$failed = $this->statusIs(self::STATUS_FAILED);
if ($failed || $this->statusIs(self::STATUS_BOOTED)) {
$status = $failed ? 'errored' : 'booted';
$error = "{$errorMessage} to a {$status} package.";
if ($failed || $this->checkStatus(self::STATUS_INITIALIZED, '>=')) {
$reason = $failed ? 'an errored package' : 'a package with a built container';
$status = $failed ? 'failed' : 'built_container';
$error = "{$errorMessage} to {$reason}.";
do_action(
$this->hookName(self::ACTION_FAILED_CONNECTION),
$packageName,
Expand Down Expand Up @@ -315,7 +316,10 @@ static function () use ($package): Properties {

return true;
} catch (\Throwable $throwable) {
if (isset($packageName)) {
if (
isset($packageName)
&& (($this->connectedPackages[$packageName] ?? false) !== true)
) {
$this->connectedPackages[$packageName] = false;
}
$this->handleFailure($throwable, self::ACTION_FAILED_BUILD);
Expand Down
Loading

0 comments on commit d4ea195

Please sign in to comment.