tech-scrappers/hrms-shared
Composer 安装命令:
composer require tech-scrappers/hrms-shared
包简介
HRMS Shared Components Package - Middleware, Services, Events, and Utilities
README 文档
README
A comprehensive shared components package for the HRMS microservices ecosystem, providing enterprise-grade middleware, services, events, and utilities for building scalable multi-tenant HR management systems with distributed database architecture.
📦 Package Contents
🗄️ Database Architecture: Distributed
Each microservice has its own PostgreSQL database instance (Docker container). Each tenant gets separate databases in each service's own DB instance.
Key Components:
- DistributedDatabaseService: Manages tenant databases on current service's DB instance
- DistributedTenantDatabaseMiddleware: Automatic tenant database switching
- DistributedDatabaseServiceProvider: Service registration and configuration
Database Naming: tenant_{tenantId}_{service}
🔒 Middleware (22 Components)
Authentication & Authorization:
- UnifiedAuthenticationMiddleware: Unified OAuth2 and API key authentication with tenant context
- OAuth2TokenValidationMiddleware: OAuth2 token validation and user context extraction
- ApiKeyAuthenticationMiddleware: API key-based authentication
- ApiKeyPermissionMiddleware: API key permission validation
- ScopeMiddleware: OAuth2 scope-based authorization
- SuperAdminMiddleware: Super admin access control
Security & Protection:
- SecurityHeadersMiddleware: Enterprise-grade security headers (CSP, HSTS, etc.)
- InputValidationMiddleware: XSS and injection prevention with request sanitization
- CsrfProtectionMiddleware: CSRF token validation and protection
- PayloadSizeLimitMiddleware: Request payload size validation
- BruteForceProtectionMiddleware: Brute force attack prevention
Rate Limiting & Performance:
- EnterpriseRateLimitMiddleware: Advanced enterprise rate limiting
- EnhancedRateLimitMiddleware: Enhanced rate limiting with burst control
- ApiKeyRateLimitMiddleware: API key-specific rate limiting
- PerformanceMonitoringMiddleware: Request performance monitoring and metrics
- ResponseCachingMiddleware: Response caching for improved performance
Multi-tenancy & Database:
- DistributedTenantDatabaseMiddleware: Distributed tenant database switching (each service has own DB instance)
Utilities:
- JsonResponseMiddleware: Consistent JSON response formatting
- StructuredLoggingMiddleware: Structured request/response logging
- EnvironmentAwareCorsMiddleware: Environment-aware CORS handling
🛠️ Services (6 Components)
Database & Multi-tenancy:
- DistributedDatabaseService: Tenant database management for distributed architecture with Docker support
API & Communication:
- ApiKeyService: API key management and validation
- ApiResponseService: Centralized API response management with error codes
- EventPublisher: Event publishing service with retry mechanisms
Security & Auditing:
- AuditLogService: Comprehensive audit logging with data sanitization
- SecurityAuditService: Security audit and compliance reporting
- SecurityService: Core security utilities and validation
Caching:
- QueryCacheService: Database query result caching
📡 Events System (25+ Event Types)
Event Infrastructure:
- EventBus: Redis-based event communication with retry mechanisms
- EventSubscriber: Event subscription and handling system
- BaseEvent: Abstract base class for all HRMS events
Tenant Events:
- TenantCreatedEvent: Tenant creation events
- TenantUpdatedEvent: Tenant update events
- TenantDeletedEvent: Tenant deletion events
- TenantMigrationEvent: Tenant database migration events
Employee Events:
- EmployeeCreatedEvent: Employee creation events
- EmployeeUpdatedEvent: Employee update events
- EmployeeDeletedEvent: Employee deletion events
- DepartmentCreated/Updated/Deleted: Department lifecycle events
- BranchCreated/Updated/Deleted: Branch lifecycle events
Other Events:
- AttendanceEvents: Check-in/out and break management
- LeaveEvents: Leave management
- ApprovalEvents: Approval workflow
- IdentityEvents: User and identity management
🏗️ Base Classes & Utilities
Base Classes:
- BaseController: Abstract base controller with tenant awareness
- BaseRepository: Abstract base repository with tenant isolation
- BaseService: Abstract base service class
Traits (7 Components):
- StandardizedResponseTrait: Standardized API response methods
- EnterpriseApiResponseTrait: Enterprise-grade API response formatting
- TenantAwareTrait: Tenant-aware functionality
- AuditableTrait: Audit logging capabilities
- AuditLogTrait: Enhanced audit logging
- ErrorHandlingTrait: Centralized error handling
Models:
- TenantAwareModel: Base model with tenant isolation
- AuditLog: Audit log model
🚀 Installation
composer require hrms/shared
📋 Usage
Service Provider Registration
In bootstrap/app.php:
->withProviders([ \Shared\Providers\SharedServicesProvider::class, \Shared\Providers\DistributedDatabaseServiceProvider::class, ])
Middleware Usage
// In routes/api.php Route::middleware(['tenant.distributed'])->group(function () { // Your tenant-specific routes Route::get('/employees', [EmployeeController::class, 'index']); }); // With authentication Route::middleware(['unified.auth', 'tenant.distributed'])->group(function () { // Protected tenant routes });
Environment Configuration
Each service needs its own database configuration:
# Service Configuration SERVICE_NAME=identity-service # or employee-service, core-service DATABASE_ARCHITECTURE_MODE=distributed # Database Configuration (Service-specific instance) DB_CONNECTION=pgsql DB_HOST=identity-db # Service-specific DB host DB_PORT=5432 DB_DATABASE=hrms_identity DB_USERNAME=postgres DB_PASSWORD=your_secure_password # Distributed Database Settings DISTRIBUTED_DATABASE_ENABLED=true DISTRIBUTED_CONNECTION_POOLING=true DISTRIBUTED_MAX_CONNECTIONS=50 DISTRIBUTED_CONNECTION_TIMEOUT=30 # Docker Settings DOCKER_ENABLED=true DB_HEALTH_CHECK_ENABLED=true
Database Service Usage
use Shared\Services\DistributedDatabaseService; // Create tenant database on current service's DB instance $dbService = app(DistributedDatabaseService::class); $dbService->createTenantDatabase([ 'id' => 'tenant-uuid', 'name' => 'Acme Corporation', 'domain' => 'acme.hrms.local', 'is_active' => true, ]); // Switch to tenant database (done automatically by middleware) $dbService->switchToTenantDatabase('tenant-uuid'); // Query tenant data $employees = DB::table('employees')->get(); // Switch back to central database $dbService->switchToCentralDatabase();
Event-Driven Tenant Provisioning
// In Identity Service (tenant creation) use Shared\Events\TenantCreatedEvent; $tenant = Tenant::create([...]); // Create tenant database on Identity Service $dbService->createTenantDatabase($tenant->toArray()); // Publish event for other services event(new TenantCreatedEvent( $tenant->id, $tenant->toArray(), auth()->id() ));
// In Employee/Core Services (event listener) class CreateTenantDatabaseListener { public function handle(TenantCreatedEvent $event): void { $dbService = app(DistributedDatabaseService::class); $dbService->createTenantDatabase($event->payload); } }
🐳 Docker Architecture
Each microservice has its own PostgreSQL container:
├── identity-db (PostgreSQL)
│ ├── hrms_identity (central)
│ ├── tenant_uuid1_identity
│ └── tenant_uuid2_identity
│
├── employee-db (PostgreSQL)
│ ├── hrms_employee (central)
│ ├── tenant_uuid1_employee
│ └── tenant_uuid2_employee
│
└── core-db (PostgreSQL)
├── hrms_core (central)
├── tenant_uuid1_core
└── tenant_uuid2_core
🧪 Testing
# Run all tests composer test # Run with coverage composer test-coverage # Code style check composer cs-check # Fix code style composer cs-fix
📊 Monitoring & Observability
Performance Monitoring:
- Request timing and performance metrics
- Database connection pool monitoring
- Query performance tracking
Structured Logging:
- JSON-formatted logs with correlation IDs
- Tenant context in all logs
- Database operation logging
Security Auditing:
- Automated security compliance checks
- Complete audit trail
- Data access logging
🔒 Security
Database Isolation:
- Each service has dedicated database instance
- No cross-service database access
- Tenant data isolated per service
Connection Security:
- SSL/TLS enabled
- Per-service credentials
- Automatic connection cleanup
Audit Logging:
- All database switches logged
- Connection errors tracked
- Tenant access monitored
📞 Support
For detailed documentation, see:
For issues or questions:
- Check logs:
storage/logs/laravel.log - Enable debug mode:
APP_DEBUG=true - Test database:
php artisan tinker→DB::connection()->getPdo()
📄 License
MIT License - see LICENSE file for details.
🤝 Contributing
- Fork the repository
- Create a feature branch
- Make your changes
- Run tests and code style checks
- Submit a pull request
tech-scrappers/hrms-shared 适用场景与选型建议
tech-scrappers/hrms-shared 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 39 次下载、GitHub Stars 达 0, 最近一次更新时间为 2025 年 11 月 05 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「middleware」 「laravel」 「shared」 「microservices」 「HRMS」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 tech-scrappers/hrms-shared 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 tech-scrappers/hrms-shared 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 tech-scrappers/hrms-shared 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Shoot aims to make providing data to your templates more manageable
Shared components for YII used in FTS
Fundamental contracts and shared abstractions for Maatify modules, including clock and telemetry interfaces.
Slim Framework 3 CSRF protection middleware utilities
Key-value shared memory store through php shared memory module.
Cloudflare Middleware For Guzzle
统计信息
- 总下载量: 39
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 1
- 点击次数: 16
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2025-11-05