Refactor singleton functionality into trait

This commit is contained in:
Sam Mousa 2018-11-12 16:58:56 +01:00
parent c70868d3fa
commit 9808ff5923
No known key found for this signature in database
GPG Key ID: A18520F9B4301C9D
6 changed files with 22 additions and 41 deletions

View File

@ -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();

View File

@ -1,6 +1,7 @@
<?php
abstract class FileManager {
use SingletonTrait;
private $fileName;
private $localPath = 'files/';

View File

@ -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());
}

View File

@ -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(

View File

@ -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;
}
}

View File

@ -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);