MigrationCommand.php
96 lines
| 1 | <?php |
| 2 | |
| 3 | namespace Give\FormMigration\Commands; |
| 4 | |
| 5 | use Give\DonationForms\Models\DonationForm as DonationFormV3; |
| 6 | use Give\DonationForms\V2\Models\DonationForm as DonationFormV2; |
| 7 | use Give\FormMigration\Concerns\Blocks\BlockDifference; |
| 8 | use Give\FormMigration\DataTransferObjects\FormMigrationPayload; |
| 9 | use Give\FormMigration\Pipeline; |
| 10 | use Give\Framework\Blocks\BlockModel; |
| 11 | use WP_CLI; |
| 12 | |
| 13 | class MigrationCommand |
| 14 | { |
| 15 | /** |
| 16 | * Prints a greeting. |
| 17 | * |
| 18 | * ## OPTIONS |
| 19 | * |
| 20 | * <id> |
| 21 | * : A form ID to migrate |
| 22 | * |
| 23 | * [--step] |
| 24 | * : Whether to inspect each step |
| 25 | */ |
| 26 | public function __invoke( $args, $assoc_args ) |
| 27 | { |
| 28 | [$formIdV2] = $args; |
| 29 | |
| 30 | $payload = FormMigrationPayload::fromFormV2( |
| 31 | DonationFormV2::find($formIdV2) |
| 32 | ); |
| 33 | |
| 34 | $pipeline = give(Pipeline::class); |
| 35 | |
| 36 | if(\WP_CLI\Utils\get_flag_value($assoc_args, 'step')) { |
| 37 | $pipeline->beforeEach(function ($stepClass, $payload) { |
| 38 | WP_CLI::log('Processing ' . $stepClass); |
| 39 | }); |
| 40 | $pipeline->afterEach([$this, 'afterEach']); |
| 41 | } |
| 42 | |
| 43 | $pipeline |
| 44 | ->process($payload) |
| 45 | ->finally(function(FormMigrationPayload $payload) { |
| 46 | $payload->formV3->save(); |
| 47 | WP_CLI::success( 'Migration Complete ' . $payload->formV3->id ); |
| 48 | }); |
| 49 | } |
| 50 | |
| 51 | public function afterEach($stepClass, FormMigrationPayload $payload, $_payload) |
| 52 | { |
| 53 | WP_CLI::log('Processed ' . $stepClass); |
| 54 | |
| 55 | foreach($payload->formV3->settings->toArray() as $key => $value) { |
| 56 | $previousValue = $_payload->formV3->settings->$key; |
| 57 | if($previousValue != $value) { // The check is loosely typed to support Enums |
| 58 | $value = is_array($value) ? '[Array]' : ( empty($value) ? '(empty)' : $value ); |
| 59 | $previousValue = is_array($previousValue) ? '[Array]' : ( empty($previousValue) ? '(empty)' : $previousValue ); |
| 60 | WP_CLI::log(''); |
| 61 | WP_CLI::log('Form Setting: ' . $key); |
| 62 | WP_CLI::log(' ' . $previousValue . ' => ' . $value); |
| 63 | WP_CLI::log(''); |
| 64 | } |
| 65 | } |
| 66 | |
| 67 | (new BlockDifference($_payload->formV3->blocks)) |
| 68 | ->skip('givewp/section') |
| 69 | ->onBlockAdded(function(BlockModel $block) { |
| 70 | WP_CLI::log(''); |
| 71 | WP_CLI::log('Block Added: ' . $block->name); |
| 72 | WP_CLI::log(' ' . json_encode($block->getAttributes())); |
| 73 | WP_CLI::log(''); |
| 74 | }) |
| 75 | ->onBlockDifference(function(BlockModel $block, $differences) { |
| 76 | WP_CLI::log(''); |
| 77 | WP_CLI::log('Block Updated: ' . $block->name); |
| 78 | foreach($differences as $key => $difference) { |
| 79 | WP_CLI::log(' ' . $key); |
| 80 | WP_CLI::log(' ' . ' ' . json_encode($difference['previous']) . ' => ' . json_encode($difference['current'])); |
| 81 | } |
| 82 | WP_CLI::log(''); |
| 83 | }) |
| 84 | ->diff($payload->formV3->blocks); |
| 85 | |
| 86 | fwrite( STDOUT, 'Continue?' . ' [enter] ' ); |
| 87 | fgets( STDIN ); |
| 88 | $this->clearOutput(); |
| 89 | } |
| 90 | |
| 91 | protected function clearOutput() |
| 92 | { |
| 93 | system('clear || cls'); // Clear screen with cross-platform. |
| 94 | } |
| 95 | } |
| 96 |