*/ private $data = null; /** * @var array */ private $changedData = []; /** * @var string[] */ private $removeData = []; private ConfigFileManager $fileManager; /** * @var string[] */ private $internalParamList = []; public function __construct(ConfigFileManager $fileManager) { $this->fileManager = $fileManager; } /** * A path to the config file. * * @todo Move to ConfigData. */ public function getConfigPath(): string { return $this->configPath; } /** * A path to the internal config file. * * @todo Move to ConfigData. */ public function getInternalConfigPath(): string { return $this->internalConfigPath; } /** * Get a parameter value. * * @param mixed $default * @return mixed */ public function get(string $name, $default = null) { $keys = explode('.', $name); $lastBranch = $this->getData(); foreach ($keys as $key) { if (!is_array($lastBranch) && !is_object($lastBranch)) { return $default; } if (!isset($lastBranch[$key])) { return $default; } if (is_array($lastBranch)) { $lastBranch = $lastBranch[$key]; continue; } $lastBranch = $lastBranch->$key; } return $lastBranch; } /** * Whether a parameter is set. */ public function has(string $name): bool { $keys = explode('.', $name); $lastBranch = $this->getData(); foreach ($keys as $key) { if (!is_array($lastBranch) && !is_object($lastBranch)) { return false; } if (!isset($lastBranch[$key])) { return false; } if (is_array($lastBranch)) { $lastBranch = $lastBranch[$key]; continue; } $lastBranch = $lastBranch->$key; } return true; } /** * Re-load data. * * @todo Get rid of this method. Use ConfigData as a dependency. * `$configData->update();` */ public function update(): void { $this->load(); } /** * @deprecated Since v7.0. * * @param string|array|\stdClass $name * @param mixed $value */ public function set($name, $value = null, bool $dontMarkDirty = false): void { if (is_object($name)) { $name = get_object_vars($name); } if (!is_array($name)) { $name = [$name => $value]; } foreach ($name as $key => $value) { if (in_array($key, $this->associativeArrayAttributeList) && is_object($value)) { $value = (array) $value; } $this->data[$key] = $value; if (!$dontMarkDirty) { $this->changedData[$key] = $value; } } } /** * @deprecated Since v7.0. */ public function remove(string $name): bool { if (array_key_exists($name, $this->data)) { unset($this->data[$name]); $this->removeData[] = $name; return true; } return false; } /** * @deprecated Since v7.0. * * @return bool */ public function save() { trigger_error( "Config::save is deprecated. Use `Espo\Core\Utils\Config\ConfigWriter` to save the config.", E_USER_DEPRECATED ); $values = $this->changedData; if (!isset($values[$this->cacheTimestamp])) { $values = array_merge($this->updateCacheTimestamp(true), $values); } $removeData = empty($this->removeData) ? null : $this->removeData; $configPath = $this->getConfigPath(); if (!$this->fileManager->isFile($configPath)) { throw new Error("Config file '{$configPath}' is not found."); } $data = include($configPath); if (!is_array($data)) { $data = include($configPath); } if (is_array($values)) { foreach ($values as $key => $value) { $data[$key] = $value; } } if (is_array($removeData)) { foreach ($removeData as $key) { unset($data[$key]); } } if (!is_array($data)) { throw new Error('Invalid config data while saving.'); } $data['microtime'] = $microtime = microtime(true); $this->fileManager->putPhpContents($configPath, $data); $this->changedData = []; $this->removeData = []; $this->load(); return true; } private function isLoaded(): bool { return isset($this->data) && !empty($this->data); } /** * @return array */ private function getData(): array { if (!$this->isLoaded()) { $this->load(); } return $this->data; } private function load(): void { $systemData = $this->fileManager->getPhpContents($this->systemConfigPath); $data = $this->fileManager->isFile($this->configPath) ? $this->fileManager->getPhpContents($this->configPath) : []; $internalData = $this->fileManager->isFile($this->internalConfigPath) ? $this->fileManager->getPhpContents($this->internalConfigPath) : []; $this->data = Util::merge($systemData, $data); $this->data = Util::merge($this->data, $internalData); $this->internalParamList = array_keys($internalData); $this->fileManager->setConfig($this); } /** * Get all parameters excluding those that are set in the internal config. */ public function getAllNonInternalData(): stdClass { $data = (object) $this->getData(); foreach ($this->internalParamList as $param) { unset($data->$param); } return $data; } /** * Whether a parameter is set in the internal config. */ public function isInternal(string $name): bool { if (!$this->isLoaded()) { $this->load(); } return in_array($name, $this->internalParamList); } /** * @deprecated * @param array $data * @return void */ public function setData($data) { if (is_object($data)) { $data = get_object_vars($data); } $this->set($data); } /** * Update cache timestamp. * * @deprecated * @return ?array */ public function updateCacheTimestamp(bool $returnOnlyValue = false) { $timestamp = [ $this->cacheTimestamp => time() ]; if ($returnOnlyValue) { return $timestamp; } $this->set($timestamp); return null; } /** * @todo Move to another class `Espo\Core\Utils\Config\ApplicationConfigProvider`. * @deprecated */ public function getSiteUrl(): string { return rtrim($this->get('siteUrl'), '/'); } }