grizzlyware/model-swapper
最新稳定版本:v2.0.1
Composer 安装命令:
composer require grizzlyware/model-swapper
包简介
Swap vendor models out for your own implementations, on the fly
README 文档
README
This package can be used to use your own applications implementation of a model which has been provided by a vendor package. This can be useful to manipulate behaviour on a model, access protected properties or use classes in your own namespace.
Extending a vendor's class is already possible, but you cannot change vendors invocation of the class. All references will still point to the original class name and namespace.
This package will change the actual class that is returned from Eloquent query builders, including all(), get(), first(), findOrFail() etc.
For example:
// Code which may exist out of your control, in a vendor's package for example \Vendor\Package\Models\CountryCode::firstOrFail() // \Vendor\Package\Models\CountryCode is returned /** * After using Model Swapper */ \Vendor\Package\Models\CountryCode::firstOrFail() // \App\Models\CountryCode is returned
Installation
You can install the package via composer:
composer require grizzlyware/model-swapper
Usage
use Grizzlyware\ModelSwapper\Facades\ModelSwapper; use Grizzlyware\ModelSwapper\Traits\IsReplacementModel; // Add the IsReplacementModel trait to your replacement class class YourApplicationsClass extends ClassFromVendorPackage { use IsReplacementModel; } // Swap the model out - this would usually be in your AppServiceProvider, in the boot method. ModelSwapper::swap( ClassFromVendorPackage::class, YourApplicationsClass::class ); // From now on, all queries for ClassFromVendorPackage will return instances of YourApplicationsClass
Advanced usage
use Grizzlyware\ModelSwapper\Contracts\ModelSwapperServiceInterface; // Or typehint from the container public function __construct(ModelSwapperServiceInterface::class $modelSwapper) // Or resolve from the container resolve(ModelSwapperServiceInterface::class)->swap( ClassFromVendorPackage::class, YourApplicationsClass::class ); // You can access the original, non-replaced model query like so: YourApplicationsClass::replacedModelQuery()->firstOrFail();
Testing
composer test
Changelog
Please see CHANGELOG for more information on what has changed recently.
Security Vulnerabilities
Please notify contact@grizzlyware.com about any security vulnerabilities, do not open an issue for them.
Credits
License
The MIT License (MIT). Please see License File for more information.
统计信息
- 总下载量: 907
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 3
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2022-12-11