From 7eeaefdfcab0825c27837b7b1d250464e83f7dc0 Mon Sep 17 00:00:00 2001 From: Mischa Braam Date: Sat, 24 Feb 2024 12:14:03 +0100 Subject: [PATCH] Install php extensions in support of Redis (#631) --- cli/ValetPlus/PhpExtension.php | 70 +++++++++++++++++++++++++++-- cli/ValetPlus/RedisPhpExtension.php | 11 +++++ cli/ValetPlus/RedisService.php | 21 +++++++++ cli/includes/facades.php | 3 ++ 4 files changed, 101 insertions(+), 4 deletions(-) create mode 100644 cli/ValetPlus/RedisPhpExtension.php diff --git a/cli/ValetPlus/PhpExtension.php b/cli/ValetPlus/PhpExtension.php index 00b25559..c4b5a5fb 100644 --- a/cli/ValetPlus/PhpExtension.php +++ b/cli/ValetPlus/PhpExtension.php @@ -28,6 +28,8 @@ class PhpExtension public const DATASTRUCTURE_EXTENSION = 'ds'; /** @var string */ public const IMAGICK_EXTENSION = 'imagick'; + /** @var string */ + public const REDIS_EXTENSION = 'redis'; protected const PHP_EXTENSIONS = [ self::XDEBUG_EXTENSION => [ @@ -46,6 +48,7 @@ class PhpExtension 'default' => false, 'brew_dependency' => 'libmemcached', 'ini_files' => [ + // also installed with redis, might be an issue when both are used and one is uninstalled '20-igbinary', '20-msgpack', '30-memcached' @@ -70,6 +73,17 @@ class PhpExtension '20-imagick.ini' ] ], + self::REDIS_EXTENSION => [ + 'default' => false, + 'php_extensions' => [ + 'igbinary' + ], + 'ini_files' => [ + // also installed with memcache, might be an issue when both are used and one is uninstalled + '20-igbinary', + '20-redis' + ] + ], ]; /** @var Brew */ @@ -203,19 +217,33 @@ protected function install($extension, $phpVersion) $installed = true; } + if ($this->hasExtraPhpExtensions($extension)) { + $phpExtensions = $this->getExtraPhpExtensions($extension); + foreach ($phpExtensions as $phpExtension) { + $this->installExtension($phpExtension, $phpVersion); + } + } + return $installed; } /** * @param $extension * @param $phpVersion - * @param $phpIniPath + * @param $phpIniConfigPath * @return bool */ protected function uninstall($extension, $phpVersion, $phpIniConfigPath) { $uninstalled = false; + if ($this->hasExtraPhpExtensions($extension)) { + $phpExtensions = $this->getExtraPhpExtensions($extension); + foreach ($phpExtensions as $phpExtension) { + $this->uninstallExtension($phpExtension, $phpVersion, $phpIniConfigPath); + } + } + if ($this->brew->installed($this->getExtensionFormula($extension, $phpVersion))) { $this->removeIniDefinition($extension, $phpIniConfigPath); $this->brew->uninstallFormula( @@ -281,18 +309,22 @@ protected function getExtensionFormula($extension, $phpVersion, $withTap = false } /** - * Check if the extension has any brew dependency + * Check if the extension has a brew dependency. * * @param mixed $extension * @return bool */ protected function hasBrewDependency($extension) { - return array_key_exists("brew_dependency", static::PHP_EXTENSIONS[$extension]); + if (array_key_exists($extension, static::PHP_EXTENSIONS)) { + return array_key_exists("brew_dependency", static::PHP_EXTENSIONS[$extension]); + } + + return false; } /** - * Get the brew dependency + * Get the brew dependency. * * @param mixed $extension * @return mixed @@ -302,6 +334,32 @@ protected function getBrewDependency($extension) return static::PHP_EXTENSIONS[$extension]["brew_dependency"]; } + /** + * Check if the extension has any extra php extension dependencies. + * + * @param $extension + * @return bool + */ + protected function hasExtraPhpExtensions($extension) + { + if (array_key_exists($extension, static::PHP_EXTENSIONS)) { + return array_key_exists("php_extensions", static::PHP_EXTENSIONS[$extension]); + } + + return false; + } + + /** + * Get the extra php extensions. + * + * @param mixed $extension + * @return mixed + */ + protected function getExtraPhpExtensions($extension) + { + return static::PHP_EXTENSIONS[$extension]["php_extensions"]; + } + /** * @param $extension * @param $phpIniConfigPath @@ -321,6 +379,10 @@ protected function removeIniDefinition($extension, $phpIniConfigPath) */ protected function getIniFiles($extension) { + if (!array_key_exists($extension, static::PHP_EXTENSIONS)) { + return []; + } + if (array_key_exists("ini_files", static::PHP_EXTENSIONS[$extension])) { return static::PHP_EXTENSIONS[$extension]['ini_files']; } diff --git a/cli/ValetPlus/RedisPhpExtension.php b/cli/ValetPlus/RedisPhpExtension.php new file mode 100644 index 00000000..49334109 --- /dev/null +++ b/cli/ValetPlus/RedisPhpExtension.php @@ -0,0 +1,11 @@ +redisPhpExtension = $redisPhpExtension; + } + /** * @inheritdoc */ @@ -41,6 +60,7 @@ public function stop(): void return; } + $this->redisPhpExtension->uninstall(); $this->brew->stopService(static::SERVICE_NAME); } @@ -53,6 +73,7 @@ public function restart(): void return; } + $this->redisPhpExtension->install(); $this->brew->restartService(static::SERVICE_NAME); } diff --git a/cli/includes/facades.php b/cli/includes/facades.php index f33efdf5..7afdc405 100644 --- a/cli/includes/facades.php +++ b/cli/includes/facades.php @@ -48,6 +48,9 @@ class Rabbitmq extends ValetPlusFacade { } +class RedisPhpExtension extends ValetPlusFacade +{ +} class Memcache extends ValetPlusFacade { }