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
|
2 |
|
public function __construct() |
17
|
|
|
{ |
18
|
2 |
|
Logger::log(get_class($this) . ' constructor invoked'); |
19
|
2 |
|
$this->init(); |
20
|
2 |
|
} |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Magic setter |
24
|
|
|
* @param $variable |
25
|
|
|
* @param $value |
26
|
|
|
*/ |
27
|
7 |
|
public function __set($variable, $value) |
28
|
|
|
{ |
29
|
7 |
|
if (property_exists(get_class($this), $variable)) { |
30
|
7 |
|
$this->$variable = $value; |
31
|
|
|
} |
32
|
7 |
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Magic getter |
36
|
|
|
* @param string $variable |
37
|
|
|
* @return $mixed |
38
|
|
|
*/ |
39
|
1 |
|
public function __get($variable) |
|
|
|
|
40
|
|
|
{ |
41
|
1 |
|
return property_exists(get_class($this), $variable) ? $this->$variable : null; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* HELPERS |
46
|
|
|
*/ |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Método que extrae el nombre de la clase |
50
|
|
|
* @return string |
51
|
|
|
*/ |
52
|
1 |
|
public function getShortName() |
53
|
|
|
{ |
54
|
1 |
|
$reflector = new \ReflectionClass(get_class($this)); |
55
|
1 |
|
return $reflector->getShortName(); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Dependency injector service invoker |
60
|
|
|
* @param string $variable |
61
|
|
|
* @param bool $singleton |
62
|
|
|
* @param string $classNameSpace |
|
|
|
|
63
|
|
|
* @return $this |
64
|
|
|
*/ |
65
|
2 |
|
public function load($variable, $singleton = true, $classNameSpace = null) |
66
|
|
|
{ |
67
|
2 |
|
$calledClass = get_called_class(); |
68
|
|
|
try { |
69
|
2 |
|
$instance = InjectorHelper::constructInyectableInstance($variable, $singleton, $classNameSpace, $calledClass); |
|
|
|
|
70
|
2 |
|
$setter = "set" . ucfirst($variable); |
71
|
2 |
|
if (method_exists($calledClass, $setter)) { |
72
|
1 |
|
$this->$setter($instance); |
73
|
|
|
} else { |
74
|
2 |
|
$this->$variable = $instance; |
75
|
|
|
} |
76
|
|
|
} catch (\Exception $e) { |
77
|
|
|
Logger::log($e->getMessage() . ': ' . $e->getFile() . ' [' . $e->getLine() . ']', LOG_ERR); |
78
|
|
|
} |
79
|
2 |
|
return $this; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* Método que inyecta automáticamente las dependencias en la clase |
84
|
|
|
*/ |
85
|
2 |
|
public function init() |
86
|
|
|
{ |
87
|
2 |
|
if (!$this->isLoaded()) { |
88
|
2 |
|
$filename = sha1(get_class($this)); |
89
|
2 |
|
$cacheFilename = "reflections" . DIRECTORY_SEPARATOR . substr($filename, 0, 2) . DIRECTORY_SEPARATOR . substr($filename, 2, 2) . DIRECTORY_SEPARATOR . $filename . ".json"; |
|
|
|
|
90
|
|
|
/** @var \PSFS\base\Cache $cacheService */ |
91
|
2 |
|
$cacheService = Cache::getInstance(); |
92
|
|
|
/** @var \PSFS\base\config\Config $configService */ |
93
|
2 |
|
$configService = Config::getInstance(); |
94
|
2 |
|
$cache = Cache::canUseMemcache() ? Cache::MEMCACHE : Cache::JSON; |
95
|
2 |
|
$properties = $cacheService->getDataFromFile($cacheFilename, $cache); |
96
|
2 |
|
if (true === $configService->getDebugMode() || !$properties) { |
97
|
2 |
|
$properties = InjectorHelper::getClassProperties(get_class($this)); |
98
|
2 |
|
$cacheService->storeData($cacheFilename, $properties, $cache); |
99
|
|
|
} |
100
|
|
|
/** @var \ReflectionProperty $property */ |
101
|
2 |
|
if (!empty($properties) && is_array($properties)) foreach ($properties as $property => $class) { |
102
|
2 |
|
$this->load($property, true, $class); |
103
|
|
|
} |
104
|
2 |
|
$this->setLoaded(); |
105
|
|
|
} else { |
106
|
1 |
|
Logger::log(get_class($this) . ' already loaded', LOG_INFO); |
107
|
|
|
} |
108
|
2 |
|
} |
109
|
|
|
} |
110
|
|
|
|
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.