smpita / configas
Typed Config Resolver for Laravel
Fund package maintenance!
smpita
Installs: 8 012
Dependents: 0
Suggesters: 0
Security: 0
Stars: 1
Watchers: 1
Forks: 0
Open Issues: 1
Requires
- php: ^8.1
- illuminate/contracts: ^10.0|^11.0
- smpita/typeas: ^3.1
Requires (Dev)
- laravel/pint: ^1.0
- orchestra/testbench: ^8.4|^9.0
- pestphp/pest: ^2.20
This package is auto-updated.
Last update: 2024-10-27 04:38:42 UTC
README
Do you use Laravel and fight the mixed signatures of config()
when performing static analysis?
Smpita/ConfigAs types your config calls for you.
This package wraps Smpita/TypeAs, a generalized PHP library useful on any PHP project employing static analysis or tight type enforcement.
Installation
You can install the package via composer:
composer require smpita/configas
Usage
Please see SIGNATURES for the list of current methods and signatures.
General Usage
Pass a key
and it will throw a ConfigAsResolutionException
if the key
isn't of the given type.
use Smpita\ConfigAs\ConfigAs; $typed = ConfigAs::int('config.key');
If you want to suppress throwing exceptions, provide a default.
use Smpita\ConfigAs\ConfigAs; $typed = ConfigAs::int('config.key', 123);
The Class Method
class()
has a slightly different signature because you need to specify the class you are expecting.
use Smpita\ConfigAs\ConfigAs; $typed = ConfigAs::class(Target::class, 'config.key');
You can still provide a default.
use Smpita\ConfigAs\ConfigAs; $typed = ConfigAs::class(Target::class, 'config.key', new Target('default'));
Nullables
If you would prefer to receive null
instead of having an exception thrown, each type method has a nullable counterpart.
use Smpita\ConfigAs\ConfigAs; ConfigAs::nullableString('config.key') === null; // true
Cache
To keep things performant, types are only validated once and results are cached in static arrays for the lifetime of the request.
To guarantee a fresh value, you may use the fresh
methods that are available for each type.
use Smpita\ConfigAs\ConfigAs; $typed = ConfigAs::freshString('config.key');
Forgetting
For each type, you can forget any given cached value.
use Smpita\ConfigAs\ConfigAs; ConfigAs::forgetFloat('config.key');
You can flush the cache of a type.
use Smpita\ConfigAs\ConfigAs; ConfigAs::flushFloats();
You can flush all keys.
use Smpita\ConfigAs\ConfigAs; ConfigAs::flush();
Resolvers
SIGNATURES#resolver-registration
You can leverage the included Smpita\TypeAs library to create your own custom resolvers. For creation, global registration, and full instructions, see the library docs.
Single use
$typed = Smpita\ConfigAs::string('config.key', null, new CustomStringResolver);
Helpers
There is a configAs()
helper method located in the Smpita\ConfigAs
namespace.
use function Smpita\ConfigAs\configAs; $configAs = configAs(); $string = $configAs->string('config.string.key'); $array = $configAs->array('config.array.key');
Resolver methods have an associated helper method located in the Smpita\ConfigAs
namespace.
The helper method names follow the ConfigAs
method names, but are prepended by config
and are camelCased.
use function Smpita\ConfigAs\configString; $typed = configString('config.key');
Deprecations
Testing
composer test
Changelog
Please see CHANGELOG for more information on what has changed recently.
Contributing
Please see CONTRIBUTING for details.
Security Vulnerabilities
Please review our security policy on how to report security vulnerabilities.
Credits
License
The MIT License (MIT). Please see License File for more information.