Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions public/Application.cfc
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ component output="false" {
include "../config/app.cfm";

function onApplicationStart() {
application.env = duplicate(this.env);
injector = new wheels.Injector("wheels.Bindings");

/* wheels/global object */
Expand Down
52 changes: 52 additions & 0 deletions tests/specs/functional/EnvHelperSpec.cfc
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
component extends="wheels.WheelsTest" {

function run() {

describe("env()", () => {

afterEach(() => {
// Clean up any test keys we added
StructDelete(application, "env");
})

it("returns value from application.env when present", () => {
application.env = {TEST_KEY: "from_dotenv"};
expect(env("TEST_KEY")).toBe("from_dotenv");
})

it("returns default when key is not found anywhere", () => {
application.env = {};
expect(env("NONEXISTENT_KEY_12345")).toBe("");
})

it("returns custom default when key is not found", () => {
application.env = {};
expect(env("NONEXISTENT_KEY_12345", "custom_default")).toBe("custom_default");
})

it("prefers application.env over system environment", () => {
// If a key exists in both, application.env should win
application.env = {PATH: "app_path_override"};
expect(env("PATH")).toBe("app_path_override");
})

it("falls back to server.system.environment", () => {
// PATH should exist in system environment on all platforms
application.env = {};
local.result = env("PATH");
expect(local.result).notToBe("");
})

})

describe("application.env population", () => {

it("application.env is populated from Application.cfc this.env", () => {
expect(StructKeyExists(application, "env")).toBeTrue();
expect(IsStruct(application.env)).toBeTrue();
})

})

}
}
23 changes: 23 additions & 0 deletions vendor/wheels/Global.cfc
Original file line number Diff line number Diff line change
Expand Up @@ -408,6 +408,29 @@ component output="false" {
return $get(argumentCollection = arguments);
}

/**
* Returns the value of an environment variable. Checks application.env (loaded from .env files) first, then falls back to system environment variables (server.system.environment). Returns the default if the variable is not found in either location.
*
* [section: Configuration]
* [category: Miscellaneous Functions]
*
* @name The environment variable name to look up.
* @default Value to return if the variable is not found.
*/
public any function env(required string name, any default="") {
if (StructKeyExists(application, "env") && StructKeyExists(application.env, arguments.name)) {
return application.env[arguments.name];
}
if (
StructKeyExists(server, "system")
&& StructKeyExists(server.system, "environment")
&& StructKeyExists(server.system.environment, arguments.name)
) {
return server.system.environment[arguments.name];
}
return arguments.default;
}

/**
* Use to configure a global setting or set a default for a function.
*
Expand Down
Loading