aybarsm / laravel-extended-support
为 Laravel Macroable Facades 和辅助工具提供的额外混合类
v1.0.7
2023-09-09 16:19 UTC
Requires
- php: ^8.1
- illuminate/contracts: ^10.0
- illuminate/support: ^10.0
Requires (Dev)
- laravel/pint: ^1.12
README
它包含用于内置宏可用的 facade 的自定义混合类,还可以配置动态加载外部混合类。它还包括一些自定义函数和辅助特质。
安装
您可以通过 composer 安装此包
composer require aybarsm/laravel-extended-support
您可以通过以下方式发布配置文件
php artisan vendor:publish --provider="Aybarsm\Laravel\Support\ExtendedSupportServiceProvider" --tag=config
配置混合类
您可以通过扩展类来移除或添加新的混合类到加载列表并修改具体实现。
return [ 'runtime' => [ 'replace_existing' => true, 'class_autoload' => true, 'required_trait' => 'Illuminate\Support\Traits\Macroable', 'bind_pattern' => '/@mixin\s*([^\s*]+)/', 'load' => [ Aybarsm\Laravel\Support\Mixins\StringableMixin::class, Aybarsm\Laravel\Support\Mixins\StrMixin::class, Aybarsm\Laravel\Support\Mixins\ArrMixin::class, Aybarsm\Laravel\Support\Mixins\FileMixin::class, Aybarsm\Laravel\Support\Mixins\RuleMixin::class, Aybarsm\Laravel\Support\Mixins\ApplicationMixin::class, Aybarsm\Laravel\Support\Mixins\CommandMixin::class, Aybarsm\Laravel\Support\Mixins\ProcessMixin::class, Aybarsm\Laravel\Support\Mixins\CollectionMixin::class, ], ], 'concretes' => [ 'ExtendedSupport' => Aybarsm\Laravel\Support\ExtendedSupport::class, 'Supplements' => [ 'Str' => [ 'SemVer' => Aybarsm\Laravel\Support\Supplements\Str\SemVer::class, ], 'Foundation' => [ 'Annotation' => Aybarsm\Laravel\Support\Supplements\Foundation\Annotation::class, ], ], ], ];
自定义混合类
您可以通过 artisan 命令创建任何新的混合类。命令会询问类名,并提供使用宏特质的类列表,以便您轻松选择或手动输入。
php artisan make:mixin ArrMixin
您可以通过以下方式发布占位符
php artisan vendor:publish --provider="Aybarsm\Laravel\Support\ExtendedSupportServiceProvider" --tag=stubs
或者,您可以直接创建一个混合类,并通过 @mixin 注解来识别宏类,然后将其添加到配置中以便加载。
<?php namespace App\Mixins; /** @mixin \Illuminate\Support\Arr */ class ArrMixin { public static function toObject(): \Closure { return fn (array|object $arr, int $flags = JSON_NUMERIC_CHECK | JSON_FORCE_OBJECT): object => json_decode(json_encode($arr, $flags)); } }
'runtime' => [ 'load' => [ Aybarsm\Laravel\Support\Mixins\StringableMixin::class, Aybarsm\Laravel\Support\Mixins\StrMixin::class, Aybarsm\Laravel\Support\Mixins\ArrMixin::class, Aybarsm\Laravel\Support\Mixins\FileMixin::class, Aybarsm\Laravel\Support\Mixins\RuleMixin::class, Aybarsm\Laravel\Support\Mixins\ApplicationMixin::class, Aybarsm\Laravel\Support\Mixins\CommandMixin::class, Aybarsm\Laravel\Support\Mixins\ProcessMixin::class, Aybarsm\Laravel\Support\Mixins\CollectionMixin::class, App\Mixins\ArrMixin::class, ], ], ];
补充
Str :: Semantic Version
添加了新的语义版本化辅助类。您可以直接创建一个新的 SemVer 实例,或者使用 Str::semVer() 宏函数。SemVer 类也是可宏化的。
use Aybarsm\Laravel\Support\Supplements\Str\SemVer; use Aybarsm\Laravel\Support\Enums\SemVerScope; // Regardless of multiple occurrences, the function always captures the first occurrence of \d+\.\d+\.\d+ $version = 'ver9.2.78beta-1.0.6'; $semVer = new SemVer($version); $semVer = Str::semVer($version); // Get scope of the Semantic Version: dump($semVer->getScope(SemVerScope::MINOR)); // Output: "2" dump($semVer->getScope(SemVerScope::MINOR, $asInteger = true)); // Output: 2 // More importantly you can easily calculate the next scopes of the Semantic Version. dump($semVer->value()); // Output: "9.2.78" $semVer = $semVer->next(SemVerScope::MINOR); dump($semVer->value()); // Output: "9.3.0" $semVer = $semVer->next(SemVerScope::PATCH); dump($semVer->value()); // Output: "9.3.1" $semVer = $semVer->next(SemVerScope::MAJOR); dump($semVer->value()); // Output: "10.0.0" // You can access the original Semantic Version by: dump($semVer->getOriginal()); // Output: "9.2.78" // If you would like to have an output with t original version structure dump($semVer->value($asOriginal = true)); // Output: "ver10.0.0beta-1.0.6" // Lastly, static function and Str macros have been implemented to validate Semantic Version string. SemVer::validate('9.2.78'); // Output: true // OR Str::isSemVer('9.2'); // Output: false
辅助特质
EnumHelper
use Aybarsm\Laravel\Support\Traits\EnumHelper; enum ProcessReturnType: int { use EnumHelper; case STATUS = 0; case SUCCESSFUL = 1; case FAILED = 2; case EXIT_CODE = 3; case OUTPUT = 4; case ERROR_OUTPUT = 5; case INSTANCE = 6; case ALL_OUTPUT = 7; } $enum = ProcessReturnType::tryFrom(ProcessReturnType::byName('EXIT_CODE')); //returns designated enum as static