PHP code example of stellarwp / foundation-wpcli

1. Go to this page and download the library: Download stellarwp/foundation-wpcli library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

stellarwp / foundation-wpcli example snippets


Acme\Plugin\Cli\Commands

 declare(strict_types=1);

namespace {{ namespace }};

use {{ foundation_wpcli_command }};

final class {{ class }} extends Command
{
	public function runCommand( array $args = [], array $assocArgs = [] ): int {
		// Run the command using services from $this->container.

		return self::SUCCESS;
	}

	protected function subcommand(): string {
		return {{ subcommand_php }};
	}

	protected function description(): string {
		return {{ description_php }};
	}

	protected function arguments(): array {
		return [
			[
				'type'        => self::FLAG,
				'name'        => 'dry-run',
				'description' => 'Preview the sync without writing changes.',
				'optional'    => true,
			],
		];
	}
}

 declare(strict_types=1);

namespace Acme\App\Cli;

use StellarWP\Foundation\Container\Contracts\Provider;
use StellarWP\Foundation\WPCli\Command;
use StellarWP\Foundation\WPCli\TimestampedLogger;
use WP_CLI;
use WP_CLI\Loggers\Regular;

final class Wp_Cli_Provider extends Provider
{
	private const string COMMAND_PREFIX = 'acme';

	/**
	 * @var list<class-string<Command>>
	 */
	private const array COMMANDS = [
		Sync_Command::class,
	];

	public function register(): void {
		if ( ! defined( 'WP_CLI' ) || ! WP_CLI ) {
			return;
		}

		$this->configureCommands();
		$this->registerTimestampedLogger();

		add_action( 'cli_init', function (): void {
			foreach ( self::COMMANDS as $commandClass ) {
				$command = $this->container->get( $commandClass );

				if ( $command instanceof Command ) {
					$command->register();
				}
			}
		}, 0, 0 );
	}

	private function configureCommands(): void {
		foreach ( self::COMMANDS as $commandClass ) {
			$this->container->when( $commandClass )
				->needs( '$commandPrefix' )
				->give( self::COMMAND_PREFIX );
		}
	}

	private function registerTimestampedLogger(): void {
		$wpCliLogger = WP_CLI::get_logger();

		if ( $wpCliLogger instanceof Regular ) {
			WP_CLI::set_logger( new TimestampedLogger( $wpCliLogger ) );
		}
	}
}
text
src/Cli/Commands/Sync_Products_Command.php