Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: PHP 8.1 & Laravel 10 support #9

Merged
merged 5 commits into from
May 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 5 additions & 9 deletions .github/workflows/run-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,14 @@ jobs:
strategy:
fail-fast: true
matrix:
php: [7.1, 8.1]
laravel: [5.4.*, 9.*]
php: [8.1]
laravel: [10.*]
exclude:
- php: 8.1
laravel: 5.4.*
- php: 7.1
laravel: 9.*
laravel: 10.*
include:
- laravel: 5.4.*
testbench: 3.4.*
- laravel: 9.*
testbench: 7.*
- laravel: 10.*
testbench: 8.*

name: PHP ${{ matrix.php }} - Laravel ${{ matrix.laravel }}

Expand Down
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,5 @@
composer.lock
/phpunit.xml
.phpunit.result.cache
.phpunit.cache
.idea
12 changes: 6 additions & 6 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,14 @@
}
],
"require": {
"php": ">=7.1",
"configcat/configcat-client": "^6.2",
"illuminate/contracts": ">=5.4"
"php": ">=8.1",
"configcat/configcat-client": "^9.0",
"illuminate/contracts": "^10.0"
},
"require-dev": {
"orchestra/testbench": ">=3.4",
"phpunit/phpunit": ">=5.7",
"mockery/mockery": ">=0.9.4"
"orchestra/testbench": "^8.0",
"phpunit/phpunit": "^9.6",
"mockery/mockery": "^1.6.7"
},
"autoload": {
"files": [
Expand Down
50 changes: 26 additions & 24 deletions src/ConfigCat.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,19 @@
use ConfigCat\Override\FlagOverrides;
use ConfigCat\Override\OverrideBehaviour;
use ConfigCat\Override\OverrideDataSource;
use ConfigCat\User;
use Illuminate\Support\Facades\File;
use PodPoint\ConfigCat\Contracts\FeatureFlagProviderContract;

class ConfigCat implements FeatureFlagProviderContract
{
/** @var ConfigCatClient */
protected $configCatClient;
/** @var mixed */
public $defaultValue = false;
/** @var string|null */
protected $userTransformer = null;
/** @var string|null */
protected $overridesFilePath;
protected ConfigCatClient|ClientInterface $configCatClient;

public mixed $defaultValue = false;

protected ?string $userTransformer = null;

protected ?string $overridesFilePath;

public function __construct(
ClientInterface $configCatClient,
Expand All @@ -31,18 +31,22 @@ public function __construct(
$this->defaultValue = $defaultValue;
$this->userTransformer = $userTransformer;
$this->overridesFilePath = $overridesFilePath;

if ($overridesFilePath) {
$this->localFile($overridesFilePath);
}
}

/**
* Retrieve a ConfigCat feature flag. According to the ConfigCat SDK it
* will return false if the flag is undefined or if something went wrong.
*
* @param string $featureKey
* @param mixed|null $default
* @param mixed|null $user
* @param string $featureKey
* @param mixed|null $default
* @param mixed|null $user
* @return mixed
*/
public function get(string $featureKey, $default = null, $user = null)
public function get(string $featureKey, mixed $default = null, mixed $user = null): mixed
{
$default = is_null($default) ? $this->defaultValue : $default;
$user = $this->transformUser(is_null($user) ? auth()->user() : $user);
Expand All @@ -52,14 +56,12 @@ public function get(string $featureKey, $default = null, $user = null)

/**
* Conditionally apply the transformation of the user representation using
* an callable Class.
*
* @param mixed|null $user
* @return \ConfigCat\User|null
* a callable Class.
*
* @see \ConfigCat\Support\DefaultUserTransformer
* @param mixed|null $user
* @return User|null
*/
private function transformUser($user = null): ?\ConfigCat\User
private function transformUser(mixed $user = null): ?User
{
if (! $user || ! $this->userTransformer || ! class_exists($this->userTransformer)) {
return null;
Expand All @@ -73,10 +75,10 @@ private function transformUser($user = null): ?\ConfigCat\User
/**
* Setup the overrides for ConfigCat options.
*
* @param string $filepath
* @param ?string $filepath
* @return FlagOverrides|null
*/
public static function overrides(string $filepath): ?FlagOverrides
public static function overrides(?string $filepath): ?FlagOverrides
{
return $filepath ? new FlagOverrides(
OverrideDataSource::localFile(self::localFile($filepath)),
Expand All @@ -90,13 +92,13 @@ public static function overrides(string $filepath): ?FlagOverrides
* will **only** be read from it if overrides are enabled from the
* configuration.
*
* @param array $flagsToOverride
* @param array $flagsToOverride
* @return void
*/
public function override(array $flagsToOverride)
public function override(array $flagsToOverride): void
{
if (! app()->environment('production') && $this->overridesFilePath) {
File::put(self::localFile($this->overridesFilePath), json_encode([
File::put($this->overridesFilePath, json_encode([
'flags' => $flagsToOverride,
]));
}
Expand All @@ -106,7 +108,7 @@ public function override(array $flagsToOverride)
* Resolve the file path to use with overrides. This will also make sure
* the path and file exist along the way.
*
* @param string $filepath
* @param string $filepath
* @return string
*/
private static function localFile(string $filepath): string
Expand Down
22 changes: 9 additions & 13 deletions src/ConfigCatServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class ConfigCatServiceProvider extends ServiceProvider
*
* @return void
*/
public function register()
public function register(): void
{
$this->registerConfigCatClient();

Expand All @@ -37,7 +37,7 @@ public function register()
*
* @return void
*/
public function boot()
public function boot(): void
{
if ($this->app->runningInConsole()) {
$this->publishes([
Expand All @@ -52,28 +52,24 @@ public function boot()
$this->validationRules();
}

private function registerConfigCatClient()
private function registerConfigCatClient(): void
{
$this->app->singleton(ClientInterface::class, function ($app) {
$logger = $app->version() >= '5.6.0'
? Log::channel($app['config']['configcat.log.channel'])
: $app['log'];
$logger = Log::channel($app['config']['configcat.log.channel']);

$options = [
ClientOptions::CACHE => new LaravelCache(Cache::store($app['config']['configcat.cache.store'])),
ClientOptions::CACHE_REFRESH_INTERVAL => $app['config']['configcat.cache.interval'],
ClientOptions::LOGGER => $logger,
ClientOptions::LOG_LEVEL => (int) $app['config']['configcat.log.level'],
ClientOptions::FLAG_OVERRIDES => $app['config']['configcat.overrides.enabled']
? ConfigCat::overrides($app['config']['configcat.overrides.file'])
: null,
ClientOptions::FLAG_OVERRIDES => ConfigCat::overrides($app['config']['configcat.overrides.enabled'] ? $app['config']['configcat.overrides.file'] : null),
];

return new ConfigCatClient($app['config']['configcat.key'], $options);
});
}

private function registerFacade()
private function registerFacade(): void
{
$this->app->singleton('configcat', function ($app) {
$default = $app['config']['configcat.default'];
Expand All @@ -93,7 +89,7 @@ private function registerFacade()
});
}

protected function bladeDirectives()
protected function bladeDirectives(): void
{
Blade::directive('configcat', function (string $featureKey, $user = null) {
$expression = $user ? "{$featureKey}, {$user}" : "{$featureKey}";
Expand All @@ -118,14 +114,14 @@ protected function bladeDirectives()
});
}

protected function middlewares()
protected function middlewares(): void
{
$this->app->make(Router::class)
->aliasMiddleware('configcat.on', CheckFeatureFlagOn::class)
->aliasMiddleware('configcat.off', CheckFeatureFlagOff::class);
}

protected function validationRules()
protected function validationRules(): void
{
Validator::extendImplicit('required_if_configcat', RequiredIfFeature::class);
}
Expand Down
10 changes: 5 additions & 5 deletions src/Contracts/FeatureFlagProviderContract.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,16 @@
interface FeatureFlagProviderContract
{
/**
* @param string $featureKey
* @param mixed|null $default
* @param mixed|null $user
* @param string $featureKey
* @param mixed|null $default
* @param mixed|null $user
* @return mixed
*/
public function get(string $featureKey, $default = null, $user = null);
public function get(string $featureKey, mixed $default = null, mixed $user = null): mixed;

/**
* @param array $flagsToOverride
* @return void
*/
public function override(array $flagsToOverride);
public function override(array $flagsToOverride): void;
}
9 changes: 5 additions & 4 deletions src/Middlewares/CheckFeatureFlagOff.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace PodPoint\ConfigCat\Middlewares;

use Closure;
use Illuminate\Http\Request;
use PodPoint\ConfigCat\Facades\ConfigCat;
use Symfony\Component\HttpFoundation\Response;

Expand All @@ -11,12 +12,12 @@ class CheckFeatureFlagOff
/**
* Aborts the Request with a 404 if a feature flag is explicitly set to true.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @param string $key
* @param Request $request
* @param Closure $next
* @param string $featureKey
* @return mixed
*/
public function handle($request, Closure $next, string $featureKey)
public function handle(Request $request, Closure $next, string $featureKey): mixed
{
abort_if(ConfigCat::get($featureKey) === true, Response::HTTP_NOT_FOUND);

Expand Down
9 changes: 5 additions & 4 deletions src/Middlewares/CheckFeatureFlagOn.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace PodPoint\ConfigCat\Middlewares;

use Closure;
use Illuminate\Http\Request;
use PodPoint\ConfigCat\Facades\ConfigCat;
use Symfony\Component\HttpFoundation\Response;

Expand All @@ -11,12 +12,12 @@ class CheckFeatureFlagOn
/**
* Aborts the Request with a 404 if a feature flag is undefined or explicitly set to false.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @param string $featureKey
* @param Request $request
* @param Closure $next
* @param string $featureKey
* @return mixed
*/
public function handle($request, Closure $next, string $featureKey)
public function handle(Request $request, Closure $next, string $featureKey): mixed
{
abort_unless(ConfigCat::get($featureKey) === true, Response::HTTP_NOT_FOUND);

Expand Down
8 changes: 4 additions & 4 deletions src/Rules/RequiredIfFeature.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@ class RequiredIfFeature
use ValidatesAttributes;

/**
* @param string $attribute
* @param mixed $value
* @param array $parameters
* @param string $attribute
* @param mixed $value
* @param array $parameters
* @return bool
*/
public function validate($attribute, $value, $parameters): bool
public function validate(string $attribute, mixed $value, array $parameters): bool
{
if (! is_string($parameters[0] ?? null)) {
throw new \InvalidArgumentException(
Expand Down
23 changes: 11 additions & 12 deletions src/Support/ConfigCatFake.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,11 @@

class ConfigCatFake
{
/** @var FeatureFlagProviderContract */
protected $provider;
/** @var array */
protected $featureFlags = [];
/** @var array */
protected $flagCounts = [];
protected FeatureFlagProviderContract $provider;

protected array $featureFlags = [];

protected array $flagCounts = [];

public function __construct(FeatureFlagProviderContract $provider, $featureFlags = [])
{
Expand All @@ -24,10 +23,10 @@ public function __construct(FeatureFlagProviderContract $provider, $featureFlags
/**
* Defines the faked feature flags.
*
* @param array $featureFlags
* @param array $featureFlags
* @return self
*/
public function fake($featureFlags = []): self
public function fake(array $featureFlags = []): self
{
$this->featureFlags = Arr::wrap($featureFlags);

Expand All @@ -38,12 +37,12 @@ public function fake($featureFlags = []): self
* Retrieve a faked feature flag if it exists. Returns false if the faked
* feature flag is undefined.
*
* @param string $featureKey
* @param mixed|null $default
* @param mixed|null $user
* @param string $featureKey
* @param mixed|null $default
* @param mixed|null $user
* @return mixed
*/
public function get(string $featureKey, $default = null, $user = null)
public function get(string $featureKey, mixed $default = null, mixed $user = null): mixed
{
$featureValue = $this->featureFlags[$featureKey] ?? ($default ?: $this->provider->defaultValue);

Expand Down
2 changes: 1 addition & 1 deletion src/Support/DefaultUserTransformer.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

class DefaultUserTransformer
{
public function __invoke(\Illuminate\Foundation\Auth\User $user)
public function __invoke(\Illuminate\Foundation\Auth\User $user): \ConfigCat\User
{
return new \ConfigCat\User($user->getKey(), $user->email);
}
Expand Down
Loading
Loading