tourze / event-automation-bundle
Symfony事件自动化处理包,支持定时和条件触发
Installs: 0
Dependents: 0
Suggesters: 0
Security: 0
Stars: 0
Watchers: 1
Forks: 0
Open Issues: 0
Type:symfony-bundle
pkg:composer/tourze/event-automation-bundle
Requires
- php: ^8.1
- doctrine/collections: ^2.3
- doctrine/dbal: ^4.0
- doctrine/doctrine-bundle: ^2.13
- doctrine/orm: ^3.0
- doctrine/persistence: ^3.1 || ^4
- dragonmantank/cron-expression: ^3.4
- psr/log: ^3|^2|^1
- symfony/config: ^6.4
- symfony/console: ^6.4
- symfony/dependency-injection: ^6.4
- symfony/doctrine-bridge: ^6.4
- symfony/event-dispatcher: ^6.4
- symfony/event-dispatcher-contracts: ^2.5 | ^3
- symfony/framework-bundle: ^6.4
- symfony/http-kernel: ^6.4
- symfony/yaml: ^6.4 || ^7.1
- tourze/doctrine-indexed-bundle: 0.0.*
- tourze/doctrine-timestamp-bundle: 0.0.*
- tourze/doctrine-track-bundle: 0.1.*
- tourze/doctrine-user-bundle: 0.0.*
- tourze/easy-admin-attribute: 0.1.*
Requires (Dev)
- phpstan/phpstan: ^2.1
- phpunit/phpunit: ^10.0
This package is auto-updated.
Last update: 2025-11-01 19:16:01 UTC
README
A Symfony bundle that provides automated event processing system with configurable triggers and context data collection.
Features
- Cron-based scheduling: Define events using standard cron expressions
- SQL-based triggers: Conditional event triggers based on database queries
- Context data collection: Flexible data gathering with parameterized queries
- Event logging: Complete audit trail of all event executions
- Symfony integration: Native integration with Symfony EventDispatcher
- Multiple trigger types: Support for both time-based and condition-based triggers
Requirements
- PHP 8.1 or higher
- Symfony 7.3 or higher
- Doctrine ORM 3.0 or higher
Installation
composer require tourze/event-automation-bundle
Quick Start
1. Create Event Configuration
use EventAutomationBundle\Entity\EventConfig; use EventAutomationBundle\Entity\ContextConfig; $eventConfig = new EventConfig(); $eventConfig->setName('Order Timeout Check') ->setIdentifier('order_timeout_check') ->setCronExpression('0 * * * *') // Run every hour ->setTriggerSql('SELECT COUNT(*) FROM orders WHERE status = "pending" AND created_at < DATE_SUB(NOW(), INTERVAL 24 HOUR)'); $contextConfig = new ContextConfig(); $contextConfig->setName('timeout_orders') ->setEntityClass('App\\Entity\\Order') ->setQuerySql('SELECT * FROM orders WHERE status = "pending" AND created_at < DATE_SUB(NOW(), INTERVAL 24 HOUR)') ->setEventConfig($eventConfig); $entityManager->persist($eventConfig); $entityManager->persist($contextConfig); $entityManager->flush();
2. Create Event Listener
use EventAutomationBundle\Event\AutomationEvent; use Symfony\Component\EventDispatcher\EventSubscriberInterface; class OrderTimeoutEventSubscriber implements EventSubscriberInterface { public static function getSubscribedEvents(): array { return [ 'order_timeout_check' => 'onOrderTimeout', ]; } public function onOrderTimeout(AutomationEvent $event): void { $context = $event->getContext(); $timeoutOrders = $context['timeout_orders'] ?? []; // Handle timeout orders... foreach ($timeoutOrders as $order) { // Process order timeout logic } } }
3. Process Events
# Run automation events
php bin/console event-automation:process
Advanced Usage
Manual Event Triggering
use EventAutomationBundle\Event\AutomationEvent; $event = new AutomationEvent($eventConfig, ['manual_trigger' => true]); $eventDispatcher->dispatch($event, $event->getName());
Complex Context Data
$contextConfig = new ContextConfig(); $contextConfig->setName('user_stats') ->setEntityClass('App\\Entity\\User') ->setQuerySql('SELECT u.* FROM users u WHERE u.last_login < :cutoff_date') ->setQueryParams(['cutoff_date' => '30 days ago']) ->setEventConfig($eventConfig);
Entities
EventConfig
Main configuration entity with:
name: Event display nameidentifier: Unique event identifiercronExpression: Cron scheduling expressiontriggerSql: SQL condition for triggeringcontextConfigs: Associated context configurationstriggerLogs: Execution history
ContextConfig
Context data configuration with:
name: Context variable nameentityClass: Target entity classquerySql: Data collection queryqueryParams: Query parameter configuration
TriggerLog
Execution log with:
contextData: Captured context dataresult: Execution result/status
Best Practices
- SQL Performance: Ensure trigger SQL queries are optimized and use proper indexes
- Cron Expressions: Use standard cron format for scheduling
- Idempotency: Design event handlers to be idempotent for repeated executions
- Error Handling: Implement proper error handling in event subscribers
- Monitoring: Set up monitoring for event execution failures
Contributing
Please see CONTRIBUTING.md for details.
License
The MIT License (MIT). Please see License File for more information.