Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix status check in PackageProxyContainer #49

Merged
merged 5 commits into from
Aug 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 */
tfrommen marked this conversation as resolved.
Show resolved Hide resolved
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