Skip to content

Commit 4741e6e

Browse files
committed
Introduces the ability to use a custom table for rollout
1 parent 033642b commit 4741e6e

6 files changed

+132
-5
lines changed

composer.json

+2
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,9 @@
1313
"require": {
1414
"php": ">=7.0",
1515
"illuminate/cache": "^5.3",
16+
"illuminate/config": "^5.3",
1617
"illuminate/console": "^5.3",
18+
"illuminate/database": "^5.3",
1719
"illuminate/support": "^5.3",
1820
"opensoft/rollout": "^2.2"
1921
},

phpunit.xml

+3
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,7 @@
1818
<directory suffix=".php">./src/</directory>
1919
</whitelist>
2020
</filter>
21+
<php>
22+
<env name="APP_KEY" value="^J#@5t24wEugLfkEQRJ6W042heex66YA"/>
23+
</php>
2124
</phpunit>

resources/config/laravel-rollout.php

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?php
2+
3+
return [
4+
'storage' => env('ROLLOUT_STORAGE', 'cache'),
5+
'table' => env('ROLLOUT_TABLE', 'rollout')
6+
];
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
3+
use Illuminate\Support\Facades\Schema;
4+
use Illuminate\Database\Schema\Blueprint;
5+
use Illuminate\Database\Migrations\Migration;
6+
7+
class CreateRolloutTable extends Migration
8+
{
9+
/**
10+
* Run the migrations.
11+
*
12+
* @return void
13+
*/
14+
public function up()
15+
{
16+
Schema::create('rollout', function (Blueprint $table) {
17+
$table->string('key')->unique();
18+
$table->text('value');
19+
$table->integer('expiration');
20+
});
21+
}
22+
23+
/**
24+
* Reverse the migrations.
25+
*
26+
* @return void
27+
*/
28+
public function down()
29+
{
30+
Schema::dropIfExists('rollout');
31+
}
32+
}

src/ServiceProvider.php

+45-1
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,17 @@
33
namespace Jaspaul\LaravelRollout;
44

55
use Opensoft\Rollout\Rollout;
6+
use Illuminate\Cache\Repository;
7+
use Illuminate\Cache\DatabaseStore;
68
use Jaspaul\LaravelRollout\Drivers\Cache;
9+
use Illuminate\Database\ConnectionInterface;
10+
use Illuminate\Contracts\Encryption\Encrypter;
711
use Jaspaul\LaravelRollout\Console\ListCommand;
812
use Jaspaul\LaravelRollout\Console\CreateCommand;
913
use Jaspaul\LaravelRollout\Console\DeleteCommand;
1014
use Jaspaul\LaravelRollout\Console\AddUserCommand;
1115
use Jaspaul\LaravelRollout\Console\EveryoneCommand;
16+
use Illuminate\Contracts\Config\Repository as Config;
1217
use Jaspaul\LaravelRollout\Console\DeactivateCommand;
1318
use Jaspaul\LaravelRollout\Console\PercentageCommand;
1419
use Jaspaul\LaravelRollout\Console\RemoveUserCommand;
@@ -23,8 +28,25 @@ class ServiceProvider extends IlluminateServiceProvider
2328
*/
2429
public function boot()
2530
{
31+
$this->publishConfigurations();
32+
33+
$this->loadMigrations();
34+
2635
$this->app->singleton(Rollout::class, function ($app) {
27-
return new Rollout(new Cache($app->make('cache.store')));
36+
$config = $app->make(Config::class);
37+
38+
if ($config->get('laravel-rollout.storage') === 'database') {
39+
$connection = $app->make(ConnectionInterface::class);
40+
$encrypter = $app->make(Encrypter::class);
41+
$table = $config->get('laravel-rollout.table');
42+
43+
$repository = new Repository(new DatabaseStore($connection, $encrypter, $table));
44+
$driver = new Cache($repository);
45+
} else {
46+
$driver = new Cache($app->make('cache.store'));
47+
}
48+
49+
return new Rollout($driver);
2850
});
2951

3052
$this->commands([
@@ -38,4 +60,26 @@ public function boot()
3860
RemoveUserCommand::class
3961
]);
4062
}
63+
64+
/**
65+
* Adds our configuration file to the publishes array.
66+
*
67+
* @return void
68+
*/
69+
protected function publishConfigurations()
70+
{
71+
$this->publishes([
72+
__DIR__.'/../resources/config/laravel-rollout.php' => config_path('laravel-rollout.php'),
73+
]);
74+
}
75+
76+
/**
77+
* Loads our migrations.
78+
*
79+
* @return void
80+
*/
81+
protected function loadMigrations()
82+
{
83+
$this->loadMigrationsFrom(__DIR__.'/../resources/migrations');
84+
}
4185
}

tests/ServiceProviderTest.php

+44-4
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,14 @@
77
use Illuminate\Container\Container;
88
use Illuminate\Contracts\Cache\Repository;
99
use Jaspaul\LaravelRollout\ServiceProvider;
10+
use Illuminate\Database\ConnectionInterface;
11+
use Illuminate\Contracts\Encryption\Encrypter;
1012
use Jaspaul\LaravelRollout\Console\ListCommand;
1113
use Jaspaul\LaravelRollout\Console\CreateCommand;
1214
use Jaspaul\LaravelRollout\Console\DeleteCommand;
1315
use Jaspaul\LaravelRollout\Console\AddUserCommand;
1416
use Jaspaul\LaravelRollout\Console\EveryoneCommand;
17+
use Illuminate\Contracts\Config\Repository as Config;
1518
use Jaspaul\LaravelRollout\Console\DeactivateCommand;
1619
use Jaspaul\LaravelRollout\Console\PercentageCommand;
1720
use Jaspaul\LaravelRollout\Console\RemoveUserCommand;
@@ -43,8 +46,17 @@ function ensure_a_service_provider_can_be_constructed()
4346
/**
4447
* @test
4548
*/
46-
function booting_registers_a_rollout_singleton_into_the_container()
49+
function booting_registers_a_cache_backed_rollout_singleton_into_the_container()
4750
{
51+
$this->container->singleton(Config::class, function ($app) {
52+
$config = Mockery::mock(Config::class);
53+
$config->shouldReceive('get')
54+
->with('laravel-rollout.storage')
55+
->andReturn('cache');
56+
57+
return $config;
58+
});
59+
4860
$this->container->singleton('cache.store', function ($app) {
4961
return Mockery::mock(Repository::class);
5062
});
@@ -58,12 +70,40 @@ function booting_registers_a_rollout_singleton_into_the_container()
5870
/**
5971
* @test
6072
*/
61-
function booting_registers_our_commands()
73+
function booting_registers_a_database_backed_rollout_singleton_into_the_container()
6274
{
63-
$this->container->singleton('cache.store', function ($app) {
64-
return Mockery::mock(Repository::class);
75+
$this->container->singleton(Config::class, function ($app) {
76+
$config = Mockery::mock(Config::class);
77+
$config->shouldReceive('get')
78+
->with('laravel-rollout.storage')
79+
->andReturn('database');
80+
81+
$config->shouldReceive('get')
82+
->with('laravel-rollout.table')
83+
->andReturn('rollout');
84+
85+
return $config;
86+
});
87+
88+
$this->container->singleton(ConnectionInterface::class, function ($app) {
89+
return Mockery::mock(ConnectionInterface::class);
90+
});
91+
92+
$this->container->singleton(Encrypter::class, function ($app) {
93+
return Mockery::mock(Encrypter::class);
6594
});
6695

96+
$this->serviceProvider->boot();
97+
98+
$result = $this->container->make(Rollout::class);
99+
$this->assertInstanceOf(Rollout::class, $result);
100+
}
101+
102+
/**
103+
* @test
104+
*/
105+
function booting_registers_our_commands()
106+
{
67107
$serviceProvider = new TestServiceProvider($this->container);
68108
$serviceProvider->boot();
69109

0 commit comments

Comments
 (0)