Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/Export.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@
use WP_CLI;
use WP_SQLite_Translator;

class Export extends Base {
class Export {

protected $translator;
protected $args = array();
protected $is_stdout = false;

public function __construct() {
$this->load_dependencies();
SQLiteDatabaseIntegrationLoader::load_plugin();
$this->translator = new WP_SQLite_Translator();
}

Expand Down
4 changes: 2 additions & 2 deletions src/Import.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@
use WP_CLI;
use WP_SQLite_Translator;

class Import extends Base {
class Import {

protected $translator;
protected $args;

public function __construct() {
$this->load_dependencies();
SQLiteDatabaseIntegrationLoader::load_plugin();
$this->translator = new WP_SQLite_Translator();
}

Expand Down
9 changes: 5 additions & 4 deletions src/Base.php → src/SQLiteDatabaseIntegrationLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@

use WP_CLI;

class Base {
final class SQLiteDatabaseIntegrationLoader {

/**
* Get the version of the SQLite integration plugin if it is installed
* and activated.
*
* @return false|string The version of the SQLite integration plugin or false if not found/activated.
*/
public static function get_sqlite_plugin_version() {
public static function get_plugin_version() {
// Check if there is a db.php file in the wp-content directory.
if ( ! file_exists( ABSPATH . '/wp-content/db.php' ) ) {
return false;
Expand Down Expand Up @@ -62,13 +62,13 @@ protected static function get_plugin_directory() {
* @return void
* @throws WP_CLI\ExitException
*/
protected function load_dependencies() {
public static function load_plugin() {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

After discussing the initial changes in this PR with @wojtekn, we concluded that this method could also be static, and there's really no reason for the command classes to extend this class. Hence, I took the opportunity to make this into a standalone class with only static methods.

$plugin_directory = self::get_plugin_directory();
if ( ! $plugin_directory ) {
WP_CLI::error( 'Could not locate the SQLite integration plugin.' );
}

$sqlite_plugin_version = self::get_sqlite_plugin_version();
$sqlite_plugin_version = self::get_plugin_version();
if ( ! $sqlite_plugin_version ) {
WP_CLI::error( 'Could not determine the version of the SQLite integration plugin.' );
}
Expand All @@ -83,6 +83,7 @@ protected function load_dependencies() {
}

// We also need to selectively load the necessary classes from the plugin.
require_once $plugin_directory . '/php-polyfills.php';
require_once $plugin_directory . '/constants.php';
require_once $plugin_directory . '/wp-includes/sqlite/class-wp-sqlite-lexer.php';
require_once $plugin_directory . '/wp-includes/sqlite/class-wp-sqlite-query-rewriter.php';
Expand Down
8 changes: 0 additions & 8 deletions src/SQLite_Command.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,6 @@ public function import( $args, $assoc_args ) {
WP_CLI::error( 'Please provide a file to import.' );
}

if ( ! Import::get_sqlite_plugin_version() ) {
WP_CLI::error( 'The SQLite integration plugin is not installed or activated.' );
}
Comment on lines -32 to -34
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I realized the SQLiteDatabaseIntegrationLoader::load_plugin() method already performs an equivalent check. No need to duplicate that logic.


$import = new Import();
$import->run( $args[0], $assoc_args );
}
Expand Down Expand Up @@ -135,10 +131,6 @@ public function export( $args, $assoc_args ) {
* @when before_wp_load
*/
public function tables( $args, $assoc_args ) {
if ( ! Base::get_sqlite_plugin_version() ) {
WP_CLI::error( 'The SQLite integration plugin is not installed or activated.' );
}

$tables = new Tables();
$tables->run( $assoc_args );
}
Expand Down
4 changes: 2 additions & 2 deletions src/Tables.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@
use PDO;
use WP_SQLite_Translator;

class Tables extends Base {
class Tables {

protected $translator;

public function __construct() {
$this->load_dependencies();
SQLiteDatabaseIntegrationLoader::load_plugin();
$this->translator = new WP_SQLite_Translator();
}

Expand Down
Loading