monamoxie/vocab-mapper
Composer 安装命令:
composer require monamoxie/vocab-mapper
包简介
Supercharge your application by providing a seamless and personalized experience. Vocab Mapper ensures that users across different regions or domains can easily interact with and navigate your application, even if they use different vocabulary for the same concepts
README 文档
README
VOCAB MAPPER
A Laravel package for both multi-tenant and single tenant architectures.
Vocab Mapper provides your tenants or users a personalized experience, by defining mappings between standard terminology (The Default Vocab) and user-preferred terminology (The Custom Vocab), especially when both terminologies are referring to the same core concept.
INSTALLATION
composer require monamoxie/vocab-mapper
VENDOR PUBLISHING
Vocab Mapper includes some default configurations out of the box, but it's recommended you publish and customize these settings to suit your needs.
PUBLISHING THE CONFIG
php artisan vendor:publish --tag=vocab-mapper-config
EXPLORING THE VOCAB CONFIG
MIGRATION SUB DIR
migration_sub_dir => null
This is the subdirectory where Vocab Mapper's migration files will be published. The default value is null, suitable for most single-tenant applications.
For multi-tenant systems needing a different subdirectory for tenant-specific migrations, you can simply set the subdirectory value here. For example, the stancl/tenancy multi-tenant package requires tenant migrations be kept in database/migrations/tenant.
If that is the case, then you should set the value of migration_sub_dir => '/tenant'
ENTITY MODEL
An entity in your application is a unique tenant or user who whiches to use a custom vocabulary for a core concept, different from the standard term.
Single-tenant systems
In single-tenant systems, this is typically the model class representing your users. For example, \App\Models\User::class, or whichever model namespace manages and identifies your users.
| key | type | Default Value |
|---|---|---|
entity_model |
string,array |
\App\Models\User::class |
Multi-tenant systems
In multi-tenant systems, this is typically the model class representing the tenant. For example, \App\Models\Tenant::class, or whichever model namespace manages and identifies your tenants.
| key | type | Recommended |
|---|---|---|
user_entity |
string,array |
\App\Models\Tenant::class |
Please refer to the section on BREAK DOWN OF TERMS for a more information on this.
ENTITY HAS UUID
Enable this option if the entity model utilizes a UUID as its primary key.
| key | type | Default Value |
|---|---|---|
user_entity_has_uuid |
boolean |
false |
PUBLISHING THE MIGRATION
php artisan vendor:publish --tag=vocab-mapper-migration
This action will publish the migration files based on the provided configuration in migration_sub_dir. If no specific path is set, the files will be published to Laravel's default migration directory.
MIGRATION
Run the artisan command responsible for handling your Database migrations.
Single-tenant systems
php artisan migrate
Multi-tenant systems
The command to execute depends on your setup or package configuration. For example, projects utilizing the stancl/tenancy package would execute:
php artisan tenants:migrate
MAKING YOUR ELOQUENT MODELS VOCABLE
A vocab is a name, identity or a domain driven terminology used by default across your application. In an educational project, it could be the word SUBJECTS for storing all the subjects taught in a school. In an automotive project, it could be the word VEHICLES for identifying all vehicles available in your system.
To make any of your models VOCABLE, (which is the ability to maintain and be represented in a different term across multiple tenants or users), you should include the HasVocab trait to the model
<?php
namespace App\Models;
use Monamoxie\VocabMapper\Traits\HasVocab;
use Illuminate\Database\Eloquent\Model;
class MyModel extends Model
{
use HasVocab;
}
That's all.
With this, this model, MyModel, should now be ready to accept and maintain different identites across each of your tenants or users.
CREATING A DEFAULT VOCAB
From any part of your application, you can call
<?php
use App\Models\MyModel;
(new MyModel)->createVocab(name: 'Vocab Name');
The name argument is optional. If not provided, the default vocab (term, identity) for this model would the table name.
MAPPING A VOCAB TO AN ENTITY
From any part of your application, you can call
<?php
use App\Models\MyModel;
(new MyModel)->mapVocabTo($entity, $customName);
where:
$entity = An instance of your entity model, as defined in the config, entity_model
$customName = The custom terminology set or choosen by this $entity.
This entity could be the currently logged in tenant or currently logged in user. It doesn't matter.
You must ensure the instance of this entity matches what you defined in the config, entity_model.
If there is a mis-match between the $entity instance and whatever was set in the config, entity_model, this will thrown an exception of type:
Monamoxie\VocabMapper\Exceptions\InvalidEntityModelException
THE DUALITY OF mapVocabTo
If you attempt to map a vocab to an entity and Vocab Mapper detects that the vocab doesn't exist, it will silently create a new vocab and proceed with the mapping.
GETTING THE VOCAB MAPPED TO AN ENTITY
From any part of your application, you can call
<?php
use App\Models\MyModel;
(new MyModel)->getVocabFor($entity);
where:
$entity is an instance of your entity model, as defined in the config, entity_model
THE VOCAB COLLECTOR
Alternatively, you could also take advantage of the Vocab Collector for getting the vocab mapped to a user:
<?php
use App\Models\MyModel;
use Monamoxie\VocabMapper\Facades\VocabCollector;
VocabCollector::getFor($entity, MyModel::class)
where:
$entity is an instance of your entity model.
This could be an instance of \App\Models\Tenant, or \App\Models\User or whatever. As long as it the namespace matches what you defined in the `user_model` config
THE DIFFERENCE BETWEEN getVocabFor and VocabCollector::getFor
(new MyModel)->getVocabFor($entity) will return an eloquent model, which is an instance of Monamoxie\VocabMapper\VocabMapper
You may want to use this to inspect the model or for any eloquent operations.
Alternatively, VocabCollector::getFor returns an instance of Monamoxie\VocabMapper\Data\VocabResponse, which is ideal for displaying this data in the client-facing part of your application.
The VocabResponse provides the custom vocabulary defined by the entity, including its singular and plural forms.
For instance, you could have a setup like this in your controller:
<?php
use App\Models\MyModel;
use Monamoxie\VocabMapper\Facades\VocabCollector;
public function index()
{
$entity = Tenant::where('id', auth()->user()->id)->first();
return view('academy.foo.bar', [
'vocab' => VocabCollector::getFor($entity, MyModel::class),
'data' => 'some random data',
'data2' => 'some other random data'
]);
}
And if it's an API driven setup, you could also have something like this. It doesn't matter.
<?php
use Illuminate\Support\Facades\Response;
use App\Models\MyModel;
use Monamoxie\VocabMapper\Facades\VocabCollector;
public function get()
{
$entity = Tenant::where('id', auth()->user()->id)->first();
return Response::json([
'message' => '',
'data' => 'SOME DATA',
'vocab' => VocabCollector::getFor($entity, MyModel::class),
], 200);
}
You are also not limited to working with just the instance of Monamoxie\VocabMapper\Data\VocabResponse.
You can convert the response into an array by calling the toArray() method on it.
VocabCollector::getFor($entity, MyModel::class)->toArray()
BREAK DOWN OF TERMS
Entity
An entity in your application is an authenticated and unique tenant or user who uses a custom vocabulary for a core concept, different from the standard term.
Vocab
The name of the concept you provide, as known or defined by you. By default, the vocab is the model's table name.
Vocab Mapper
A mapping of custom names to existing vocabs. It is advisable to store custom names in their plural form. For instance, use "subjects" instead of "subject." The VocabResponse will handle the conversion between singular and plural forms in its response.
Handler
The class namespace responsible for managing this vocab, typically the model class for that vocab, but it can also be a non-model class.
TESTING
vendor/bin/phpunit
For console-based coverage tests:
vendor/bin/phpunit --coverage-text
OR
For html-based coverage tests:
vendor/bin/phpunit --coverage-html test-coverage
CONTRIBUTING
vendor/bin/testbench workbench:install
LICENSE
The MIT License (MIT). Please see License File for more information.
monamoxie/vocab-mapper 适用场景与选型建议
monamoxie/vocab-mapper 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 351 次下载、GitHub Stars 达 0, 最近一次更新时间为 2024 年 05 月 21 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「package」 「model」 「mapper」 「laravel」 「domains」 「vocabulary」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 monamoxie/vocab-mapper 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 monamoxie/vocab-mapper 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 monamoxie/vocab-mapper 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
data-mapper php library
Convention-based Alternative ORM
A package for automatically encrypting and decrypting Eloquent attributes in Laravel 5.5+, based on configuration settings.
Interfaces for web resource models and services to retrieve and create them
Laravel 5 - Repositories to the database layer
Mapper for accessing resources in KWCMS
统计信息
- 总下载量: 351
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 1
- 点击次数: 21
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2024-05-21
