This repository has been archived on 2024-06-26. You can view files and clone it, but cannot push or open issues or pull requests.
sakura/app/Config.php

72 lines
1.8 KiB
PHP

<?php
/**
* Holds the configuration manager.
* @package Sakura
*/
namespace Sakura;
use Sakura\Exceptions\ConfigNonExistentException;
use Sakura\Exceptions\ConfigParseException;
use Sakura\Exceptions\ConfigValueNotFoundException;
/**
* Handles the configuration settings of Sakura.
* @package Sakura
* @author Julian van de Groep <me@flash.moe>
*/
class Config
{
/**
* Storage for the parsed config file.
* @var array
*/
private static $config = [];
/**
* Loads and parses the configuration file.
* @throws ConfigNonExistentException
* @throws ConfigParseException
* @param string $path
*/
public static function load(string $path): void
{
// Check if the configuration file exists
if (!file_exists($path)) {
throw new ConfigNonExistentException;
}
// Attempt to load the configuration file
$config = parse_ini_file($path, true);
if (is_array($config)) {
self::$config = $config;
} else {
throw new ConfigParseException;
}
}
/**
* Get a value from the configuration.
* @param string $section
* @param string $key
* @throws ConfigValueNotFoundException
* @return array|string
*/
public static function get(string $section, string $key = null)
{
// Check if the key that we're looking for exists
if (array_key_exists($section, self::$config)) {
if ($key) {
// If we also have a subkey return the proper data
return self::$config[$section][$key];
}
// else we just return the default value
return self::$config[$section];
}
throw new ConfigValueNotFoundException;
}
}