Skip to content

Commit

Permalink
pkgs: add PHP to formats.nix
Browse files Browse the repository at this point in the history
  • Loading branch information
thenhnn committed May 13, 2024
1 parent 1042fd8 commit 88802aa
Show file tree
Hide file tree
Showing 3 changed files with 125 additions and 0 deletions.
21 changes: 21 additions & 0 deletions nixos/doc/manual/development/settings-options.section.md
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,27 @@ have a predefined type and string generator already declared under
: Outputs the given attribute set as an Elixir map, instead of the
default Elixir keyword list

`pkgs.formats.php { finalVariable }`

: A function taking an attribute set with values

`finalVariable`

: The variable that will store generated expression (usually `$config`). If set to `null`, generated expression will contain `return`.

It returns a set with PHP-Config-specific attributes `type`, `lib`, and
`generate` as specified [below](#pkgs-formats-result).

The `lib` attribute contains functions to be used in settings, for
generating special PHP values:

`mkRaw phpCode`

: Outputs the given string as raw PHP code

`mkMixedArray list set`

: Creates PHP array that contains both indexed and associative values

[]{#pkgs-formats-result}
These functions all return an attribute set with these values:
Expand Down
62 changes: 62 additions & 0 deletions pkgs/pkgs-lib/formats.nix
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,68 @@ rec {

};

# Format for defining configuration of some PHP services, that use "include 'config.php';" approach.
php = {finalVariable ? null}: with lib; let
toPHP = value: with builtins;
if value == null then "null" else
if value == true then "true" else
if value == false then "false" else
if isInt value || isFloat value then toString value else
if isString value then string value else
if isAttrs value then attrs value else
if isList value then list value else
abort "formats.php: should never happen (value = ${value})";

escapePHP = escape [ "'" ];
string = value: "'${escapePHP value}'";

listContent = values: concatStringsSep ", " (map toPHP values);
list = values: "[" + (listContent values) + "]";

attrsContent = values: pipe values [
(mapAttrsToList (k: v: "${toPHP k} => ${toPHP v}"))
(concatStringsSep ", ")
];
attrs = set:
if set ? _phpType then specialType set
else
"[" + attrsContent set + "]";

mixedArray = {list, set}: "[${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: assert list != []; set: {_phpType = "mixed_array"; value = { inherit list set;}; };
mkRaw = raw: {_phpType = "raw"; value = raw;};
};

generate = name: value: pkgs.writeTextFile {
name = "config.php";
text = if finalVariable == null then "<?php\nreturn ${toPHP value};\n" else "<?php\n${finalVariable} = ${toPHP value};\n";
};

};

/* For configurations of Elixir project, like config.exs or runtime.exs
Most Elixir project are configured using the [Config] Elixir DSL
Expand Down
42 changes: 42 additions & 0 deletions pkgs/pkgs-lib/tests/formats.nix
Original file line number Diff line number Diff line change
Expand Up @@ -425,4 +425,46 @@ in runBuildTests {
\u0627\u0644\u062c\u0628\u0631 = \u0623\u0643\u062b\u0631 \u0645\u0646 \u0645\u062c\u0631\u062f \u0623\u0631\u0642\u0627\u0645
'';
};

phpAtoms = shouldPass rec {
format = formats.php { finalVariable = "$config"; };
input = {
null = null;
false = false;
true = true;
int = 10;
float = 3.141;
str = "foo";
str_special = "foo\ntesthello'''";
attrs.foo = null;
list = [ null null ];
mixed = format.lib.mkMixedArray [ 10 3.141 ] {
str = "foo";
attrs.foo = null;
};
raw = format.lib.mkRaw "random_function()";
};
expected = ''
<?php
$config = ['attrs' => ['foo' => null], 'false' => false, 'float' => 3.141000, 'int' => 10, 'list' => [null, null], 'mixed' => [10, 3.141000, 'attrs' => ['foo' => null], 'str' => 'foo'], 'null' => null, 'raw' => random_function(), 'str' => 'foo', 'str_special' => 'foo
testhello\'\'\'${"'"}, 'true' => true];
'';
};

phpReturn = shouldPass {
format = formats.php { };
input = {
int = 10;
float = 3.141;
str = "foo";
str_special = "foo\ntesthello'''";
attrs.foo = null;
};
expected = ''
<?php
return ['attrs' => ['foo' => null], 'float' => 3.141000, 'int' => 10, 'str' => 'foo', 'str_special' => 'foo
testhello\'\'\'${"'"}];
'';
};

}

0 comments on commit 88802aa

Please sign in to comment.