1
|
|
|
<?php |
2
|
|
|
namespace PSFS\base; |
3
|
|
|
|
4
|
|
|
use PSFS\base\config\Config; |
5
|
|
|
use PSFS\base\exception\ConfigException; |
6
|
|
|
use PSFS\base\types\helpers\InjectorHelper; |
7
|
|
|
use PSFS\base\types\traits\SingletonTrait; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Class Singleton |
11
|
|
|
* @package PSFS\base |
12
|
|
|
*/ |
13
|
|
|
class Singleton |
14
|
|
|
{ |
15
|
|
|
use SingletonTrait; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Singleton constructor. |
19
|
|
|
* @throws \Exception |
20
|
|
|
* @throws exception\GeneratorException |
21
|
|
|
* @throws ConfigException |
22
|
|
|
*/ |
23
|
3 |
|
public function __construct() |
24
|
|
|
{ |
25
|
3 |
|
Logger::log(static::class . ' constructor invoked'); |
26
|
3 |
|
$this->init(); |
27
|
3 |
|
} |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @param string $variable |
31
|
|
|
* @param mixed $value |
32
|
|
|
*/ |
33
|
8 |
|
public function __set($variable, $value) |
34
|
|
|
{ |
35
|
8 |
|
if ($this->__isset($variable)) { |
36
|
8 |
|
$this->$variable = $value; |
37
|
|
|
} |
38
|
8 |
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @param string $name |
42
|
|
|
* @return bool |
43
|
|
|
*/ |
44
|
8 |
|
public function __isset($name) |
45
|
|
|
{ |
46
|
8 |
|
return property_exists(get_class($this), $name); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @param string $variable |
51
|
|
|
* @return mixed |
52
|
|
|
*/ |
53
|
1 |
|
public function __get($variable) |
54
|
|
|
{ |
55
|
1 |
|
return $this->__isset($variable) ? $this->$variable : null; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* HELPERS |
60
|
|
|
*/ |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* @return string |
64
|
|
|
* @throws \ReflectionException |
65
|
|
|
*/ |
66
|
1 |
|
public function getShortName() |
67
|
|
|
{ |
68
|
1 |
|
$reflector = new \ReflectionClass(get_class($this)); |
69
|
1 |
|
return $reflector->getShortName(); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* @param string $variable |
74
|
|
|
* @param bool $singleton |
75
|
|
|
* @param string $classNameSpace |
76
|
|
|
* @return $this |
77
|
|
|
* @throws \Exception |
78
|
|
|
*/ |
79
|
3 |
|
public function load($variable, $singleton = true, $classNameSpace = null) |
80
|
|
|
{ |
81
|
3 |
|
$calledClass = static::class; |
82
|
|
|
try { |
83
|
3 |
|
$instance = InjectorHelper::constructInyectableInstance($variable, $singleton, $classNameSpace, $calledClass); |
84
|
3 |
|
$setter = 'set' . ucfirst($variable); |
85
|
3 |
|
if (method_exists($calledClass, $setter)) { |
86
|
2 |
|
$this->$setter($instance); |
87
|
|
|
} else { |
88
|
3 |
|
$this->$variable = $instance; |
89
|
|
|
} |
90
|
|
|
} catch (\Exception $e) { |
91
|
|
|
Logger::log($e->getMessage() . ': ' . $e->getFile() . ' [' . $e->getLine() . ']', LOG_ERR); |
92
|
|
|
throw $e; |
93
|
|
|
} |
94
|
3 |
|
return $this; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* @throws \Exception |
99
|
|
|
* @throws exception\GeneratorException |
100
|
|
|
* @throws ConfigException |
101
|
|
|
*/ |
102
|
3 |
|
public function init() |
103
|
|
|
{ |
104
|
3 |
|
if (!$this->isLoaded()) { |
105
|
3 |
|
$filename = sha1(get_class($this)); |
106
|
3 |
|
$cacheFilename = 'reflections' . DIRECTORY_SEPARATOR . substr($filename, 0, 2) . DIRECTORY_SEPARATOR . substr($filename, 2, 2) . DIRECTORY_SEPARATOR . $filename . ".json"; |
107
|
|
|
/** @var \PSFS\base\Cache $cacheService */ |
108
|
3 |
|
$cacheService = Cache::getInstance(); |
109
|
|
|
/** @var \PSFS\base\config\Config $configService */ |
110
|
3 |
|
$configService = Config::getInstance(); |
111
|
3 |
|
$cache = Cache::canUseMemcache() ? Cache::MEMCACHE : Cache::JSON; |
112
|
3 |
|
$properties = $cacheService->getDataFromFile($cacheFilename, $cache); |
113
|
3 |
|
if (true === $configService->getDebugMode() || !$properties) { |
114
|
3 |
|
$properties = InjectorHelper::getClassProperties(get_class($this)); |
115
|
3 |
|
$cacheService->storeData($cacheFilename, $properties, $cache); |
116
|
|
|
} |
117
|
|
|
/** @var \ReflectionProperty $property */ |
118
|
3 |
|
if (!empty($properties) && is_array($properties)) { |
119
|
3 |
|
foreach ($properties as $property => $class) { |
120
|
3 |
|
$this->load($property, true, $class); |
121
|
|
|
} |
122
|
|
|
} |
123
|
3 |
|
$this->setLoaded(); |
124
|
|
|
} else { |
125
|
1 |
|
Logger::log(get_class($this) . ' already loaded', LOG_INFO); |
126
|
|
|
} |
127
|
3 |
|
} |
128
|
|
|
} |
129
|
|
|
|