| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149 |
- <?php
- namespace ModulesGarden\ProxmoxAddon\Core\App\Controllers\Instances\Addon;
- use ModulesGarden\ProxmoxAddon\Core\App\Controllers\Interfaces\AddonController;
- use ModulesGarden\ProxmoxAddon\Core\Configuration\Data;
- use ModulesGarden\ProxmoxAddon\Core\DependencyInjection;
- use ModulesGarden\ProxmoxAddon\Core\ServiceLocator;
- /**
- * Module configuration wrapper
- */
- class Config extends \ModulesGarden\ProxmoxAddon\Core\App\Controllers\Instances\AddonController implements AddonController
- {
- /**
- * @var array
- * list of params passed by WHMCS
- */
- private $params = [];
- /**
- * @var array
- * module configuration list
- */
- protected $config = [];
- /**
- * @var null|\ModulesGarden\ProxmoxAddon\Core\Configuration\Data
- *
- */
- protected $data = null;
- /**
- * @var array
- * list of values to be returned as a part of
- */
- protected $configFields = [
- 'name',
- 'description',
- 'version',
- 'author',
- 'fields',
- 'systemName',
- 'debug',
- 'moduleIcon',
- 'clientareaName'
- ];
- /**
- * @param array $params - WHMCS params for function _config
- * @return array
- */
- public function execute($params = [])
- {
- if (!$this->config)
- {
- $this->setParams($params);
- $this->loadConfig();
- return $this->getConfig();
- }
- return $this->config;
- }
- /**
- * @param array $params
- */
- protected function setParams($params = [])
- {
- if (is_array($params))
- {
- $this->params = $params;
- }
- }
- /**
- * loads module configuration from files yaml configs and moduleVersion.php
- */
- protected function loadConfig()
- {
- if (!$this->data)
- {
- $this->data = DependencyInjection::call(Data::class);
- }
- }
- /**
- * parses config data
- */
- public function getConfig()
- {
- if ($this->config)
- {
- return $this->config;
- }
- try
- {
- //Before loading the config
- $params = [];
- $return = ServiceLocator::call(\ModulesGarden\ProxmoxAddon\Core\Configuration\Addon\Config\Before::class)->execute($params);
- foreach ($this->configFields as $field)
- {
- $value = $this->data->{$field};
- if (isset($return[$field]) === false && $value !== null)
- {
- if (is_numeric($value))
- {
- $return[$field] = (int) $value;
- }
- else
- {
- $return[$field] = $value;
- }
- }
- }
- //After loading the config
- $return = ServiceLocator::call(\ModulesGarden\ProxmoxAddon\Core\Configuration\Addon\Config\After::class)->execute($return);
- $this->config = $return;
- return $return;
- }
- catch (\Exception $ex)
- {
- ServiceLocator::call(\ModulesGarden\ProxmoxAddon\Core\HandlerError\ErrorManager::class)->addError(self::class, $ex->getMessage(), $return);
- return $return ?: [];
- }
- }
- public function getConfigValue($key, $defaultValue = null)
- {
- if (!$this->config)
- {
- $this->execute();
- }
- if (!isset($this->config[$key]))
- {
- return $defaultValue;
- }
- return $this->config[$key];
- }
- }
|