Skip to content

Commit

Permalink
Install php extensions in support of Redis (#631)
Browse files Browse the repository at this point in the history
  • Loading branch information
mischabraam committed Feb 24, 2024
1 parent bbc7f38 commit 7eeaefd
Show file tree
Hide file tree
Showing 4 changed files with 101 additions and 4 deletions.
70 changes: 66 additions & 4 deletions cli/ValetPlus/PhpExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 => [
Expand All @@ -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'
Expand All @@ -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 */
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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'];
}
Expand Down
11 changes: 11 additions & 0 deletions cli/ValetPlus/RedisPhpExtension.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

declare(strict_types=1);

namespace WeProvide\ValetPlus;

class RedisPhpExtension extends AbstractPhpExtension
{
/** @var string */
protected const EXTENSION_NAME = PhpExtension::REDIS_EXTENSION;
}
21 changes: 21 additions & 0 deletions cli/ValetPlus/RedisService.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@

namespace WeProvide\ValetPlus;

use Valet\Brew;
use Valet\CommandLine;
use Valet\Configuration;
use Valet\Filesystem;

use function Valet\info;

/**
Expand All @@ -14,6 +19,20 @@ class RedisService extends AbstractService
/** @var string */
protected const SERVICE_NAME = 'redis';

/** @var RedisPhpExtension */
protected $redisPhpExtension;

public function __construct(
Configuration $configuration,
Brew $brew,
Filesystem $files,
CommandLine $cli,
RedisPhpExtension $redisPhpExtension
) {
parent::__construct($configuration, $brew, $files, $cli);
$this->redisPhpExtension = $redisPhpExtension;
}

/**
* @inheritdoc
*/
Expand Down Expand Up @@ -41,6 +60,7 @@ public function stop(): void
return;
}

$this->redisPhpExtension->uninstall();
$this->brew->stopService(static::SERVICE_NAME);
}

Expand All @@ -53,6 +73,7 @@ public function restart(): void
return;
}

$this->redisPhpExtension->install();
$this->brew->restartService(static::SERVICE_NAME);
}

Expand Down
3 changes: 3 additions & 0 deletions cli/includes/facades.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,9 @@ class Rabbitmq extends ValetPlusFacade
{
}

class RedisPhpExtension extends ValetPlusFacade
{
}
class Memcache extends ValetPlusFacade
{
}
Expand Down

0 comments on commit 7eeaefd

Please sign in to comment.