-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathenv.pas
125 lines (111 loc) · 3.1 KB
/
env.pas
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
(*
Gets .ENV details for the Laravel's project
*)
unit env;
interface
uses
System.SysUtils, System.Classes;
type
TLaravelConfig = record
// App configurations
AppName: string;
AppEnv: string;
AppKey: string;
AppKey2: string;
AppDebug: Boolean;
AppURL: string;
// Database configurations
DBConnection: string;
DBHost: string;
DBPort: Integer;
DBDatabase: string;
DBUsername: string;
DBPassword: string;
// Amazon configurations
AWSAccessKeyID: string;
AWSSecretAccessKey: string;
AWSDefaultRegion: string;
AWSBucket: string;
AWSUsePathStyleEndpoint: Boolean;
// Redis configurations
RedisHost: string;
RedisPort: Integer;
RedisPassword: string;
// Mail configurations
MailMailer: string;
MailHost: string;
MailPort: Integer;
MailUsername: string;
MailPassword: string;
MailEncryption: string;
MailFromAddress: string;
MailFromName: string;
// Log configurations
LogChannel: string;
LogDeprecationsChannel: string;
LogLevel: string;
end;
TEnvConfig = class
private
FEnvList: TStringList;
FLaravelConfig: TLaravelConfig;
function GetValue(const Key: string; const DefaultValue: string = ''): string;
function GetIntValue(const Key: string; const DefaultValue: Integer = 0): Integer;
function GetBoolvalue(const Key: string; const DefaultValue: Boolean = False): Boolean;
procedure ParseLaravelConfig;
public
constructor Create(const FileName: string);
destructor Destroy; override;
property LaravelConfig: TLaravelConfig read FLaravelConfig;
end;
implementation
{ TEnvConfig }
constructor TEnvConfig.Create(const FileName: string);
begin
FEnvList := TStringList.Create;
try
FEnvList.LoadFromFile(FileName);
ParseLaravelConfig;
except
// Handle file not found or other errors like multiline files #TODO
on E: Exception do
raise Exception.Create('Error loading .env file: ' + E.Message);
end;
end;
destructor TEnvConfig.Destroy;
begin
FEnvList.Free;
inherited;
end;
function TEnvConfig.GetBoolvalue(const Key: string;
const DefaultValue: Boolean): Boolean;
begin
Result := StrToBoolDef(FEnvList.Values[Key], DefaultValue);
end;
function TEnvConfig.GetIntValue(const Key: string;
const DefaultValue: Integer): Integer;
begin
Result := StrToIntDef(FEnvList.Values[Key], DefaultValue);
end;
function TEnvConfig.GetValue(const Key, DefaultValue: string): string;
begin
Result := FEnvList.Values[Key];
if Result = '' then
Result := DefaultValue;
end;
procedure TEnvConfig.ParseLaravelConfig;
begin
with FLaravelConfig do
begin
// App Configurations
AppName := GetValue('APP_NAME');
AppEnv := GetValue('APP_ENV');
DBConnection := GetValue('DB_CONNECTION');
DBHost := GetValue('DB_HOST');
DBPort := GetIntValue('DB_PORT');
DBDatabase := GetValue('DB_DATABASE');
DBUsername := GetValue('DB_USERNAME');
DBPassword := GetValue('DB_PASSWORD');
end;
end;
end.