Moved to a more rigid class architecture

This commit is contained in:
Paul Kirby 2018-09-11 15:10:15 -05:00
parent 59c3118c79
commit 05a4709b07
13 changed files with 170 additions and 164 deletions

View file

@ -7,16 +7,11 @@
class LBRY_Admin class LBRY_Admin
{ {
private static $instance = null; /**
* [__construct description]
public static function get_instance() */
public function __construct()
{ {
// Create the object
if (self::$instance === null) {
self::$instance = new self;
}
return self::$instance;
} }
/** /**
@ -49,6 +44,6 @@ class LBRY_Admin
*/ */
public function options_page_html() public function options_page_html()
{ {
require_once(LBRY_URI . '/templates/options_page.php'); require_once(LBRY_ABSPATH . 'templates/options_page.php');
} }
} }

View file

@ -7,17 +7,13 @@
class LBRY_Daemon class LBRY_Daemon
{ {
private static $instance = null;
private $address = 'localhost:5279'; private $address = 'localhost:5279';
public static function get_instance() /**
* LBRY Daemon Object constructor
*/
public function __construct()
{ {
// Create the object
if (self::$instance === null) {
self::$instance = new self;
}
return self::$instance;
} }
/** /**
@ -43,8 +39,6 @@ class LBRY_Daemon
'include_unconfirmed' => false 'include_unconfirmed' => false
)); ));
error_log(print_r($result, true));
return json_decode($result)->result; return json_decode($result)->result;
} }

View file

@ -7,15 +7,10 @@
class LBRY_Daemon_Logger class LBRY_Daemon_Logger
{ {
private static $instance = null; /**
* [__construct description]
public static function get_instance() */
public function __construct()
{ {
// Create the object
if (self::$instance === null) {
self::$instance = new self;
}
return self::$instance;
} }
} }

View file

@ -7,15 +7,10 @@
class LBRY_Network class LBRY_Network
{ {
private static $instance = null; /**
* [__construct description]
public static function get_instance() */
public function __construct()
{ {
// Create the object
if (self::$instance === null) {
self::$instance = new self;
}
return self::$instance;
} }
} }

View file

@ -7,15 +7,10 @@
class LBRY_Network_Parser class LBRY_Network_Parser
{ {
private static $instance = null; /**
* [__construct description]
public static function get_instance() */
public function __construct()
{ {
// Create the object
if (self::$instance === null) {
self::$instance = new self;
}
return self::$instance;
} }
} }

View file

@ -7,15 +7,10 @@
class LBRY_Network_Publisher class LBRY_Network_Publisher
{ {
private static $instance = null; /**
* [__construct description]
public static function get_instance() */
public function __construct()
{ {
// Create the object
if (self::$instance === null) {
self::$instance = new self;
}
return self::$instance;
} }
} }

View file

@ -7,15 +7,10 @@
class LBRY_Notifier class LBRY_Notifier
{ {
private static $instance = null; /**
* [__construct description]
public static function get_instance() */
public function __construct()
{ {
// Create the object
if (self::$instance === null) {
self::$instance = new self;
}
return self::$instance;
} }
} }

View file

@ -9,16 +9,11 @@
class LBRY_Speech class LBRY_Speech
{ {
private static $instance = null; /**
* [__construct description]
public static function get_instance() */
public function __construct()
{ {
// Create the object
if (self::$instance === null) {
self::$instance = new self;
}
return self::$instance;
} }
public function get_address() public function get_address()

View file

@ -7,15 +7,10 @@
class LBRY_Speech_Parser class LBRY_Speech_Parser
{ {
private static $instance = null; /**
* [__construct description]
public static function get_instance() */
public function __construct()
{ {
// Create the object
if (self::$instance === null) {
self::$instance = new self;
}
return self::$instance;
} }
} }

View file

@ -8,15 +8,10 @@
class LBRY_Post_Handler class LBRY_Post_Handler
{ {
private static $instance = null; /**
* [__construct description]
public static function get_instance() */
public function __construct()
{ {
// Create the object
if (self::$instance === null) {
self::$instance = new self;
}
return self::$instance;
} }
} }

View file

@ -7,44 +7,130 @@
class LBRYPress class LBRYPress
{ {
// TODO: Remove Singleton pattern, replace with dependency injection for all classes
// private static $instance = null;
//
// public static function get_instance()
// {
// // Create the object
// if (self::$instance === null) {
// self::$instance = new self;
// }
//
// return self::$instance;
// }
protected $daemon;
protected $admin;
public function __construct(LBRY_Daemon $daemon = null, LBRY_Admin $admin = null) /**
* Version of LBRYPress
*/
public $version = '0.0.1';
/**
* The single instance of this class
*/
protected static $_instance = null;
/**
* The LBRY Daemon Interface
*/
public $daemon = null;
/**
* The LBRY Admin object
*/
public $admin = null;
/**
* The LBRY Spee.ch object
*/
public $speech = null;
/**
* Main LBRYPress Instance.
*
* Ensures only one instance of LBRYPress is loaded or can be loaded.
*/
public static function instance()
{ {
$this->daemon = $daemon ?? new LBRY_Daemon(); if (is_null(self::$_instance)) {
$this->admin = $admin ?? new LBRY_Admin(); self::$_instance = new self();
error_log('new LBRYPress constructed'); }
return self::$_instance;
}
/**
* LBRYPress Constructor
*/
public function __construct()
{
$this->define_constants();
spl_autoload_register(array($this, 'lbry_autoload_register'));
$this->init();
$this->init_hooks();
}
/**
* Define a constant if its not already set.
*
* @param string $name Constant name.
* @param string|bool $value Constant value.
*/
private function define($name, $value)
{
if (! defined($name)) {
define($name, $value);
}
}
/**
* Define all LBRYPress constants
*/
private function define_constants()
{
$upload_dir = wp_upload_dir(null, false);
$this->define('LBRY_ABSPATH', dirname(LBRY_PLUGIN_FILE) . '/');
$this->define('LBRY_NAME', plugin_basename(LBRY_PLUGIN_FILE));
$this->define('LBRY_VERSION', $this->version);
// Library Options Names
$this->define('LBRY_WALLET', 'lbry_wallet'); // the wallet address
$this->define('LBRY_SPEECH', 'lbry_speech'); // the spee.ch address
$this->define('LBRY_LICENSE', 'lbry_license'); // the license to publish with to the LBRY network
$this->define('LBRY_LBC_PUBLISH', 'lbry_lbc_publish'); // amount of lbc to use per publish
}
/**
* Autoloader Registration
*/
private function lbry_autoload_register($class)
{
$file_name = LBRY_ABSPATH . 'classes/' . $class . '.php';
if (file_exists($file_name)) {
require $file_name;
return;
}
}
/**
* Initialize this class itself
*/
private function init()
{
$this->daemon = new LBRY_Daemon();
$this->admin = new LBRY_Admin();
$this->speech = new LBRY_Speech();
} }
/** /**
* Set up all hooks and actions necessary for the plugin to run * Set up all hooks and actions necessary for the plugin to run
*/ */
public function init() private function init_hooks()
{ {
// Initialize the admin interface register_activation_hook(LBRY_PLUGIN_FILE, array($this, 'activate'));
register_deactivation_hook(LBRY_PLUGIN_FILE, array($this, 'deactivate'));
// Admin request
if (is_admin()) {
$this->admin->settings_init(); $this->admin->settings_init();
} }
}
/** /**
* Run during plugin activation * Run during plugin activation
*/ */
public function activate() public function activate()
{ {
$LBRY_Daemon = LBRY_Daemon::get_instance();
// Add options to the options table we need // Add options to the options table we need
if (! get_option(LBRY_WALLET)) { if (! get_option(LBRY_WALLET)) {
$wallet_address = $this->daemon->wallet_unused_address(); $wallet_address = $this->daemon->wallet_unused_address();

View file

@ -23,19 +23,11 @@ The above copyright notice and this permission notice shall be included in all c
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/ */
defined('ABSPATH') || die('No Peeking!'); defined('ABSPATH') || die(); // Exit if accessed directly
// Global Constants
define('LBRY_NAME', 'LBRYPress');
define('LBRY_URI', dirname(__FILE__));
define('LBRY_REQUIRED_PHP_VERSION', '5.3'); // TODO: Figure out what versions we actually need define('LBRY_REQUIRED_PHP_VERSION', '5.3'); // TODO: Figure out what versions we actually need
define('LBRY_REQUIRED_WP_VERSION', '3.1'); define('LBRY_REQUIRED_WP_VERSION', '3.1');
define('LBRY_PLUGIN_FILE', __FILE__);
// Library Options Names
define('LBRY_WALLET', 'lbry_wallet'); // the wallet address
define('LBRY_SPEECH', 'lbry_speech'); // the spee.ch address
define('LBRY_LICENSE', 'lbry_license'); // the license to publish with to the LBRY network
define('LBRY_LBC_PUBLISH', 'lbry_lbc_publish'); // amount of lbc to use per publish
/** /**
* Checks if the system requirements are met * Checks if the system requirements are met
@ -65,45 +57,25 @@ function lbry_requirements_error()
{ {
global $wp_version; global $wp_version;
require_once(LBRY_URI . '/templates/requirements-error.php'); require_once(dirname(__FILE__) . '/templates/requirements-error.php');
} }
/**
* Autoloader Registration
*/
function lbry_autoload_register($class)
{
$file_name = LBRY_URI . '/classes/' . $class . '.php';
if (file_exists($file_name)) { /**
require $file_name; * Returns the singal instance of LBRYPress
*/
function LBRY()
{
if (lbry_requirements_met()) {
if (! class_exists('LBRYPress')) {
require_once(dirname(__FILE__) . '/classes/LBRYPress.php');
}
return LBRYPress::instance();
} else {
add_action('admin_notices', 'lbry_requirements_error');
return; return;
} }
} }
spl_autoload_register('lbry_autoload_register'); // Kickoff
LBRY();
/**
* Check requirements and load main class
* The main program needs to be in a separate file that only gets loaded if the plugin requirements are met. Otherwise older PHP installations could crash when trying to parse it.
*/
if (lbry_requirements_met()) {
add_action('init', function () {
$LBRYPress = new LBRYPress();
$LBRYPress->init();
});
// register_activation_hook(__FILE__, function () {
// LBRYPress::get_instance()->activate();
// });
//
// register_deactivation_hook(__FILE__, function () {
// LBRYPress::get_instance()->deactivate();
// });
} else {
add_action('admin_notices', 'lbry_requirements_error');
}
/**
* Admin Set up
*/

View file

@ -1,8 +1,7 @@
<?php <?php
$LBRY_Daemon = LBRY_Daemon::get_instance(); $LBRY = LBRY();
$LBRY_Speech = LBRY_Speech::get_instance(); $wallet_balance = $LBRY->daemon->wallet_balance();
$wallet_balance = $LBRY_Daemon->wallet_balance(); $speech_address = $LBRY->speech->get_address() || '';
$speech_address = $LBRY_Speech->get_address() || '';
?> ?>
<div class="wrap"> <div class="wrap">
<h1><?= esc_html(get_admin_page_title()); ?></h1> <h1><?= esc_html(get_admin_page_title()); ?></h1>