inmanturbo / laravel-delegator
这是我创建的包:laravel-delegator
Requires
- php: ^8.1
- illuminate/contracts: ^10.0
- laravel/octane: ^1.5
- spatie/laravel-package-tools: ^1.14.0
Requires (Dev)
- laravel/jetstream: ^3.0
- laravel/pint: ^1.0
- nunomaduro/collision: ^7.9
- nunomaduro/larastan: ^2.0.1
- orchestra/testbench: ^8.0
- pestphp/pest: ^2.0
- pestphp/pest-plugin-arch: ^2.0
- pestphp/pest-plugin-laravel: ^2.0
- phpstan/extension-installer: ^1.1
- phpstan/phpstan-deprecation-rules: ^1.0
- phpstan/phpstan-phpunit: ^1.0
- spatie/docker: ^1.10
- spatie/valuestore: ^1.3
This package is auto-updated.
Last update: 2024-03-19 14:39:35 UTC
README
与 spatie/laravel-multitenancy 类似,但此包不限制您只能使用单个模型类或“租户”。
您可以有一个数据库模型,如 App\Models\TeamDatabase
或 App\Models\AppDatabase
,当请求改变时动态设置一些配置值(以及任何其他操作),例如。您还可以在切换用户或团队时执行某些操作。
支持我们
安装
您可以通过 composer 安装此包
composer require inmanturbo/laravel-delegator
您可以使用以下命令发布配置文件:
php artisan vendor:publish --tag="delegator-config"
这是已发布配置文件的内容
return [ /* * The connection name to reach the delegator database */ 'delegator_database_connection_name' => null, 'candidates' => [ // 'team' => [ // /* // * This class is responsible for determining which candidate should be current // * for the given request. // * // * This class must implement the `Inmanturbo\Delegator\CandidateFinder\Contracts\CandidateFinder` interface. // * // */ // 'candidate_finder' => null, // /* // * These fields are used by candidates:artisan command to match one or more tenant // */ // 'candidate_artisan_search_fields' => [ // 'id', // ], // /* // * These tasks will be performed when switching candidates. // * // * A valid task is any class that implements the `Inmanturbo\Delegator\Tasks\Contractstractstracts\SwitchCandidateTask` interface. // */ // 'switch_candidate_tasks' => [ // \Inmanturbo\Delegator\Tasks\SwitchCandidateConfigTask::class, // ], // /* // * This class is the model used for storing configuration on candidates. // * // * It must implemement the `Inmanturbo\Delegator\Models\Contracts\CandidateModel` interface. // */ // 'model' => \App\Models\Team::class, // /* // * The connection name to reach the candidate database. // * // * Set to `null` to use the default connection. // */ // 'candidate_database_connection_name' => null, // /* // * This key will be used to bind the current candidate in the container. // */ // 'current_candidate_container_key' => 'currentTeam', // /* // * You can customize some of the behavior of this package by using your own custom action. // * Your custom action should always extend the default one. // */ // 'actions' => [ // 'make_current_action' => \Inmanturbo\Delegator\Actions\MakeCandidateCurrentAction::class, // 'forget_current_action' => \Inmanturbo\Delegator\Actions\ForgetCandidateCurrentAction::class, // 'migrate_action' => \Inmanturbo\Delegator\Actions\MigrateCandidateAction::class, // ], // ], ], ];
使用方法
首先发布配置,然后在 candidates
数组中取消注释第一个 candidate
,并填写您的正确值。
您可以使用任何实现了 Inmanturbo\Delegator\Models\Contracts\CandidateModel
接口的模型。
您可以使用 traits Inmanturbo\Delegator\Models\Concerns\HasCandidateMethods
,或者编写自己的方法来满足接口。
设置候选人为当前
$candidate = App\Models\Team::first(); // <-- must be a configured candidate `model` which implements $candidate->makeCurrent();
上述操作将执行 config('delegator.candidates.team.actions.make_current_action')
,这将调用 makeCurrent()
在 config('delegator.candidates.team.actions.switch_candidate_tasks')
下列出的所有任务上。
您可以使用这些任务来更改或设置配置键和值,启动割草机等。
在相同类的另一个模型实例上调用 makeCurrent()
之前,将首先执行 config('delegator.candidates.team.actions.forget_current_action')
,这将调用 forgetCurrent()
在所有相同的任务上。
您可以通过将它们的配置添加到 delegator
配置文件中的 candidates
数组中,以这种方式使用任意多的模型类。
每个类将单独跟踪,因此您可以有多个不同类型的当前模型,但每次只能有一个类型的当前模型。
测试
composer test
变更日志
请参阅 CHANGELOG 了解最近更改的更多信息。
贡献
请参阅 CONTRIBUTING 了解详细信息。
安全漏洞
请参阅 我们的安全策略 了解如何报告安全漏洞。
致谢
- inmanturbo
- 所有贡献者
- 此包深受 spatie/laravel-multitenancy 的启发
许可证
MIT 许可证 (MIT)。请参阅 许可证文件 了解更多信息。