Skip to content

Commit 1f5933f

Browse files
committed
refine configuration and env
1 parent 96e0743 commit 1f5933f

File tree

1 file changed

+65
-25
lines changed

1 file changed

+65
-25
lines changed

configuration.md

Lines changed: 65 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -4,58 +4,98 @@
44
## Introduction
55
All your Supercharge configuration files are stored in the `config` directory. Your Supercharge application and the framework core follow these configuration files. You can adjust and tweak the app’s behavior using different values for individual options.
66

7-
We highly recommend putting all configuration files into the `config` directory because these files are automatically loaded when starting your application.
87

9-
Here’s a preview how Supercharge recommends the usage of environment variables to configure your app:
8+
## Using Environment Variables
9+
Applications run in different environments using custom configurations. For example, while in local development you may use a different cache driver then in production.
1010

11-
![How to use Supercharge Config and Env](/images/docs/config-env.png)
11+
Supercharge loads all environment variables configured in a `.env` file when starting your application. A new Supercharge application contains a `.env.example` file. You can copy the `.env.example` file and rename the copied file to `.env` and adjust its values.
1212

1313

14-
## Retrieving Configuration Values
15-
Supercharge loads all your configuration values into memory when starting the application’s Node.js server. You can access the config values anywhere in your application using the `Config` facade.
14+
### Environment File Security
15+
You should not commit your application’s `.env` file to your source control repository. Each developer in your team may require a different configuration. Also, exposing the `.env` file to your source control may leak sensitive credentials in case attackers gain access to the repository.
1616

17-
Use the `Config.get` method to retrieve configuration values. All configurations start with the configuration file name followed by a defined key. You may also retrieve nested config values using the “dot” syntax. The dot notation starts with the configuration file name followed by the nested object path:
1817

19-
```ts
20-
import { Config } from '@supercharge/facades'
18+
### Retrieving Environment Values
19+
All variables in your `.env` file are loaded into the global [`process.env`](https://nodejs.org/docs/latest/api/process.html#process_process_env) environment object. Instead of accessing environment variables directly on `process.env`, you should use Supercharge’s `Env` facade.
2120

22-
const appName = Config.get('app.name', 'My Supercharge App')
21+
The `Env` facade provides a convenient access the values of environment variables:
22+
23+
```js
24+
import { Env } from '@supercharge/facades'
25+
26+
const appName = Env.get('APP_NAME')
2327
```
2428

25-
You may also provide a default value as the second argument when retrieving a config value. This default value is used when no config value exists for the provided key:
29+
You may also provide a default value as the second argument to the `Env` facade when retrieving an environment value:
2630

2731
```ts
28-
const appName = Config.get('app.name', 'My Supercharge App')
32+
const appName = Env.get('APP_NAME', 'Supercharge')
2933
```
3034

3135

32-
## Retrieving Environment Values
33-
All variables in your `.env` file are loaded into the global [`process.env`](https://nodejs.org/docs/latest-v16.x/api/process.html#process_process_env) environment object.
34-
35-
We recommend using environment variables to configure your application. This approach follows the concept of [the 12 factor app](https://12factor.net/). The framework provides a `Env` facade to access the values of environment variables:
36+
### Ensure Environment Values
37+
In some situations your application won’t work when a given environment variable is not configured. The `Env` facade allows you to require an environment variable using the `Env.getOrFail` method:
3638

3739
```js
3840
import { Env } from '@supercharge/facades'
3941

40-
const appName = Env.get('APP_NAME')
42+
const appKey = Env.getOrFail('APP_KEY')
4143
```
4244

43-
The `Env` facade accepts a default value as the second argument when retrieving the value of an environment variable. This is useful to provide thoughtful default values for your application:
45+
46+
### Determine the Current Environment
47+
You may want to determine the current application environment. The `Env` facade provides convenience methods to detect the current environment. It compares the `NODE_ENV` environment variable against a given value:
4448

4549
```ts
46-
const appName = Env.get('APP_NAME', 'Supercharge')
50+
import { Env } from '@supercharge/facades'
51+
52+
const isProduction = Env.isProduction()
53+
// true when NODE_ENV is set to "production"
54+
55+
const isNotProduction = Env.isNotProduction()
56+
// true when NODE_ENV is not set to "production"
57+
58+
const isTesting = Env.isTesting()
59+
// true when NODE_ENV is set to "test" or "testing"
60+
61+
const isLocal = Env.is('local')
4762
```
4863

49-
```info
50-
The idea in Supercharge is to feed your configuration from environment variables. We recommend to use the `Env` facade in your config files and not directly in any application logic.
64+
65+
## Using Configuration Values
66+
We highly recommend putting all configuration files into the `config` directory because these files are automatically loaded when your application starts.
67+
68+
Here’s a preview how Supercharge recommends the usage of environment variables to configure your app:
69+
70+
![How to use Supercharge Config and Env](/images/docs/config-env.png)
71+
72+
73+
### Retrieving Configuration Values
74+
Supercharge loads all your configuration values into memory when starting the application’s HTTP server or Craft CLI. You can access the config values anywhere in your application using the `Config` facade or via the app instance using the `app.config()` method.
75+
76+
Use the `Config.get` method to retrieve configuration values. All configurations start with the configuration file name followed by a defined key. You may also retrieve nested config values using the “dot” syntax. The dot notation starts with the configuration file name followed by the nested object path:
77+
78+
```ts
79+
import { Config } from '@supercharge/facades'
80+
81+
const appName = Config.get('app.name')
5182
```
5283

84+
You may also provide a default value as the second argument to `Config.get` when retrieving a config value. This default value is used when no config value exists for the provided key:
85+
86+
```ts
87+
const appName = Config.get('app.name', 'My Supercharge App')
88+
```
5389

54-
## Environment Variables
55-
Applications run in different environments with a custom configuration. For example, while in development you may want a different mailing driver than in production or testing.
5690

57-
Supercharge loads all environment variables configured in a `.env` file when starting your application. A new Supercharge application contains a `.env.example` file. You may copy the `.env.example` file and rename the copy to `.env` for new applications.
91+
### Ensure Configuration Values
92+
You may need to ensure a configuration value while building an application or community package. For example, a file cache driver requires a caching directory that you need to configure. In such situations, you may use the `Config.ensure` method to require a
5893

59-
```warning
60-
You should not commit your application’s `.env` file to your source control repository. Each developer in your team may require a different configuration. Also, exposing the `.env` file to your source control may leak sensitive credentials in case attackers gain access to the repository.
94+
```js
95+
import { Config } from '@supercharge/facades'
96+
97+
Config.ensure('cache.location')
98+
// throws if the configuration for `cache.location` is missing
6199
```
100+
101+

0 commit comments

Comments
 (0)