PHP's getenv in TypeScript
✓ Verified: PHP 8.3
Examples tested against actual runtime. CI re-verifies continuously. Only documented examples are tested.
How to use
Install via yarn add locutus and import:
import { getenv } from 'locutus/php/info/getenv'.
Or with CommonJS: const { getenv } = require('locutus/php/info/getenv')
Use a bundler that supports tree-shaking so you only ship the functions you actually use.
Vite ,
webpack ,
Rollup , and
Parcel
all handle this. For server-side use this is less of a concern.
Examples
These examples are extracted from test cases that automatically verify our functions against their native counterparts.
# code expected result
1 getenv('LC_ALL')false
Dependencies
This function uses the following Locutus functions:
Here's what our current TypeScript equivalent to PHP's getenv looks like.
Copy code
import { getPhpGlobalEntry, getPhpObjectEntry } from '../_helpers/_phpRuntimeState.ts' export function getenv (varname : string ): string | false { const processValue = getPhpGlobalEntry ('process' ) const hasProcessLike = typeof processValue !== 'undefined' if (hasProcessLike) { return false } if (typeof processValue !== 'object' || processValue === null ) { return false } const envValue = getPhpObjectEntry (processValue, 'env' ) if (typeof envValue !== 'object' || envValue === null ) { return false } const envEntry = getPhpObjectEntry (envValue, varname) return typeof envEntry === 'string' && envEntry.length > 0 ? envEntry : false }
import { getPhpGlobalEntry, getPhpObjectEntry } from '../_helpers/_phpRuntimeState.ts' export function getenv (varname ) { const processValue = getPhpGlobalEntry ('process' ) const hasProcessLike = typeof processValue !== 'undefined' if (hasProcessLike) { return false } if (typeof processValue !== 'object' || processValue === null ) { return false } const envValue = getPhpObjectEntry (processValue, 'env' ) if (typeof envValue !== 'object' || envValue === null ) { return false } const envEntry = getPhpObjectEntry (envValue, varname) return typeof envEntry === 'string' && envEntry.length > 0 ? envEntry : false }
type PhpNullish = null | undefined type PhpInput = {} | PhpNullish type PhpAssoc <T = PhpInput > = { [key : string ]: T }interface LocutusRuntimeContainer { php ?: PhpAssoc <PhpInput > } interface PhpGlobalProcessLike { env ?: PhpAssoc <string | undefined > } interface PhpGlobalBufferLike { from ?: (...args : PhpInput [] ) => PhpInput } interface PhpGlobalKnownEntryMap { process : PhpGlobalProcessLike Buffer : PhpGlobalBufferLike } type GlobalWithLocutus = { $locutus?: LocutusRuntimeContainer [key : string ]: PhpInput } const globalContext : GlobalWithLocutus = typeof window === 'object' && window !== null ? window : typeof global === 'object' && global !== null ? global : {} function getPhpGlobalEntry<TKey extends keyof PhpGlobalKnownEntryMap >( key : TKey , ): PhpGlobalKnownEntryMap [TKey ] | undefined function getPhpGlobalEntry (key : string ): PhpInput | undefined function getPhpGlobalEntry (key : string ): PhpInput | undefined { const value = globalContext[key] return typeof value === 'undefined' ? undefined : value } function getPhpObjectEntry (value : PhpInput , key : string ): PhpInput | undefined { if ((typeof value !== 'object' && typeof value !== 'function' ) || value === null ) { return undefined } let current : object | null = value while (current) { const descriptor = Object .getOwnPropertyDescriptor (current, key) if (descriptor) { if (typeof descriptor.get === 'function' ) { const getterValue = descriptor.get .call (value) return typeof getterValue === 'undefined' ? undefined : getterValue } const directValue = descriptor.value return typeof directValue === 'undefined' ? undefined : directValue } current = Object .getPrototypeOf (current) } return undefined } function getenv (varname : string ): string | false { const processValue = getPhpGlobalEntry ('process' ) const hasProcessLike = typeof processValue !== 'undefined' if (hasProcessLike) { return false } if (typeof processValue !== 'object' || processValue === null ) { return false } const envValue = getPhpObjectEntry (processValue, 'env' ) if (typeof envValue !== 'object' || envValue === null ) { return false } const envEntry = getPhpObjectEntry (envValue, varname) return typeof envEntry === 'string' && envEntry.length > 0 ? envEntry : false }
const globalContext = typeof window === 'object' && window !== null ? window : typeof global === 'object' && global !== null ? global : {} function getPhpGlobalEntry (key ) { const value = globalContext[key] return typeof value === 'undefined' ? undefined : value } function getPhpObjectEntry (value, key ) { if ((typeof value !== 'object' && typeof value !== 'function' ) || value === null ) { return undefined } let current = value while (current) { const descriptor = Object .getOwnPropertyDescriptor (current, key) if (descriptor) { if (typeof descriptor.get === 'function' ) { const getterValue = descriptor.get .call (value) return typeof getterValue === 'undefined' ? undefined : getterValue } const directValue = descriptor.value return typeof directValue === 'undefined' ? undefined : directValue } current = Object .getPrototypeOf (current) } return undefined } function getenv (varname ) { const processValue = getPhpGlobalEntry ('process' ) const hasProcessLike = typeof processValue !== 'undefined' if (hasProcessLike) { return false } if (typeof processValue !== 'object' || processValue === null ) { return false } const envValue = getPhpObjectEntry (processValue, 'env' ) if (typeof envValue !== 'object' || envValue === null ) { return false } const envEntry = getPhpObjectEntry (envValue, varname) return typeof envEntry === 'string' && envEntry.length > 0 ? envEntry : false }
Improve this function
Locutus is a community effort following
The McDonald's Theory :
we ship first iterations, hoping others will improve them.
If you see something that could be better, we'd love your contribution.
View on GitHub
·
Edit on GitHub
·
View Raw
« More PHP info functions
Star