diff --git a/src/Illuminate/Foundation/Console/ConfigViewCommand.php b/src/Illuminate/Foundation/Console/ConfigViewCommand.php new file mode 100644 index 000000000000..6ae9d7e4f909 --- /dev/null +++ b/src/Illuminate/Foundation/Console/ConfigViewCommand.php @@ -0,0 +1,120 @@ +argument('section'); + if (! empty($section)) { + if (! array_key_exists($section, $this->laravel['config'])) { + $this->error($section.' does not exist in your configuration.'); + + return; + } + + // If the requested string is a string, and not something that can be displayed in a table, then just print + // it out in the console. + $config = $this->laravel['config'][$section]; + if (! is_array($config)) { + $this->line(''.$config.''); + + return; + } + + $configs = [ + $section => $config, + ]; + } else { + $configs = $this->laravel['config']; + ksort($configs); + } + + foreach ($configs as $section => $config) { + $depth = $this->getConfigDepth($config); + $tree = $this->generateTree($config, $depth); + $this->line('Configuration: '.$section.''); + $this->table([/* don't display any headers */], $tree, 'default'); + } + } + + /** + * Take a configuration array, recursively traverse and create a padded tree array to output as a table. + * + * @param array $data + * @param int $total_depth + * @param int $current_depth + * @return array + */ + private function generateTree($data, $total_depth, $current_depth = 1) + { + ksort($data); + $table = []; + foreach ($data as $key => $value) { + $row = []; + if ($current_depth > 1) { + $row = array_merge($row, array_fill(0, ($current_depth - 1), '')); + } + + $row[] = ''.$key.''; + if (! is_array($value)) { + $row[] = (is_bool($value)) ? (($value) ? 'true' : 'false') : $value; + } + + // Pad the row out with empty columns equal to the total subtracting the current recursive depth. + $empty_columns = ($total_depth - $current_depth); + $empty_columns = array_fill(count($row) - 1, $empty_columns, ''); + $row = array_merge($row, $empty_columns); + $table[] = $row; + if (is_array($value)) { + $table = array_merge($table, $this->generateTree($value, $total_depth, $current_depth + 1)); + } + } + + return $table; + } + + /** + * Determine the depth of this configuration so we know how to pad the outputted table of data. + * + * @param array $config + * @return int + */ + private function getConfigDepth($config) + { + $total_depth = 1; + foreach ($config as $data) { + if (is_array($data)) { + $depth = $this->getConfigDepth($data) + 1; + if ($depth > $total_depth) { + $total_depth = $depth; + } + } + } + + return $total_depth; + } +} diff --git a/src/Illuminate/Foundation/Providers/ArtisanServiceProvider.php b/src/Illuminate/Foundation/Providers/ArtisanServiceProvider.php index e72079b7de1d..4dc68b643b70 100755 --- a/src/Illuminate/Foundation/Providers/ArtisanServiceProvider.php +++ b/src/Illuminate/Foundation/Providers/ArtisanServiceProvider.php @@ -28,6 +28,7 @@ use Illuminate\Routing\Console\MiddlewareMakeCommand; use Illuminate\Foundation\Console\ConfigCacheCommand; use Illuminate\Foundation\Console\ConfigClearCommand; +use Illuminate\Foundation\Console\ConfigViewCommand; use Illuminate\Foundation\Console\ConsoleMakeCommand; use Illuminate\Foundation\Console\EnvironmentCommand; use Illuminate\Foundation\Console\KeyGenerateCommand; @@ -58,6 +59,7 @@ class ArtisanServiceProvider extends ServiceProvider 'ClearResets' => 'command.auth.resets.clear', 'ConfigCache' => 'command.config.cache', 'ConfigClear' => 'command.config.clear', + 'ConfigView' => 'command.config.view', 'Down' => 'command.down', 'Environment' => 'command.environment', 'KeyGenerate' => 'command.key.generate', @@ -214,6 +216,18 @@ protected function registerConfigClearCommand() }); } + /** + * Register the command. + * + * @return void + */ + protected function registerConfigViewCommand() + { + $this->app->singleton('command.config.view', function ($app) { + return new ConfigViewCommand; + }); + } + /** * Register the command. *