From 83f819c9de14305ba0db1c3f5a86bc102d3f553d Mon Sep 17 00:00:00 2001 From: Imani Manyara Date: Mon, 5 Oct 2026 05:21:04 -0400 Subject: [PATCH] Base the install command on package-tools' InstallCommand Owner decision 6: install commands subclass laranail/package-tools' InstallCommand and keep laranail/console's display API through its two traits, so neither package depends on the other. A contract test pins the name, option, description, visibility, output and exit codes; it passed against the old base before the change. --- CHANGELOG.md | 11 +++ src/Commands/InstallCommand.php | 48 +++++++++-- src/Providers/ConfettiServiceProvider.php | 5 ++ tests/Feature/InstallCommandContractTest.php | 88 ++++++++++++++++++++ 4 files changed, 145 insertions(+), 7 deletions(-) create mode 100644 tests/Feature/InstallCommandContractTest.php diff --git a/CHANGELOG.md b/CHANGELOG.md index dda54ce..55132de 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,17 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] +### Changed + +- `laranail::confetti.install` now extends laranail/package-tools' + `InstallCommand` and takes laranail/console's display API and run lifecycle + from its `InteractsWithConsoleServices` and `InteractsWithConsoleWriter` + traits instead of its `Command` base. Name, option, description, listing + visibility, output and exit codes are unchanged and pinned by a new contract + test. The command is bound in the container because the new base takes the + `Package` in its constructor, and `handle()`'s `ConfettiConfig` parameter is + now optional so the override stays compatible with the base's `handle(): int`. + ### Fixed - **`tests.yml` no longer skips a markdown-only pull request.** The `pest` diff --git a/src/Commands/InstallCommand.php b/src/Commands/InstallCommand.php index 6ef9cdd..6765cae 100644 --- a/src/Commands/InstallCommand.php +++ b/src/Commands/InstallCommand.php @@ -4,10 +4,12 @@ namespace Simtabi\Laranail\Confetti\Commands; +use Simtabi\Laranail\Package\Tools\Package; use Simtabi\Laranail\Confetti\Enums\AssetMode; -use Simtabi\Laranail\Console\Tools\Commands\Command; use Simtabi\Laranail\Confetti\Support\ConfettiConfig; -use Simtabi\Laranail\Console\Tools\Commands\Concerns\SupportsNamespacedNames; +use Simtabi\Laranail\Console\Tools\Commands\Concerns\InteractsWithConsoleWriter; +use Simtabi\Laranail\Console\Tools\Commands\Concerns\InteractsWithConsoleServices; +use Simtabi\Laranail\Package\Tools\Commands\InstallCommand as PackageToolsInstallCommand; /** * `laranail::confetti.install` publishes the config and explains what is left @@ -17,17 +19,49 @@ * asset mode needs no publish step. The command exists to publish the config * for editing and to say plainly which of the three ways of getting the runtime * onto a page the application has chosen. + * + * The base is package-tools' install command, which carries the `::` name + * support. laranail/console's display API and managed run lifecycle come from + * its two traits rather than its base class, so neither package has to depend + * on the other. `handle()` is this command's own: the base's generic publish + * pipeline would print different steps, and the output here is the contract. */ -final class InstallCommand extends Command +final class InstallCommand extends PackageToolsInstallCommand { - use SupportsNamespacedNames; + use InteractsWithConsoleServices; + use InteractsWithConsoleWriter; - protected $signature = 'laranail::confetti.install {--force : Overwrite an existing published config}'; + public const string SIGNATURE = 'laranail::confetti.install {--force : Overwrite an existing published config}'; - protected $description = 'Publish the confetti config and print the remaining setup steps'; + public const string DESCRIPTION = 'Publish the confetti config and print the remaining setup steps'; - public function handle(ConfettiConfig $config): int + public function __construct(Package $package) { + // Listed in `php artisan list`, as it always has been: the base hides + // install commands by default, so visibility is passed explicitly. + parent::__construct($package, self::SIGNATURE, hidden: false); + + // The base writes `Install {package}` as the description during + // construction; restore the one this command has always shown. Both the + // property and Symfony's copy are set, because the parent constructor + // has already pushed the property through setDescription(). + $this->description = self::DESCRIPTION; + $this->setDescription(self::DESCRIPTION); + + // Booted eagerly, as console's own base does, so `$this->services` + // exists straight after construction. + $this->bootConsoleSupport(); + } + + /** + * The config stays method-injected, but optional: the base declares + * `handle(): int`, and a required parameter would be an incompatible + * override. Artisan still injects it on every run. + */ + public function handle(?ConfettiConfig $config = null): int + { + $config ??= $this->laravel->make(ConfettiConfig::class); + $this->callSilently('vendor:publish', array_filter([ '--tag' => 'laranail::confetti-config', '--force' => $this->option('force') ? true : null, diff --git a/src/Providers/ConfettiServiceProvider.php b/src/Providers/ConfettiServiceProvider.php index 9aa10f6..dac0044 100644 --- a/src/Providers/ConfettiServiceProvider.php +++ b/src/Providers/ConfettiServiceProvider.php @@ -58,6 +58,11 @@ public function configurePackage(Package $package): void public function packageRegistered(): void { + // The install command takes the Package in its constructor, which the + // container cannot autowire. Binding it keeps the command registered by + // class name, so it is still only constructed when Artisan resolves it. + $this->app->bind(InstallCommand::class, fn (): InstallCommand => new InstallCommand($this->package)); + $this->app->singleton(ConfettiConfig::class, fn (): ConfettiConfig => ConfettiConfig::fromArray( (array) $this->app->make(Repository::class)->get('laranail.confetti', []), )); diff --git a/tests/Feature/InstallCommandContractTest.php b/tests/Feature/InstallCommandContractTest.php new file mode 100644 index 0000000..1e1214e --- /dev/null +++ b/tests/Feature/InstallCommandContractTest.php @@ -0,0 +1,88 @@ +toBeInstanceOf(InstallCommand::class); + + /** @var Command $command */ + return $command; +} + +it('keeps its name, aliases, description and listing visibility', function (): void { + $command = confettiInstallCommand(); + + expect($command->getName())->toBe('laranail::confetti.install') + ->and($command->getAliases())->toBe([]) + ->and($command->getDescription())->toBe('Publish the confetti config and print the remaining setup steps') + ->and($command->isHidden())->toBeFalse(); +}); + +it('keeps exactly one option of its own, and no arguments', function (): void { + $definition = confettiInstallCommand()->getNativeDefinition(); + + expect(array_keys($definition->getOptions()))->toBe(['force']) + ->and($definition->getArguments())->toBe([]); + + $option = $definition->getOption('force'); + + expect($option->acceptValue())->toBeFalse() + ->and($option->getDescription())->toBe('Overwrite an existing published config'); +}); + +it('publishes the config and prints every remaining setup step', function (): void { + $this->artisan('laranail::confetti.install') + ->expectsOutputToContain('Published config/laranail/confetti.php') + ->doesntExpectOutputToContain('public/vendor/confetti') + ->expectsOutputToContain('One step left: get the runtime onto your pages.') + ->expectsOutputToContain('') + ->expectsOutputToContain('CONFETTI_AUTO_INJECT=true') + ->expectsOutputToContain('->plugins([ConfettiPlugin::make()])') + ->expectsOutputToContain('Confetti::realistic()->shoot();') + ->expectsOutputToContain('Verify with php artisan laranail::confetti.doctor.') + ->assertExitCode(0); +}); + +it('accepts --force', function (): void { + $this->artisan('laranail::confetti.install', ['--force' => true]) + ->expectsOutputToContain('Published config/laranail/confetti.php') + ->assertExitCode(0); +}); + +it('publishes the browser bundle only in published asset mode', function (): void { + config()->set('laranail.confetti.assets.mode', AssetMode::Published->value); + app()->forgetInstance(ConfettiConfig::class); + + $this->artisan('laranail::confetti.install') + ->expectsOutputToContain('Published the browser bundle to public/vendor/confetti') + ->assertExitCode(0); +}); + +it('extends the package-tools install base and keeps both console traits', function (): void { + expect(is_subclass_of(InstallCommand::class, PackageToolsInstallCommand::class))->toBeTrue(); + + $traits = class_uses(InstallCommand::class); + + expect($traits)->toHaveKey(InteractsWithConsoleServices::class) + ->and($traits)->toHaveKey(InteractsWithConsoleWriter::class); +});