Refactor singleton functionality into trait
This commit is contained in:
parent
c70868d3fa
commit
9808ff5923
|
@ -1,19 +1,6 @@
|
|||
<?php
|
||||
|
||||
class FileDownloader extends FileManager {
|
||||
|
||||
private static $instance = null;
|
||||
|
||||
public static function getInstance() {
|
||||
if (self::$instance === null) {
|
||||
self::$instance = new FileDownloader();
|
||||
}
|
||||
|
||||
return self::$instance;
|
||||
}
|
||||
|
||||
private function __construct() {}
|
||||
|
||||
public function download() {
|
||||
$fullFilePath = $this->getFullFilePath();
|
||||
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
<?php
|
||||
|
||||
abstract class FileManager {
|
||||
use SingletonTrait;
|
||||
private $fileName;
|
||||
private $localPath = 'files/';
|
||||
|
||||
|
|
|
@ -8,16 +8,6 @@ class FileUploader extends FileManager {
|
|||
private $permission;
|
||||
private $storage;
|
||||
|
||||
private static $instance = null;
|
||||
|
||||
public static function getInstance() {
|
||||
if (self::$instance === null) {
|
||||
self::$instance = new FileUploader();
|
||||
}
|
||||
|
||||
return self::$instance;
|
||||
}
|
||||
|
||||
private function __construct() {
|
||||
$this->storage = new \Upload\Storage\FileSystem($this->getLocalPath());
|
||||
}
|
||||
|
|
|
@ -1,17 +1,9 @@
|
|||
<?php
|
||||
class MailSender {
|
||||
use SingletonTrait;
|
||||
|
||||
private $mailOptions = [];
|
||||
private $mailerInstance;
|
||||
private static $instance = NULL;
|
||||
|
||||
public static function getInstance() {
|
||||
if(MailSender::$instance === NULL) {
|
||||
MailSender::$instance = new MailSender();
|
||||
}
|
||||
|
||||
return MailSender::$instance;
|
||||
}
|
||||
|
||||
private function __construct() {
|
||||
$this->setConnectionSettings(
|
||||
|
|
|
@ -0,0 +1,18 @@
|
|||
<?php
|
||||
|
||||
trait SingletonTrait
|
||||
{
|
||||
private static $instance;
|
||||
|
||||
private function __construct()
|
||||
{
|
||||
}
|
||||
|
||||
public static function getInstance()
|
||||
{
|
||||
if (!isset(static::$instance)) {
|
||||
static::$instance = new static();
|
||||
}
|
||||
return static::$instance;
|
||||
}
|
||||
}
|
|
@ -1,7 +1,8 @@
|
|||
<?php
|
||||
|
||||
class Session {
|
||||
static $instance = null;
|
||||
use SingletonTrait;
|
||||
|
||||
private $sessionPrefix = '';
|
||||
|
||||
private function __construct() {
|
||||
|
@ -17,14 +18,6 @@ class Session {
|
|||
session_destroy();
|
||||
}
|
||||
|
||||
public static function getInstance() {
|
||||
if (!self::$instance) {
|
||||
self::$instance = new Session();
|
||||
}
|
||||
|
||||
return self::$instance;
|
||||
}
|
||||
|
||||
public function createSession($userId, $staff = false) {
|
||||
$this->store('userId', $userId);
|
||||
$this->store('staff', $staff);
|
||||
|
|
Loading…
Reference in New Issue