DataSL.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. <?php
  2. namespace ModulesGarden\ProxmoxAddon\Core\SL\Data;
  3. use ModulesGarden\ProxmoxAddon\Core\SL\Configuration;
  4. use ModulesGarden\ProxmoxAddon\Core\SL\Register;
  5. use ModulesGarden\ProxmoxAddon\Core\SL\Rewrite;
  6. use ModulesGarden\ProxmoxAddon\Core\SL\InterfaceConfig;
  7. /**
  8. * @author Rafał Ossowski <rafal.os@modulesgarden.com>
  9. */
  10. class DataSL
  11. {
  12. /**
  13. * Exemple:
  14. *
  15. * array(
  16. * 0 => array(
  17. * "name" => string(className),
  18. * "method" => string(static method),
  19. * "args" => array(
  20. * string,
  21. * number,
  22. * object(set string ClassName)
  23. * )
  24. * )
  25. * )
  26. *
  27. * @var array()
  28. */
  29. private $configurations = [];
  30. /**
  31. * Exemple:
  32. *
  33. * array(
  34. * string(aliens/className) => array(
  35. * "namespace" => string,
  36. * "alias" => string,
  37. * "singleton" => bool,
  38. * "auto" => bool
  39. * )
  40. * )
  41. *
  42. * @var array()
  43. */
  44. private $register = [];
  45. /**
  46. * Exemple:
  47. *
  48. * array(
  49. * string(old className) => string(new className)
  50. * )
  51. *
  52. * @var array()
  53. */
  54. private $rewrites = [];
  55. /**
  56. * Exemple:
  57. *
  58. * array(
  59. * string(className) => string(aliens)
  60. * )
  61. *
  62. * @var array()
  63. */
  64. private $aliens = [];
  65. private $interfaceConfig = [];
  66. /**
  67. * Exemple:
  68. *
  69. * array(
  70. * 0 => string(aliens/className)
  71. * )
  72. *
  73. * @var array()
  74. */
  75. private $auto = [];
  76. public function __construct()
  77. {
  78. $this->getConfigurations();
  79. $this->getRewrites();
  80. //$this->getInterfaceConfig();
  81. $this->getRegisters();
  82. }
  83. /**
  84. * @return array()
  85. */
  86. public function getConfigurations()
  87. {
  88. if (empty($this->configurations) === true)
  89. {
  90. $this->configurations = Configuration::get();
  91. }
  92. return $this->configurations;
  93. }
  94. /**
  95. * @return array()
  96. */
  97. public function getInterfaceConfig()
  98. {
  99. if (empty($this->interfaceConfig) === true)
  100. {
  101. $this->interfaceConfig = InterfaceConfig::get();
  102. }
  103. return $this->interfaceConfig;
  104. }
  105. /**
  106. * @return array()
  107. */
  108. public function getRegisters()
  109. {
  110. if (empty($this->register) === true)
  111. {
  112. $this->register = Register::get();
  113. $this->loadRegistry();
  114. }
  115. return $this->register;
  116. }
  117. /**
  118. * @return array()
  119. */
  120. public function getRewrites()
  121. {
  122. if (empty($this->rewrites) === true)
  123. {
  124. $this->rewrites = Rewrite::get();
  125. }
  126. return $this->rewrites;
  127. }
  128. public function getAllAlias()
  129. {
  130. return $this->aliens;
  131. }
  132. /**
  133. * @param string $name
  134. * @param string|null $old
  135. * @return string
  136. */
  137. public function getRewrite($name, $old = null)
  138. {
  139. return array_key_exists($name, $this->getRewrites()) ? (array_get($this->rewrites, $name) == $old) ? $name : $this->getRewrite(array_get($this->getRewrites(), $name), $name) : $name;
  140. }
  141. /**
  142. * @param string $name
  143. * @return boolean
  144. */
  145. public function isRewrite($name)
  146. {
  147. return array_key_exists($name, $this->getRewrites());
  148. }
  149. /**
  150. * @param string $name
  151. * @return string
  152. */
  153. public function getAlias($name)
  154. {
  155. return array_key_exists($name, $this->aliens) ? array_get($this->aliens, $name) : $name;
  156. }
  157. /**
  158. * @param string $name
  159. * @return boolean
  160. */
  161. public function getSingleton($name)
  162. {
  163. return array_key_exists($name, $this->getRegisters()) ? array_get($this->getRegisters(), $name)['singleton'] : true;
  164. }
  165. /**
  166. * @param string $name
  167. * @return boolean
  168. */
  169. public function isRegistry($name)
  170. {
  171. return array_key_exists($name, $this->getRegisters());
  172. }
  173. /**
  174. * @param string $name
  175. * @return string
  176. */
  177. public function getClassName($name)
  178. {
  179. return array_key_exists($name, $this->getRegisters()) ? $this->getRegisters()[$name]['class'] : $name;
  180. }
  181. /**
  182. * @return array()
  183. */
  184. public function getAutoRunRegisters()
  185. {
  186. return $this->auto;
  187. }
  188. private function loadRegistry()
  189. {
  190. $registres = $this->register;
  191. $this->register = [];
  192. foreach ($registres as $registry)
  193. {
  194. $key = (string) (preg_replace('/\s+/', '', $registry['alias']) == "") ? $registry['namespace'] : $registry['alias'];
  195. $rewrite = $this->getRewrite((string) $registry['namespace']);
  196. if ($key == $registry['alias'])
  197. {
  198. $this->aliens[(string) $registry['namespace']] = $key;
  199. $this->register[(string) $registry['namespace']] = [
  200. 'class' => (string) $rewrite,
  201. 'singleton' => (bool) (int) $registry['singleton'],
  202. 'auto' => (bool) (int) $registry['auto']
  203. ];
  204. }
  205. if ($rewrite != $registry['namespace'])
  206. {
  207. if ($key == $registry['alias'])
  208. {
  209. $this->aliens[(string) $rewrite] = $key;
  210. $this->register[(string) $rewrite] = [
  211. 'class' => (string) $rewrite,
  212. 'singleton' => (bool) (int) $registry['singleton'],
  213. 'auto' => (bool) (int) $registry['auto']
  214. ];
  215. }
  216. $this->register[(string) $rewrite] = [
  217. 'class' => (string) $rewrite,
  218. 'singleton' => (bool) (int) $registry['singleton'],
  219. 'auto' => (bool) (int) $registry['auto']
  220. ];
  221. }
  222. $this->register[(string) $key] = [
  223. 'class' => (string) $rewrite,
  224. 'singleton' => (bool) (int) $registry['singleton'],
  225. 'auto' => (bool) (int) $registry['auto']
  226. ];
  227. if ($this->register[$key]['auto'] === false)
  228. {
  229. $this->auto[] = $this->register[$key]['class'];
  230. }
  231. }
  232. }
  233. }