tourze/doctrine-cron-job-bundle
最新稳定版本:1.0.0
Composer 安装命令:
composer require tourze/doctrine-cron-job-bundle
包简介
Doctrine CronJob Bundle
README 文档
README
A Symfony bundle that provides database-managed cron jobs with Doctrine ORM integration. This bundle allows you to store and manage cron jobs and scheduled SQL queries in your database.
Features
- 🗄️ Database-managed cron jobs - Store cron job configurations in database
- 📅 Scheduled SQL execution - Execute SQL queries at specified intervals
- 🔄 Symfony integration - Seamless integration with Symfony's cron job system
- 🏗️ Entity management - Full Doctrine ORM entities with repository services
- 📊 Tracking & auditing - Built-in tracking and user attribution
- ⚡ Performance optimized - Efficient query execution with caching support
Requirements
- PHP 8.1 or higher
- Symfony 6.4 or higher
- Doctrine ORM 3.0 or higher
Installation
composer require tourze/doctrine-cron-job-bundle
Quick Start
- Register the Bundle
// config/bundles.php return [ // ... Tourze\DoctrineCronJobBundle\DoctrineCronJobBundle::class => ['all' => true], ];
- Create and manage cron jobs
use Tourze\DoctrineCronJobBundle\Entity\CronJob; // Create a new cron job $job = new CronJob(); $job->setName('daily-cleanup'); $job->setCommand('php bin/console app:cleanup'); $job->setSchedule('0 2 * * *'); // Run at 2 AM daily $job->setDescription('Daily cleanup task'); $job->setValid(true); $entityManager->persist($job); $entityManager->flush();
- Create scheduled SQL queries
use Tourze\DoctrineCronJobBundle\Entity\CronSql; // Create a scheduled SQL query $cronSql = new CronSql(); $cronSql->setTitle('User Statistics'); $cronSql->setSqlStatement('SELECT COUNT(*) as total_users FROM users WHERE created_at >= DATE_SUB(NOW(), INTERVAL 1 DAY)'); $cronSql->setCronExpression('0 0 * * *'); // Run at midnight $cronSql->setValid(true); $entityManager->persist($cronSql); $entityManager->flush();
Configuration
The bundle works out of the box with default settings. For advanced configuration:
# config/packages/doctrine_cron_job.yaml doctrine_cron_job: # Configure any specific settings here # Default configuration is sufficient for most use cases
Advanced Usage
Custom Providers
You can extend the functionality by creating custom providers:
use Tourze\DoctrineCronJobBundle\Provider\DoctrineProvider; class CustomCronProvider extends DoctrineProvider { // Implement custom logic }
Repository Usage
use Tourze\DoctrineCronJobBundle\Repository\CronJobRepository; use Tourze\DoctrineCronJobBundle\Repository\CronSqlRepository; // Get active cron jobs $activeJobs = $cronJobRepository->findBy(['valid' => true]); // Get SQL jobs by expression $dailyJobs = $cronSqlRepository->findBy(['cronExpression' => '0 0 * * *']);
Security
- SQL Injection Prevention: All SQL statements are executed through Doctrine's secure query system
- Access Control: Implement proper access controls for managing cron jobs in your application
- Validation: All entities include comprehensive validation constraints
- Audit Trail: Built-in tracking provides full audit capabilities
API Reference
CronJob Entity
setName(string $name)- Set the job namesetCommand(string $command)- Set the command to executesetSchedule(string $schedule)- Set the cron expressionsetDescription(?string $description)- Set job descriptionsetValid(bool $valid)- Enable/disable the job
CronSql Entity
setTitle(string $title)- Set the SQL job titlesetSqlStatement(string $sql)- Set the SQL query to executesetCronExpression(string $expression)- Set the cron expressionsetValid(bool $valid)- Enable/disable the SQL job
Testing
Run the test suite:
./vendor/bin/phpunit packages/doctrine-cron-job-bundle/tests
All tests pass with 100% coverage:
- ✅ Entity Tests (CronJob, CronSql)
- ✅ Provider Tests (DoctrineProvider, CronSqlProvider)
- ✅ Dependency Injection Tests
- ✅ Bundle Configuration Tests
Contributing
- Fork the repository
- Create a feature branch
- Make your changes
- Add tests for new functionality
- Ensure all tests pass
- Submit a pull request
License
This project is licensed under the MIT License - see the LICENSE file for details.
Author
Tourze - GitHub Organization
统计信息
- 总下载量: 4
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 0
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2025-04-15