1515 * create a class called 'AppConfig' on the directory APP_DIR/Config and
1616 * use it to read and write configurations.
1717 *
18+ * ## Environment Variable Support
19+ *
20+ * This driver supports environment variable substitution using the 'env:' prefix.
21+ * The following configuration sections support this feature:
22+ * - Database connections (all properties)
23+ * - SMTP connections (all properties)
24+ * - Environment variables (value field)
25+ * - Scheduler password
26+ *
27+ * Values stored in the generated PHP class can use the 'env:' prefix, and they
28+ * will be resolved to their environment variable values when read.
29+ *
1830 * @author Ibrahim
1931 */
2032class ClassDriver implements ConfigurationDriver {
@@ -60,6 +72,9 @@ public static function a($file, $str, $tabSize = 0) {
6072 * a named constant at run time using the function 'define'. This means
6173 * the constant will be accesaable anywhere within the appllication's environment.
6274 *
75+ * Note: The value parameter supports the 'env:' prefix for referencing
76+ * system environment variables (e.g., "env:MY_VAR").
77+ *
6378 * @param string $name The name of the named constant such as 'MY_CONSTANT'.
6479 *
6580 * @param mixed $value The value of the constant.
@@ -77,6 +92,10 @@ public function addEnvVar(string $name, mixed $value = null, ?string $descriptio
7792 /**
7893 * Adds new database connections information or update existing connection.
7994 *
95+ * Note: When using this driver, connection properties support environment variable
96+ * substitution using the 'env:' prefix. Values will be stored in the generated PHP class
97+ * and resolved when read.
98+ *
8099 * @param ConnectionInfo $dbConnectionsInfo An object which holds connection information.
81100 */
82101 public function addOrUpdateDBConnection (ConnectionInfo $ dbConnectionsInfo ) {
@@ -86,6 +105,10 @@ public function addOrUpdateDBConnection(ConnectionInfo $dbConnectionsInfo) {
86105 /**
87106 * Adds new SMTP account or Updates an existing one.
88107 *
108+ * Note: When using this driver, SMTP account properties support environment variable
109+ * substitution using the 'env:' prefix. Values will be stored in the generated PHP class
110+ * and resolved when read.
111+ *
89112 * @param SMTPAccount $emailAccount An instance of 'SMTPAccount'.
90113 */
91114 public function addOrUpdateSMTPAccount (SMTPAccount $ emailAccount ) {
@@ -157,6 +180,9 @@ public function getBaseURL(): string {
157180 /**
158181 * Returns database connection information given connection name.
159182 *
183+ * Note: Connection properties that use the 'env:' prefix will be
184+ * automatically resolved to their environment variable values.
185+ *
160186 * @param string $conName The name of the connection.
161187 *
162188 * @return ConnectionInfo|null The method will return an object of type
@@ -174,10 +200,30 @@ public function getDBConnection(string $conName) {
174200 /**
175201 * Returns an associative array that contain the information of database connections.
176202 *
203+ * Note: Connection properties that use the 'env:' prefix will be
204+ * automatically resolved to their environment variable values.
205+ *
177206 * @return array An associative array of objects of type ConnectionInfo.
178207 */
179208 public function getDBConnections (): array {
180- return $ this ->configVars ['database-connections ' ];
209+ $ connections = $ this ->configVars ['database-connections ' ];
210+
211+ foreach ($ connections as $ name => $ connObj ) {
212+ if ($ connObj instanceof ConnectionInfo) {
213+ $ connObj ->setHost (Controller::resolveEnvValue ($ connObj ->getHost ()));
214+ $ connObj ->setUsername (Controller::resolveEnvValue ($ connObj ->getUsername ()));
215+ $ connObj ->setPassword (Controller::resolveEnvValue ($ connObj ->getPassword ()));
216+ $ connObj ->setDBName (Controller::resolveEnvValue ($ connObj ->getDBName ()));
217+
218+ $ extras = $ connObj ->getExtars ();
219+ foreach ($ extras as $ key => $ value ) {
220+ $ extras [$ key ] = Controller::resolveEnvValue ($ value );
221+ }
222+ $ connObj ->setExtras ($ extras );
223+ }
224+ }
225+
226+ return $ connections ;
181227 }
182228
183229 public function getDescription (string $ langCode ) {
@@ -198,12 +244,25 @@ public function getDescriptions(): array {
198244 /**
199245 * Returns an associative array of application constants.
200246 *
247+ * Note: Environment variable values that use the 'env:' prefix will be
248+ * automatically resolved to their system environment variable values.
249+ *
201250 * @return array The indices of the array are names of the constants and
202251 * values are sub-associative arrays. Each sub-array will have two indices,
203252 * 'value' and 'description'.
204253 */
205254 public function getEnvVars (): array {
206- return $ this ->configVars ['env-vars ' ];
255+ $ vars = $ this ->configVars ['env-vars ' ];
256+
257+ foreach ($ vars as $ name => $ varData ) {
258+ if (is_array ($ varData ) && isset ($ varData ['value ' ])) {
259+ $ vars [$ name ]['value ' ] = Controller::resolveEnvValue ($ varData ['value ' ]);
260+ } else {
261+ $ vars [$ name ] = Controller::resolveEnvValue ($ varData );
262+ }
263+ }
264+
265+ return $ vars ;
207266 }
208267 /**
209268 * Returns a string that represents the URL of home page of the application.
@@ -221,9 +280,17 @@ public function getHomePage() : string {
221280 public function getPrimaryLanguage () : string {
222281 return $ this ->configVars ['site ' ]['primary-lang ' ];
223282 }
224-
283+ /**
284+ * Returns sha256 hash of the password which is used to prevent unauthorized
285+ * access to run the tasks or access scheduler web interface.
286+ *
287+ * Note: The scheduler password value supports environment variable substitution
288+ * using the 'env:' prefix (e.g., "env:SCHEDULER_PASS").
289+ *
290+ * @return string Password hash or the string 'NO_PASSWORD' if there is no password.
291+ */
225292 public function getSchedulerPassword (): string {
226- return $ this ->configVars ['scheduler-password ' ];
293+ return Controller:: resolveEnvValue ( $ this ->configVars ['scheduler-password ' ]) ;
227294 }
228295
229296 public function getSMTPAccount (string $ name ) {
@@ -242,15 +309,48 @@ public function getSMTPAccount(string $name) {
242309 * will return an object of type SMTPAccount. Else, the
243310 * method will return null.
244311 *
312+ */ /**
313+ * Returns SMTP connection given its name.
314+ *
315+ * Note: SMTP account properties that use the 'env:' prefix will be
316+ * automatically resolved to their environment variable values.
317+ *
318+ * @param string $name The name of the account.
319+ *
320+ * @return SMTPAccount|null If the account is found, The method
321+ * will return an object of type SMTPAccount. Else, the
322+ * method will return null.
323+ *
245324 */
246325 public function getSMTPConnection (string $ name ) {
247326 if (isset ($ this ->getSMTPConnections ()[$ name ])) {
248327 return $ this ->getSMTPConnections ()[$ name ];
249328 }
250329 }
251-
330+ /**
331+ * Returns an array that contains all added SMTP accounts.
332+ *
333+ * Note: SMTP account properties that use the 'env:' prefix will be
334+ * automatically resolved to their environment variable values.
335+ *
336+ * @return array An associative array of SMTPAccount objects.
337+ */
252338 public function getSMTPConnections (): array {
253- return $ this ->configVars ['smtp-connections ' ];
339+ $ connections = $ this ->configVars ['smtp-connections ' ];
340+
341+ foreach ($ connections as $ name => $ smtpObj ) {
342+ if ($ smtpObj instanceof SMTPAccount) {
343+ $ smtpObj ->setServerAddress (Controller::resolveEnvValue ($ smtpObj ->getServerAddress ()));
344+ $ smtpObj ->setPort (Controller::resolveEnvValue ($ smtpObj ->getPort ()));
345+ $ smtpObj ->setUsername (Controller::resolveEnvValue ($ smtpObj ->getUsername ()));
346+ $ smtpObj ->setPassword (Controller::resolveEnvValue ($ smtpObj ->getPassword ()));
347+ $ smtpObj ->setAddress (Controller::resolveEnvValue ($ smtpObj ->getAddress ()));
348+ $ smtpObj ->setSenderName (Controller::resolveEnvValue ($ smtpObj ->getSenderName ()));
349+ $ smtpObj ->setAccessToken (Controller::resolveEnvValue ($ smtpObj ->getAccessToken ()));
350+ }
351+ }
352+
353+ return $ connections ;
254354 }
255355
256356 public function getTheme (): string {
0 commit comments