mabe/backup-bundle

此 Symfony 扩展包可以从指定的实体生成 json 备份。

安装: 60

依赖者: 0

建议者: 0

安全性: 0

星星: 2

观察者: 1

分支: 0

开放问题: 0

类型:symfony-bundle

v0.5.5 2018-11-20 02:25 UTC

This package is auto-updated.

Last update: 2024-09-20 17:43:14 UTC


README

Build Status License: MIT

BackupBundle

此 Symfony 扩展包可以从指定的实体生成 json 备份。

需求

安装

步骤 1: 下载扩展包

打开命令行,进入您的项目目录,并执行以下命令以下载此扩展包的最新稳定版本

$ composer require mabe/backup-bundle

此命令需要您全局安装了 Composer,如 Composer 文档中的安装章节所述。

步骤 2: 启用扩展包(如果您使用 Symfony Flex,则可以跳过此步骤)

然后,通过将扩展包添加到项目 app/AppKernel.php 文件中注册的扩展包列表来启用它

<?php
// app/AppKernel.php

// ...
class AppKernel extends Kernel
{
    public function registerBundles()
    {
        $bundles = array(
            // ...
            new Mabe\BackupBundle\MabeBackupBundle(),
        );

        // ...
    }

    // ...
}

步骤 3: 配置

mabe_backup:
    jobs:
        # Job name can be anything except reserved names.
        # Job must have at least one entity and backup location configured.
        job1:
            entities:
                # Test1 entity will backup all entity properties
                AppBundle\Entity\Test1: ~
                # Test2 entity will backup only given properties
                AppBundle\Entity\Test2:
                    properties: ["username", "birthDate"]
            # Service id        
            target: "app.backup.service"
        job2:
            entities:
                # Test3 entity will use JMS groups
                # NOTE: Groups are optional and their names are case sensitive
                AppBundle\Entity\Test3:
                    groups: ["backup"]
                target: "app.backup.local_saver"

步骤 4: 创建用于保存的目标服务

在扩展包的许多版本中,保存方式已从指定本地目录和 gaufrette 文件系统演变为创建一个保存服务。这允许您创建自己的保存方式,无论是本地、gaufrette、数据库、redis 还是您决定的任何其他方式。您所需要做的就是实现 SaverInterface 在您的逻辑中。

    app.backup.local_saver:
        class: App\Backup\Saver
        public: true # Only for Symfony 4

您本地的目录保存类可能看起来像这样

// .../App/Backup/Saver.php

use Mabe\BackupBundle\Saver\SaverInterface;

class Saver implements SaverInterface
{
    public function save($json, $filename)
    {
        file_put_contents('/your/directory/'.$filename, $json);
    }
}

如果您想轻松使用 gaufrette,有一个接口供您使用。您只需按此方式传递您的文件系统即可

    app.backup.saver:
        class: Mabe\BackupBundle\Saver\GaufretteSaver
        arguments: ["your_gaufrette_fs", "@knp_gaufrette.filesystem_map"]
        public: true # Only for Symfony 4

步骤 5: 仅适用于 Symfony 4

Symfony 4 不再注册来自命令文件夹的扩展包命令,如 https://github.com/symfony/symfony/blob/master/UPGRADE-4.0.md#httpkernel 中所述。按以下方式注册命令

// config/services.yml
services:

    # Explicit command registration
    App\Command\BackupCommand:
        class: 'Mabe\BackupBundle\Command\BackupCommand'
        tags: ['console.command']

用法

运行所有配置的备份

$ php bin/console mabe:backup

列出作业

$ php bin/console mabe:backup --list

您也可以指定要运行的特定作业,如下所示

$ php bin/console mabe:backup job1 job2 job3

帮助

$ php bin/console mabe:backup --help

高级用法

您可以创建一个监听器来修改您的实体在 pre_backup 事件之前,或者在备份完成后做一些事情(例如发送邮件)。

// src/AppBundle/Listener/BackupListener.php

namespace AppBundle\Listener;

use Mabe\BackupBundle\Event\BackupEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;

class BackupListener implements EventSubscriberInterface
{

    public function preBackup(BackupEvent $event)
    {
        $object = $event->getObject();
        $job = $event->getActiveJob();
        if($object instanceof User) {
            if (!$object->isEnabled()) {
                // You can skip object backup if your conditions are not met
                $event->setSerialize(false);
            }
        }
    }

    public function postBackup(BackupEvent $event)
    {
        // do something
    }

    public function backupFinished(BackupEvent $event)
    {
        // send mail, etc..
    }

    public static function getSubscribedEvents()
    {
        return array(
            BackupEvent::PRE_BACKUP => 'preBackup',
            BackupEvent::POST_BACKUP => 'postBackup',
            BackupEvent::BACKUP_FINISHED => 'backupFinished'
        );
    }
}

...并注册服务

# ..app/config/services.yml

services:
    app.mabe_backup.listener:
        class: AppBundle\Listener\BackupListener
        tags:
            - { name: kernel.event_subscriber }

运行测试

./vendor/bin/simple-phpunit