Merge pull request #383 from SamMousa/singleton-trait

Refactor singleton functionality into trait
This commit is contained in:
Ivan Diaz 2018-11-28 00:15:00 -03:00 committed by GitHub
commit e0d4a46929
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 29 additions and 40 deletions

View File

@ -1,18 +1,7 @@
<?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() {}
use SingletonTrait;
public function download() {
$fullFilePath = $this->getFullFilePath();

View File

@ -1,21 +1,13 @@
<?php
class FileUploader extends FileManager {
use SingletonTrait;
private $maxSize = 1;
private $fileName;
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,23 @@
<?php
/**
* Trait SingletonTrait
* This trait helps implementing singleton classes
* It must be used on each class in a hierarchy that needs to be a singleton
*/
trait SingletonTrait
{
protected 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);