Skip to content

Commit

Permalink
Updated use of singleton to ensure classes get used
Browse files Browse the repository at this point in the history
  • Loading branch information
DanielHudson2 committed Jun 7, 2024
1 parent 8e09f89 commit 93afef5
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 20 deletions.
3 changes: 2 additions & 1 deletion includes/classes/MailCatcher/MailCatcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,10 @@
* Disables all emails from being sent and instead
* routes to a local mailcatcher instance if available.
*/
class MailCatcher extends Singleton {
class MailCatcher {

use Environment;
use Singleton;

/**
* SMTP hostname or IP
Expand Down
3 changes: 2 additions & 1 deletion includes/classes/RemoteFiles/RemoteFiles.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,10 @@
* This class is built upon BE Media from Production so all due credit to those authors.
* http://www.github.com/billerickson/be-media-from-production
*/
class RemoteFiles extends Singleton {
class RemoteFiles {

use Environment;
use Singleton;

/**
* Production URL
Expand Down
48 changes: 30 additions & 18 deletions includes/classes/Singleton.php
Original file line number Diff line number Diff line change
@@ -1,34 +1,46 @@
<?php
/**
* Singleton definition for this plugin's classes
* Singleton trait
*
* @package Satellite
*/

namespace Eighteen73\Satellite;

/**
* Abstract class
*/
abstract class Singleton {
trait Singleton {

/**
* Return instance of class
* The class instance
*
* @return self
* @var self|null
*/
public static function instance(): Singleton {
static $instance;

if ( empty( $instance ) ) {
$class = get_called_class();
private static $instance = null;

$instance = new $class();

if ( method_exists( $instance, 'setup' ) ) {
$instance->setup();
}
/**
* Get the current instance
*
* @return Singleton
*/
final public static function instance(): self {
if ( self::$instance === null ) {
self::$instance = new self();
}
return self::$instance;
}

/**
* Constructor
*/
private function __construct() {
// Intentionally empty
}

return $instance;
/**
* Class clone
*
* @return void
*/
private function __clone() {
// Intentionally empty
}
}

0 comments on commit 93afef5

Please sign in to comment.