blob: 9bd1de958d0be0610d53595e8c528c7926712a83 (
plain) (
blame)
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
|
{pkgs, lib}: {
# Format for defining configuration of some PHP services, that use "include 'config.php';" approach.
format = {finalVariable ? null}: let
toPHP = value: {
"null" = "null";
"bool" = if value then "true" else "false";
"int" = toString value;
"float" = toString value;
"string" = string value;
"set" = attrs value;
"list" = list value;
}
.${builtins.typeOf value} or
(abort "should never happen: unknown value type ${builtins.typeOf value}");
# https://www.php.net/manual/en/language.types.string.php#language.types.string.syntax.single
escapeSingleQuotedString = lib.escape [ "'" "\\" ];
string = value: "'${escapeSingleQuotedString value}'";
listContent = values: lib.concatStringsSep ", " (map toPHP values);
list = values: "[" + (listContent values) + "]";
attrsContent = values: lib.pipe values [
(lib.mapAttrsToList (k: v: "${toPHP k} => ${toPHP v}"))
(lib.concatStringsSep ", ")
];
attrs = set:
if set ? _phpType then specialType set
else
"[" + attrsContent set + "]";
mixedArray = {list, set}: if list == [] then attrs set else "[${listContent list}, ${attrsContent set}]";
specialType = {value, _phpType}: {
"mixed_array" = mixedArray value;
"raw" = value;
}.${_phpType};
type = with lib.types;
nullOr (oneOf [
bool
int
float
str
(attrsOf type)
(listOf type)
])
// {
description = "PHP value";
};
in {
inherit type;
lib = {
mkMixedArray = list: set: {_phpType = "mixed_array"; value = { inherit list set;}; };
mkRaw = raw: {_phpType = "raw"; value = raw;};
};
generate = name: value: pkgs.writeTextFile {
inherit name;
text = let
# strict_types enabled here to easily debug problems when calling functions of incorrect type using `mkRaw`.
phpHeader = "<?php\ndeclare(strict_types=1);\n";
in if finalVariable == null then phpHeader + "return ${toPHP value};\n" else phpHeader + "\$${finalVariable} = ${toPHP value};\n";
};
};
}
|