1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34: 35: 36: 37: 38: 39: 40: 41: 42: 43: 44: 45: 46: 47: 48: 49: 50: 51: 52: 53: 54: 55: 56: 57: 58: 59: 60: 61: 62: 63: 64: 65: 66: 67: 68: 69: 70: 71: 72: 73: 74: 75: 76: 77: 78: 79: 80: 81: 82: 83: 84: 85: 86: 87: 88: 89:
<?php
/*
* Copyright 2017 Jack Sleight <http://jacksleight.com/>
* This source file is subject to the MIT license that is bundled with this package in the file LICENCE.
*/
namespace Coast;
/**
* PHP file based config object.
*/
class Config
{
/**
* Config opts.
* @var array
*/
protected $_opts = [];
/**
* Construct a new config object.
* @param array $files List of PHP files to parse.
*/
public function __construct($files = array())
{
$this->load($files);
}
/**
* Load files.
* @param string $name
* @return mixed
*/
public function load($files)
{
if (!is_array($files)) {
$files = [$files];
}
foreach ($files as $file) {
$this->fromArray(require (string) $file);
}
}
/**
* Import from an array.
* @param string $name
* @return mixed
*/
public function fromArray(array $opts)
{
$this->_opts = \Coast\array_merge_smart(
$this->_opts,
$opts
);
return $this;
}
public function opt($name, $value = null)
{
if (func_num_args() > 1) {
$this->_opts[$name] = $value;
return $this;
}
return isset($this->_opts[$name])
? $this->_opts[$name]
: null;
}
public function opts(array $opts = null)
{
if (func_num_args() > 0) {
foreach ($opts as $name => $value) {
$this->opt($name, $value);
}
return $this;
}
return $this->_opts;
}
public function __get($name)
{
return $this->opt($name);
}
public function __isset($name)
{
return $this->opt($name) !== null;
}
}