diff --git a/src/Commands/DisableFeatureCommand.php b/src/Commands/DisableFeatureCommand.php index 04ca944..50765b3 100644 --- a/src/Commands/DisableFeatureCommand.php +++ b/src/Commands/DisableFeatureCommand.php @@ -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 { @@ -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. @@ -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) { @@ -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'); } } } \ No newline at end of file diff --git a/src/Commands/EnableFeatureCommand.php b/src/Commands/EnableFeatureCommand.php index dd59f10..e92ad40 100644 --- a/src/Commands/EnableFeatureCommand.php +++ b/src/Commands/EnableFeatureCommand.php @@ -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 { @@ -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. @@ -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) { @@ -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'); } } } \ No newline at end of file diff --git a/src/Commands/InsertNewFeaturesCommand.php b/src/Commands/InsertNewFeaturesCommand.php index d98ad1f..8e4ae18 100644 --- a/src/Commands/InsertNewFeaturesCommand.php +++ b/src/Commands/InsertNewFeaturesCommand.php @@ -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. @@ -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 { diff --git a/src/Commands/ListFeaturesCommand.php b/src/Commands/ListFeaturesCommand.php index 28aee4e..e2bbd29 100644 --- a/src/Commands/ListFeaturesCommand.php +++ b/src/Commands/ListFeaturesCommand.php @@ -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()); + } + } } \ No newline at end of file diff --git a/src/Exceptions/NoFeaturesException.php b/src/Exceptions/NoFeaturesException.php new file mode 100644 index 0000000..b079f24 --- /dev/null +++ b/src/Exceptions/NoFeaturesException.php @@ -0,0 +1,8 @@ +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 *