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

70 lines
1.6 KiB
PHP
Raw Normal View History

2016-07-26 17:29:53 +00:00
<?php
/**
* Holds the configuration manager.
* @package Sakura
*/
namespace Sakura;
2016-09-11 14:25:22 +00:00
use Sakura\Exceptions\ConfigNonExistentException;
use Sakura\Exceptions\ConfigParseException;
2016-07-26 17:29:53 +00:00
/**
* Handles the configuration settings of Sakura.
* @package Sakura
* @author Julian van de Groep <me@flash.moe>
*/
class Config
{
/**
2016-08-05 02:35:37 +00:00
* Storage for the parsed config file.
2016-07-26 17:29:53 +00:00
* @var array
*/
private static $config = [];
/**
2016-10-07 17:55:50 +00:00
* Loads and parses the configuration file.
2016-09-11 14:25:22 +00:00
* @throws ConfigNonExistentException
* @throws ConfigParseException
2016-08-05 02:35:37 +00:00
* @param string $path
2016-07-26 17:29:53 +00:00
*/
2016-12-04 16:33:52 +00:00
public static function load(string $path): void
2016-07-26 17:29:53 +00:00
{
// Check if the configuration file exists
if (!file_exists($path)) {
2016-09-11 14:25:22 +00:00
throw new ConfigNonExistentException;
2016-07-26 17:29:53 +00:00
}
// Attempt to load the configuration file
$config = parse_ini_file($path, true);
if (is_array($config)) {
self::$config = $config;
} else {
2016-09-11 14:25:22 +00:00
throw new ConfigParseException;
2016-07-26 17:29:53 +00:00
}
}
/**
* Get a value from the configuration.
2016-08-05 02:35:37 +00:00
* @param string $section
* @param string $key
* @return array|string
2016-07-26 17:29:53 +00:00
*/
2016-12-04 16:33:52 +00:00
public static function get(string $section, string $key = null)
2016-07-26 17:29:53 +00:00
{
// 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];
}
2017-04-08 16:08:03 +00:00
return null;
2016-07-26 17:29:53 +00:00
}
}