-
Notifications
You must be signed in to change notification settings - Fork 0
/
yaml_only.php
47 lines (36 loc) · 1.41 KB
/
yaml_only.php
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
<?php
declare(strict_types=1);
use Symfony\Component\Yaml\Exception\ParseException;
use Symfony\Component\Yaml\Parser;
use Symfony\Component\Yaml\Yaml;
require_once __DIR__ . '/vendor/autoload.php';
class YamlConfigLoader
{
/**
* Loads a YAML file.
*
* @throws \InvalidArgumentException when the given file is not a local file or when it does not exist
*/
public function loadFile(string $file): ?array
{
if (!class_exists(Parser::class)) {
throw new \RuntimeException('Unable to load YAML config files as the Symfony Yaml Component is not installed.');
}
if (!stream_is_local($file)) {
throw new \InvalidArgumentException(sprintf('This is not a local file "%s".', $file));
}
if (!is_file($file)) {
throw new \InvalidArgumentException(sprintf('The file "%s" does not exist.', $file));
}
$this->yamlParser = new Parser();
try {
$configuration = $this->yamlParser->parseFile($file, Yaml::PARSE_CONSTANT | Yaml::PARSE_CUSTOM_TAGS);
} catch (ParseException $e) {
throw new \InvalidArgumentException(sprintf('The file "%s" does not contain valid YAML: ', $file).$e->getMessage(), 0, $e);
}
return $configuration;
}
}
$loader = new YamlConfigLoader();
$configuration = $loader->loadFile(__DIR__ . '/config/system_settings.yaml');
dump($configuration);