diff --git a/app/Console/Commands/Environment/Addons/RunHooksCommand.php b/app/Console/Commands/Environment/Addons/RunHooksCommand.php new file mode 100644 index 000000000..a2d21c59f --- /dev/null +++ b/app/Console/Commands/Environment/Addons/RunHooksCommand.php @@ -0,0 +1,68 @@ +/hooks/" script for the given lifecycle event when addon hooks are enabled. + */ + public function handle(): int + { + if (!config('addons.hooks_enabled')) { + return self::SUCCESS; + } + + $event = $this->argument('event'); + if (!Str::isMatch('/^[a-z0-9-]+$/', $event)) { + $this->components->error("Invalid hook event name: {$event}"); + + return self::INVALID; + } + + $hooks = Collection::make(File::glob(base_path("addons/*/hooks/{$event}")) ?: []) + ->filter(fn (string $hook) => is_executable($hook)) + ->values(); + + if ($hooks->isEmpty()) { + return self::SUCCESS; + } + + if ($this->input->isInteractive() && !$this->confirm( + sprintf('Execute %d addon hook script(s) for the "%s" event? They run with the privileges of this process.', $hooks->count(), $event) + )) { + return self::SUCCESS; + } + + $hooks->each($this->runHook(...)); + + return self::SUCCESS; + } + + /** + * Streams a single hook's output, reporting a non-zero exit without aborting the remaining hooks. + */ + private function runHook(string $hook): void + { + $this->components->info("Running addon hook: {$hook}"); + + $result = Process::path(base_path()) + ->forever() + ->run([$hook], fn (string $type, string $output) => $this->output->write($output)); + + if ($result->failed()) { + $this->components->warn("Addon hook exited with an error: {$hook}"); + } + } +} diff --git a/composer.json b/composer.json index 30636ebd1..b797fdc4c 100644 --- a/composer.json +++ b/composer.json @@ -87,6 +87,9 @@ "Illuminate\\Foundation\\ComposerScripts::postAutoloadDump", "@php artisan package:discover --ansi || true" ], + "post-install-cmd": [ + "@php artisan p:environment:addons:run-hooks post-install || true" + ], "post-root-package-install": [ "@php -r \"file_exists('.env') || copy('.env.example', '.env');\"" ], diff --git a/config/addons.php b/config/addons.php new file mode 100644 index 000000000..f15e97990 --- /dev/null +++ b/config/addons.php @@ -0,0 +1,17 @@ +/hooks/" during lifecycle events such as post-install + | (see the p:environment:addons:run-hooks command). These scripts run with + | the privileges of the invoking process — often root during an upgrade — so + | only enable this if you trust every installed addon. + | + */ + 'hooks_enabled' => env('ADDONS_HOOKS_ENABLED', false), +]; diff --git a/tests/Unit/Console/Commands/Environment/Addons/RunHooksCommandTest.php b/tests/Unit/Console/Commands/Environment/Addons/RunHooksCommandTest.php new file mode 100644 index 000000000..3ce5bce39 --- /dev/null +++ b/tests/Unit/Console/Commands/Environment/Addons/RunHooksCommandTest.php @@ -0,0 +1,118 @@ + false]); + Process::fake(); + $this->makeHook('example'); + + $this->runHooks()->assertExitCode(Command::SUCCESS); + + Process::assertNothingRan(); + } + + public function testExecutableHooksRunForEvent(): void + { + config(['addons.hooks_enabled' => true]); + Process::fake(); + $first = $this->makeHook('alpha'); + $second = $this->makeHook('beta'); + + $this->runHooks()->assertExitCode(Command::SUCCESS); + + Process::assertRan(fn ($process) => $process->command === [$first]); + Process::assertRan(fn ($process) => $process->command === [$second]); + } + + public function testNonExecutableFilesAreIgnored(): void + { + config(['addons.hooks_enabled' => true]); + Process::fake(); + $executable = $this->makeHook('alpha'); + $ignored = $this->makeHook('beta', executable: false); + + $this->runHooks()->assertExitCode(Command::SUCCESS); + + Process::assertRan(fn ($process) => $process->command === [$executable]); + Process::assertDidntRun(fn ($process) => $process->command === [$ignored]); + } + + public function testInvalidEventNameIsRejected(): void + { + config(['addons.hooks_enabled' => true]); + Process::fake(); + + $this->runHooks('BadEvent') + ->expectsOutputToContain('Invalid hook event name') + ->assertExitCode(Command::INVALID); + + Process::assertNothingRan(); + } + + public function testFailingHookDoesNotAbortRemainingHooks(): void + { + config(['addons.hooks_enabled' => true]); + $failing = $this->makeHook('broken'); + $passing = $this->makeHook('healthy'); + + Process::fake([ + '*broken*' => Process::result(exitCode: 3), + '*' => Process::result(), + ]); + + $this->runHooks() + ->expectsOutputToContain('exited with an error') + ->assertExitCode(Command::SUCCESS); + + Process::assertRan(fn ($process) => $process->command === [$failing]); + Process::assertRan(fn ($process) => $process->command === [$passing]); + } + + public function testDeclinedConfirmationSkipsHooks(): void + { + config(['addons.hooks_enabled' => true]); + Process::fake(); + $this->makeHook('example'); + + $this->artisan('p:environment:addons:run-hooks', ['event' => 'post-install']) + ->expectsConfirmation('Execute 1 addon hook script(s) for the "post-install" event? They run with the privileges of this process.', 'no') + ->assertExitCode(Command::SUCCESS); + + Process::assertNothingRan(); + } + + private function runHooks(string $event = 'post-install'): \Illuminate\Testing\PendingCommand + { + return $this->artisan('p:environment:addons:run-hooks', ['event' => $event, '--no-interaction' => true]); + } + + private function makeHook(string $addon, string $event = 'post-install', bool $executable = true): string + { + $path = base_path("addons/{$addon}/hooks/{$event}"); + + File::ensureDirectoryExists(dirname($path)); + File::put($path, "#!/usr/bin/env bash\nexit 0\n"); + + if ($executable) { + chmod($path, 0o755); + } + + return $path; + } +}