Skip to content

Commit

Permalink
Merge pull request #1 from javorszky/no-more-singleton
Browse files Browse the repository at this point in the history
No more singleton
  • Loading branch information
pmgarman authored Oct 27, 2017
2 parents bd95b01 + f759982 commit a1e2202
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 21 deletions.
9 changes: 8 additions & 1 deletion includes/class-wp-plugin-base-cli.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
<?php
namespace Mindsize\Plugin_Base;
use WP_CLI_Command;
use WP_CLI;

if ( ! defined( 'ABSPATH' ) ) {
exit;
}

if( ! class_exists( 'WP_CLI_Command' ) ) {
return;
Expand Down Expand Up @@ -27,4 +34,4 @@ public function ping() {
WP_CLI::line( 'pong' );
}

}
}
24 changes: 8 additions & 16 deletions includes/class-wp-plugin-base.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
<?php
namespace Mindsize\Plugin_Base;
use WP_CLI;

if ( ! defined( 'ABSPATH' ) ) {
exit;
}

/**
* WP Plugin Base main class file.
Expand All @@ -11,27 +17,13 @@
* @license http://www.gnu.org/licenses/gpl-2.0.html GNU General Public License v2.0
*/
class WP_Plugin_Base {

protected static $single_instance = null;

/**
* Get a single instance of the plugin.
*/
public static function get_instance() {
if( null === self::$single_instance ) {
self::$single_instance = new self();
}

return self::$single_instance;
}

/**
* Constructor.
*/
public function __construct() {
// Register the CLI command if we're running WP_CLI
if( defined( 'WP_CLI' ) && WP_CLI ) {
WP_CLI::add_command( 'wp-plugin-base', 'WP_Plugin_Base_CLI' );
WP_CLI::add_command( 'wp-plugin-base', __NAMESPACE__ . '\\WP_Plugin_Base_CLI' );
}
}
}
}
26 changes: 26 additions & 0 deletions includes/class-wp-plugin-factory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php
namespace Mindsize\Plugin_Base;

if ( ! defined( 'ABSPATH' ) ) {
exit;
}

/**
* Plugin factory, so we get one shared instance of the class that's not a singleton.
*
* @since 1.0.0
* @author Mindsize <[email protected]>
* @copyright Copyright (c) 2017 Mindsize <[email protected]>
* @license http://www.gnu.org/licenses/gpl-2.0.html GNU General Public License v2.0
*/
class WP_Plugin_Factory {
public static function create() {
static $plugin = null;

if ( null === $plugin ) {
$plugin = new WP_Plugin_Base();
}

return $plugin;
}
}
15 changes: 11 additions & 4 deletions wp-plugin-base.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
<?php
namespace Mindsize\Plugin_Base;

if ( ! defined( 'ABSPATH' ) ) {
exit;
}

/**
* Plugin Name: WordPress Plugin Base
* Description: This is a sample plugin that was created with mindsize/wp-plugin-base
Expand All @@ -19,10 +25,11 @@
require( WP_PLUGIN_BASE_DIR . 'vendor/autoload_52.php' );
}

if( class_exists( 'WP_Plugin_Base' ) ) {
if( class_exists( __NAMESPACE__ .'\\WP_Plugin_Factory' ) ) {

function wp_plugin_base() {
return WP_Plugin_Base::get_instance();
return WP_Plugin_Factory::create();
}

add_action( 'plugins_loaded', 'wp_plugin_base' );
}
add_action( 'plugins_loaded', __NAMESPACE__ . '\\wp_plugin_base' );
}

0 comments on commit a1e2202

Please sign in to comment.