PHP code example of hpwebdeveloper / laravel-env-settings
1. Go to this page and download the library: Download hpwebdeveloper/laravel-env-settings library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
hpwebdeveloper / laravel-env-settings example snippets
// Before: scattered, untyped, invisible to code review
$domain = config('services.auth0.domain'); // typo? runtime surprise
$model = env('OPENAI_TEXT_MODEL'); // string? null? who knows
$mode = env('PAYMENT_MODE', 'test'); // what's production's value? check the server
// After: typed, environment-aware, in version control
envSettings(AuthSettings::class)->domain // string, IDE autocomplete
envSettings(AiSettings::class)->text_model // defined per environment
envSettings(PaymentSettings::class)->mode // visible in git, reviewable in PRs
namespace App\Settings;
use HpWebDeveloper\LaravelEnvSettings\EnvironmentSettings;
class AuthSettings extends EnvironmentSettings
{
public function __construct(
public string $domain,
public int $timeout,
public bool $mfa_enabled,
) {}
public static function development(): static
{
return new static(domain: 'dev.auth.example.com', timeout: 30, mfa_enabled: false);
}
public static function production(): static
{
return new static(domain: 'auth.example.com', timeout: 10, mfa_enabled: true);
}
}
public function __construct(
public string $provider, // varies by environment
public float $temperature = 0.2, // the same everywhere — written once
) {}
public static function development(): static { return new static(provider: 'ollama'); }
public static function production(): static { return new static(provider: 'openai'); }
// and the one environment that genuinely differs just passes it
public static function staging(): static { return new static(provider: 'openai', temperature: 0.9); }
// The helper — template-typed, so your IDE autocompletes the result
$domain = envSettings(AuthSettings::class)->domain;
// Constructor injection
public function __construct(private AuthSettings $auth) {}
// The container
app(AuthSettings::class)->timeout; // 10 in production, 30 in development
enum PaymentMode: string
{
case Live = 'live';
case Sandbox = 'sandbox';
}
public function __construct(
public PaymentMode $mode,
) {}
// development(): mode: PaymentMode::Sandbox
// production(): mode: PaymentMode::Live
use HpWebDeveloper\LaravelEnvSettings\Attributes\Environment;
#[Environment('production', 'prod')]
#[Environment('demo')] // a second environment sharing production values
public static function production(): static { ... }
#[Environment('qa', 'uat')] // the method name need not match the environment
public static function qualityAssurance(): static { ... }
use HpWebDeveloper\LaravelEnvSettings\Attributes\Sensitive;
public function __construct(
#[Sensitive] public string $passphrase,
) {}