ListCommand.php
10 months ago
ProductsCommand.php
9 months ago
ResetCommand.php
9 months ago
SetupCommand.php
9 months ago
ListCommand.php
87 lines
| 1 | <?php |
| 2 | |
| 3 | declare( strict_types=1 ); |
| 4 | |
| 5 | namespace Automattic\WooCommerce\Internal\CLI\Migrator\Commands; |
| 6 | |
| 7 | use Automattic\WooCommerce\Internal\CLI\Migrator\Core\PlatformRegistry; |
| 8 | use WP_CLI; |
| 9 | |
| 10 | /** |
| 11 | * Lists all registered migration platforms. |
| 12 | */ |
| 13 | class ListCommand { |
| 14 | |
| 15 | /** |
| 16 | * The platform registry. |
| 17 | * |
| 18 | * @var PlatformRegistry |
| 19 | */ |
| 20 | private PlatformRegistry $platform_registry; |
| 21 | |
| 22 | /** |
| 23 | * Initialize the command with its dependencies. |
| 24 | * |
| 25 | * @param PlatformRegistry $platform_registry The platform registry. |
| 26 | * |
| 27 | * @internal |
| 28 | */ |
| 29 | final public function init( PlatformRegistry $platform_registry ): void { |
| 30 | $this->platform_registry = $platform_registry; |
| 31 | } |
| 32 | |
| 33 | /** |
| 34 | * Lists all registered migration platforms. |
| 35 | * |
| 36 | * ## EXAMPLES |
| 37 | * |
| 38 | * $ wp wc migrate list |
| 39 | * |
| 40 | * @param array $args The positional arguments (unused). |
| 41 | * @param array $assoc_args The associative arguments (unused). |
| 42 | * |
| 43 | * @return void |
| 44 | */ |
| 45 | public function __invoke( array $args, array $assoc_args ): void { |
| 46 | // phpcs:ignore Generic.CodeAnalysis.UnusedFunctionParameter.FoundAfterLastUsed |
| 47 | unset( $args, $assoc_args ); |
| 48 | |
| 49 | $platforms = $this->platform_registry->get_platforms(); |
| 50 | |
| 51 | if ( empty( $platforms ) ) { |
| 52 | WP_CLI::line( 'No migration platforms are registered.' ); |
| 53 | return; |
| 54 | } |
| 55 | |
| 56 | $formatted_items = array(); |
| 57 | $platform_count = count( $platforms ); |
| 58 | $current_index = 0; |
| 59 | |
| 60 | foreach ( $platforms as $id => $details ) { |
| 61 | $formatted_items[] = array( |
| 62 | 'id' => $id, |
| 63 | 'name' => $details['name'] ?? '', |
| 64 | 'fetcher' => $details['fetcher'] ?? '', |
| 65 | 'mapper' => $details['mapper'] ?? '', |
| 66 | ); |
| 67 | |
| 68 | // Add separator row between platforms (but not after the last one). |
| 69 | ++$current_index; |
| 70 | if ( $current_index < $platform_count ) { |
| 71 | $formatted_items[] = array( |
| 72 | 'id' => str_repeat( '-', 20 ), |
| 73 | 'name' => str_repeat( '-', 25 ), |
| 74 | 'fetcher' => str_repeat( '-', 30 ), |
| 75 | 'mapper' => str_repeat( '-', 30 ), |
| 76 | ); |
| 77 | } |
| 78 | } |
| 79 | |
| 80 | WP_CLI\Utils\format_items( |
| 81 | 'table', |
| 82 | $formatted_items, |
| 83 | array( 'id', 'name', 'fetcher', 'mapper' ) |
| 84 | ); |
| 85 | } |
| 86 | } |
| 87 |