AzuraCast/src/Config.php

61 lines
1.3 KiB
PHP
Raw Normal View History

<?php
2021-07-19 05:53:45 +00:00
declare(strict_types=1);
namespace App;
use const EXTR_OVERWRITE;
class Config
{
2020-10-25 09:53:52 +00:00
protected string $baseFolder;
public function __construct(Environment $environment)
{
$this->baseFolder = $environment->getConfigDirectory();
}
/**
* @param string $name
* @param array $inject_vars Variables to pass into the scope of the configuration.
*
2021-07-19 05:53:45 +00:00
* @return array<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);
2021-06-08 06:40:49 +00:00
if (is_file($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));
}
}