Repository navigation
Expand file tree
/
Copy pathConfigFactory.php
More file actions
148 lines (120 loc) · 4.61 KB
/
Copy pathConfigFactory.php
File metadata and controls
148 lines (120 loc) · 4.61 KB
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
<?php
declare(strict_types=1);
/*
* This file is part of the Composer package "cpsit/project-builder".
*
* Copyright (C) 2022 Elias Häußler <e.haeussler@familie-redlich.de>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
namespace CPSIT\ProjectBuilder\Builder\Config;
use CPSIT\ProjectBuilder\Exception;
use CPSIT\ProjectBuilder\Helper;
use CPSIT\ProjectBuilder\Json;
use CPSIT\ProjectBuilder\Paths;
use CuyZ\Valinor;
use Opis\JsonSchema;
use stdClass;
use Symfony\Component\Filesystem;
use Symfony\Component\Yaml;
use function json_decode;
/**
* ConfigFactory.
*
* @author Elias Häußler <e.haeussler@familie-redlich.de>
* @license GPL-3.0-or-later
*
* @internal
*/
final class ConfigFactory
{
private static ?string $cacheDirectory = null;
private function __construct(
private readonly Valinor\Mapper\TreeMapper $mapper,
private readonly Json\SchemaValidator $schemaValidator,
) {}
public static function create(): self
{
self::$cacheDirectory ??= Helper\FilesystemHelper::getNewTemporaryDirectory();
$mapper = (new Valinor\MapperBuilder())
->withCache(new Valinor\Cache\FileSystemCache(self::$cacheDirectory))
->mapper()
;
return new self($mapper, new Json\SchemaValidator(new JsonSchema\Validator()));
}
public function buildFromFile(string $file, string $identifier): Config
{
$fileType = FileType::fromFile($file);
$content = file_get_contents($file);
if (false === $content) {
// @codeCoverageIgnoreStart
throw Exception\InvalidConfigurationException::forFile($file);
// @codeCoverageIgnoreEnd
}
$config = $this->buildFromString($content, $identifier, $fileType);
return $config->setDeclaringFile($file);
}
public function buildFromString(string $content, string $identifier, FileType $fileType): Config
{
$parsedContent = $this->parseContent($content, $fileType);
$validationResult = $this->schemaValidator->validate(
$parsedContent,
Filesystem\Path::join(
Helper\FilesystemHelper::getPackageDirectory(),
Paths::PROJECT_SCHEMA_CONFIG,
),
);
if (!$validationResult->isValid()) {
throw Exception\InvalidConfigurationException::forValidationErrors($validationResult->error());
}
$source = $this->generateMapperSource($content, $identifier, $fileType);
return $this->mapper->map(Config::class, $source);
}
private function generateMapperSource(string $content, string $identifier, FileType $fileType): Valinor\Mapper\Source\Source
{
$parsedContent = match ($fileType) {
FileType::Yaml => Yaml\Yaml::parse($content),
FileType::Json => json_decode($content, true, 512, JSON_THROW_ON_ERROR),
};
// @codeCoverageIgnoreStart
if (!is_array($parsedContent)) {
throw Exception\InvalidConfigurationException::forSource($content);
}
// @codeCoverageIgnoreEnd
// Enforce custom identifier
$parsedContent['identifier'] = $identifier;
// Unset $schema property
unset($parsedContent['$schema']);
return Valinor\Mapper\Source\Source::array($parsedContent);
}
private function parseContent(string $content, FileType $fileType): stdClass
{
$parsedContent = match ($fileType) {
FileType::Yaml => Yaml\Yaml::parse($content, Yaml\Yaml::PARSE_OBJECT_FOR_MAP),
FileType::Json => json_decode($content, false, 512, JSON_THROW_ON_ERROR),
};
if (!$parsedContent instanceof stdClass) {
throw Exception\InvalidConfigurationException::forSource($content);
}
// Unset $schema property
unset($parsedContent->{'$schema'});
return $parsedContent;
}
public function __destruct()
{
if (null !== self::$cacheDirectory) {
(new Filesystem\Filesystem())->remove(self::$cacheDirectory);
}
}
}