Config.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. <?php
  2. namespace ThurData\Servers\KerioEmail\Core\App\Controllers\Instances\Addon;
  3. use ThurData\Servers\KerioEmail\Core\App\Controllers\Interfaces\AddonController;
  4. use ThurData\Servers\KerioEmail\Core\Configuration\Data;
  5. use ThurData\Servers\KerioEmail\Core\DependencyInjection;
  6. use ThurData\Servers\KerioEmail\Core\ServiceLocator;
  7. /**
  8. * Module configuration wrapper
  9. */
  10. class Config extends \ThurData\Servers\KerioEmail\Core\App\Controllers\Instances\AddonController implements AddonController
  11. {
  12. /**
  13. * @var array
  14. * list of params passed by WHMCS
  15. */
  16. private $params = [];
  17. /**
  18. * @var array
  19. * module configuration list
  20. */
  21. protected $config = [];
  22. /**
  23. * @var null|\ThurData\Servers\KerioEmail\Core\Configuration\Data
  24. *
  25. */
  26. protected $data = null;
  27. /**
  28. * @var array
  29. * list of values to be returned as a part of
  30. */
  31. protected $configFields = [
  32. 'name',
  33. 'description',
  34. 'version',
  35. 'author',
  36. 'fields',
  37. 'systemName',
  38. 'debug',
  39. 'moduleIcon',
  40. 'clientareaName'
  41. ];
  42. /**
  43. * @param array $params - WHMCS params for function _config
  44. * @return array
  45. */
  46. public function execute($params = [])
  47. {
  48. if (!$this->config)
  49. {
  50. $this->setParams($params);
  51. $this->loadConfig();
  52. return $this->getConfig();
  53. }
  54. return $this->config;
  55. }
  56. /**
  57. * @param array $params
  58. */
  59. protected function setParams($params = [])
  60. {
  61. if (is_array($params))
  62. {
  63. $this->params = $params;
  64. }
  65. }
  66. /**
  67. * loads module configuration from files yaml configs and moduleVersion.php
  68. */
  69. protected function loadConfig()
  70. {
  71. if (!$this->data)
  72. {
  73. $this->data = DependencyInjection::call(Data::class);
  74. }
  75. }
  76. /**
  77. * parses config data
  78. */
  79. public function getConfig()
  80. {
  81. if ($this->config)
  82. {
  83. return $this->config;
  84. }
  85. try
  86. {
  87. //Before loading the config
  88. $params = [];
  89. $return = ServiceLocator::call(\ThurData\Servers\KerioEmail\Core\Configuration\Addon\Config\Before::class)->execute($params);
  90. foreach ($this->configFields as $field)
  91. {
  92. $value = $this->data->{$field};
  93. if (isset($return[$field]) === false && $value !== null)
  94. {
  95. if (is_numeric($value))
  96. {
  97. $return[$field] = (int)$value;
  98. }
  99. else
  100. {
  101. $return[$field] = $value;
  102. }
  103. }
  104. }
  105. //After loading the config
  106. $return = ServiceLocator::call(\ThurData\Servers\KerioEmail\Core\Configuration\Addon\Config\After::class)->execute($return);
  107. $this->config = $return;
  108. return $return;
  109. }
  110. catch (\Exception $ex)
  111. {
  112. ServiceLocator::call(\ThurData\Servers\KerioEmail\Core\HandlerError\ErrorManager::class)->addError(self::class, $ex->getMessage(), $return);
  113. return $return ? : [];
  114. }
  115. }
  116. public function getConfigValue($key, $defaultValue = null)
  117. {
  118. if (!$this->config)
  119. {
  120. $this->execute();
  121. }
  122. if (!isset($this->config[$key]))
  123. {
  124. return $defaultValue;
  125. }
  126. return $this->config[$key];
  127. }
  128. }