PHP code example of alexkart / typed-registry-laravel
1. Go to this page and download the library: Download alexkart/typed-registry-laravel 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/ */
alexkart / typed-registry-laravel example snippets
// config/app.php
return [
'name' => typedEnv()->getStringOr('APP_NAME', 'Laravel'),
'debug' => typedEnv()->getBoolOr('APP_DEBUG', false),
'port' => typedEnv()->getIntOr('APP_PORT', 8080), // "8080" → 8080
'timeout' => typedEnv()->getFloatOr('TIMEOUT', 2.5), // "2.5" → 2.5
'max_items' => typedEnv()->getInt('MAX_ITEMS'), // Throws if missing
];
use TypedRegistry\Laravel\Facades\TypedConfig;
class UserController
{
public function index()
{
$perPage = TypedConfig::getInt('app.pagination.per_page');
$appName = TypedConfig::getString('app.name');
$features = TypedConfig::getStringList('app.enabled_features');
// Or use the helper
$timeout = typedConfig()->getFloat('app.timeout');
}
}
// config/app.php
return [
// Automatic type casting from .env strings:
'port' => typedEnv()->getInt('PORT'), // "8080" → int(8080)
'rate' => typedEnv()->getFloat('RATE'), // "2.5" → float(2.5)
'limit' => typedEnv()->getFloat('LIMIT'), // "1e3" → float(1000.0)
'debug' => typedEnv()->getBool('APP_DEBUG'), // "true" → bool(true)
// With defaults (never throws):
'name' => typedEnv()->getStringOr('APP_NAME', 'Laravel'),
'timeout' => typedEnv()->getFloatOr('TIMEOUT', 30.0),
];
// config/auth.php
return [
// Problem: typedEnv() casts "123456" to int, getStringOr returns ''
// 'password' => typedEnv()->getStringOr('API_PASSWORD', ''),
// Solution: typedEnvString() keeps all values as strings
'password' => typedEnvString()->getStringOr('API_PASSWORD', ''),
'token' => typedEnvString()->getString('API_TOKEN'),
];
use TypedRegistry\Laravel\Facades\TypedConfig;
// In controllers, services, jobs, etc.
$driver = TypedConfig::getString('database.default');
$port = TypedConfig::getInt('database.connections.mysql.port');
$options = TypedConfig::getStringMap('database.connections.mysql.options');
// With defaults:
$perPage = TypedConfig::getIntOr('app.pagination.per_page', 15);
$driver = typedConfig()->getString('database.default');
->getString('KEY'); // string - throws if missing/wrong type
->getInt('KEY'); // int
->getBool('KEY'); // bool
->getFloat('KEY'); // float
->getNullableString('KEY'); // string|null
->getNullableInt('KEY'); // int|null
->getNullableBool('KEY'); // bool|null
->getNullableFloat('KEY'); // float|null
->getStringOr('KEY', 'default'); // Returns default if missing/wrong type
->getIntOr('KEY', 8080);
->getBoolOr('KEY', false);
->getFloatOr('KEY', 1.5);
->getStringList('KEY'); // list<string>
->getIntList('KEY'); // list<int>
->getBoolList('KEY'); // list<bool>
->getFloatList('KEY'); // list<float>
->getStringMap('KEY'); // array<string, string>
->getIntMap('KEY'); // array<string, int>
->getBoolMap('KEY'); // array<string, bool>
->getFloatMap('KEY'); // array<string, float>
// Integer casting (handles edge cases)
"123" → int(123)
"-456" → int(-456)
"0" → int(0)
"042" → int(42) // Leading zeros removed
"-042" → int(-42) // Negative with leading zeros
"+42" → int(42) // Leading plus removed
" 042 " → int(42) // Whitespace trimmed
// Integer overflow protection (values exceeding PHP_INT_MAX/MIN)
"9223372036854775808" → float(9.223372036854776E+18) // Too large for int
"-9223372036854775809" → float(-9.223372036854776E+18) // Too small for int
// Float casting (decimal point or scientific notation)
"3.14" → float(3.14)
"0.0" → float(0.0)
"1e3" → float(1000.0) // Scientific notation
"2.5e-4" → float(0.00025) // Scientific with decimal
"1E10" → float(10000000000.0) // Uppercase E
"042.5" → float(42.5)
// No casting
"Laravel" → "Laravel" // Non-numeric
"123abc" → "123abc" // Mixed alphanumeric
"" → "" // Empty string
// Laravel's Env handles these:
"true" → bool(true)
"false" → bool(false)
"null" → null
"(null)" → null
// All values become strings
"123456" → "123456" // Numeric string preserved
"3.14" → "3.14" // Float string preserved
"1e3" → "1e3" // Scientific notation preserved
"042" → "042" // Leading zeros preserved
"Laravel" → "Laravel" // Non-numeric unchanged
// Laravel's Env converts these first, then we cast to string:
"true" → "1" // bool(true) → string
"false" → "" // bool(false) → string
"null" → null // Stays null (not scalar)
// config/app.php
return [
'port' => 8080, // ✅ int - TypedConfig::getInt() works
'port_str' => '8080', // ❌ string - TypedConfig::getInt() throws
];
use TypedRegistry\RegistryTypeError;
try {
$port = TypedConfig::getInt('app.name'); // If 'app.name' is a string
} catch (RegistryTypeError $e) {
// "[typed-registry] key 'app.name' must be int, got 'Laravel'"
}
// Returns default value on missing key OR type mismatch
$port = typedEnv()->getIntOr('NONEXISTENT_PORT', 8080); // 8080
$timeout = TypedConfig::getFloatOr('cache.timeout', 3.0); // 3.0
// config/app.php
return [
'name' => typedEnv()->getStringOr('APP_NAME', 'Laravel'),
'env' => typedEnv()->getStringOr('APP_ENV', 'production'),
'debug' => typedEnv()->getBoolOr('APP_DEBUG', false),
'url' => typedEnv()->getStringOr('APP_URL', 'http://localhost'),
'timezone' => 'UTC',
'locale' => typedEnv()->getStringOr('APP_LOCALE', 'en'),
'providers' => [
// Service providers...
],
];
// app/Http/Controllers/DashboardController.php
use TypedRegistry\Laravel\Facades\TypedConfig;
class DashboardController extends Controller
{
public function index()
{
$appName = TypedConfig::getString('app.name');
$isDebug = TypedConfig::getBool('app.debug');
$locale = TypedConfig::getString('app.locale');
return view('dashboard', compact('appName', 'isDebug', 'locale'));
}
}
/** @var int $port */
$port = TypedConfig::getInt('app.port'); // PHPStan knows this is int
/** @var list<string> $hosts */
$hosts = TypedConfig::getStringList('app.hosts'); // PHPStan knows the shape