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

Allow more recent versions in composer.json for packages #33

Draft
wants to merge 7 commits into
base: master
Choose a base branch
from
Draft
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
17 changes: 11 additions & 6 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@
"type": "library",
"require": {
"guzzlehttp/guzzle": "^7",
"illuminate/support": "^6|^7|^8|^9|^10.0|^11.0",
"illuminate/http": "^6|^7|^8|^9|^10.0|^11.0",
"unleash/client": "^1",
"symfony/cache": "^5.3|^6.1",
"illuminate/support": "^10.0|^11.0",
"illuminate/http": "^10.0|^11.0",
"unleash/client": "^v2",
"symfony/cache": "^6.2|^7.2",
"psr/cache": "^1.0|^2.0|^3.0",
"psr/simple-cache": "^1.0|^2.0|^3.0"
},
Expand All @@ -23,11 +23,16 @@
"email": "[email protected]"
}
],
"minimum-stability": "dev",
"prefer-stable": true,
"extra": {
"laravel": {
"providers": [
"JWebb\\Unleash\\Providers\\ServiceProvider"
]
"JWebb\\Unleash\\ServiceProvider"
],
"aliases": {
"Unleash": "JWebb\\Unleash\\Facades\\Unleash"
}
}
}
}
5 changes: 2 additions & 3 deletions config/unleash.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
<?php

return [

/*
|--------------------------------------------------------------------------
| Enable/disable Laravel Unleash
Expand All @@ -23,7 +22,7 @@
|
*/

'url' => env('UNLEASH_URL'),
'url' => env('UNLEASH_URL', 'https://localhost'),

/*
|--------------------------------------------------------------------------
Expand Down Expand Up @@ -119,7 +118,7 @@
|
*/

'api_key' => env('UNLEASH_API_KEY', null),
'api_key' => env('UNLEASH_API_KEY'),

/*
|--------------------------------------------------------------------------
Expand Down
6 changes: 5 additions & 1 deletion src/Cache/CacheBridge.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,12 @@ public function setMultiple($values, $ttl = null): bool
public function deleteMultiple($keys): bool
{
foreach ($keys as $key) {
$this->delete($key);
if ($this->delete($key) === false) {
return false;
}
}

return true;
}

/**
Expand Down
10 changes: 6 additions & 4 deletions src/Facades/Unleash.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@
namespace JWebb\Unleash\Facades;

use Illuminate\Support\Facades\Facade;
use Unleash\Client\Configuration\Context;
use Unleash\Client\DTO\Variant;

/**
* @method static array setClient(Unleash\Client\Unleash $client)
* @method static bool isEnabled(string $featureName, ?\Unleash\Client\Configuration\Context $context = null, bool $default = false)
* @method static array getFeatures(bool $onlyEnabled = false, ?\Unleash\Client\Configuration\Context $context = null)
* @method static \Unleash\Client\DTO\Variant getVariant(string $featureName, ?\Unleash\Client\Configuration\Context $context = null, ?\Unleash\Client\DTO\Variant $fallbackVariant = null)
* @method static array setClient(\Unleash\Client\Unleash $client)
* @method static bool isEnabled(string $featureName, ?Context $context = null, bool $default = false)
* @method static array getFeatures(bool $onlyEnabled = false, ?Context $context = null)
* @method static Variant getVariant(string $featureName, ?Context $context = null, ?Variant $fallbackVariant = null)
* @method static void register()
* @method static void getRepository()
*/
Expand Down
13 changes: 3 additions & 10 deletions src/Middleware/CheckFeature.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,14 @@
namespace JWebb\Unleash\Middleware;

use Closure;
use Illuminate\Http\Request;
use JWebb\Unleash\Unleash;

class CheckFeature
{
/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @param $featureName
* @return mixed
*/
public function handle($request, Closure $next, $featureName)
public function handle(Request $request, Closure $next, $featureName): mixed
{
if (!app(Unleash::class)->isEnabled($featureName)) {
if (! app(Unleash::class)->isEnabled($featureName)) {
abort(404);
}

Expand Down
90 changes: 0 additions & 90 deletions src/Providers/ServiceProvider.php

This file was deleted.

4 changes: 0 additions & 4 deletions src/Providers/UnleashStrategiesProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,9 @@
use Unleash\Client\Strategy\AbstractStrategyHandler;
use Unleash\Client\Strategy\ApplicationHostnameStrategyHandler;
use Unleash\Client\Strategy\DefaultStrategyHandler;
use Unleash\Client\Strategy\GradualRolloutRandomStrategyHandler;
use Unleash\Client\Strategy\GradualRolloutSessionIdStrategyHandler;
use Unleash\Client\Strategy\GradualRolloutStrategyHandler;
use Unleash\Client\Strategy\GradualRolloutUserIdStrategyHandler;
use Unleash\Client\Strategy\IpAddressStrategyHandler;
use Unleash\Client\Strategy\UserIdStrategyHandler;
use Unleash\Strategy\StrategyHandler;

class UnleashStrategiesProvider implements UnleashStrategiesProviderInterface
{
Expand Down
93 changes: 93 additions & 0 deletions src/ServiceProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
<?php

namespace JWebb\Unleash;

use GuzzleHttp\Client;
use Illuminate\Contracts\Container\BindingResolutionException;
use Illuminate\Contracts\Container\Container;
use Illuminate\Foundation\Application as LaravelApplication;
use Illuminate\Support\Facades\Blade;
use Illuminate\Support\ServiceProvider as IlluminateServiceProvider;
use JWebb\Unleash\Interfaces\UnleashCacheHandlerInterface;
use Unleash\Client\UnleashBuilder;

class ServiceProvider extends IlluminateServiceProvider
{
/**
* @throws BindingResolutionException
*/
public function boot(): void
{
$this->bootConfig();
$this->bootBladeDirectives();
}

public function register(): void
{
$this->app->singleton(Unleash::class, function (Container $app) {
$strategyProvider = $app->config->get('unleash.strategy_provider');
$contextProvider = $app->config->get('unleash.context_provider');

$builder = UnleashBuilder::create()
->withInstanceId($app->config->get('unleash.instance_id'))
->withAppUrl($app->config->get('unleash.url'))
->withAppName($app->config->get('unleash.environment')) // Same as `withGitlabEnvironment(...)`
->withContextProvider(new $contextProvider())
->withStrategies(...(new $strategyProvider())->getStrategies())
->withAutomaticRegistrationEnabled(!! $app->config->get('unleash.automatic_registration'))
->withMetricsEnabled(!! $app->config->get('unleash.metrics'));

if (!! $app->config->get('unleash.http_client_override.enabled')) {
$builder = $builder->withHttpClient(new Client($app->config->get('unleash.http_client_override.config')));
}

if (!! $app->config->get('unleash.cache.enabled')) {
/** @var UnleashCacheHandlerInterface $cacheHandler */
$cacheHandler = $app->config->get('unleash.cache.handler');

$builder = $builder->withCacheHandler(
(new $cacheHandler())->init(),
$app->config->get('unleash.cache.ttl')
);
}
if (!! $app->config->get('unleash.api_key')) {
$builder = $builder->withHeader('Authorization', $app->config->get('unleash.api_key'));
}

return new Unleash($builder->build());
});
}

private function bootConfig(): void
{
$source = realpath($raw = __DIR__.'/../config/unleash.php') ?: $raw;

if ($this->app instanceof LaravelApplication && $this->app->runningInConsole()) {
$this->publishes([$source => $this->app->configPath('unleash.php')]);
} elseif ($this->app instanceof LumenApplication) {
$this->app->configure('unleash');
}

$this->mergeConfigFrom($source, 'unleash');
}

/**
* @throws BindingResolutionException
*/
private function bootBladeDirectives(): void
{
$unleash = $this->app->make(Unleash::class);

Blade::if('feature', function ($featureName, $context = null, $default = false) use ($unleash) {
return $unleash->isEnabled($featureName, $context, $default);
});

Blade::if('featureEnabled', function (string $feature) use ($unleash) {
return $this->app->make(Unleash::class)->isEnabled($feature);
});

Blade::if('featureDisabled', function (string $feature) use ($unleash) {
return $this->app->make(Unleash::class)->isEnabled($feature) === false;
});
}
}
34 changes: 25 additions & 9 deletions src/Unleash.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

namespace JWebb\Unleash;

use Exception;
use Illuminate\Support\Facades\Log;
use ReflectionException;
use ReflectionProperty;
use Unleash\Client\Configuration\Context;
use Unleash\Client\DTO\Feature;
use Unleash\Client\DTO\Variant;
Expand Down Expand Up @@ -40,9 +44,15 @@ public function setClient($client): void
*/
public function isEnabled(string $featureName, ?Context $context = null, bool $default = false): bool
{
if (! config('unleash.enabled')) {
Log::debug('Unleash is disabled, returning disabled state for feature');

return false;
}

try {
return $this->client->isEnabled($featureName, $context, $default);
} catch (\Exception $e) {
} catch (Exception $e) {
return $default;
}
}
Expand All @@ -52,10 +62,15 @@ public function isEnabled(string $featureName, ?Context $context = null, bool $d
* @param bool $onlyEnabled
* @param Context|null $context
* @return array
* @throws \ReflectionException
*/
public function getFeatures(bool $onlyEnabled = false, ?Context $context = null): array
{
if (! config('unleash.enabled')) {
Log::debug('Unleash is disabled, returning empty list of features');

return [];
}

try {
$features = $this->getRepository()->getFeatures();

Expand All @@ -72,15 +87,17 @@ public function getFeatures(bool $onlyEnabled = false, ?Context $context = null)
}) : $mappedFeatures;

return $toggles;
} catch (\Exception $e) {
} catch (Exception $e) {
Log::error('Error getting features from Unleash', ['error' => $e->getMessage()]);

return [];
}
}

/**
* @param string $featureName
* @param Context|null $context
* @param Variant|null $fallbackVariant
* @param string $featureName
* @param Context|null $context
* @param Variant|null $fallbackVariant
* @return Variant
*/
public function getVariant(string $featureName, ?Context $context = null, ?Variant $fallbackVariant = null): Variant
Expand All @@ -97,12 +114,11 @@ public function register(): bool
}

/**
* @throws \ReflectionException
* @throws ReflectionException
*/
protected function getRepository(): UnleashRepository
{
$reflectionProperty = new \ReflectionProperty(get_class($this->client), 'repository');
$reflectionProperty->setAccessible(true);
$reflectionProperty = new ReflectionProperty(get_class($this->client), 'repository');

return $reflectionProperty->getValue($this->client);
}
Expand Down