50 lines
986 B
PHP
50 lines
986 B
PHP
<?php
|
|
|
|
namespace Civi\Mailinglistsync;
|
|
|
|
use Civi\Mailinglistsync\Exceptions\ContactSyncException;
|
|
use Civi\Mailinglistsync\Exceptions\MailinglistException;
|
|
use CRM_Mailinglistsync_ExtensionUtil as E;
|
|
|
|
trait Singleton {
|
|
|
|
/**
|
|
* Singleton instance.
|
|
*/
|
|
private static $instance;
|
|
|
|
/**
|
|
* Protect singleton from being instantiated.
|
|
*/
|
|
protected function __construct() {}
|
|
|
|
/**
|
|
* Protect singleton from being cloned.
|
|
*/
|
|
protected function __clone() {}
|
|
|
|
/**
|
|
* Protect unserialize method to prevent cloning of the instance.
|
|
* @throws \Exception
|
|
*/
|
|
public function __wakeup()
|
|
{
|
|
throw new ContactSyncException(
|
|
"Cannot unserialize a singleton.",
|
|
MailinglistException::ERROR_CODE_UNSERIALIZE_SINGLETON
|
|
);
|
|
}
|
|
|
|
/**
|
|
* Returns the singleton instance.
|
|
*
|
|
* @return self
|
|
*/
|
|
public static function getInstance(): self
|
|
{
|
|
if (!self::$instance) {
|
|
self::$instance = new self();
|
|
}
|
|
return self::$instance;
|
|
}
|
|
}
|