定制 fourdotsix/tenancy-mapping 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

fourdotsix/tenancy-mapping

Composer 安装命令:

composer require fourdotsix/tenancy-mapping

包简介

Tenant data handling for stancl/tenancy

README 文档

README

fourdotsix.com Logo
four●six // tenancy-mapping

Tenant data handling (mapping)

Latest Version on Packagist GitHub Tests Action Status GitHub Code Style Action Status Total Downloads

Tenant specific data handling for stancl/tenancy.

What?

Mappings are the key-value pairs that can be used to get non-sensitive data while maintaining the tenant context. These are compiled and stored in an in-memory database, ex: Redis, giving faster data access. On a base level, there are two types of mappings:

  • Descriptors: These are useful when different tenant types refers to some part of the application differently.

    For example: An LMS SaaS application, can serve both businesses and educational institutions. An educational institution might refer to it's learners as Students while businesses might refer them as Employees. In such cases descriptor comes handy for dynamic descriptors. The descriptor mappings would be as such:

    ---
    # file: university.yml
    name: Institution ORG mappings
    description: Mappings for tenant university.example.co
    mappings:
        learner: student
        institute: university

    This can be accessed as such:

        descriptor('learner')->title(); // Student
        descriptor(key: 'institute', default: 'institute'); // university
        descriptor(key: 'institute', default: 'institute', i18n: true); // महाविद्यालय
  • Settings: As the name suggests, these can be used for non-sensitive tenant specific settings. It provides faster access to settings data using key-value pairs and keeping these in YAML files can provide an audit trail for changes in GIT. Settings are merged by default, i.e., there can be a separate tenant's YAML file created with overrides and compilation will merge the settings in generic settings YAML file during compilation. This can be disabled in config file, if needed.

Why?

When creating a SaaS application, the biggest challenge is customization according to the tenant. Not all organizations are the same, therefore, tailoring specific software features can become impossible while onboarding new tenants and scalability can really take a hit.

Installation

  1. Install the package via composer:

    composer require fourdotsix/tenancy-mapping
  2. Publish the config file with:

    php artisan vendor:publish --tag="tenancy-mapping-config"
  3. Publish the migrations with:

    php artisan vendor:publish --tag="tenancy-mapping-migrations"
  4. Publish the mappings folder with:

    php artisan vendor:publish --tag="tenancy-mapping-mappings"
  5. Add the Tenancy Bootstrapper in your config/tenancy.php:

    'bootstrappers' => [
        ...
        // Integration bootstrappers
        ...
        Fourdotsix\TenancyMapping\Bootstrappers\MappingTenancyBootstrapper::class,
    ],
  6. (optional|recommended) Set TenantType enum: This helps in creating a consistent and safe way of defining tenant types. Although it's optional, it's still recommended.

    • Create an Enum and implement Fourdotsix\TenancyMapping\Contracts\TenantType, Ex:

      <?php
      
      namespace App\Enums;
      
      use Fourdotsix\TenancyMapping\Concerns\EnumToArray;
      use Fourdotsix\TenancyMapping\Contracts\TenantType as Contract;
      
      enum TenantType: string implements Contract
      {
          use EnumToArray;
      
          case Institute = 'institute';
          case University = 'university';
          case Business = 'business';
      }
    • Add casts in your Tenant Model:

      protected $casts = [
          ...
          'type' => TenantType::class,
          ...
      ];
  7. (optional) Overriding MappingType: Although this might not be needed in normal scenarios, but by overriding MappingType Enum you can add new mapping types as well as override Enum methods. After creating a new MappingType Enum you can directly use that in functions & Facades while fetching mappings.

    • Create an Enum that implements Fourdotsix\TenancyMapping\Contracts\MappingType and uses trait Fourdotsix\TenancyMapping\Concerns\FunctionalMappingTypes, Ex:

      <?php
      
      namespace App\Enums;
      
      use Fourdotsix\TenancyMapping\Concerns\FunctionalMappingTypes;
      use Fourdotsix\TenancyMapping\Contracts\MappingType as Contract;
      
      enum MappingType: string implements Contract
      {
          use FunctionalMappingTypes;
      
          case Descriptor = 'descriptor';
          case Settings = 'settings';
      
      }

Usage

Mappings are key-value pairs, i.e. even nested mappings are compiled into dot-notation keys. Although, still a nested mapping can have an array as a value if it's a YAML array, i.e. YAML arrays are preserved and not converted into dot-notation.

YAML Mapping Files

Mappings are saved in the mapping directory defined in the config. By default, it doesn't needs to be changed. But feel free to change it if needed. All the mapping files follow a basic structure, i.e. they all have name, description & mappings, where mappings is the object that contains all the mappings:

---
# Descriptor
name: Generic descriptor mappings
description: Descriptor Mappings for all tenant types (generic)
mappings:
  example: one
  exampleTwo: two
  ...
---
name: Settings for all tenant types (Generic)
description: These are generic settings for all the tenant types
mappings:
  fallback:
    - logo: https://example.com/images/logo.svg
    - currency: USD
  tracking:
    - active: yes
    - domain: null
  ...

Compilation

Compilation can be made using artisan command:

php artisan mappings:compile

You can force clear old compilation values using --force. These comes handy during deployments to make sure the new mappings are added and the removed mappings are cleared.

Compilations can be cleared too using artisan command:

php artisan mappings:clear

Fetching Mapping

Mappings can be fetched either by using mapping helpers or Mapping Facade.

Helper

Mappings can be fetched using helper methods. descriptor() helper returns Illuminate\Support\Stringable making it easier to manipulate strings.

use Fourdotsix\TenancyMapping\Enums\MappingType;
...
// Getting a mapping value
$mapping = mapping(MappingType::Descriptor, 'institute'); // university

// Getting a Descriptor's Value
$descriptor = descriptor(key: 'learner', default: 'learner', i18n: false); // student
$descriptor->plural()->title(); // Students

// Getting a settings map value
$setting = settings_map(key: 'fallback.logo', default: 'https://example.com/example.png'); // (string) https://example.com/images/logo.svg
$track = settings_map(key: 'tracking.active', default: false, return_type: 'bool'); // (bool) true

Facade

Facade let's you fetch mapping values as-is, without any manipulations as oppose to manipulations done by helper functions. Facade resolves \Fourdotsix\TenancyMapping\Mapping making available all the methods too.

use Fourdotsix\TenancyMapping\Facades\Mapping;
use Fourdotsix\TenancyMapping\Enums\MappingType;
...
// Fetch a specific mapping by key
$mapping = Mapping::get(type: MappingType::Descriptor, key: 'learner'); // student

// Get all the mappings for a Mapping Type
$mappings = Mapping::all(MappingType::Descriptor); // array of all the mappings

// Compile Mappings for a mapping type & tenant
Mapping::compile($tenant, MappingType::Settings);

// Clear compiled mappings for a specific mapping type
Mapping::clear(MappingType::Descriptor);

Testing 🚧

🚧 WIP

composer test

Changelog

Please see CHANGELOG 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.



From the folks at Four Dot Six (4.6)

fourdotsix/tenancy-mapping 适用场景与选型建议

fourdotsix/tenancy-mapping 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 21 次下载、GitHub Stars 达 1, 最近一次更新时间为 2025 年 11 月 08 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「laravel」 「Four Dot Six」 「tenancy-mapping」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 fourdotsix/tenancy-mapping 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 fourdotsix/tenancy-mapping 我们能提供哪些服务?
定制开发 / 二次开发

基于 fourdotsix/tenancy-mapping 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 21
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 1
  • 点击次数: 22
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 1
  • Watchers: 0
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2025-11-08