|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +/** |
| 6 | + * This file is part of CodeIgniter 4 framework. |
| 7 | + * |
| 8 | + * (c) CodeIgniter Foundation <[email protected]> |
| 9 | + * |
| 10 | + * For the full copyright and license information, please view |
| 11 | + * the LICENSE file that was distributed with this source code. |
| 12 | + */ |
| 13 | + |
| 14 | +namespace Tests\Support\Commands\Modern; |
| 15 | + |
| 16 | +use CodeIgniter\CLI\AbstractCommand; |
| 17 | +use CodeIgniter\CLI\Attributes\Command; |
| 18 | +use CodeIgniter\CLI\CLI; |
| 19 | +use CodeIgniter\CLI\Input\Argument; |
| 20 | +use CodeIgniter\CLI\Input\Option; |
| 21 | +use CodeIgniter\CodeIgniter; |
| 22 | +use CodeIgniter\Exceptions\RuntimeException; |
| 23 | + |
| 24 | +#[Command(name: 'app:about', description: 'Displays basic application information.', group: 'Fixtures')] |
| 25 | +final class AppAboutCommand extends AbstractCommand |
| 26 | +{ |
| 27 | + protected function configure(): void |
| 28 | + { |
| 29 | + $this |
| 30 | + ->addArgument(new Argument(name: 'required', description: 'Unused required argument.', required: true)) |
| 31 | + ->addArgument(new Argument(name: 'optional', description: 'Unused optional argument.', default: 'val')) |
| 32 | + ->addArgument(new Argument(name: 'array', description: 'Unused array argument.', isArray: true, default: ['a', 'b'])) |
| 33 | + ->addOption(new Option(name: 'foo', shortcut: 'f', description: 'Option that requires a value.', requiresValue: true, default: 'qux')) |
| 34 | + ->addOption(new Option(name: 'bar', shortcut: 'a', description: 'Option that optionally accepts a value.', acceptsValue: true)) |
| 35 | + ->addOption(new Option(name: 'baz', shortcut: 'b', description: 'Option that allows multiple values.', requiresValue: true, isArray: true, default: ['a'])) |
| 36 | + ->addOption(new Option(name: 'quux', description: 'Negatable option.', negatable: true, default: false)) |
| 37 | + ->addUsage('app:about required-value'); |
| 38 | + } |
| 39 | + |
| 40 | + protected function execute(array $arguments, array $options): int |
| 41 | + { |
| 42 | + CLI::write(sprintf('CodeIgniter Version: %s', CLI::color(CodeIgniter::CI_VERSION, 'red'))); |
| 43 | + |
| 44 | + return EXIT_SUCCESS; |
| 45 | + } |
| 46 | + |
| 47 | + public function bomb(): int |
| 48 | + { |
| 49 | + try { |
| 50 | + CLI::color('test', 'white', 'Background'); |
| 51 | + |
| 52 | + return EXIT_SUCCESS; |
| 53 | + } catch (RuntimeException $e) { |
| 54 | + $this->renderThrowable($e); |
| 55 | + |
| 56 | + return EXIT_ERROR; |
| 57 | + } |
| 58 | + } |
| 59 | + |
| 60 | + public function helpMe(): int |
| 61 | + { |
| 62 | + return $this->call('help'); |
| 63 | + } |
| 64 | + |
| 65 | + /** |
| 66 | + * @param array<string, list<string|null>|string|null>|null $options |
| 67 | + */ |
| 68 | + public function callHasUnboundOption(string $name, ?array $options = null): bool |
| 69 | + { |
| 70 | + return $this->hasUnboundOption($name, $options); |
| 71 | + } |
| 72 | + |
| 73 | + /** |
| 74 | + * @param array<string, list<string|null>|string|null>|null $options |
| 75 | + * @param list<string|null>|string|null $default |
| 76 | + * |
| 77 | + * @return list<string|null>|string|null |
| 78 | + */ |
| 79 | + public function callGetUnboundOption(string $name, ?array $options = null, array|string|null $default = null): array|string|null |
| 80 | + { |
| 81 | + return $this->getUnboundOption($name, $options, $default); |
| 82 | + } |
| 83 | + |
| 84 | + /** |
| 85 | + * @return list<string> |
| 86 | + */ |
| 87 | + public function callGetUnboundArguments(): array |
| 88 | + { |
| 89 | + return $this->getUnboundArguments(); |
| 90 | + } |
| 91 | + |
| 92 | + public function callGetUnboundArgument(int $index): string |
| 93 | + { |
| 94 | + return $this->getUnboundArgument($index); |
| 95 | + } |
| 96 | + |
| 97 | + /** |
| 98 | + * @return array<string, list<string|null>|string|null> |
| 99 | + */ |
| 100 | + public function callGetUnboundOptions(): array |
| 101 | + { |
| 102 | + return $this->getUnboundOptions(); |
| 103 | + } |
| 104 | + |
| 105 | + /** |
| 106 | + * @return array<string, list<string>|string> |
| 107 | + */ |
| 108 | + public function callGetValidatedArguments(): array |
| 109 | + { |
| 110 | + return $this->getValidatedArguments(); |
| 111 | + } |
| 112 | + |
| 113 | + /** |
| 114 | + * @return list<string>|string |
| 115 | + */ |
| 116 | + public function callGetValidatedArgument(string $name): array|string |
| 117 | + { |
| 118 | + return $this->getValidatedArgument($name); |
| 119 | + } |
| 120 | + |
| 121 | + /** |
| 122 | + * @return array<string, bool|list<string>|string|null> |
| 123 | + */ |
| 124 | + public function callGetValidatedOptions(): array |
| 125 | + { |
| 126 | + return $this->getValidatedOptions(); |
| 127 | + } |
| 128 | + |
| 129 | + /** |
| 130 | + * @return bool|list<string>|string|null |
| 131 | + */ |
| 132 | + public function callGetValidatedOption(string $name): array|bool|string|null |
| 133 | + { |
| 134 | + return $this->getValidatedOption($name); |
| 135 | + } |
| 136 | +} |
0 commit comments