承接 tourze/symfony-schedule-entity-clean-bundle 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

tourze/symfony-schedule-entity-clean-bundle

Composer 安装命令:

composer require tourze/symfony-schedule-entity-clean-bundle

包简介

Symfony bundle for automatic scheduled cleanup of Doctrine entities based on creation time and cron expressions

README 文档

README

English | 中文

[Latest Version] (https://packagist.org/packages/tourze/symfony-schedule-entity-clean-bundle) [PHP Version] (https://packagist.org/packages/tourze/symfony-schedule-entity-clean-bundle) [License] (https://packagist.org/packages/tourze/symfony-schedule-entity-clean-bundle) [Build Status] (https://github.com/tourze/php-monorepo/actions) [Code Coverage] (https://codecov.io/gh/tourze/php-monorepo)

A Symfony bundle for automatically cleaning old entity data based on scheduled cron expressions.

Features

  • Automatically clean old entity data based on cron expressions
  • Configurable retention period for each entity
  • Custom retention period via environment variables
  • Asynchronous processing using Symfony Messenger
  • Event dispatching after cleaning operations

Table of Contents

Installation

composer require tourze/symfony-schedule-entity-clean-bundle

Register the bundle in your bundles.php:

return [
    // ...
    Tourze\ScheduleEntityCleanBundle\ScheduleEntityCleanBundle::class => ['all' => true],
    // ...
];

Quick Start

  1. Mark your entity class with the AsScheduleClean attribute:
<?php

namespace App\Entity;

use Doctrine\ORM\Mapping as ORM;
use Tourze\ScheduleEntityCleanBundle\Attribute\AsScheduleClean;

#[ORM\Entity]
#[AsScheduleClean(expression: '0 0 * * *', defaultKeepDay: 30)]
class LogEntry
{
    #[ORM\Id]
    #[ORM\GeneratedValue]
    #[ORM\Column]
    private ?int $id = null;

    #[ORM\Column]
    private \DateTimeImmutable $createTime;

    // Other properties and methods...
}
  1. Ensure your entity has a createTime field. The bundle uses this field to determine which records to delete.

  2. The bundle will automatically register a cron job that runs every minute to check if any entities need cleaning.

Configuration

The AsScheduleClean attribute accepts the following parameters:

  • expression: A cron expression that determines when to clean the entity (default: '0 0 * * *', which runs at midnight every day)
  • defaultKeepDay: The number of days to keep records (default: 7)
  • keepDayEnv: An optional environment variable name that can override the defaultKeepDay value

Console Commands

schedule-entity-clean:run

This command is responsible for periodically cleaning entity data based on the configured cron expressions. It runs every minute by default and checks all entities marked with the AsScheduleClean attribute.

The command:

  • Scans all entities for AsScheduleClean attributes
  • Checks if the cron expression is due for execution
  • Verifies that entities have a createTime field
  • Dispatches asynchronous cleanup messages for eligible entities

Usage:

php bin/console schedule-entity-clean:run

Note: This command is automatically registered as a cron job and runs every minute. You don't need to call it manually unless for testing purposes.

Advanced Usage

Environment Variable Configuration

You can override the default keep days using environment variables:

#[AsScheduleClean(
    expression: '0 2 * * *', 
    defaultKeepDay: 30, 
    keepDayEnv: 'LOG_RETENTION_DAYS'
)]
class LogEntry
{
    // ...
}

Then set the environment variable:

LOG_RETENTION_DAYS=60

Multiple Cleaning Schedules

You can apply multiple cleaning schedules to the same entity:

#[AsScheduleClean(expression: '0 0 * * 0', defaultKeepDay: 7)]   // Weekly cleanup
#[AsScheduleClean(expression: '0 0 1 * *', defaultKeepDay: 90)]  // Monthly deep cleanup
class LogEntry
{
    // ...
}

Events

The bundle dispatches a ScheduleEntityCleanFinishEvent after successfully cleaning entity data. You can listen to this event to perform custom actions after the cleaning process.

<?php

namespace App\EventListener;

use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Tourze\ScheduleEntityCleanBundle\Event\ScheduleEntityCleanFinishEvent;

class EntityCleanSubscriber implements EventSubscriberInterface
{
    public static function getSubscribedEvents(): array
    {
        return [
            ScheduleEntityCleanFinishEvent::class => 'onEntityCleaned',
        ];
    }

    public function onEntityCleaned(ScheduleEntityCleanFinishEvent $event): void
    {
        $modelClass = $event->getModelClass();
        // Do something with the model class
    }
}

Requirements

  • PHP 8.1 or higher
  • Symfony 6.4 or higher
  • Doctrine ORM
  • Entity must have a createTime field

License

The MIT License (MIT). Please see License File for more information.

tourze/symfony-schedule-entity-clean-bundle 适用场景与选型建议

tourze/symfony-schedule-entity-clean-bundle 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 29.91k 次下载、GitHub Stars 达 0, 最近一次更新时间为 2025 年 03 月 30 日, 在 PHP 生态内属于活跃度较高的组件。

我们在过去多个企业项目中使用过 tourze/symfony-schedule-entity-clean-bundle 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 tourze/symfony-schedule-entity-clean-bundle 我们能提供哪些服务?
定制开发 / 二次开发

基于 tourze/symfony-schedule-entity-clean-bundle 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 29.91k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 27
  • 依赖项目数: 18
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2025-03-30