tourze/doctrine-cron-job-bundle

Doctrine CronJob Bundle

Installs: 4

Dependents: 0

Suggesters: 0

Security: 0

Stars: 0

Watchers: 1

Forks: 0

Open Issues: 0

pkg:composer/tourze/doctrine-cron-job-bundle

0.1.3 2025-05-24 17:09 UTC

This package is auto-updated.

Last update: 2025-11-01 19:13:51 UTC


README

English | δΈ­ζ–‡

Latest Version Total Downloads PHP Version License Coverage

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

  1. Register the Bundle
// config/bundles.php
return [
    // ...
    Tourze\DoctrineCronJobBundle\DoctrineCronJobBundle::class => ['all' => true],
];
  1. 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();
  1. 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 name
  • setCommand(string $command) - Set the command to execute
  • setSchedule(string $schedule) - Set the cron expression
  • setDescription(?string $description) - Set job description
  • setValid(bool $valid) - Enable/disable the job

CronSql Entity

  • setTitle(string $title) - Set the SQL job title
  • setSqlStatement(string $sql) - Set the SQL query to execute
  • setCronExpression(string $expression) - Set the cron expression
  • setValid(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

  1. Fork the repository
  2. Create a feature branch
  3. Make your changes
  4. Add tests for new functionality
  5. Ensure all tests pass
  6. Submit a pull request

License

This project is licensed under the MIT License - see the LICENSE file for details.

Author

Tourze - GitHub Organization