first commit
This commit is contained in:
@@ -0,0 +1,46 @@
|
||||
<?php
|
||||
|
||||
namespace WPML\CLI\Core;
|
||||
|
||||
use WPML\CLI\Core\Commands\ClearCacheFactory;
|
||||
use WPML\CLI\Core\Commands\ICommand;
|
||||
|
||||
class BootStrap {
|
||||
const MAIN_COMMAND = 'wpml';
|
||||
|
||||
/**
|
||||
* @throws \Exception The exception thrown by \WP_CLI::add_command.
|
||||
*/
|
||||
public function init() {
|
||||
$commands_factory = [
|
||||
ClearCacheFactory::class,
|
||||
];
|
||||
|
||||
foreach ( $commands_factory as $command_factory ) {
|
||||
$command_factory_obj = new $command_factory();
|
||||
|
||||
$command = $command_factory_obj->create();
|
||||
|
||||
$this->add_command( $this->getFullCommand( $command ), $command );
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $command_text The subcommand.
|
||||
* @param callable $command Command implementation as a class, function or closure.
|
||||
*
|
||||
* @throws \Exception The exception thrown by \WP_CLI::add_command.
|
||||
*/
|
||||
private function add_command( $command_text, $command ) {
|
||||
\WP_CLI::add_command( $command_text, $command );
|
||||
}
|
||||
|
||||
/**
|
||||
* @param ICommand $command Command implementation as a class, function or closure.
|
||||
*
|
||||
* @return string The sub command prefixed by the top-level command (all trimmed).
|
||||
*/
|
||||
private function getFullCommand( $command ) {
|
||||
return trim( self::MAIN_COMMAND . ' ' . $command->get_command() );
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
<?php
|
||||
namespace WPML\CLI\Core\Commands;
|
||||
|
||||
interface IWPML_Core extends IWPML_Command_Factory {
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
<?php
|
||||
namespace WPML\CLI\Core\Commands;
|
||||
|
||||
class ClearCache implements ICommand {
|
||||
|
||||
/**
|
||||
* @var \WPML_Cache_Directory
|
||||
*/
|
||||
private $cache_directory;
|
||||
|
||||
public function __construct( \WPML_Cache_Directory $cache_directory ) {
|
||||
$this->cache_directory = $cache_directory;
|
||||
}
|
||||
|
||||
/**
|
||||
* Clear the WPML cache
|
||||
*
|
||||
* ## EXAMPLE
|
||||
*
|
||||
* wp wpml clear-cache
|
||||
*
|
||||
* @when wpml_loaded
|
||||
*
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
public function __invoke( $args, $assoc_args ) {
|
||||
icl_cache_clear();
|
||||
$this->cache_directory->remove();
|
||||
|
||||
\WP_CLI::success( 'WPML cache cleared' );
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function get_command() {
|
||||
return 'clear-cache';
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
<?php
|
||||
namespace WPML\CLI\Core\Commands;
|
||||
|
||||
interface ICommand {
|
||||
|
||||
/**
|
||||
* @param string[] $args
|
||||
* @param array<string,string> $assoc_args
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
public function __invoke( $args, $assoc_args );
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function get_command();
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
<?php
|
||||
namespace WPML\CLI\Core\Commands;
|
||||
|
||||
use function WPML\Container\make;
|
||||
|
||||
class ClearCacheFactory implements IWPML_Core {
|
||||
|
||||
/**
|
||||
* @return ClearCache
|
||||
* @throws \WPML\Auryn\InjectionException If it's not possible to create the instance (see \WPML\Auryn\Injector::make).
|
||||
*/
|
||||
public function create() {
|
||||
return make( ClearCache::class );
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
<?php
|
||||
|
||||
namespace WPML\CLI\Core\Commands;
|
||||
|
||||
interface IWPML_Command_Factory {
|
||||
|
||||
/**
|
||||
* @return ICommand
|
||||
*/
|
||||
public function create();
|
||||
}
|
||||
Reference in New Issue
Block a user