Skip to content

Commit d4ea195

Browse files
authored
Merge pull request #49 from inpsyde/fix/status-check-in-package-proxy-container
Fix status check in `PackageProxyContainer`
2 parents bd65297 + 5c6b990 commit d4ea195

File tree

4 files changed

+133
-152
lines changed

4 files changed

+133
-152
lines changed

docs/Package.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -235,8 +235,8 @@ $theme->boot();
235235

236236
To note:
237237

238-
- `Package::connect()` must be called **before** boot. If called later, no connections happen and it returns `false`
239-
- 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.
238+
- `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`
239+
- 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.
240240

241241
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:
242242

src/Container/PackageProxyContainer.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,9 @@ private function tryContainer(): bool
5555

5656
/** TODO: We need a better way to deal with status checking besides equality */
5757
if (
58-
$this->package->statusIs(Package::STATUS_READY)
58+
$this->package->statusIs(Package::STATUS_INITIALIZED)
59+
|| $this->package->statusIs(Package::STATUS_MODULES_ADDED)
60+
|| $this->package->statusIs(Package::STATUS_READY)
5961
|| $this->package->statusIs(Package::STATUS_BOOTED)
6062
) {
6163
$this->container = $this->package->container();

src/Package.php

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -275,9 +275,10 @@ public function connect(Package $package): bool
275275

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

316317
return true;
317318
} catch (\Throwable $throwable) {
318-
if (isset($packageName)) {
319+
if (
320+
isset($packageName)
321+
&& (($this->connectedPackages[$packageName] ?? false) !== true)
322+
) {
319323
$this->connectedPackages[$packageName] = false;
320324
}
321325
$this->handleFailure($throwable, self::ACTION_FAILED_BUILD);

0 commit comments

Comments
 (0)