<?php
// namespace TestSuite;

/**
 * 配置信息
 */
class Config
{
  protected $config;

  /**
   * 读取并加载配置文件
   *
   * @throws \Exception
   */
  public function __construct()
  {
    $configFile = dirname(__DIR__) . '/config.json';
    if (!file_exists($configFile)) {
      throw new \Exception('缺少config.json文件！');
    }

    $config = json_decode(file_get_contents($configFile), true);
    if (count($config) == 0) {
      throw new \Exception('config.json文件格式不正确！');
    }

    foreach($config as $key=>$val) {
      $this->set($key, $val);
    }
  }

  /**
   * 获取配置信息
   *
   * @param  string $name Name of configuration attribute/value
   * @return string|integer|float|boolean|array
   */
  public function get($name)
  {
    return $this->config[$name] ?: null;
  }

  /**
   * 设置配置信息
   *
   * @param string $name  Name of value
   * @param string|boolean|integer|float|array $value Value to set
   */
  public function set($name, $value)
  {
    $this->config[$name] = $value;
  }
}
