Skip to content

armgit / Vvveb

Public Imported from GitHub
0

Powerful and easy to use cms to build websites, blogs or ecommerce stores.

4.0 KB
Raw Blame
<?php

/**
 * Vvveb
 *
 * Copyright (C) 2022  Ziadin Givan
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as
 * published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
 *
 */

namespace Vvveb\System;

class Config {
	/** @var array<string, mixed> */
	private array $config = [];

	/** @var ?self */
	private static ?self $instance = null;

	/** @return self */
	public static function getInstance(): self {
		if (self::$instance) {
			return self::$instance;
		} else {
			return self::$instance = new self();
		}
	}

	/**
	 * @param string $namespace
	 *
	 * @return void
	 */
	private function loadConfig(string $namespace): void {
		//avoid checking file for non existing namespace
		if (array_key_exists($namespace, $this->config) &&
			$this->config[$namespace] === null) {
			return;
		}

		$file = DIR_ROOT . "config/$namespace.php";

		if (file_exists($file)) {
			try {
				$this->config[$namespace] = include $file;
			} catch (\Exception $e) {
			}
		} else {
			$this->config[$namespace] = null;
		}
	}

	/**
	 * @param string $namespace
	 *
	 * @return bool
	 */
	private function saveConfig(string $namespace): bool {
		$data       = $this->config[$namespace];
		$configFile = DIR_ROOT . "config/$namespace.php";

		if ($bytes = file_put_contents($configFile, "<?php\n return " . var_export($data, true) . ';', LOCK_EX)) {
			clearstatcache(true, $configFile);

			if (function_exists('opcache_invalidate')) {
				opcache_invalidate($configFile);
			}

			return true;
		}

		return false;
	}

	/**
	 * @param string $path
	 * @param mixed  $default
	 *
	 * @return mixed
	 */
	public function get(string $path, mixed $default = null): mixed {
		$p         = strtok($path, '.');
		$namespace = $p;

		//load config for namespace
		if (!isset($this->config[$namespace])) {
			$this->loadConfig($namespace);

			if (!isset($this->config[$namespace])) {
				return null;
			}
		}

		$a = $this->config;

		while ($p !== false) {
			if (!isset($a[$p])) {
				return $default;
			}

			$a = $a[$p];
			$p = strtok('.');
		}

		return $a;
	}

	/**
	 * @param string $path
	 * @param mixed  $value
	 *
	 * @return bool
	 */
	public function set(string $path, mixed $value): bool {
		$p         = strtok($path, '.');
		$namespace = $p;

		if (!$namespace) {
			return false;
		}

		$a = &$this->config;
		//make sure the namespace is loaded to avoid overriding existing data
		if (!isset($this->config[$namespace])) {
			$this->loadConfig($namespace);
		}

		while ($p !== false) {
			if (!isset($a[$p])) {
				$a[$p] = [];
			}

			$prev = &$a;
			$last = $p;

			$a = &$a[$p];
			$p = strtok('.');
		}

		if (empty($a)) {
			$a = $value;
		} else {
			//if value already set go back one level
			if (!is_array($a)) {
				$prev[$last] = $value;
			} else {
				if (is_array($value)) {
					$a = array_merge($a, $value);
				} else {
					$a[$p] = $value;
				}
			}
		}

		return $this->saveConfig($namespace);
	}

	/**
	 * @param string $path
	 *
	 * @return bool
	 */
	public function unset(string $path): bool {
		$p         = strtok($path, '.');
		$namespace = $p;

		if (!$namespace) {
			return false;
		}

		$a = &$this->config;
		//make sure the namespace is loaded to avoid overriding existing data
		if (!isset($this->config[$namespace])) {
			$this->loadConfig($namespace);
		}

		while ($p !== false) {
			if (!isset($a[$p])) {
				$a[$p] = [];
			}

			$prev = &$a;
			$last = $p;

			$a = &$a[$p];
			$p = strtok('.');
		}

		unset($prev[$last]);

		return $this->saveConfig($namespace);
	}
}