ocus / laravel-launchdarkly
Wrap of the package launchdarkly/server-sdk for laravel.
Requires
- php: ^8.2
- ext-json: *
- illuminate/contracts: ^10.14
- launchdarkly/server-sdk: ^6.0
- spatie/laravel-package-tools: ^1.15
Requires (Dev)
- nunomaduro/collision: ^7.6
- nunomaduro/larastan: ^2.6
- orchestra/testbench: ^8.5
- pestphp/pest: ^2.8
- pestphp/pest-plugin-laravel: ^2.0
- phpstan/extension-installer: ^1.3
- phpstan/phpstan-deprecation-rules: ^1.1
- phpstan/phpstan-phpunit: ^1.3
- phpunit/phpunit: ^10.2
README
This package provides the LaunchDarkly PHP SDK in a Laravel ecosystem for an easy use with the framework. It in includes the facade to use it in your code. And in memory mock to help you in your tests.
Installation
You can install the package via composer:
composer require ocus/laravel-launchdarkly
You can publish the config file with:
php artisan vendor:publish --provider="Ocus\LaravelLaunchDarkly\LaravelLaunchDarklyServiceProvider" --tag="launchdarkly-config"
You can add in you .env
file the LaunchDarkly SDK key
LAUNCH_DARKLY_SDK_KEY="sdk-<your-sdk-key>"
Usage
First to use it you have to implement the Ocus\LaravelLaunchDarkly\Contracts\IsLaunchDarklyUser
interface in the model
that you what tu use as a LaunchDarkly user.
/**
* @return LDUser
*
* @see \LaunchDarkly\LDUserBuilder
* @link https://docs.launchdarkly.com/sdk/features/user-config#php
*/
public function getLaunchDarklyUserAttribute(): \LaunchDarkly\LDUser
{
return (new \LaunchDarkly\LDUserBuilder($this->getKey()))
->secondary(self::class)
->email($this->email)
->name($this->name)
->build();
}
In the code
use Ocus\LaravelLaunchDarkly\Facades\LaunchDarkly;
// Will return what you setup on your LaunchDarkly dashboard
LaunchDarkly::variation('your.flag.key', $user->launch_darkly_user);
In routes
You can use the launch-darkly
middleware in your routes definition:
Route::middleware('launch-darkly:my-key,my-value')->get('/my-route', ...);
All value types are supported except json.
In the tests
use Ocus\LaravelLaunchDarkly\Facades\LaunchDarkly;
// Fake the LaunchDarkly feature requester with a in memory one
LaunchDarkly::fake();
// Add a value for a given flag key
LaunchDarkly::addFlagValue('your.flag.key', true);
// Will return true
LaunchDarkly::variation('your.flag.key', $user->launch_darkly_user);
Testing
composer test
License
The Apache License (Apache 2.0). Please see License File for more information.