Last active
April 10, 2016 15:06
-
-
Save themsaid/d2e7dc55c23e1da80cd9d5de47889d67 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\Console\Commands; | |
use Illuminate\Filesystem\Filesystem; | |
use Illuminate\Console\Command; | |
use Illuminate\Support\Arr; | |
class ConfigManager extends Command | |
{ | |
/** | |
* The name and signature of the console command. | |
* | |
* @var string | |
*/ | |
protected $signature = 'config:manage {file?}'; | |
/** | |
* The console command description. | |
* | |
* @var string | |
*/ | |
protected $description = 'Manage application config files'; | |
/** | |
* @var \Illuminate\Filesystem\Filesystem | |
*/ | |
private $disk; | |
/** | |
* The path to the configuration files. | |
* | |
* @var string | |
*/ | |
private $configPath; | |
/** | |
* Create a new command instance. | |
* | |
* @return void | |
*/ | |
public function __construct(Filesystem $disk, $configPath = '') | |
{ | |
parent::__construct(); | |
$this->disk = $disk; | |
$this->configPath = $configPath ? $configPath : config_path(); | |
} | |
/** | |
* Execute the console command. | |
* | |
* @return mixed | |
*/ | |
public function handle() | |
{ | |
$configFiles = $this->configFiles(); | |
// If the user didn't provide a file, we'll ask him to choose one of | |
// the files already existing in the config directory. | |
if (is_null($file = $this->argument('file'))) { | |
$file = $this->choice('Please select a file', $configFiles); | |
} | |
// Otherwise, if a file was provided we'll check if the file doesn't | |
// exist, and if so, we'll show an error and exit. | |
else { | |
if (! in_array($file, $configFiles)) { | |
return $this->error('The provided config file was not found!'); | |
} | |
} | |
// Once a file is selected we'll bring the content of this file, which is | |
// an array, and present it in a table view. We'll use the renderRows() | |
// method to convert the file content to renderable table rows. | |
$this->displayTable($file); | |
// After displaying the table, we'll ask the user to check if he wants to | |
// add a new configuration file. | |
if ($this->confirm('Would you like to create a new file?')) { | |
// If the answer is "yes", we ask him to provide a file name and check | |
// to see if we can create it by checking the file already exists. | |
$newFile = $this->ask('File name [ex: facebook]'); | |
if (in_array($newFile, $configFiles)) { | |
return $this->error('File already exists!'); | |
} | |
file_put_contents($this->configPath."/{$newFile}.php", "<?php\n\n return [];"); | |
return $this->info("{$newFile}.php was created successfully!"); | |
} | |
return $this->warn("No file was created!"); | |
} | |
/** | |
* The array of all available application config files. | |
* | |
* @return array | |
*/ | |
private function configFiles() | |
{ | |
return array_map(function ($file) { | |
return basename($file, '.php'); | |
}, $this->disk->files($this->configPath)); | |
} | |
/** | |
* Display a table with the file content. | |
* | |
* @param $file | |
* @return void | |
*/ | |
private function displayTable($file) | |
{ | |
if (! $file) { | |
return; | |
} | |
$this->table( | |
['Key', 'Value'], | |
$this->renderRows((array) include $this->configPath."/{$file}.php") | |
); | |
} | |
/** | |
* Render the table rows from the given array of values. | |
* | |
* @param $fileContent | |
* @return array | |
*/ | |
private function renderRows($array) | |
{ | |
$array = Arr::dot($array); | |
$output = []; | |
foreach ($array as $key => $value) { | |
$output[] = [$key, $value]; | |
} | |
return $output; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment