apajo / symfony-multi-tenancy-bundle
Symfony 多租户组件
0.4.6
2024-09-23 19:41 UTC
Requires
- php: ^8.2
- doctrine/annotations: ^2.0
- doctrine/doctrine-bundle: ^2.12
- doctrine/doctrine-migrations-bundle: ^3.3
- knplabs/gaufrette: ^0.11.1
- symfony/config: ^6.4|^7.1
- symfony/dependency-injection: ^6.4|^7.1
- symfony/filesystem: ^6.4|^7.1
- symfony/mailer: ^6.4|^7.1
- symfony/orm-pack: ^2.4
- symfony/runtime: ^6.4|^7.1
- symfony/security-bundle: ^6.4|^7.1
Requires (Dev)
- phpunit/phpunit: ^9.5
- symfony/phpunit-bridge: ^5.4 | ^v6.4.0|^7.0
README
描述
在 Symfony 中有许多提供多租户的包,但它们都集中在数据库上。这个包的目标是为管理系统中的任何类型的配置提供一种方法。
该组件旨在在更高层次上提供多租户支持。它提供了一种根据租户(数据库、媒体提供者、邮件发送器等)动态更改系统配置的方法。
它还捆绑了管理每个租户迁移的方法。
该包的开发处于早期阶段,欢迎任何反馈。
要求
- Symfony 6.4 / 7.1
- Doctrine Bundle 2.12
- Doctrine Migrations Bundle 3.3
- Symfony Security Bundle
- PHP 8.2
安装
composer require apajo/symfony-multi-tenancy-bundle
配置
doctrine.yml
您需要 2 个连接和实体管理器
doctrine: dbal: default_connection: default connections: default: url: '%env(DEFAULT_DATABASE_URL)%' driver: pdo_mysql charset: utf8 server_version: '8' tenant: url: '%env(TENANT_DATABASE_URL)%' driver: pdo_mysql charset: utf8 server_version: '8' orm: default_entity_manager: default auto_generate_proxy_classes: true entity_managers: default: connection: default naming_strategy: doctrine.orm.naming_strategy.underscore_number_aware tenant: connection: tenant naming_strategy: doctrine.orm.naming_strategy.underscore_number_aware mappings:
在此情况下,它们分别命名为 default
和 tenant
,但您可以按自己的意愿命名。
注意!第三方包可能需要存在
default
连接,因此您可能希望保留default
名称。
default
连接和实体管理器对所有单个租户是通用的。租户特定的 tenant
连接和实体管理器。
doctrine_migrations.yml
doctrine_migrations: migrations_paths: 'App\Migrations\Default': '%kernel.project_dir%/migrations/default' 'App\Migrations\Tenant': '%kernel.project_dir%/migrations/tenant'
配置
apajo_multi_tenancy: adapters: # Adapters dynamically change the system configuration for selected tenant - aPajo\MultiTenancyBundle\Adapter\Database\DatabaseAdapter - aPajo\MultiTenancyBundle\Adapter\Filesystem\FilesystemAdapter - aPajo\MultiTenancyBundle\Adapter\Mailer\MailerAdapter tenant: # Tenant (entity) configuration class: App\Entity\Tenant # Must implement TenantInterface identifier: key # Identifier column name (must be unique field) entity_manager: default # Tenant entity manager name resolvers: # Resolvers resolve the tenant based on the request - aPajo\MultiTenancyBundle\Service\Resolver\HostBasedResolver migrations: # Migration configuration namespace: App\Migrations\Tenant entity_manager: tenant
适配器
适配器负责根据租户表值在运行时进行动态配置更改。
有关(内置)适配器的更多信息,请参阅适配器目录
解析器
解析器负责解析当前租户。
有关(内置)解析器的更多信息,请参阅解析器目录
数据库迁移
创建迁移(或 diff)
php bin/console tenants:migrations:diff
迁移租户
php bin/console tenants:migrations:migrate:all
迁移单个租户
# TODO: Implement # php bin/console tenants:migrations:migrate [tenant_id]
示例
切换/选择租户
use aPajo\MultiTenancyBundle\Service\EnvironmentProvider; use aPajo\MultiTenancyBundle\Entity\TenantInterface; use aPajo\MultiTenancyBundle\Event\TenantSelectEvent; use Symfony\Component\EventDispatcher\EventDispatcherInterface class Tenant implements TenantInterface { // ... } class MyTenantService { public function __construct ( private EnvironmentProvider $environmentProvider, private EventDispatcherInterface $dispatcher, ) { } /** * Use the EnvironmentProvider to select a different tenant */ public function select () { $tenant = new Tenant(); $environmentProvider->select($tenant); // Now the system is configured based on the tenant } /** * You can also dispatch an event to select a new tenant */ public function alternativeSelect () { $tenant = new Tenant(); $event = new TenantSelectEvent($tenant); $this->dispatcher->dispatch($event); } }
遍历所有租户环境
use aPajo\MultiTenancyBundle\Service\EnvironmentProvider; use aPajo\MultiTenancyBundle\Entity\TenantInterface; class MyTenantService { public function __construct ( private EnvironmentProvider $environmentProvider, ) { $environmentProvider->forAll(function (TenantInterface $tenant) { // Each iteration will have tenant specific configuration/environment }); } }
问题
请在 GitHub 问题下报告问题
已知问题
- 重置到默认/初始租户无效
贡献
请随时贡献