This repository has been archived by the owner on Aug 18, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
YamlFileLoader.php
97 lines (80 loc) · 2.62 KB
/
YamlFileLoader.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
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
90
91
92
93
94
95
96
97
<?php
declare(strict_types=1);
/*
* This file is part of the Zikula package.
*
* Copyright Zikula - https://ziku.la/
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Zikula\Component\Wizard;
use Symfony\Component\Config\Loader\FileLoader;
use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException;
use Symfony\Component\Yaml\Parser as YamlParser;
/**
* YamlFileLoader loads YAML files.
*/
class YamlFileLoader extends FileLoader
{
private $yamlParser;
private $content;
/**
* {@inheritdoc}
*/
public function load($resource, string $type = null)
{
$path = $this->locator->locate($resource);
$this->content = $this->loadFile($path);
}
/**
* {@inheritdoc}
*/
public function supports($resource, string $type = null)
{
return is_string($resource) && 'yml' === pathinfo($resource, PATHINFO_EXTENSION);
}
public function getContent(): ?array
{
return $this->content;
}
/**
* Loads a YAML file.
*
* @throws InvalidArgumentException when the given file is not a local file or when it does not exist
*/
private function loadFile(string $file): array
{
if (!stream_is_local($file)) {
throw new InvalidArgumentException(sprintf('This is not a local file "%s".', $file));
}
if (!file_exists($file)) {
throw new InvalidArgumentException(sprintf('The service file "%s" is not valid.', $file));
}
if (null === $this->yamlParser) {
$this->yamlParser = new YamlParser();
}
return $this->validate($this->yamlParser->parse(file_get_contents($file)), $file);
}
/**
* Validates a YAML file.
*
* @param mixed $content
* @return array
*
* @throws InvalidArgumentException When service file is not valid
*/
private function validate($content, string $file): array
{
if (null === $content) {
return $content;
}
if (!is_array($content)) {
throw new InvalidArgumentException(sprintf('The yaml file "%s" is not valid. It should contain an array. Check your YAML syntax.', $file));
}
if (isset($content['stages']) && !is_array($content['stages'])) {
throw new InvalidArgumentException(sprintf('The "stages" key should contain an array in %s. Check your YAML syntax.', $file));
}
return $content;
}
}