Download the PHP package monkeyscloud/monkeyslegion-tenancy without Composer
On this page you can find all versions of the php package monkeyscloud/monkeyslegion-tenancy. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download monkeyscloud/monkeyslegion-tenancy
More information about monkeyscloud/monkeyslegion-tenancy
Files in monkeyscloud/monkeyslegion-tenancy
Package monkeyslegion-tenancy
Short Description Enterprise multi-tenant patterns for MonkeysLegion: single-DB, schema-per-tenant, database-per-tenant isolation with domain resolution and tenant-aware infrastructure
License MIT
Homepage https://monkeyslegion.com/docs/packages/tenancy
Informations about the package monkeyslegion-tenancy
MonkeysLegion Tenancy v2
Enterprise multi-tenant patterns for MonkeysLegion: single-DB with tenant_id scoping, schema-per-tenant, and database-per-tenant. Domain/subdomain identification, tenant-aware cache/queue/storage. Essential for B2B SaaS. Ground-up build for PHP 8.4 with property hooks, backed enums, and zero magic.
Features
| Feature | Status |
|---|---|
| Three Isolation Modes | Single-DB (tenant_id scoping), Schema-per-Tenant, Database-per-Tenant |
| Five Resolution Strategies | Domain, Subdomain, HTTP Header, URL Path, Query Parameter + Chain |
| Tenant Context | Static per-request holder with scoped run() execution |
| Entity Scoping | #[BelongsToTenant] attribute, automatic WHERE injection, cross-tenant protection |
| PSR-15 Middleware | Auto-resolution → status check → driver activation → cleanup |
| Lifecycle Management | Create, suspend, activate, delete with schema/DB provisioning |
| Tenant-Aware Cache | Transparent key prefixing: tenant:{id}: |
| Tenant-Aware Queue | Per-tenant queue names, payload enrichment, context restoration |
| Tenant-Aware Storage | Path scoping: tenants/{key}/ with traversal protection |
| Tenant-Aware Session | Session key prefixing for shared infrastructure |
| Migration Orchestration | Per-tenant migrations, auto-generated central tenants table |
| Event System | 7 lifecycle + resolution events for audit/telemetry |
| PHP 8.4 Native | Property hooks, backed enums, asymmetric visibility |
Requirements
- PHP 8.4 or higher
monkeyscloud/monkeyslegion-database(ConnectionManager)monkeyscloud/monkeyslegion-events(Event dispatching)psr/http-message^2.0psr/http-server-middleware^1.0
Installation
Architecture
The package is organized into clear namespaces:
Attribute/: Entity attributes (#[BelongsToTenant])Context/: Per-request tenant holder (TenantContext)Contracts/: Core interfaces (TenantInterface,TenancyDriverInterface,TenantResolverInterface)Driver/: Isolation implementations (SingleDatabaseDriver,SchemaDatabaseDriver,SeparateDatabaseDriver)Entity/: DefaultTenantentity with property hooks and lifecycle methodsEnum/: Backed enums (TenancyMode,TenantStatus,ResolutionStrategy)Event/: Lifecycle and resolution events (7 total)Infrastructure/: Tenant-aware adapters for cache, queue, storage, sessionLifecycle/: Provisioning and management (TenantManager)Middleware/: PSR-15 middleware for automatic resolutionMigration/: Central table creation and per-tenant migration runnerResolver/: Resolution strategies (Domain, Subdomain, Header, Path, QueryParam, Chain)Scope/: Automatic query scoping and entity lifecycle hooks
Configuration
Copy the example config to your application's config directory:
Isolation Modes
Single Database (single_db)
All tenants share one database. Isolation is achieved via automatic WHERE tenant_id = :current clauses injected by TenantScope.
Best for: SaaS startups, low-to-medium tenant counts, cost-sensitive deployments.
Schema per Tenant (schema)
Each tenant gets a dedicated PostgreSQL schema (or MySQL database). The SchemaDatabaseDriver switches search_path / USE per request.
Best for: Mid-size SaaS, compliance-sensitive industries, moderate isolation needs.
Database per Tenant (database)
Each tenant gets a fully separate database. The SeparateDatabaseDriver routes to a dedicated ConnectionInterface per tenant.
Best for: Enterprise SaaS, maximum isolation, regulated industries (HIPAA, SOC2).
Tenant Resolution
The middleware resolves tenants by trying resolvers in the order configured:
Example: Subdomain Resolution
With base_domain = "example.com":
| Request Host | Resolved Tenant Key |
|---|---|
acme.example.com |
acme |
globex.example.com |
globex |
example.com |
null (central context) |
nested.sub.example.com |
null (nested not supported) |
Tenant Context
The TenantContext is the central access point for the current tenant:
Entity Scoping
Automatic WHERE Injection
Automatic Insert Data
Cross-Tenant Validation
Entity Lifecycle Listener
Lifecycle Management
The TenantManager provides a complete provisioning pipeline:
Auto-Generated Central Table
The tenants table is created automatically by the package:
This creates:
Tenant-Aware Infrastructure
Cache
Queue (Per-Tenant Isolation)
Storage (Path Scoping)
Session
Events
All lifecycle and resolution events extend MonkeysLegion\Events\Event:
| Event | Dispatched When |
|---|---|
TenantResolved |
Tenant identified from request (includes resolver class) |
TenantNotFound |
Resolution failed (includes host + path) |
TenantSwitched |
Context changed from one tenant to another |
TenantCreated |
New tenant provisioned |
TenantSuspended |
Tenant suspended (includes reason) |
TenantActivated |
Tenant reactivated |
TenantDeleted |
Tenant deleted (includes ID + key) |
Middleware Setup
Register the middleware in your HTTP pipeline:
Tenant Entity
The default Tenant entity uses PHP 8.4 property hooks:
Security Posture
- Cross-tenant protection —
TenantScope::validate()prevents accessing rows from other tenants - Path traversal prevention —
TenantStorageAdapterrejects../paths - Suspended tenant blocking — middleware returns 503 for inactive tenants
- Automatic cleanup —
try/finallyensures driver disconnect on all code paths - Scoped execution —
TenantContext::run()guarantees context restoration
Testing
License
MIT © MonkeysCloud
All versions of monkeyslegion-tenancy with dependencies
monkeyscloud/monkeyslegion-core Version *
monkeyscloud/monkeyslegion-di Version *
monkeyscloud/monkeyslegion-database Version *
monkeyscloud/monkeyslegion-entity Version *
monkeyscloud/monkeyslegion-events Version *
monkeyscloud/monkeyslegion-http Version *
psr/http-message Version ^2.0
psr/http-server-middleware Version ^1.0
psr/http-server-handler Version ^1.0