1616use App \ConfigManager ;
1717use App \Exceptions \RuntimeException ;
1818use App \Exceptions \UnsupportedConfigActionException ;
19- use Illuminate \Console \Scheduling \Schedule ;
2019use Illuminate \Support \Arr ;
2120use LaravelZero \Framework \Commands \Command ;
2221use Symfony \Component \Console \Completion \CompletionInput ;
@@ -53,12 +52,14 @@ public function __construct()
5352 * @throws \Psr\Container\ContainerExceptionInterface
5453 * @throws \Psr\Container\NotFoundExceptionInterface
5554 */
56- public function handle (ExecutableFinder $ executableFinder ): int
55+ public function handle (): int
5756 {
5857 $ file = $ this ->configFile ();
59- $ this ->output ->note ("The config file( $ file) is being operated. " );
58+
59+ $ this ->output ->note ("The config file [ $ file] is being operated. " );
6060 file_exists ($ file ) or $ this ->configManager ->putFile ($ file );
6161 $ this ->configManager ->replaceFrom ($ file );
62+
6263 $ action = $ this ->argument ('action ' );
6364 $ key = $ this ->argument ('key ' );
6465
@@ -97,25 +98,10 @@ public function handle(ExecutableFinder $executableFinder): int
9798
9899 break ;
99100 case 'edit ' :
100- $ editor = value (function () use ($ executableFinder ) {
101- if ($ editor = $ this ->option ('editor ' )) {
102- return $ editor ;
103- }
104-
105- $ editors = windows_os () ? self ::WINDOWS_EDITORS : self ::UNIX_EDITORS ;
106-
107- foreach ($ editors as $ editor ) {
108- if ($ executableFinder ->find ($ editor )) {
109- return $ editor ;
110- }
111- }
112-
113- throw new RuntimeException ('Unable to find a default editor or specify the editor. ' );
114- });
115-
116- tap (new Process ([$ editor , $ file ]), static function (Process $ process ): void {
117- Process::isTtySupported () and $ process ->setTty (true );
118- })->setTimeout (null )->mustRun ();
101+ tap (
102+ new Process ([$ this ->editor (), $ file ]),
103+ static fn (Process $ process ): bool => Process::isTtySupported () and $ process ->setTty (true )
104+ )->setTimeout (null )->mustRun ();
119105
120106 break ;
121107 default :
@@ -152,14 +138,6 @@ public function complete(CompletionInput $input, CompletionSuggestions $suggesti
152138 }
153139 }
154140
155- /**
156- * @noinspection PhpMissingParentCallCommonInspection
157- */
158- public function schedule (Schedule $ schedule ): void
159- {
160- // $schedule->command(static::class)->everyMinute();
161- }
162-
163141 public static function hydratedActions (): string
164142 {
165143 return implode (', ' , self ::ACTIONS );
@@ -196,50 +174,6 @@ protected function initialize(InputInterface $input, OutputInterface $output): v
196174 }
197175 }
198176
199- /**
200- * @throws \JsonException
201- */
202- private function argToValue (string $ arg ): mixed
203- {
204- // if (0 === strncasecmp($arg, 'null', 4)) {
205- // return;
206- // }
207- //
208- // if (0 === strncasecmp($arg, 'true', 4)) {
209- // return true;
210- // }
211- //
212- // if (0 === strncasecmp($arg, 'false', 5)) {
213- // return false;
214- // }
215- //
216- // if (is_numeric($arg)) {
217- // return str_contains($arg, '.') ? (float) $arg : (int) $arg;
218- // }
219-
220- if (str ($ arg )->isJson ()) {
221- return json_decode ($ arg , true , 512 , \JSON_THROW_ON_ERROR );
222- }
223-
224- return $ arg ;
225- }
226-
227- /**
228- * @noinspection JsonEncodingApiUsageInspection
229- */
230- private function valueToArg (mixed $ value ): string
231- {
232- // if (null === $value) {
233- // return 'null';
234- // }
235- //
236- // if (\is_scalar($value)) {
237- // return var_export($value, true);
238- // }
239-
240- return (string ) json_encode ($ value , \JSON_PRETTY_PRINT | \JSON_UNESCAPED_UNICODE );
241- }
242-
243177 private function configFile (): string
244178 {
245179 if ($ file = $ this ->option ('file ' )) {
@@ -252,4 +186,36 @@ private function configFile(): string
252186
253187 return ConfigManager::localPath ();
254188 }
189+
190+ private function editor (): string
191+ {
192+ if ($ editor = $ this ->option ('editor ' )) {
193+ return $ editor ;
194+ }
195+
196+ $ editors = windows_os () ? self ::WINDOWS_EDITORS : self ::UNIX_EDITORS ;
197+
198+ foreach ($ editors as $ editor ) {
199+ if (resolve (ExecutableFinder::class)->find ($ editor )) {
200+ return $ editor ;
201+ }
202+ }
203+
204+ throw new RuntimeException ('Unable to find a default editor or specify the editor. ' );
205+ }
206+
207+ /**
208+ * @throws \JsonException
209+ */
210+ private function argToValue (string $ arg ): mixed
211+ {
212+ return str ($ arg )->isJson () ? json_decode ($ arg , true , 512 , \JSON_THROW_ON_ERROR ) : $ arg ;
213+ }
214+
215+ private function valueToArg (mixed $ value ): string
216+ {
217+ return \is_string ($ value )
218+ ? $ value
219+ : json_encode ($ value , ConfigManager::JSON_OPTIONS );
220+ }
255221}
0 commit comments