|
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(get_class($this) . ' 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
|
|
|
*/ |
|
65
|
1 |
|
public function getShortName() |
|
66
|
|
|
{ |
|
67
|
1 |
|
$reflector = new \ReflectionClass(get_class($this)); |
|
68
|
1 |
|
return $reflector->getShortName(); |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* @param string $variable |
|
73
|
|
|
* @param bool $singleton |
|
74
|
|
|
* @param string $classNameSpace |
|
|
|
|
|
|
75
|
|
|
* @return $this |
|
76
|
|
|
* @throws \Exception |
|
77
|
|
|
*/ |
|
78
|
3 |
|
public function load($variable, $singleton = true, $classNameSpace = null) |
|
79
|
|
|
{ |
|
80
|
3 |
|
$calledClass = static::class; |
|
81
|
|
|
try { |
|
82
|
3 |
|
$instance = InjectorHelper::constructInyectableInstance($variable, $singleton, $classNameSpace, $calledClass); |
|
|
|
|
|
|
83
|
3 |
|
$setter = 'set' . ucfirst($variable); |
|
84
|
3 |
|
if (method_exists($calledClass, $setter)) { |
|
85
|
2 |
|
$this->$setter($instance); |
|
86
|
|
|
} else { |
|
87
|
3 |
|
$this->$variable = $instance; |
|
88
|
|
|
} |
|
89
|
|
|
} catch (\Exception $e) { |
|
90
|
|
|
Logger::log($e->getMessage() . ': ' . $e->getFile() . ' [' . $e->getLine() . ']', LOG_ERR); |
|
91
|
|
|
throw $e; |
|
92
|
|
|
} |
|
93
|
3 |
|
return $this; |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
/** |
|
97
|
|
|
* @throws \Exception |
|
98
|
|
|
* @throws exception\GeneratorException |
|
99
|
|
|
* @throws ConfigException |
|
100
|
|
|
*/ |
|
101
|
3 |
|
public function init() |
|
102
|
|
|
{ |
|
103
|
3 |
|
if (!$this->isLoaded()) { |
|
104
|
3 |
|
$filename = sha1(get_class($this)); |
|
105
|
3 |
|
$cacheFilename = 'reflections' . DIRECTORY_SEPARATOR . substr($filename, 0, 2) . DIRECTORY_SEPARATOR . substr($filename, 2, 2) . DIRECTORY_SEPARATOR . $filename . ".json"; |
|
|
|
|
|
|
106
|
|
|
/** @var \PSFS\base\Cache $cacheService */ |
|
107
|
3 |
|
$cacheService = Cache::getInstance(); |
|
108
|
|
|
/** @var \PSFS\base\config\Config $configService */ |
|
109
|
3 |
|
$configService = Config::getInstance(); |
|
110
|
3 |
|
$cache = Cache::canUseMemcache() ? Cache::MEMCACHE : Cache::JSON; |
|
111
|
3 |
|
$properties = $cacheService->getDataFromFile($cacheFilename, $cache); |
|
112
|
3 |
|
if (true === $configService->getDebugMode() || !$properties) { |
|
113
|
3 |
|
$properties = InjectorHelper::getClassProperties(get_class($this)); |
|
114
|
3 |
|
$cacheService->storeData($cacheFilename, $properties, $cache); |
|
115
|
|
|
} |
|
116
|
|
|
/** @var \ReflectionProperty $property */ |
|
117
|
3 |
|
if (!empty($properties) && is_array($properties)) { |
|
118
|
3 |
|
foreach ($properties as $property => $class) { |
|
119
|
3 |
|
$this->load($property, true, $class); |
|
120
|
|
|
} |
|
121
|
|
|
} |
|
122
|
3 |
|
$this->setLoaded(); |
|
123
|
|
|
} else { |
|
124
|
1 |
|
Logger::log(get_class($this) . ' already loaded', LOG_INFO); |
|
125
|
|
|
} |
|
126
|
3 |
|
} |
|
127
|
|
|
} |
|
128
|
|
|
|
This check looks for
@paramannotations where the type inferred by our type inference engine differs from the declared type.It makes a suggestion as to what type it considers more descriptive.
Most often this is a case of a parameter that can be null in addition to its declared types.