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

Package - remove deprecated $defaultModules from boot()-method. #53

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
9 changes: 6 additions & 3 deletions docs/Package.md
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ $plugin->boot();
static::assertTrue($myService->isBooted());
```

### Deprecated boot parameters
### Removed boot parameters

Before Modularity v1.7.0, it was an accepted practice to pass default modules to `Package::boot()`,
as in:
Expand All @@ -185,14 +185,17 @@ add_action(
);
```

This is now deprecated to allow a better separation of the "building" and "booting" steps.
This is now removed to allow a better separation of the "building" and "booting" steps.

While it still works (and it will work up to version 2.0), it will emit a deprecation notice.

The replacement is using `Package::addModule()`:

```php
plugin()->addModule(new ModuleOne())->addModule(new ModuleTwo())->boot();
plugin()
->addModule(new ModuleOne())
->addModule(new ModuleTwo())
->boot();
```

There's only one case in which calling `Package::boot()` with default modules will throw an
Expand Down
61 changes: 4 additions & 57 deletions src/Package.php
Original file line number Diff line number Diff line change
Expand Up @@ -357,15 +357,16 @@ public function build(): Package
}

/**
* @param Module ...$defaultModules Deprecated, use `addModule()` to add default modules.
* @return bool
*/
public function boot(Module ...$defaultModules): bool
public function boot(): bool
{
try {
// Call build() if not called yet, and ensure any new module passed here is added
// as well, throwing if the container was already built.
$this->doBuild(...$defaultModules);
if (!$this->built) {
$this->build();
}

// Don't allow booting the application multiple times.
$this->assertStatus(self::STATUS_BOOTING, 'boot application', '<');
Expand All @@ -392,60 +393,6 @@ public function boot(Module ...$defaultModules): bool
return true;
}

/**
* @param Module ...$defaultModules
* @return void
*/
private function doBuild(Module ...$defaultModules): void
{
if ($defaultModules) {
$this->deprecatedArgument(
sprintf(
'Passing default modules to %1$s::boot() is deprecated since version 1.7.0.'
. ' Please add modules via %1$s::addModule().',
__CLASS__
),
__METHOD__,
'1.7.0'
);
}

if (!$this->built) {
$defaultModules and array_map([$this, 'addModule'], $defaultModules);
$this->build();

return;
}

if (
!$defaultModules
|| ($this->checkStatus(self::STATUS_INITIALIZED, '>'))
|| ($this->statusIs(self::STATUS_FAILED))
) {
// If we don't have default modules, there's nothing to do, and if the status is beyond
// initialized or is failed, we do nothing as well and let `boot()` throw.
return;
}

$backup = $this->status;

try {
// simulate idle status to prevent `addModule()` from throwing
// only if we don't have a container yet
$this->hasContainer or $this->status = self::STATUS_IDLE;

foreach ($defaultModules as $defaultModule) {
// If a module was added by `build()` or `addModule()` we can skip it, a
// deprecation was trigger to make it noticeable without breakage
if (!$this->moduleIs($defaultModule->id(), self::MODULE_ADDED)) {
$this->addModule($defaultModule);
}
}
} finally {
$this->status = $backup;
}
}

/**
* @param Module $module
* @param string $status
Expand Down
55 changes: 1 addition & 54 deletions tests/unit/PackageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -231,29 +231,6 @@ public function testBootWithExecutableModuleFailed(): void
static::assertTrue($package->moduleIs($moduleId, Package::MODULE_EXECUTION_FAILED));
}

/**
* @test
* @runInSeparateProcess
*/
public function testBootPassingModulesEmitDeprecation(): void
{
$module1 = $this->stubModule('module_1', ServiceModule::class);
$module1->allows('services')->andReturn($this->stubServices('service_1'));

$package = Package::new($this->stubProperties('test', true));

$this->convertDeprecationsToExceptions();
try {
$count = 0;
$package->boot($module1);
} catch (\Throwable $throwable) {
$count++;
$this->assertThrowableMessageMatches($throwable, 'boot().+?deprecated.+?1\.7');
} finally {
static::assertSame(1, $count);
}
}

/**
* @test
*/
Expand Down Expand Up @@ -821,35 +798,6 @@ public function run(ContainerInterface $container): bool
static::assertSame('Works!', $actual);
}

/**
* @test
*/
public function testBuildPassingModulesToBoot(): void
{
$module1 = $this->stubModule('module_1', ServiceModule::class);
$module1->expects('services')->andReturn($this->stubServices('service_1'));

$module2 = $this->stubModule('module_2', ServiceModule::class);
$module2->expects('services')->andReturn($this->stubServices('service_2'));

$module3 = $this->stubModule('module_3', ServiceModule::class);
$module3->expects('services')->andReturn($this->stubServices('service_3'));

$package = Package::new($this->stubProperties('test', true))
->addModule($module1)
->addModule($module2)
->build();

$this->ignoreDeprecations();
$package->boot($module2, $module3);

$container = $package->container();

static::assertSame('service_1', $container->get('service_1')['id']);
static::assertSame('service_2', $container->get('service_2')['id']);
static::assertSame('service_3', $container->get('service_3')['id']);
}

/**
* @test
*/
Expand All @@ -875,8 +823,7 @@ public function testBootFailsIfPassingNotAddedModulesAfterContainer(): void
static::assertSame('service_2', $container->get('service_2')['id']);

$this->expectExceptionMessageMatches("/can't add module module_3/i");
$this->ignoreDeprecations();
$package->boot($module2, $module3);
$package->addModule($module3);
}

/**
Expand Down
Loading