Skip to content

Commit

Permalink
Merge pull request #29 from szepeviktor/class-move
Browse files Browse the repository at this point in the history
Move main plugin class to a PSR-4 file
  • Loading branch information
HPiirainen authored May 8, 2024
2 parents 9847ed3 + 7485af7 commit 5a562e4
Show file tree
Hide file tree
Showing 4 changed files with 144 additions and 136 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
},
"autoload": {
"psr-4": {
"Geniem\\Oopi\\": ["src/", "plugin.php"]
"Geniem\\Oopi\\": "src/"
}
},
"require-dev": {
Expand Down
134 changes: 0 additions & 134 deletions plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,6 @@

namespace Geniem\Oopi;

use Geniem\Oopi\Exception\TypeException;
use Geniem\Oopi\Importable\AttachmentImportable;
use Geniem\Oopi\Importable\PostImportable;
use Geniem\Oopi\Importable\TermImportable;
use Geniem\Oopi\Importer\AttachmentImporter;
use Geniem\Oopi\Importer\PostImporter;
use Geniem\Oopi\Importer\TermImporter;
use Geniem\Oopi\Interfaces\Importable;
use Geniem\Oopi\Interfaces\Importer;
use Geniem\Oopi\Localization\LanguageUtil;
use ReflectionClass;
use ReflectionException;

if ( ! defined( 'ABSPATH' ) ) {
exit;
}
Expand All @@ -34,127 +21,6 @@
require_once __DIR__ . '/vendor/autoload.php';
}

/**
* The base class for the plugin.
*
* @package Geniem
*/
class Plugin {

/**
* Holds the general plugin data.
*
* @var array
*/
protected static $plugin_data = [
'TABLE_NAME' => 'oopi_log',
];

/**
* Run tasks when plugin is activated.
*/
public static function install() {
// Install log handler.
Log::install();

// Register cron jobs.
CronJobs::install();
}


/**
* Run tasks when plugin is deactivated.
*/
public static function uninstall() {
// Remove cron jobs.
CronJobs::uninstall();
}

/**
* A map of importable class names and their corresponding import handlers.
*
* @var Importable[]
*/
private static $importables = [];

/**
* Initialize the plugin.
*/
public static function init() {
// Set the plugin version.
$plugin_data = get_file_data( __FILE__, [ 'Version' => 'Version' ], 'plugin' );
self::$plugin_data = array_merge( $plugin_data, self::$plugin_data );

// Set the basic settings.
Settings::init( self::$plugin_data );

// Initialize the import handlers for importables.
self::set_initial_import_handlers();

// Register the cron job hooks.
CronJobs::init();

// Initialize plugin controllers after plugins are loaded.
add_action( 'wp_loaded', function() {
LanguageUtil::init();
} );
}

/**
* Sets the initial state of global handlers for all importables.
*/
protected static function set_initial_import_handlers() {
static::$importables = [
PostImportable::class => PostImporter::class,
TermImportable::class => TermImporter::class,
AttachmentImportable::class => AttachmentImporter::class,
];
}

/**
* Add or override a global importable and/or its import handler.
*
* @param string $importable_class The importable class name.
* @param string $importer_class The importer class name.
*
* @throws ReflectionException|TypeException If any of the given classes are missing or they are of the wrong type,
* an error is thrown.
*/
public static function set_importable( string $importable_class, string $importer_class ) {
$importable_reflect = new ReflectionClass( $importable_class );
$importer_reflect = new ReflectionClass( $importer_class );

$importable_interface = Importable::class;
$importer_interface = Importer::class;

$valid_classes =
$importable_reflect->implementsInterface( Importable::class )
&& $importer_reflect->implementsInterface( Importer::class );

if ( $valid_classes ) {
static::$importables[ $importable_class ] = $importer_class;
}
else {
throw new TypeException(
"The passed classes must implement the following interfaces:
importable: $importable_interface, importer: $importer_interface."
);
}
}

/**
* Get the global importer for the given importable class name.
*
* @param string $importable_class The importable class name.
*
* @return ?Importer
*/
public static function get_importer( string $importable_class ) : ?Importer {
$importer = new static::$importables[ $importable_class ]();
return $importer;
}
}

// Initialize the plugin.
Plugin::init();

Expand Down
140 changes: 140 additions & 0 deletions src/Plugin.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
<?php
/**
* The main plugin class.
*/

namespace Geniem\Oopi;

use Geniem\Oopi\Exception\TypeException;
use Geniem\Oopi\Importable\AttachmentImportable;
use Geniem\Oopi\Importable\PostImportable;
use Geniem\Oopi\Importable\TermImportable;
use Geniem\Oopi\Importer\AttachmentImporter;
use Geniem\Oopi\Importer\PostImporter;
use Geniem\Oopi\Importer\TermImporter;
use Geniem\Oopi\Interfaces\Importable;
use Geniem\Oopi\Interfaces\Importer;
use Geniem\Oopi\Localization\LanguageUtil;
use ReflectionClass;
use ReflectionException;

/**
* The base class for the plugin.
*
* @package Geniem
*/
class Plugin {

/**
* Holds the general plugin data.
*
* @var array
*/
protected static $plugin_data = [
'TABLE_NAME' => 'oopi_log',
];

/**
* Run tasks when plugin is activated.
*/
public static function install() {
// Install log handler.
Log::install();

// Register cron jobs.
CronJobs::install();
}


/**
* Run tasks when plugin is deactivated.
*/
public static function uninstall() {
// Remove cron jobs.
CronJobs::uninstall();
}

/**
* A map of importable class names and their corresponding import handlers.
*
* @var Importable[]
*/
private static $importables = [];

/**
* Initialize the plugin.
*/
public static function init() {
// Set the plugin version.
$plugin_data = get_file_data( dirname( __DIR__ ) . '/plugin.php', [ 'Version' => 'Version' ], 'plugin' );
self::$plugin_data = array_merge( $plugin_data, self::$plugin_data );

// Set the basic settings.
Settings::init( self::$plugin_data );

// Initialize the import handlers for importables.
self::set_initial_import_handlers();

// Register the cron job hooks.
CronJobs::init();

// Initialize plugin controllers after plugins are loaded.
add_action( 'wp_loaded', function() {
LanguageUtil::init();
} );
}

/**
* Sets the initial state of global handlers for all importables.
*/
protected static function set_initial_import_handlers() {
static::$importables = [
PostImportable::class => PostImporter::class,
TermImportable::class => TermImporter::class,
AttachmentImportable::class => AttachmentImporter::class,
];
}

/**
* Add or override a global importable and/or its import handler.
*
* @param string $importable_class The importable class name.
* @param string $importer_class The importer class name.
*
* @throws ReflectionException|TypeException If any of the given classes are missing or they are of the wrong type,
* an error is thrown.
*/
public static function set_importable( string $importable_class, string $importer_class ) {
$importable_reflect = new ReflectionClass( $importable_class );
$importer_reflect = new ReflectionClass( $importer_class );

$importable_interface = Importable::class;
$importer_interface = Importer::class;

$valid_classes =
$importable_reflect->implementsInterface( Importable::class )
&& $importer_reflect->implementsInterface( Importer::class );

if ( $valid_classes ) {
static::$importables[ $importable_class ] = $importer_class;
}
else {
throw new TypeException(
"The passed classes must implement the following interfaces:
importable: $importable_interface, importer: $importer_interface."
);
}
}

/**
* Get the global importer for the given importable class name.
*
* @param string $importable_class The importable class name.
*
* @return ?Importer
*/
public static function get_importer( string $importable_class ) : ?Importer {
$importer = new static::$importables[ $importable_class ]();
return $importer;
}
}
4 changes: 3 additions & 1 deletion src/Util.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@

namespace Geniem\Oopi;

use Geniem\Oopi\Interfaces\ErrorHandler;/**
use Geniem\Oopi\Interfaces\ErrorHandler;

/**
* Class Util
* A collection of utility functions.
*
Expand Down

0 comments on commit 5a562e4

Please sign in to comment.