Skip to content

Commit 9175ea4

Browse files
committed
Initial commit
0 parents commit 9175ea4

17 files changed

+914
-0
lines changed

.gitignore

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
/vendor
2+
3+
.phpunit.result.cache
4+
phpunit.xml
5+
6+
composer.lock

.travis.yml

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
language: php
2+
3+
php:
4+
- '7.2'
5+
- '7.3'
6+
7+
cache:
8+
directories:
9+
- $HOME/.composer/cache
10+
11+
install:
12+
- composer install --no-interaction
13+
14+
script:
15+
- phpunit --coverage-clover=coverage.xml
16+
17+
after_success:
18+
- bash <(curl -s https://codecov.io/bash)

README.md

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
# Laravel Unleash
2+
3+
[![Build Status](https://travis-ci.org/mikefrancis/laravel-unleash.svg?branch=master)](https://travis-ci.org/mikefrancis/laravel-unleash) [![codecov](https://codecov.io/gh/mikefrancis/laravel-unleash/branch/master/graph/badge.svg)](https://codecov.io/gh/mikefrancis/laravel-unleash)
4+
5+
An [Unleash](https://unleash.github.io) client for Laravel.
6+
7+
## Installation
8+
9+
```bash
10+
composer require mikefrancis/laravel-unleash
11+
```
12+
13+
Export package config:
14+
15+
```bash
16+
php artisan vendor:publish --provider="MikeFrancis\LaravelUnleash\ServiceProvider"
17+
```
18+
19+
## Configuration
20+
21+
Documentation for configuration can be found in [config/unleash.php](https://github.com/mikefrancis/laravel-unleash/blob/master/config/unleash.php).
22+
23+
## Usage
24+
25+
```php
26+
27+
use \MikeFrancis\LaravelUnleash\Unleash;
28+
29+
$unleash = app(Unleash::class);
30+
31+
if ($unleash->isFeatureEnabled('myAwesomeFeature')) {
32+
// Congratulations, you can see this awesome feature!
33+
}
34+
35+
if ($unleash->isFeatureDisabled('myAwesomeFeature')) {
36+
// Check back later for more features!
37+
}
38+
39+
$allFeatures = $unleash->getFeatures();
40+
```
41+
42+
### Blade
43+
44+
Blade directive for checking if a feature is **enabled**:
45+
46+
```php
47+
@featureEnabled('myAwesomeFeature')
48+
Congratulations, you can see this awesome feature!
49+
@endfeatureEnabled
50+
```
51+
52+
Or if a feature is **disabled**:
53+
54+
```php
55+
@featureDisabled('myAwesomeFeature')
56+
Check back later for more features!
57+
@endfeatureDisabled
58+
```

composer.json

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
{
2+
"name": "mikefrancis/laravel-unleash",
3+
"description": "An Unleash client for Laravel",
4+
"type": "library",
5+
"require": {
6+
"guzzlehttp/guzzle": "^6.3",
7+
"illuminate/support": "^5.8",
8+
"illuminate/http": "^5.8"
9+
},
10+
"require-dev": {
11+
"phpunit/phpunit": "^8.3"
12+
},
13+
"autoload": {
14+
"psr-4": {
15+
"MikeFrancis\\LaravelUnleash\\": "src/"
16+
}
17+
},
18+
"autoload-dev": {
19+
"psr-4": {
20+
"MikeFrancis\\LaravelSecureHeaders\\Tests\\": "tests/"
21+
}
22+
},
23+
"license": "MIT",
24+
"authors": [
25+
{
26+
"name": "Mike Francis",
27+
"email": "mikeffrancis@gmail.com"
28+
}
29+
],
30+
"extra": {
31+
"laravel": {
32+
"providers": [
33+
"MikeFrancis\\LaravelUnleash\\ServiceProvider"
34+
]
35+
}
36+
}
37+
}

config/unleash.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
return [
4+
// URL of the Unleash server.
5+
// This should be the base URL, do not include /api or anything else.
6+
'url' => env('UNLEASH_URL'),
7+
8+
// Globally control whether Unleash is enabled or disabled.
9+
// If not enabled, no API requests will be made and all "enabled" checks will return `false` and
10+
// "disabled" checks will return `true`.
11+
'isEnabled' => env('UNLEASH_ENABLED', true),
12+
13+
// Allow the Unleash API response to be cached.
14+
'cache' => [
15+
'isEnabled' => false,
16+
'ttl' => 3600,
17+
],
18+
19+
// Mapping of strategies used to guard features on Unleash. The default strategies are already
20+
// mapped below, and more strategies can be added - they just need to implement the
21+
// `\MikeFrancis\LaravelUnleash\Strategies\Strategy` interface. If you would like to disable
22+
// a built-in strategy, please comment it out or remove it below.
23+
'strategies' => [
24+
'applicationHostname' => \MikeFrancis\LaravelUnleash\Strategies\ApplicationHostnameStrategy::class,
25+
'remoteAddress' => \MikeFrancis\LaravelUnleash\Strategies\RemoteAddressStrategy::class,
26+
'userWithIds' => \MikeFrancis\LaravelUnleash\Strategies\UserWithIdStrategy::class,
27+
],
28+
];

phpunit.xml.dist

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<phpunit backupGlobals="false"
3+
backupStaticAttributes="false"
4+
bootstrap="vendor/autoload.php"
5+
colors="true"
6+
convertErrorsToExceptions="true"
7+
convertNoticesToExceptions="true"
8+
convertWarningsToExceptions="true"
9+
processIsolation="false"
10+
stopOnFailure="false">
11+
<testsuites>
12+
<testsuite name="Unit">
13+
<directory suffix="Test.php">./tests</directory>
14+
</testsuite>
15+
</testsuites>
16+
<filter>
17+
<whitelist>
18+
<directory suffix=".php">./src</directory>
19+
<exclude>
20+
<file>./src/ServiceProvider.php</file>
21+
</exclude>
22+
</whitelist>
23+
</filter>
24+
</phpunit>

src/Client.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
3+
namespace MikeFrancis\LaravelUnleash;
4+
5+
use GuzzleHttp\Client as GuzzleClient;
6+
use Illuminate\Contracts\Config\Repository as Config;
7+
8+
class Client extends GuzzleClient
9+
{
10+
public function __construct(Config $config)
11+
{
12+
parent::__construct([
13+
'base_uri' => $config->get('unleash.url'),
14+
]);
15+
}
16+
}

src/ServiceProvider.php

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
<?php
2+
3+
namespace MikeFrancis\LaravelUnleash;
4+
5+
use Illuminate\Support\Facades\Blade;
6+
use Illuminate\Support\ServiceProvider as IlluminateServiceProvider;
7+
8+
class ServiceProvider extends IlluminateServiceProvider
9+
{
10+
/**
11+
* Register bindings in the container.
12+
*
13+
* @return void
14+
*/
15+
public function register()
16+
{
17+
$this->mergeConfigFrom($this->getConfigPath(), 'unleash');
18+
}
19+
20+
/**
21+
* Perform post-registration booting of services.
22+
*
23+
* @return void
24+
*/
25+
public function boot()
26+
{
27+
$this->publishes([
28+
$this->getConfigPath() => config_path('unleash.php'),
29+
]);
30+
31+
32+
Blade::if('featureEnabled', function (string $feature) {
33+
$client = app(Client::class);
34+
$unleash = app(Unleash::class, ['client' => $client]);
35+
36+
return $unleash->isFeatureEnabled($feature);
37+
});
38+
39+
Blade::if('featureDisabled', function (string $feature) {
40+
$client = app(Client::class);
41+
$unleash = app(Unleash::class, ['client' => $client]);
42+
43+
return !$unleash->isFeatureEnabled($feature);
44+
});
45+
}
46+
47+
/**
48+
* Get the path to the config.
49+
*
50+
* @return string
51+
*/
52+
private function getConfigPath(): string
53+
{
54+
return __DIR__ . '/../config/unleash.php';
55+
}
56+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
3+
namespace MikeFrancis\LaravelUnleash\Strategies;
4+
5+
use Illuminate\Http\Request;
6+
use MikeFrancis\LaravelUnleash\Strategies\Contracts\Strategy;
7+
8+
class ApplicationHostnameStrategy implements Strategy
9+
{
10+
public function isEnabled(array $params, Request $request): bool
11+
{
12+
$applicationHostnames = explode(',', array_get($params, 'applicationHostname', ''));
13+
14+
if (count($applicationHostnames) === 0) {
15+
return false;
16+
}
17+
18+
return in_array($request->getHost(), $applicationHostnames);
19+
}
20+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
namespace MikeFrancis\LaravelUnleash\Strategies\Contracts;
4+
5+
use Illuminate\Http\Request;
6+
7+
interface Strategy
8+
{
9+
public function isEnabled(array $params, Request $request): bool;
10+
}

0 commit comments

Comments
 (0)