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