From 03836b42765a04f2dbbb2a39582117b46301574c Mon Sep 17 00:00:00 2001 From: Sean Fisher Date: Fri, 10 May 2024 12:16:10 -0400 Subject: [PATCH] Adding support for installing redis object cache (#550) * Adding support for installing redis object cache * Changelog --- CHANGELOG.md | 2 ++ .../testing/class-installation-manager.php | 2 ++ src/mantle/testing/class-utils.php | 16 ++++++++-- .../concerns/trait-rsync-installation.php | 32 +++++++++++++++---- 4 files changed, 43 insertions(+), 9 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 20e32aed..624f50bc 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,6 +10,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Added - Added a `classname`/`the_classname` helper to generate complex class names. +- Added support for installing the Redis `object-cache.php` drop-in during + testing with `with_object_cache()`. ## Fixed diff --git a/src/mantle/testing/class-installation-manager.php b/src/mantle/testing/class-installation-manager.php index 7d5d2d67..b86a5737 100644 --- a/src/mantle/testing/class-installation-manager.php +++ b/src/mantle/testing/class-installation-manager.php @@ -55,6 +55,8 @@ public function __construct() { if ( Utils::env_bool( 'MANTLE_INSTALL_OBJECT_CACHE', false ) ) { $this->with_object_cache(); + } elseif ( $object_cache = Utils::env( 'MANTLE_INSTALL_OBJECT_CACHE', false ) ) { + $this->with_object_cache( $object_cache ); } if ( Utils::env_bool( 'MANTLE_USE_SQLITE', false ) ) { diff --git a/src/mantle/testing/class-utils.php b/src/mantle/testing/class-utils.php index 17144f68..7892dfed 100644 --- a/src/mantle/testing/class-utils.php +++ b/src/mantle/testing/class-utils.php @@ -292,9 +292,21 @@ public static function shell_safe( string|bool $string ): string { */ public static function install_wordpress( string $directory ): void { $install_vip_mu_plugins = static::env_bool( 'MANTLE_INSTALL_VIP_MU_PLUGINS', false ); - $install_object_cache = static::env_bool( 'MANTLE_INSTALL_OBJECT_CACHE', false ); $use_sqlite_db = static::env_bool( 'MANTLE_USE_SQLITE', false ); + // Handle the legacy values for MANTLE_INSTALL_OBJECT_CACHE. + if ( static::env_bool( 'MANTLE_INSTALL_OBJECT_CACHE', false ) ) { + $install_object_cache = 'memcached'; + } else { + $install_object_cache = static::env( 'MANTLE_INSTALL_OBJECT_CACHE', false ); + + if ( $install_object_cache && ! in_array( $install_object_cache, [ 'memcached', 'redis' ], true ) ) { + static::error( '🚨 Invalid value for MANTLE_INSTALL_OBJECT_CACHE (' . $install_object_cache . '). Ignoring...' ); + + $install_object_cache = false; + } + } + $branch = static::env( 'MANTLE_CI_BRANCH', 'HEAD' ); // Compile the variables to pass to the shell script. @@ -334,7 +346,7 @@ public static function install_wordpress( string $directory ): void { static::shell_safe( static::env( 'WP_VERSION', 'latest' ) ), static::shell_safe( static::env( 'WP_SKIP_DB_CREATE', 'false' ) ), static::shell_safe( $install_vip_mu_plugins ? 'true' : 'false' ), - static::shell_safe( $install_object_cache ? 'true' : 'false' ), + static::shell_safe( $install_object_cache ), ] )->implode( ' ' ), ); diff --git a/src/mantle/testing/concerns/trait-rsync-installation.php b/src/mantle/testing/concerns/trait-rsync-installation.php index 3a3ca8e1..17b5418a 100644 --- a/src/mantle/testing/concerns/trait-rsync-installation.php +++ b/src/mantle/testing/concerns/trait-rsync-installation.php @@ -77,7 +77,7 @@ public function with_default_exclusions(): static { '.phpunit.result.cache', 'node_modules', 'phpstan.neon', - ] + ] ); } @@ -173,23 +173,41 @@ public function with_vip_mu_plugins( bool $install = true ): static { * Will only be applied if the codebase is not already within a WordPress and * is being rsync-ed to one. * - * @param bool $install Install the object cache drop-in into the codebase. + * @param bool|string $install The object cache provider to install (redis/memcached) + * or true to install the default Memcached object cache (legacy). */ - public function with_object_cache( bool $install = true ): static { + public function with_object_cache( bool|string $install = true ): static { if ( $this->is_within_wordpress_install() ) { return $this; } - // Check if Memcached is installed. - if ( ! class_exists( \Memcached::class ) && ! Utils::env( 'MANTLE_REQUIRE_OBJECT_CACHE', false ) ) { - Utils::error( 'Memcached is not installed. Cannot install object cache. Skipping...' ); + // Allow object cache to be disabled. + if ( ! $install ) { + putenv( 'MANTLE_INSTALL_OBJECT_CACHE=' ); + + return $this; + } elseif ( true === $install ) { + $install = 'memcached'; + } + + if ( ! in_array( $install, [ 'redis', 'memcached' ], true ) ) { + Utils::error( 'Invalid object cache provider. Must be either "redis" or "memcached". Skipping...' ); return $this; } + if ( 'memcached' === $install ) { + // Check if Memcached is installed before proceeding. + if ( ! class_exists( \Memcached::class ) && ! Utils::env( 'MANTLE_REQUIRE_OBJECT_CACHE', false ) ) { + Utils::error( 'Memcached is not installed. Cannot install object cache. Skipping...' ); + + return $this; + } + } + $this->add_exclusion( 'object-cache.php' ); - putenv( 'MANTLE_INSTALL_OBJECT_CACHE=' . ( $install ? '1' : '0' ) ); + putenv( 'MANTLE_INSTALL_OBJECT_CACHE=' . $install ); return $this; }