Skip to content

Commit

Permalink
Replace textinput with selection
Browse files Browse the repository at this point in the history
  • Loading branch information
matgeroe committed Apr 29, 2019
1 parent 3fe98f2 commit 64a9482
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 7 deletions.
8 changes: 6 additions & 2 deletions src/Commands/DisableFeatureCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@

use Illuminate\Console\Command;
use MatthiasWilbrink\FeatureToggle\Exceptions\FeatureNotFoundException;
use MatthiasWilbrink\FeatureToggle\Exceptions\NoFeaturesException;
use MatthiasWilbrink\FeatureToggle\Facades\Feature;
use MatthiasWilbrink\FeatureToggle\Models\Feature as FeatureModel;

class DisableFeatureCommand extends Command
{
Expand All @@ -13,7 +15,7 @@ class DisableFeatureCommand extends Command
*
* @var string
*/
protected $signature = 'feature:disable {name : Feature name}';
protected $signature = 'feature:disable';

/**
* The console command description.
Expand All @@ -30,7 +32,7 @@ class DisableFeatureCommand extends Command
public function handle()
{
try {
$featureName = $this->argument('name');
$featureName = $this->choice('Which feature would you like to turn off?', Feature::options(FeatureModel::ON));

if (Feature::disable($featureName) !== null) {

Expand All @@ -42,6 +44,8 @@ public function handle()

} catch (FeatureNotFoundException $exception) {
$this->warn("Feature {$exception->featureName} could not be found");
} catch (NoFeaturesException $exception) {
$this->call('feature:list');
}
}
}
8 changes: 6 additions & 2 deletions src/Commands/EnableFeatureCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@

use Illuminate\Console\Command;
use MatthiasWilbrink\FeatureToggle\Exceptions\FeatureNotFoundException;
use MatthiasWilbrink\FeatureToggle\Exceptions\NoFeaturesException;
use MatthiasWilbrink\FeatureToggle\Facades\Feature;
use MatthiasWilbrink\FeatureToggle\Models\Feature as FeatureModel;

class EnableFeatureCommand extends Command
{
Expand All @@ -13,7 +15,7 @@ class EnableFeatureCommand extends Command
*
* @var string
*/
protected $signature = 'feature:enable {name : Feature name}';
protected $signature = 'feature:enable';

/**
* The console command description.
Expand All @@ -30,7 +32,7 @@ class EnableFeatureCommand extends Command
public function handle()
{
try {
$featureName = $this->argument('name');
$featureName = $this->choice('Which feature would you like to turn on?', Feature::options(FeatureModel::OFF));

if (Feature::enable($featureName) !== null) {

Expand All @@ -42,6 +44,8 @@ public function handle()

} catch (FeatureNotFoundException $exception) {
$this->warn("Feature {$exception->featureName} could not be found");
} catch (NoFeaturesException $exception) {
$this->call('feature:list');
}
}
}
6 changes: 4 additions & 2 deletions src/Commands/InsertNewFeaturesCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class InsertNewFeaturesCommand extends Command
*
* @var string
*/
protected $description= 'Reads features from config, and inserts them in the database';
protected $description = 'Reads features from config, and inserts them in the database';

/**
* Execute the console command.
Expand All @@ -29,8 +29,10 @@ class InsertNewFeaturesCommand extends Command
*/
public function handle()
{
$this->info('Reading features defined in config into the database');

collect(Config::get('features.features', null))
->each(function (int $default, string $feature ) {
->each(function (int $default, string $feature) {
if (Feature::createFeature($feature, $default) !== null) {
$this->info("Feature created: {$feature}.");
} else {
Expand Down
11 changes: 10 additions & 1 deletion src/Commands/ListFeaturesCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,15 @@ class ListFeaturesCommand extends Command
*/
public function handle()
{
$this->table(['id', 'name', 'state', 'created_at', 'updated_at'], Feature::allArray());
if (empty(Feature::allArray())) {
$this->call('feature:create');
}

if (empty(Feature::allArray())) {
$this->warn('Please define features in the config file');
} else {
$this->table(['id', 'name', 'state', 'created_at', 'updated_at'], Feature::allArray());
}

}
}
8 changes: 8 additions & 0 deletions src/Exceptions/NoFeaturesException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

namespace MatthiasWilbrink\FeatureToggle\Exceptions;

class NoFeaturesException extends \Exception
{

}
16 changes: 16 additions & 0 deletions src/Managers/FeatureManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Illuminate\Support\Facades\Config;
use MatthiasWilbrink\FeatureToggle\Exceptions\FeatureNotFoundException;
use MatthiasWilbrink\FeatureToggle\Exceptions\NoFeaturesException;
use MatthiasWilbrink\FeatureToggle\Models\Feature;

/**
Expand Down Expand Up @@ -133,6 +134,21 @@ public function allArray()
})->toArray();
}

/**
* Get features as options for commands
* @param int $filter
* @return array
* @throws NoFeaturesException
*/
public function options(int $filter)
{
$options = $this->all()->where('state', $filter);
if ($options->isNotEmpty()) {
return $options->pluck('name')->toArray();
}
throw new NoFeaturesException();
}

/**
* Get feature by name if it exists
*
Expand Down

0 comments on commit 64a9482

Please sign in to comment.