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