Command::diff_items( $args,  $assoc_args )

Compare items between environments.


Description Description

: Alias for the WordPress site to compare to.
[] : Limit comparison to a specific resource, instead of the collection.
[–fields=] : Limit comparison to specific fields.


Top ↑

Source Source

File: libraries/lifterlms-cli/src/Commands/Restful/Command.php

	public function diff_items( $args, $assoc_args ) {

		list( $alias ) = $args;
		if ( ! array_key_exists( $alias, \WP_CLI::get_runner()->aliases ) ) {
			\WP_CLI::error( "Alias '{$alias}' not found." );
		}
		$resource = isset( $args[1] ) ? $args[1] : null;
		$fields   = \WP_CLI\Utils\get_flag_value( $assoc_args, 'fields', null );

		list( $from_status, $from_body, $from_headers ) = $this->do_request( 'GET', $this->get_base_route(), array() );

		$php_bin          = \WP_CLI::get_php_binary();
		$script_path      = $GLOBALS['argv'][0];
		$other_args       = implode( ' ', array_map( 'escapeshellarg', array( $alias, 'rest', $this->name, 'list' ) ) );
		$other_assoc_args = \WP_CLI\Utils\assoc_args_to_str( array( 'format' => 'envelope' ) );
		$full_command     = "{$php_bin} {$script_path} {$other_args} {$other_assoc_args}";
		$process          = \WP_CLI\Process::create(
			$full_command,
			null,
			array(
				'HOME'                => getenv( 'HOME' ),
				'WP_CLI_PACKAGES_DIR' => getenv( 'WP_CLI_PACKAGES_DIR' ),
				'WP_CLI_CONFIG_PATH'  => getenv( 'WP_CLI_CONFIG_PATH' ),
			)
		);
		$result           = $process->run();
		$response         = json_decode( $result->stdout, true );
		$to_headers       = $response['headers'];
		$to_body          = $response['body'];
		$to_api_url       = $response['api_url'];

		if ( ! is_null( $resource ) ) {
			$field    = is_numeric( $resource ) ? 'id' : 'slug';
			$callback = function( $value ) use ( $field, $resource ) {
				if ( isset( $value[ $field ] ) && $resource == $value[ $field ] ) {
					return true;
				}
				return false;
			};
			foreach ( array( 'to_body', 'from_body' ) as $response_type ) {
				$$response_type = array_filter( $$response_type, $callback );
			}
		}

		$display_items = array();
		do {
			$from_item = $to_item = array();
			if ( ! empty( $from_body ) ) {
				$from_item = array_shift( $from_body );
				if ( ! empty( $to_body ) && ! empty( $from_item['slug'] ) ) {
					foreach ( $to_body as $i => $item ) {
						if ( ! empty( $item['slug'] ) && $item['slug'] === $from_item['slug'] ) {
							$to_item = $item;
							unset( $to_body[ $i ] );
							break;
						}
					}
				}
			} elseif ( ! empty( $to_body ) ) {
				$to_item = array_shift( $to_body );
			}

			if ( ! empty( $to_item ) ) {
				foreach ( array( 'to_item', 'from_item' ) as $item ) {
					if ( isset( $$item['_links'] ) ) {
						unset( $$item['_links'] );
					}
				}
				$display_items[] = array(
					'from' => self::limit_item_to_fields( $from_item, $fields ),
					'to'   => self::limit_item_to_fields( $to_item, $fields ),
				);
			}
		} while ( count( $from_body ) || count( $to_body ) );

		\WP_CLI::line( \cli\Colors::colorize( "%R(-) {$this->api_url} %G(+) {$to_api_url}%n" ) );
		foreach ( $display_items as $display_item ) {
			$this->show_difference(
				$this->name,
				array(
					'from' => $display_item['from'],
					'to'   => $display_item['to'],
				)
			);
		}
	}

Top ↑

User Contributed Notes User Contributed Notes

You must log in before being able to contribute a note or feedback.