Some values differ per machine and must not stand in the code: the database password, an
API key, a debug switch. padEnv reads them from the server's environment or from
a .env file beside the code, so the same application runs unchanged on a laptop
and on the production server, and the secrets never reach git. The cache keeps what is slow
to make from one request to the next, and a rate limit counts how often a visitor tries.
padEnv ( 'KEY', $default ) looks in three places and answers the first that
has the key: the real environment of the server (getenv, $_ENV,
$_SERVER - what Apache's SetEnv or PHP-FPM's env[...]
sets), then the .env in the application's root - apps/shop/.env -
then _common's .env for an application that runs with
_common, then .env in the PAD home. Without the key it answers the default - a closure as
the default is called. This manual has a .env of its own:
MANUAL_SITE_NAME="PAD manual"
MANUAL_PAGE_SIZE=20
MANUAL_DEBUG=false
|
PHP fragments/environment_and_cache_1.php |
HTML fragments/environment_and_cache_1.pad |
Result |
|---|---|---|
|
|
|
The engine loads its library before it reads any configuration, so the place a secret is
needed first can ask for it - _config/config.php:
$padSqlPassword = padEnv ( 'padSqlPassword' );
$padSqlUser = padEnv ( 'padSqlUser', 'shop' );
# a comment
APP_NAME=Shop a bare value, trimmed - # after a space starts a comment
export DB_HOST=localhost export in front, as a shell script writes it
GREETING="Hello\nWorld" double quotes: \n \r \t \" \\ \$ and ${OTHER}
PATTERN='${not} \n expanded' single quotes: the text exactly as written
DEBUG=false true, false, null and empty are TRUE, FALSE, NULL and ''
NOTE="two
lines" a quoted value may run over several lines
"false" is the word, not FALSE.${OTHER} is OTHER from the real environment, else as set
earlier in the same file, else empty; it works in bare values too.HTTP_ is never read from the real environment:
those are the headers of the request, which the client sends.apps/ - the web server serves www/ only - so an
application's .env is not served. The repository's
.gitignore keeps the .env of the PAD home out of git; keep an
application's out the same way when it holds a real secret.A slow query, a remote service, a computed report: padRemember ( $key, $ttl,
$callback ) answers what is kept under the key, or runs the callback, keeps its answer
for $ttl seconds and answers that. padCachePut,
padCacheGet, padCacheHas and padCacheForget do the same
by hand.
|
PHP fragments/environment_and_cache_2.php |
HTML fragments/environment_and_cache_2.pad |
Result |
|---|---|---|
|
|
Maintenance on Sunday night Nothing kept under this key |
padCachePut is not told - or
NULL for ever, a DateTimeInterface (until then) or a
DateInterval. A ttl of 0 or less keeps nothing.NULL counts: a callback that found nothing is not asked again until
the entry expires. A closure as the default of padCacheGet is called.DATA/cache/app/<application>/, one per key; padCacheFlush ()
removes them all. A file is written beside its place and then renamed over it, so a
request reads the old value or the new one, never half of one.padCacheForget, and the next request makes it again.padRateLimit ( $key, $maxAttempts, $decaySeconds ) counts a hit and answers
TRUE while the key stays within the limit - five attempts a minute - and FALSE over it. The
first hit starts a window of $decaySeconds; when it ends, the key starts from
nothing again. A login form, a contact form, an expensive search: key it on the visitor, or
on the account that is tried.
|
PHP fragments/environment_and_cache_3.php |
HTML fragments/environment_and_cache_3.pad |
Result |
|---|---|---|
|
|
0 attempts left - try again in 1 minute. |
padRateLimitRemaining ( $key, $maxAttempts ) - the hits left in the
window; padRateLimitAvailableIn ( $key ) - the seconds until it ends, 0 when no
window runs; padRateLimitClear ( $key ) - after a login that succeeded.padCacheFlush () clears them too -
and a rate limit and a cache key of the same name are two entries.| function | what it does |
|---|---|
padEnv ( $key, $default = NULL ) | the value of an environment key - the real environment, then the application's .env, then _common's, then the PAD home's .env, else the default |
padCacheGet ( $key, $default = NULL ) | the value kept under the key, else the default |
padCachePut ( $key, $value, $ttl = 3600 ) | keeps the value for $ttl seconds - NULL for ever; TRUE when it is kept |
padCacheHas ( $key ) | whether the key holds a value that has not expired |
padCacheForget ( $key ) | removes the key; TRUE when it held a value |
padCacheFlush () | removes every entry of this application, rate limits included |
padRemember ( $key, $ttl, $callback ) | the kept value, or the callback's answer - kept for $ttl seconds |
padRateLimit ( $key, $maxAttempts, $decaySeconds = 60 ) | counts a hit: TRUE within the limit, FALSE over it |
padRateLimitRemaining ( $key, $maxAttempts ) | the hits left in the current window |
padRateLimitAvailableIn ( $key ) | the seconds until the current window ends |
padRateLimitClear ( $key ) | ends the window - the key starts from nothing |