Commands
2 weeks ago
stubs
2 weeks ago
CommandBase.php
2 weeks ago
CommandManager.php
2 weeks ago
Synopsis.php
2 weeks ago
CommandManager.php
81 lines
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * Registers framework commands with WP-CLI under a configurable base prefix. |
| 5 | * Resolves command classes from the container and validates they extend CommandBase. |
| 6 | * Bridges the application container and WordPress CLI for artisan-style command discovery. |
| 7 | * |
| 8 | * @package Framework |
| 9 | * @subpackage Console |
| 10 | * @since 1.0.0 |
| 11 | */ |
| 12 | namespace Kirki\Framework\Console; |
| 13 | |
| 14 | \defined('ABSPATH') || exit; |
| 15 | use RuntimeException; |
| 16 | use function Kirki\Framework\app; |
| 17 | class CommandManager |
| 18 | { |
| 19 | /** |
| 20 | * The base command name |
| 21 | * |
| 22 | * @var string |
| 23 | * |
| 24 | * @since 1.0.0 |
| 25 | */ |
| 26 | protected $command_base = 'kirki'; |
| 27 | /** |
| 28 | * Register a command |
| 29 | * |
| 30 | * @param string $name The name. |
| 31 | * @param mixed $command The command. |
| 32 | * |
| 33 | * @return void |
| 34 | * |
| 35 | * @since 1.0.0 |
| 36 | */ |
| 37 | public function register(string $name, $command) |
| 38 | { |
| 39 | $command = $this->resolve($command); |
| 40 | $args = $command->args(); |
| 41 | \WP_CLI::add_command($this->make($name), $command, $args); |
| 42 | } |
| 43 | /** |
| 44 | * Resolve the command |
| 45 | * |
| 46 | * @param mixed $command The command. |
| 47 | * |
| 48 | * @return CommandBase |
| 49 | * |
| 50 | * @throws \RuntimeException |
| 51 | * |
| 52 | * @since 1.0.0 |
| 53 | */ |
| 54 | protected function resolve($command) |
| 55 | { |
| 56 | if (\is_object($command)) { |
| 57 | if (!$command instanceof CommandBase) { |
| 58 | throw new RuntimeException(\sprintf("Command [%s] must extend [%s]", \get_class($command), CommandBase::class)); |
| 59 | } |
| 60 | return $command; |
| 61 | } |
| 62 | if (!\class_exists($command)) { |
| 63 | throw new RuntimeException(\sprintf("Command class [%s] not found", $command)); |
| 64 | } |
| 65 | return app()->make($command); |
| 66 | } |
| 67 | /** |
| 68 | * Make the command name |
| 69 | * |
| 70 | * @param mixed $command The command. |
| 71 | * |
| 72 | * @return string |
| 73 | * |
| 74 | * @since 1.0.0 |
| 75 | */ |
| 76 | protected function make($command) |
| 77 | { |
| 78 | return \sprintf('%s %s', $this->command_base, $command); |
| 79 | } |
| 80 | } |
| 81 |