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

[Feature] Database Storage #4

Merged
merged 4 commits into from
May 24, 2017
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
vendor
composer.lock
.php_cs.cache
coverage
2 changes: 2 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@
"require": {
"php": ">=7.0",
"illuminate/cache": "^5.3",
"illuminate/config": "^5.3",
"illuminate/console": "^5.3",
"illuminate/database": "^5.3",
"illuminate/support": "^5.3",
"opensoft/rollout": "^2.2"
},
Expand Down
3 changes: 3 additions & 0 deletions phpunit.xml
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,7 @@
<directory suffix=".php">./src/</directory>
</whitelist>
</filter>
<php>
<env name="APP_KEY" value="^J#@5t24wEugLfkEQRJ6W042heex66YA"/>
</php>
</phpunit>
33 changes: 29 additions & 4 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,6 @@ A Laravel package for [opensoft/rollout](https://github.com/opensoft/rollout)
composer require jaspaul/laravel-rollout
```

### Setting up a Cache

Be sure to [enable the cache](https://laravel.com/docs/5.4/cache) for your Laravel application. This package uses the cache to store the rollout settings.

### Configuring the Service Provider

Open config/app.php and register the required service provider above your application providers.
Expand All @@ -28,6 +24,35 @@ Open config/app.php and register the required service provider above your applic
]
```

### Setting up Storage

#### Publish the Configuration

```sh
php artisan vendor:publish --provider 'Jaspaul\LaravelRollout\ServiceProvider'
```

#### Setting up a Cache

If you intend to use cache to store the settings for rollout, be sure to [enable the cache](https://laravel.com/docs/5.4/cache) for your Laravel application. Note if you are using the cache, a cache clear during deployment will cause your rollout settings to be purged. If you require persistence for these settings use the option below.

#### Setting up Persistent Storage

This will allow you to have rollout settings be persisted even if you clear the application cache for every deployment.

##### Running the Migrations

```sh
php artisan migrate
```

##### Configuring your Environment

```
ROLLOUT_STORAGE=database
ROLLOUT_TABLE=rollout
```

### Implementing Interfaces

Your rollout users must implement the `\Jaspaul\LaravelRollout\Helpers\User` interface. Often this will be your main user object:
Expand Down
6 changes: 6 additions & 0 deletions resources/config/laravel-rollout.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?php

return [
'storage' => env('ROLLOUT_STORAGE', 'cache'),
'table' => env('ROLLOUT_TABLE', 'rollout')
];
32 changes: 32 additions & 0 deletions resources/migrations/2017_05_24_000000_create_rollout_table.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateRolloutTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('rollout', function (Blueprint $table) {
$table->string('key')->unique();
$table->text('value');
$table->integer('expiration');
});
}

/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('rollout');
}
}
46 changes: 45 additions & 1 deletion src/ServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,17 @@
namespace Jaspaul\LaravelRollout;

use Opensoft\Rollout\Rollout;
use Illuminate\Cache\Repository;
use Illuminate\Cache\DatabaseStore;
use Jaspaul\LaravelRollout\Drivers\Cache;
use Illuminate\Database\ConnectionInterface;
use Illuminate\Contracts\Encryption\Encrypter;
use Jaspaul\LaravelRollout\Console\ListCommand;
use Jaspaul\LaravelRollout\Console\CreateCommand;
use Jaspaul\LaravelRollout\Console\DeleteCommand;
use Jaspaul\LaravelRollout\Console\AddUserCommand;
use Jaspaul\LaravelRollout\Console\EveryoneCommand;
use Illuminate\Contracts\Config\Repository as Config;
use Jaspaul\LaravelRollout\Console\DeactivateCommand;
use Jaspaul\LaravelRollout\Console\PercentageCommand;
use Jaspaul\LaravelRollout\Console\RemoveUserCommand;
Expand All @@ -23,8 +28,25 @@ class ServiceProvider extends IlluminateServiceProvider
*/
public function boot()
{
$this->publishConfigurations();

$this->loadMigrations();

$this->app->singleton(Rollout::class, function ($app) {
return new Rollout(new Cache($app->make('cache.store')));
$config = $app->make(Config::class);

if ($config->get('laravel-rollout.storage') === 'database') {
$connection = $app->make(ConnectionInterface::class);
$encrypter = $app->make(Encrypter::class);
$table = $config->get('laravel-rollout.table');

$repository = new Repository(new DatabaseStore($connection, $encrypter, $table));
$driver = new Cache($repository);
} else {
$driver = new Cache($app->make('cache.store'));
}

return new Rollout($driver);
});

$this->commands([
Expand All @@ -38,4 +60,26 @@ public function boot()
RemoveUserCommand::class
]);
}

/**
* Adds our configuration file to the publishes array.
*
* @return void
*/
protected function publishConfigurations()
{
$this->publishes([
__DIR__.'/../resources/config/laravel-rollout.php' => config_path('laravel-rollout.php'),
]);
}

/**
* Loads our migrations.
*
* @return void
*/
protected function loadMigrations()
{
$this->loadMigrationsFrom(__DIR__.'/../resources/migrations');
}
}
48 changes: 44 additions & 4 deletions tests/ServiceProviderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,14 @@
use Illuminate\Container\Container;
use Illuminate\Contracts\Cache\Repository;
use Jaspaul\LaravelRollout\ServiceProvider;
use Illuminate\Database\ConnectionInterface;
use Illuminate\Contracts\Encryption\Encrypter;
use Jaspaul\LaravelRollout\Console\ListCommand;
use Jaspaul\LaravelRollout\Console\CreateCommand;
use Jaspaul\LaravelRollout\Console\DeleteCommand;
use Jaspaul\LaravelRollout\Console\AddUserCommand;
use Jaspaul\LaravelRollout\Console\EveryoneCommand;
use Illuminate\Contracts\Config\Repository as Config;
use Jaspaul\LaravelRollout\Console\DeactivateCommand;
use Jaspaul\LaravelRollout\Console\PercentageCommand;
use Jaspaul\LaravelRollout\Console\RemoveUserCommand;
Expand Down Expand Up @@ -43,8 +46,17 @@ function ensure_a_service_provider_can_be_constructed()
/**
* @test
*/
function booting_registers_a_rollout_singleton_into_the_container()
function booting_registers_a_cache_backed_rollout_singleton_into_the_container()
{
$this->container->singleton(Config::class, function ($app) {
$config = Mockery::mock(Config::class);
$config->shouldReceive('get')
->with('laravel-rollout.storage')
->andReturn('cache');

return $config;
});

$this->container->singleton('cache.store', function ($app) {
return Mockery::mock(Repository::class);
});
Expand All @@ -58,12 +70,40 @@ function booting_registers_a_rollout_singleton_into_the_container()
/**
* @test
*/
function booting_registers_our_commands()
function booting_registers_a_database_backed_rollout_singleton_into_the_container()
{
$this->container->singleton('cache.store', function ($app) {
return Mockery::mock(Repository::class);
$this->container->singleton(Config::class, function ($app) {
$config = Mockery::mock(Config::class);
$config->shouldReceive('get')
->with('laravel-rollout.storage')
->andReturn('database');

$config->shouldReceive('get')
->with('laravel-rollout.table')
->andReturn('rollout');

return $config;
});

$this->container->singleton(ConnectionInterface::class, function ($app) {
return Mockery::mock(ConnectionInterface::class);
});

$this->container->singleton(Encrypter::class, function ($app) {
return Mockery::mock(Encrypter::class);
});

$this->serviceProvider->boot();

$result = $this->container->make(Rollout::class);
$this->assertInstanceOf(Rollout::class, $result);
}

/**
* @test
*/
function booting_registers_our_commands()
{
$serviceProvider = new TestServiceProvider($this->container);
$serviceProvider->boot();

Expand Down