/
Schema.php
109 lines (96 loc) · 2.84 KB
/
Schema.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
98
99
100
101
102
103
104
105
106
107
108
109
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);
namespace Magento\MagentoCloud\Config;
use Magento\MagentoCloud\Filesystem\SystemList;
use Magento\MagentoCloud\Filesystem\Driver\File;
use Symfony\Component\Yaml\Parser;
use Symfony\Component\Yaml\Yaml;
/**
* Configuration schema for .magento.env.yaml file
*/
class Schema
{
public const SCHEMA_TYPE = 'type';
public const SCHEMA_ALLOWED_VALUES = 'allowed';
public const SCHEMA_VALUE_VALIDATORS = 'validators';
public const SCHEMA_STAGES = 'stages';
public const SCHEMA_SYSTEM = 'system';
public const SCHEMA_DEFAULT_VALUE = 'default';
public const SCHEMA_DESCRIPTION = 'description';
public const SCHEMA_SKIP_DUMP = 'skip_dump';
public const SCHEMA_MAGENTO_VERSION = 'magento_version';
public const SCHEMA_EXAMPLES = 'examples';
public const SCHEMA_EXAMPLE_COMMENT = 'comment';
/**
* @var SystemList
*/
private $systemList;
/**
* @var Parser
*/
private $parser;
/**
* @var File
*/
private $file;
/**
* @var array
*/
private $defaults = [];
/**
* @param SystemList $systemList
* @param Parser $parser
* @param File $file
*/
public function __construct(
SystemList $systemList,
Parser $parser,
File $file
) {
$this->systemList = $systemList;
$this->parser = $parser;
$this->file = $file;
}
/**
* Returns default values for given stage.
*
* @param string $stage
* @return array
* @throws \Magento\MagentoCloud\Filesystem\FileSystemException
*/
public function getDefaults(string $stage): array
{
if (isset($this->defaults[$stage])) {
return $this->defaults[$stage];
}
foreach ($this->getSchema() as $itemName => $itemOptions) {
if (array_key_exists($stage, $itemOptions[self::SCHEMA_DEFAULT_VALUE])) {
$this->defaults[$stage][$itemName] = $itemOptions[self::SCHEMA_DEFAULT_VALUE][$stage];
}
}
return $this->defaults[$stage];
}
/**
* Returns configuration schema.
*
* Each configuration item can have next options:
* 'type' - possible types (string, integer, array, etc..)
* 'value_validation' - array of possible values or callback validation function
* 'stage' - possible stages in which item can be configured
* 'default_values' - array of default values
*
* @return array
* @throws \Magento\MagentoCloud\Filesystem\FileSystemException
*/
public function getSchema(): array
{
return $this->parser->parse(
$this->file->fileGetContents($this->systemList->getConfig() . '/schema.yaml'),
Yaml::PARSE_CONSTANT
);
}
}