forked from devsecopsmaturitymodel/DevSecOps-MaturityModel
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgenerateDimensions.php
More file actions
152 lines (128 loc) · 3.99 KB
/
generateDimensions.php
File metadata and controls
152 lines (128 loc) · 3.99 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
149
150
151
152
<?php
/**
* data/generateDimensions.php
*
* @package default
* @see functions.php
*/
require_once "functions.php";
if (ENFORCE_DATA_GENERATION_DURING_RUNTIME) {
$files = glob("data/dimensions-subdimensions-activities/*/*.yaml");
$dimensions=array();
foreach ($files as $filename) {
//echo "Found $filename";
if (preg_match("/_meta.yaml/", $filename)) continue;
$dimension = getDimensions($filename);
if (array_key_exists("_yaml_references", $dimension)) {
unset($dimension['_yaml_references']);
}
$dimensions = array_merge_recursive($dimensions, $dimension);
}
$files = glob("data/custom/*/*.yaml");
$dimensionsCustom=array();
$dimensionsAggregated=array();
foreach ($files as $filename) {
//echo "Found $filename";
$dimensionCustom = getDimensions($filename);
$dimensionsCustom = array_merge_recursive($dimensionsCustom, $dimensionCustom);
}
if (sizeof($files) > 0) {
$dimensions = array_merge_recursive_ex($dimensions, $dimensionsCustom);
foreach (getActions($dimensions) as list($dimension, $subdimension, $activities)) {
if (!array_key_exists($dimension, $dimensionsAggregated)) $dimensionsAggregated[$dimension] = array();
if (!array_key_exists($subdimension, $dimensionsAggregated[$dimension])) $dimensionsAggregated[$dimension][$subdimension] = array();
foreach ($activities as $activityName => $activity) {
if (isActivityExisting($dimensionsCustom, $activityName)) {
$dimensionsAggregated[$dimension][$subdimension][$activityName] = $activity;
}
}
}
} else {
$dimensionsAggregated=$dimensions;
}
foreach ($dimensions as $dimension => $subdimensions) {
ksort($subdimensions);
foreach ($subdimensions as $subdimension => $elements) {
if (substr($subdimension, 0, 1) == "_")
continue;
foreach ($elements as $activityName => $activity)
if (!array_key_exists("level", $activity)) {
echo "'$activityName' is not complete!";
echo "<pre>";
var_dump($activity);
echo "</pre>";
exit;
}
}
}
resolve_json_ref($dimensionsAggregated);
$dimensionsString = yaml_emit($dimensionsAggregated);
file_put_contents("data/generated/dimensions.yaml", $dimensionsString);
}
/**
*
* @param unknown $dimensions
* @param unknown $activityName
* @return unknown
*/
function isActivityExisting($dimensions, $activityName) {
foreach (getActions($dimensions) as list($dimension, $subdimension, $activities)) {
foreach ($activities as $activity => $activityContent) {
if ($activity == $activityName) {
return true;
}
}
}
return false;
}
/**
*
* @param array $array1
* @param array $array2
* @return unknown
*/
function array_merge_recursive_ex(array $array1, array $array2) {
$merged = $array1;
foreach ($array2 as $key => & $value) {
if (is_array($value) && isset($merged[$key]) && is_array($merged[$key])) {
$merged[$key] = array_merge_recursive_ex($merged[$key], $value);
} else if (is_numeric($key)) {
if (!in_array($value, $merged)) {
$merged[] = $value;
}
} else {
$merged[$key] = $value;
}
}
return $merged;
}
/**
* Traverse in-place an array and replaces json-pointers.
*
* @param unknown $data (reference)
*/
function resolve_json_ref(&$data) {
/**
*
* @param unknown $value (reference)
* @param unknown $key
* @param unknown $ctx
*/
function resolve_json_ref_cb(&$value, $key, $ctx) {
if (! is_array($value)) {
return;
}
if (!array_key_exists('$ref', $value)) {
$ctx["ctx"] = &$value;
array_walk($value, "resolve_json_ref_cb", $ctx);
return;
}
// echo "key: $key\nvalue: ".var_dump($ctx). "\n";
$ctx["ctx"][$key] = readYaml($value['$ref']);
}
// Create a context variable to store
// the reference to the actual data, so that
// resolve_reference can replace them.
$ctx = array("ctx" => null);
array_walk($data, "resolve_json_ref_cb", $ctx);
}