DataSL.php 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. <?php
  2. namespace ThurData\Servers\KerioEmail\Core\SL\Data;
  3. use ThurData\Servers\KerioEmail\Core\SL\Configuration;
  4. use ThurData\Servers\KerioEmail\Core\SL\Register;
  5. use ThurData\Servers\KerioEmail\Core\SL\Rewrite;
  6. use ThurData\Servers\KerioEmail\Core\SL\InterfaceConfig;
  7. /**
  8. * @autor ThurData <info@thrudata.ch>
  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())
  140. ? (array_get($this->rewrites, $name) == $old)
  141. ? $name
  142. : $this->getRewrite(array_get($this->getRewrites(), $name), $name)
  143. : $name;
  144. }
  145. /**
  146. * @param string $name
  147. * @return boolean
  148. */
  149. public function isRewrite($name)
  150. {
  151. return array_key_exists($name, $this->getRewrites());
  152. }
  153. /**
  154. * @param string $name
  155. * @return string
  156. */
  157. public function getAlias($name)
  158. {
  159. return array_key_exists($name, $this->aliens)?array_get($this->aliens, $name):$name;
  160. }
  161. /**
  162. * @param string $name
  163. * @return boolean
  164. */
  165. public function getSingleton($name)
  166. {
  167. return array_key_exists($name, $this->getRegisters())?array_get($this->getRegisters(), $name)['singleton']:true;
  168. }
  169. /**
  170. * @param string $name
  171. * @return boolean
  172. */
  173. public function isRegistry($name)
  174. {
  175. return array_key_exists($name, $this->getRegisters());
  176. }
  177. /**
  178. * @param string $name
  179. * @return string
  180. */
  181. public function getClassName($name)
  182. {
  183. return array_key_exists($name, $this->getRegisters())?$this->getRegisters()[$name]['class']:$name;
  184. }
  185. /**
  186. * @return array()
  187. */
  188. public function getAutoRunRegisters()
  189. {
  190. return $this->auto;
  191. }
  192. private function loadRegistry()
  193. {
  194. $registres = $this->register;
  195. $this->register = [];
  196. foreach ($registres as $registry)
  197. {
  198. $key = (string)(preg_replace('/\s+/', '', $registry['alias']) == "")
  199. ? $registry['namespace']
  200. : $registry['alias'];
  201. $rewrite = $this->getRewrite((string)$registry['namespace']);
  202. if ($key == $registry['alias'])
  203. {
  204. $this->aliens[(string)$registry['namespace']] = $key;
  205. $this->register[(string)$registry['namespace']] = [
  206. 'class' => (string)$rewrite,
  207. 'singleton' => (bool)(int)$registry['singleton'],
  208. 'auto' => (bool)(int)$registry['auto']
  209. ];
  210. }
  211. if ($rewrite != $registry['namespace'])
  212. {
  213. if ($key == $registry['alias'])
  214. {
  215. $this->aliens[(string)$rewrite] = $key;
  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)$rewrite] = [
  223. 'class' => (string)$rewrite,
  224. 'singleton' => (bool)(int)$registry['singleton'],
  225. 'auto' => (bool)(int)$registry['auto']
  226. ];
  227. }
  228. $this->register[(string)$key] = [
  229. 'class' => (string)$rewrite,
  230. 'singleton' => (bool)(int)$registry['singleton'],
  231. 'auto' => (bool)(int)$registry['auto']
  232. ];
  233. if ($this->register[$key]['auto'] === false)
  234. {
  235. $this->auto[] = $this->register[$key]['class'];
  236. }
  237. }
  238. }
  239. }