1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace PSFS\base; |
4
|
|
|
|
5
|
|
|
use PSFS\base\config\Config; |
6
|
|
|
use PSFS\base\types\helpers\InjectorHelper; |
7
|
|
|
use PSFS\base\types\SingletonTrait; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Class Singleton |
11
|
|
|
* @package PSFS\base |
12
|
|
|
*/ |
13
|
|
|
class Singleton |
14
|
|
|
{ |
15
|
|
|
use SingletonTrait; |
16
|
|
|
/** |
17
|
|
|
* @var bool Flag that indicated if the class is already loaded |
18
|
|
|
*/ |
19
|
|
|
protected $loaded = false; |
20
|
3 |
|
|
21
|
|
|
public function __construct() |
22
|
3 |
|
{ |
23
|
3 |
|
Logger::log(get_class($this) . ' constructor invoked'); |
24
|
3 |
|
$this->init(); |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Magic setter |
29
|
|
|
* @param $variable |
30
|
|
|
* @param $value |
31
|
8 |
|
*/ |
32
|
|
|
public function __set($variable, $value) |
33
|
8 |
|
{ |
34
|
8 |
|
if (property_exists(get_class($this), $variable)) { |
35
|
8 |
|
$this->$variable = $value; |
36
|
8 |
|
} |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Magic getter |
41
|
|
|
* @param string $variable |
42
|
|
|
* @return $mixed |
43
|
1 |
|
*/ |
44
|
|
|
public function __get($variable) |
|
|
|
|
45
|
1 |
|
{ |
46
|
|
|
return property_exists(get_class($this), $variable) ? $this->$variable : null; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Método que devuelve si una clase está isntanciada correctamente |
51
|
|
|
* @return bool |
52
|
3 |
|
*/ |
53
|
|
|
public function isLoaded() |
54
|
3 |
|
{ |
55
|
|
|
return $this->loaded; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Método que configura como cargada una clase |
60
|
|
|
* @param bool $loaded |
61
|
3 |
|
*/ |
62
|
|
|
public function setLoaded($loaded = true) |
63
|
3 |
|
{ |
64
|
3 |
|
$this->loaded = $loaded; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* HELPERS |
69
|
|
|
*/ |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* Método que extrae el nombre de la clase |
73
|
|
|
* @return string |
74
|
1 |
|
*/ |
75
|
|
|
public function getShortName() |
76
|
1 |
|
{ |
77
|
1 |
|
$reflector = new \ReflectionClass(get_class($this)); |
78
|
|
|
return $reflector->getShortName(); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* Dependency injector service invoker |
83
|
|
|
* @param string $variable |
84
|
|
|
* @param bool $singleton |
85
|
|
|
* @param string $classNameSpace |
|
|
|
|
86
|
|
|
* @return $this |
87
|
3 |
|
*/ |
88
|
|
|
public function load($variable, $singleton = true, $classNameSpace = null) |
89
|
3 |
|
{ |
90
|
|
|
$calledClass = get_called_class(); |
91
|
3 |
|
try { |
92
|
3 |
|
$instance = InjectorHelper::constructInyectableInstance($variable, $singleton, $classNameSpace, $calledClass); |
|
|
|
|
93
|
3 |
|
$setter = "set" . ucfirst($variable); |
94
|
2 |
|
if (method_exists($calledClass, $setter)) { |
95
|
2 |
|
$this->$setter($instance); |
96
|
3 |
|
} else { |
97
|
|
|
$this->$variable = $instance; |
98
|
3 |
|
} |
99
|
1 |
|
} catch (\Exception $e) { |
100
|
|
|
Logger::log($e->getMessage() . ': ' . $e->getFile() . ' [' . $e->getLine() . ']', LOG_ERR); |
101
|
3 |
|
} |
102
|
|
|
return $this; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* Método que inyecta automáticamente las dependencias en la clase |
107
|
3 |
|
*/ |
108
|
|
|
public function init() |
109
|
3 |
|
{ |
110
|
3 |
|
if (!$this->isLoaded()) { |
111
|
|
|
$cacheFilename = "reflections" . DIRECTORY_SEPARATOR . sha1(get_class($this)) . ".json"; |
112
|
3 |
|
/** @var \PSFS\base\Cache $cacheService */ |
113
|
|
|
$cacheService = Cache::getInstance(); |
114
|
3 |
|
/** @var \PSFS\base\config\Config $configService */ |
115
|
3 |
|
$configService = Config::getInstance(); |
116
|
3 |
|
$properties = $cacheService->getDataFromFile($cacheFilename, Cache::JSON); |
117
|
3 |
|
if (true === $configService->getDebugMode() || null === $properties) { |
118
|
3 |
|
$properties = InjectorHelper::getClassProperties(get_class($this)); |
119
|
3 |
|
$cacheService->storeData($cacheFilename, $properties, Cache::JSON); |
120
|
|
|
} |
121
|
3 |
|
/** @var \ReflectionProperty $property */ |
122
|
3 |
|
if (!empty($properties) && is_array($properties)) foreach ($properties as $property => $class) { |
123
|
3 |
|
$this->load($property, true, $class); |
124
|
3 |
|
} |
125
|
3 |
|
$this->setLoaded(); |
126
|
3 |
|
} else { |
127
|
|
|
Logger::log(get_class($this) . ' already loaded', LOG_INFO); |
128
|
3 |
|
} |
129
|
|
|
} |
130
|
|
|
} |
131
|
|
|
|
Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a
@return
annotation as described here.