ExportCli.php
61 lines
| 1 | <?php |
| 2 | |
| 3 | namespace Automattic\WooCommerce\Blueprint\Cli; |
| 4 | |
| 5 | use Automattic\WooCommerce\Blueprint\ExportSchema; |
| 6 | use Automattic\WooCommerce\Blueprint\UseWPFunctions; |
| 7 | |
| 8 | /** |
| 9 | * Class ExportCli |
| 10 | * |
| 11 | * This class handles the CLI commands for exporting schemas. |
| 12 | * |
| 13 | * @package Automattic\WooCommerce\Blueprint\Cli |
| 14 | */ |
| 15 | class ExportCli { |
| 16 | use UseWPFunctions; |
| 17 | |
| 18 | /** |
| 19 | * The path where the exported schema will be saved. |
| 20 | * |
| 21 | * @var string The path where the exported schema will be saved. |
| 22 | */ |
| 23 | private string $save_to; |
| 24 | |
| 25 | /** |
| 26 | * ExportCli constructor. |
| 27 | * |
| 28 | * @param string $save_to The path where the exported schema will be saved. |
| 29 | */ |
| 30 | public function __construct( $save_to ) { |
| 31 | $this->save_to = $save_to; |
| 32 | } |
| 33 | |
| 34 | /** |
| 35 | * Run the export process. |
| 36 | * |
| 37 | * @param array $args The arguments for the export process. |
| 38 | */ |
| 39 | public function run( $args = array() ) { |
| 40 | if ( ! isset( $args['steps'] ) ) { |
| 41 | $args['steps'] = array(); |
| 42 | } |
| 43 | |
| 44 | $exporter = new ExportSchema(); |
| 45 | |
| 46 | $result = $exporter->export( $args['steps'] ); |
| 47 | if ( is_wp_error( $result ) ) { |
| 48 | \WP_CLI::error( $result->get_error_message() ); |
| 49 | return; |
| 50 | } |
| 51 | |
| 52 | $is_saved = $this->wp_filesystem_put_contents( $this->save_to, wp_json_encode( $result, JSON_PRETTY_PRINT ) ); |
| 53 | |
| 54 | if ( false === $is_saved ) { |
| 55 | \WP_CLI::error( "Failed to save to {$this->save_to}" ); |
| 56 | } else { |
| 57 | \WP_CLI::success( "Exported JSON to {$this->save_to}" ); |
| 58 | } |
| 59 | } |
| 60 | } |
| 61 |