AzuraCast/src/Config.php

59 lines
1.3 KiB
PHP
Raw Normal View History

<?php
namespace App;
use const EXTR_OVERWRITE;
class Config
{
2020-10-25 09:53:52 +00:00
protected string $baseFolder;
public function __construct(Environment $settings)
{
2020-10-25 09:53:52 +00:00
$this->baseFolder = $settings->getConfigDirectory();
}
/**
* @param string $name
* @param array $inject_vars Variables to pass into the scope of the configuration.
*
* @return mixed[]
2020-07-08 07:03:50 +00:00
* @noinspection PhpIncludeInspection
* @noinspection UselessUnsetInspection
*/
2020-10-25 09:53:52 +00:00
public function get(string $name, array $inject_vars = []): array
{
$path = $this->getPath($name);
if (file_exists($path)) {
unset($name);
extract($inject_vars, EXTR_OVERWRITE);
unset($inject_vars);
return require $path;
}
return [];
}
/**
* Return the configuration path resolved by the specified name.
*
* @param string $name
*/
2020-10-25 09:53:52 +00:00
public function getPath(string $name): string
{
2020-10-25 09:53:52 +00:00
return $this->baseFolder . DIRECTORY_SEPARATOR . str_replace(['.', '..'], ['', ''], $name) . '.php';
}
/**
* Indicate whether a given configuration file name exists.
*
* @param string $name
*/
2020-10-25 09:53:52 +00:00
public function has(string $name): bool
{
return file_exists($this->getPath($name));
}
}