From 09627d33c1e367844f36cf5ed11b3be101d7ad14 Mon Sep 17 00:00:00 2001 From: Mior Muhammad Zaki Date: Thu, 12 Dec 2024 00:27:20 +0800 Subject: [PATCH] [5.x] Throw exceptions when trying to use Installer on PHP environment without required extensions (#373) * [5.x] Add polyfill for `mb_rtrim()` for PHP 8.3 and below Signed-off-by: Mior Muhammad Zaki * wip Signed-off-by: Mior Muhammad Zaki * Apply fixes from StyleCI * remove example extension Signed-off-by: Mior Muhammad Zaki * wip Signed-off-by: Mior Muhammad Zaki * wip Signed-off-by: Mior Muhammad Zaki * formatting * remove test * Apply fixes from StyleCI --------- Signed-off-by: Mior Muhammad Zaki Co-authored-by: StyleCI Bot Co-authored-by: Taylor Otwell --- src/NewCommand.php | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/src/NewCommand.php b/src/NewCommand.php index 7af1dcae..e0744ece 100644 --- a/src/NewCommand.php +++ b/src/NewCommand.php @@ -85,6 +85,8 @@ protected function interact(InputInterface $input, OutputInterface $output) | |___| (_| | | | (_| |\ V / __/ | |______\__,_|_| \__,_| \_/ \___|_|'.PHP_EOL.PHP_EOL); + $this->ensureExtensionsAreAvailable($input, $output); + if (! $input->getArgument('name')) { $input->setArgument('name', text( label: 'What is the name of your project?', @@ -147,6 +149,38 @@ protected function interact(InputInterface $input, OutputInterface $output) // } } + /** + * Ensure that the required PHP extensions are installed. + * + * @param \Symfony\Component\Console\Input\InputInterface $input + * @param \Symfony\Component\Console\Output\OutputInterface $output + * @return void + * + * @throws \RuntimeException + */ + protected function ensureExtensionsAreAvailable(InputInterface $input, OutputInterface $output): void + { + $availableExtensions = get_loaded_extensions(); + + $missingExtensions = collect([ + 'ctype', + 'filter', + 'hash', + 'mbstring', + 'openssl', + 'session', + 'tokenizer', + ])->reject(fn ($extension) => in_array($extension, $availableExtensions)); + + if ($missingExtensions->isEmpty()) { + return; + } + + throw new \RuntimeException( + sprintf('The following PHP extensions are required but are not installed: %s', $missingExtensions->join(', ', ', and ')) + ); + } + /** * Execute the command. *