smpita/configas
Composer 安装命令:
composer require smpita/configas
包简介
Typed Config Resolver for Laravel
README 文档
README
Typed Config Resolver for Laravel
Don't settle for mixed signatures from config() when you can enforce types.
Table of Contents
This package wraps Smpita/TypeAs, a generalized PHP library useful on any PHP project employing static analysis or tight type enforcement.
Quick Start
Installation
Install the package via composer:
composer require smpita/configas
See SIGNATURES for the full list of methods and signatures.
Resolving types
use Smpita\ConfigAs\ConfigAs; // Throws \Smpita\ConfigAs\ConfigAsResolutionException if 'config.key' can't resolve to the type. $array = ConfigAs::array('config.key'); $bool = ConfigAs::bool('config.key'); $class = ConfigAs::class(Expected::class, 'config.key'); $float = ConfigAs::float('config.key'); $int = ConfigAs::int('config.key'); $string = ConfigAs::string('config.key'); // Returns null if 'config.key' can't resolve to the type. $nullableArray = ConfigAs::nullableArray('config.key'); $nullableBool = ConfigAs::nullableBool('config.key'); $nullableClass = ConfigAs::nullableClass(Expected::class, 'config.key'); $nullableFloat = ConfigAs::nullableFloat('config.key'); $nullableInt = ConfigAs::nullableInt('config.key'); $nullableString = ConfigAs::nullableString('config.key');
To suppress throwing exceptions, provide a default.
use Smpita\ConfigAs\ConfigAs; // Returns the default if passed null, or if 'config.key' can't resolve to the type. $array = ConfigAs::array('config.key', []); $bool = ConfigAs::bool('config.key', false); $class = ConfigAs::class(Expected::class, 'config.key', new StdClass()); $float = ConfigAs::float('config.key', 0.0); $int = ConfigAs::int('config.key', 0); $string = ConfigAs::string('config.key', ''); // Nullable types can specify defaults. $nullableArray = ConfigAs::nullableArray('config.key', []); $nullableBool = ConfigAs::nullableBool('config.key', false); $nullableClass = ConfigAs::nullableClass(Expected::class, 'config.key', new StdClass()); $nullableFloat = ConfigAs::nullableFloat('config.key', 0.0); $nullableInt = ConfigAs::nullableInt('config.key', 0); $nullableString = ConfigAs::nullableString('config.key', '');
Cache
To keep things performant, types are validated once and results are cached in static arrays for the lifetime of the request.
Cache Invalidation
You can flush all caches or invalidate any given key.
use Smpita\ConfigAs\ConfigAs; // Invalidate all caches ConfigAs::flush(); // Invalidate all of a type ConfigAs::flushArrays(); ConfigAs::flushBools(); ConfigAs::flushClasses(); ConfigAs::flushInts(); ConfigAs::flushStrings(); // Invalidate a specific key ConfigAs::forgetArray('config.key'); ConfigAs::forgetBool('config.key'); ConfigAs::forgetClass(Expected::class, 'config.key'); ConfigAs::forgetFloat('config.key'); ConfigAs::forgetInt('config.key'); ConfigAs::forgetString('config.key'); ConfigAs::forgetNullableArray('config.key'); ConfigAs::forgetNullableBool('config.key'); ConfigAs::forgetNullableClass(Expected::class, 'config.key'); ConfigAs::forgetNullableFloat('config.key'); ConfigAs::forgetNullableInt('config.key'); ConfigAs::forgetNullableString('config.key');
Fresh values
To bypass the cache, you may use the fresh methods that are available for each type.
This method bypasses the cache entirely. Returns from these methods will not be cached.
To update a cache, invalidate the config.key, then use a standard resolver.
use Smpita\ConfigAs\ConfigAs; $array = ConfigAs::freshArray('config.key'); $bool = ConfigAs::freshBool('config.key'); $class = ConfigAs::freshClass(Expected::class, 'config.key'); $float = ConfigAs::freshFloat('config.key'); $int = ConfigAs::freshInt('config.key'); $string = ConfigAs::freshString('config.key'); $nullableArray = ConfigAs::freshNullableArray('config.key'); $nullableBool = ConfigAs::freshNullableBool('config.key'); $nullableClass = ConfigAs::freshNullableClass(Expected::class, 'config.key'); $nullableFloat = ConfigAs::freshNullableFloat('config.key'); $nullableInt = ConfigAs::freshNullableInt('config.key'); $nullableString = ConfigAs::freshNullableString('config.key');
Helpers
There are helper methods located in the Smpita\ConfigAs namespace.
use function Smpita\ConfigAs\configArray; use function Smpita\ConfigAs\configBool; use function Smpita\ConfigAs\configClass; use function Smpita\ConfigAs\configFloat; use function Smpita\ConfigAs\configInt; use function Smpita\ConfigAs\configString; use function Smpita\ConfigAs\configNullableArray; use function Smpita\ConfigAs\configNullableBool; use function Smpita\ConfigAs\configNullableClass; use function Smpita\ConfigAs\configNullableFloat; use function Smpita\ConfigAs\configNullableInt; use function Smpita\ConfigAs\configNullableString; $array = configArray('config.key'); $bool = configBool('config.key'); $class = configClass(Expected::class, 'config.key'); $float = configFloat('config.key'); $int = configInt('config.key'); $string = configString('config.key'); $nullableArray = configNullableArray('config.key'); $nullableBool = configNullableBool('config.key'); $nullableClass = configNullableClass(Expected::class, 'config.key'); $nullableFloat = configNullableFloat('config.key'); $nullableInt = configNullableInt('config.key'); $nullableString = configNullableString('config.key');
Additionally:
use function Smpita\ConfigAs\configAs; $configAs = configAs(); $array = $configAs->array('config.key'); $bool = $configAs->bool('config.key'); $class = $configAs->class(Expected::class, 'config.key'); $float = $configAs->float('config.key'); $int = $configAs->int('config.key'); $string = $configAs->string('config.key'); $nullableArray = $configAs->nullableArray('config.key'); $nullableBool = $configAs->nullableBool('config.key'); $nullableClass = $configAs->nullableClass(Expected::class, 'config.key'); $nullableFloat = $configAs->nullableFloat('config.key'); $nullableInt = $configAs->nullableInt('config.key'); $nullableString = $configAs->nullableString('config.key');
Custom Resolvers
SIGNATURES#resolver-registration
ConfigAs methods will pass custom resolves to the underlying Smpita\TypeAs library to enable you to use your own custom resolvers. For creation, global registration, and full instructions, see the library docs.
Single use
$string = \Smpita\ConfigAs::string('config.key', null, new CustomStringResolver());
Deprecations
Testing
composer test
Changelog
Please see RELEASES 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.
smpita/configas 适用场景与选型建议
smpita/configas 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 27.74k 次下载、GitHub Stars 达 1, 最近一次更新时间为 2024 年 02 月 01 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「laravel」 「smpita」 「configas」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 smpita/configas 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 smpita/configas 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 smpita/configas 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Guaranteed type control for PHP
Typed Container Resolver for Laravel
Alfabank REST API integration
Laravel package for Accurate Online API integration.
Shared RCX Laravel DataTables UI and configuration helpers.
Boot a Laravel project on any machine with one command: app:serve installs missing tools (PHP, Node, Composer, Herd, Docker), creates .env, sets up the database, runs migrations, builds assets, starts a queue worker and serves via Herd, Sail or artisan serve; app:down cleanly stops everything it sta
统计信息
- 总下载量: 27.74k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 1
- 点击次数: 13
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2024-02-01