Data.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. <?php
  2. namespace ThurData\Servers\KerioEmail\Core\Configuration;
  3. use ThurData\Servers\KerioEmail\Core\FileReader\Reader;
  4. use ThurData\Servers\KerioEmail\Core\ModuleConstants;
  5. /**
  6. * Description of Data
  7. *
  8. * @autor ThurData <info@thrudata.ch>
  9. */
  10. class Data
  11. {
  12. protected static $data = [];
  13. public function __construct()
  14. {
  15. $this->load();
  16. }
  17. public function __get($name)
  18. {
  19. $this->load();
  20. if (array_key_exists(lcfirst($name), self::$data))
  21. {
  22. return self::$data[lcfirst($name)];
  23. }
  24. return null;
  25. }
  26. public function getAll()
  27. {
  28. return self::$data;
  29. }
  30. private function load()
  31. {
  32. if (count(self::$data) == 0)
  33. {
  34. $this->loadConfig();
  35. }
  36. }
  37. /**
  38. * Loads YML configuration files
  39. */
  40. private function loadConfig()
  41. {
  42. $dataDev = $this->read(ModuleConstants::getDevConfigDir() . DS . 'configuration.yml');
  43. $dataCore = $this->read(ModuleConstants::getCoreConfigDir() . DS . 'configuration.yml');
  44. $data = $this->parseConfigData($dataDev, $dataCore);
  45. $this->loadPackageConfig($data);
  46. self::$data = $data ? : [];
  47. }
  48. /**
  49. * @param self::$data $data
  50. *
  51. * overwrites module version and wiki url basing on moduleVersion.php file,
  52. * this file is added automatically during package creation
  53. */
  54. private function loadPackageConfig(&$data)
  55. {
  56. if (file_exists(ModuleConstants::getModuleRootDir() . DS . 'moduleVersion.php'))
  57. {
  58. include ModuleConstants::getModuleRootDir() . DS . 'moduleVersion.php';
  59. if ($moduleVersion)
  60. {
  61. $data['version'] = $moduleVersion;
  62. }
  63. }
  64. if ($data['description'] && strpos($data['description'], ':WIKI_URL:'))
  65. {
  66. $data['description'] = str_replace(':WIKI_URL:', ($moduleWikiUrl ? : 'https://www.docs.thurdata.com/'), $data['description']);
  67. }
  68. $data['debug'] = (file_exists(ModuleConstants::getModuleRootDir() . DIRECTORY_SEPARATOR . '.debug'))
  69. ? true : false;
  70. }
  71. private function parseConfigData($dataDev, $dataCore)
  72. {
  73. if (!$dataDev && $dataCore)
  74. {
  75. return $dataCore;
  76. }
  77. if (!$dataDev && $dataCore)
  78. {
  79. return $dataCore;
  80. }
  81. foreach ($dataCore as $coreKey => $core)
  82. {
  83. $isFind = false;
  84. foreach ($dataDev as $devKey => $dev)
  85. {
  86. if ($devKey === $coreKey)
  87. {
  88. $isFind = true;
  89. break;
  90. }
  91. }
  92. if (!$isFind)
  93. {
  94. $dataDev[$coreKey] = $core;
  95. }
  96. }
  97. return $dataDev;
  98. }
  99. private function read($name)
  100. {
  101. return Reader::read($name)->get();
  102. }
  103. }