2020-02-06 02:35:13 +00:00
|
|
|
<?php
|
2020-10-14 22:19:31 +00:00
|
|
|
|
2021-07-19 05:53:45 +00:00
|
|
|
declare(strict_types=1);
|
|
|
|
|
2020-02-06 02:35:13 +00:00
|
|
|
namespace App;
|
|
|
|
|
|
|
|
use const EXTR_OVERWRITE;
|
|
|
|
|
|
|
|
class Config
|
|
|
|
{
|
2020-10-25 09:53:52 +00:00
|
|
|
protected string $baseFolder;
|
2020-02-06 02:35:13 +00:00
|
|
|
|
2020-12-04 08:41:55 +00:00
|
|
|
public function __construct(Environment $environment)
|
2020-02-06 02:35:13 +00:00
|
|
|
{
|
2020-12-04 08:41:55 +00:00
|
|
|
$this->baseFolder = $environment->getConfigDirectory();
|
2020-02-06 02:35:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @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-02-06 02:35:13 +00:00
|
|
|
*/
|
2020-10-25 09:53:52 +00:00
|
|
|
public function get(string $name, array $inject_vars = []): array
|
2020-02-06 02:35:13 +00:00
|
|
|
{
|
2020-10-14 22:19:31 +00:00
|
|
|
$path = $this->getPath($name);
|
2020-02-06 02:35:13 +00:00
|
|
|
|
2021-06-08 06:40:49 +00:00
|
|
|
if (is_file($path)) {
|
2020-02-06 02:35:13 +00:00
|
|
|
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-02-06 02:35:13 +00:00
|
|
|
{
|
2020-10-25 09:53:52 +00:00
|
|
|
return $this->baseFolder . DIRECTORY_SEPARATOR . str_replace(['.', '..'], ['', ''], $name) . '.php';
|
2020-02-06 02:35:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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
|
2020-02-06 02:35:13 +00:00
|
|
|
{
|
2020-10-14 22:19:31 +00:00
|
|
|
return file_exists($this->getPath($name));
|
2020-02-06 02:35:13 +00:00
|
|
|
}
|
|
|
|
}
|