|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace X\Commands\Commands; |
| 4 | + |
| 5 | +use Kirby\CLI\CLI; |
| 6 | +use Kirby\Toolkit\Str; |
| 7 | +use X\Commands\Lib\Toolkit; |
| 8 | + |
| 9 | +use function Laravel\Prompts\info; |
| 10 | +use function Laravel\Prompts\table; |
| 11 | +use function Laravel\Prompts\error; |
| 12 | +use function Laravel\Prompts\suggest; |
| 13 | + |
| 14 | +class Options extends Command |
| 15 | +{ |
| 16 | + public function __construct(CLI $cli) |
| 17 | + { |
| 18 | + parent::__construct($cli); |
| 19 | + |
| 20 | + if($cli->arg('--all')) { |
| 21 | + return $this->getOptions(); |
| 22 | + } |
| 23 | + |
| 24 | + $this->selectPlugin(); |
| 25 | + } |
| 26 | + |
| 27 | + private function selectPlugin() |
| 28 | + { |
| 29 | + $plugins = $this->kirby->plugins(); |
| 30 | + $fullPluginNames = array_keys(array_map(function($plugin) { |
| 31 | + return $plugin->name(); |
| 32 | + }, $plugins)); |
| 33 | + |
| 34 | + foreach($fullPluginNames as $plugin) { |
| 35 | + $pluginParts = explode('/', $plugin); |
| 36 | + $pluginNames[] = $pluginParts[1]." ({$plugin})"; |
| 37 | + } |
| 38 | + |
| 39 | + $safePluginNames = array_merge($fullPluginNames, $pluginNames); |
| 40 | + |
| 41 | + $selectedPlugin = suggest( |
| 42 | + label: 'Do you want options for a specific plugin? (Leavy empty for all)', |
| 43 | + options: $safePluginNames, |
| 44 | + placeholder: 'E.g. ray or bnomei/...', |
| 45 | + ); |
| 46 | + |
| 47 | + return $this->getOptions($selectedPlugin); |
| 48 | + } |
| 49 | + |
| 50 | + private function getOptions(string $plugin = null) |
| 51 | + { |
| 52 | + $allOptions = $this->kirby->options(); |
| 53 | + |
| 54 | + if(!empty($plugin)) { |
| 55 | + if(Str::contains($plugin, '(')) { |
| 56 | + $plugin = Str::between($plugin, '(', ')'); |
| 57 | + } |
| 58 | + |
| 59 | + |
| 60 | + if(!empty($allOptions[Str::replace($plugin, '/', '.')])) { |
| 61 | + $pluginOptions = $allOptions[Str::replace($plugin, '/', '.')]; |
| 62 | + return $this->printOptions($plugin, $pluginOptions); |
| 63 | + } |
| 64 | + |
| 65 | + return error('❌ Plugin not found.'); |
| 66 | + } |
| 67 | + |
| 68 | + return $this->printAllOptions($allOptions); |
| 69 | + } |
| 70 | + |
| 71 | + private function printOptions(string $plugin, array $pluginOptions) |
| 72 | + { |
| 73 | + $pluginOptions = Toolkit::flattenArray($pluginOptions); |
| 74 | + |
| 75 | + $table = []; |
| 76 | + foreach($pluginOptions as $option => $default) |
| 77 | + { |
| 78 | + $default = Toolkit::stringify($default); |
| 79 | + |
| 80 | + array_push($table, [ |
| 81 | + 'option' => $option, |
| 82 | + 'default' => $default, |
| 83 | + ]); |
| 84 | + } |
| 85 | + |
| 86 | + table( |
| 87 | + headers: ['Option', 'Default'], |
| 88 | + rows: $table, |
| 89 | + ); |
| 90 | + } |
| 91 | + |
| 92 | + private function printAllOptions(array $allOptions) |
| 93 | + { |
| 94 | + foreach($allOptions as $group => $options) { |
| 95 | + $groupOptions = Toolkit::flattenArray($options); |
| 96 | + |
| 97 | + if(is_array($groupOptions)) { |
| 98 | + $table = []; |
| 99 | + |
| 100 | + foreach($groupOptions as $option => $default) { |
| 101 | + array_push($table, [ |
| 102 | + 'group' => $group, |
| 103 | + 'option' => $option, |
| 104 | + 'default' => $default, |
| 105 | + ]); |
| 106 | + } |
| 107 | + |
| 108 | + if(empty($table)) { |
| 109 | + continue; |
| 110 | + } |
| 111 | + |
| 112 | + info($group); |
| 113 | + |
| 114 | + table( |
| 115 | + headers: ['Option', 'Default'], |
| 116 | + rows: array_map(function($item) { |
| 117 | + return [ |
| 118 | + $item['option'], |
| 119 | + Str::short(Toolkit::stringify($item['default']), 50), |
| 120 | + ]; |
| 121 | + }, $table), |
| 122 | + ); |
| 123 | + } |
| 124 | + |
| 125 | + if(!is_array($groupOptions)) { |
| 126 | + if($groupOptions instanceof \Closure) { |
| 127 | + $groupOptions = '-- Closure --'; |
| 128 | + } |
| 129 | + |
| 130 | + info($group); |
| 131 | + |
| 132 | + table( |
| 133 | + headers: ['Option', 'Default'], |
| 134 | + rows: [ |
| 135 | + [$group, Toolkit::stringify($groupOptions)], |
| 136 | + ], |
| 137 | + ); |
| 138 | + } |
| 139 | + } |
| 140 | + } |
| 141 | +} |
0 commit comments