actcmsvn/data-synchronize 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

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

actcmsvn/data-synchronize

Composer 安装命令:

composer require actcmsvn/data-synchronize

包简介

Make site ready in few steps

README 文档

README

Usage

Exporter

There are two ways to create an exporter.

Exporter

Create an exporter using the command

You can use the php artisan data-synchronize:make:exporter command to create an exporter.

php artisan data-synchronize:make:exporter PostExporter

Manually create an exporter

This is how an exporter should look like, below is an example of a PostExporter class.

<?php

namespace ACTCMS\Blog\Exporters;

use ACTCMS\Blog\Models\Post;
use ACTCMS\DataSynchronize\Exporter\ExportColumn;
use ACTCMS\DataSynchronize\Exporter\Exporter;
use Illuminate\Support\Collection;

class PostExporter extends Exporter
{
    public function label(): string
    {
        return 'Posts';
    }

    public function columns(): array
    {
        return [
            ExportColumn::make('name'),
            ExportColumn::make('description'),
            ExportColumn::make('created_at'),
        ];
    }

    public function collection(): Collection
    {
        return Post::all();
    }
}

This is how to use the exporter in a controller.

<?php

namespace ACTCMS\Blog\Http\Controllers;

use ACTCMS\DataSynchronize\Exporter\Exporter;
use ACTCMS\DataSynchronize\Http\Controllers\ExportController;
use ACTCMS\Blog\Exporters\PostExporter;

class ExportPostController extends ExportController
{
    protected function getExporter(): Exporter
    {
        return PostExporter::make();
    }
}

And then register the route in your routes file.

use ACTCMS\Base\Facades\AdminHelper;
use Illuminate\Support\Facades\Route;
use ACTCMS\Blog\Http\Controllers\ExportPostController;

AdminHelper::registerRoutes(function () {
    Route::prefix('tools/data-synchronize')->name('tools.data-synchronize.')->group(function () {
        Route::group(['prefix' => 'export/posts', 'as' => 'export.posts.', 'permission' => 'posts.export'], function () {
            Route::get('/', [ExportPostController::class, 'index'])->name('index');
            Route::post('/', [ExportPostController::class, 'store'])->name('store');
        });
    });
});

Each exporter route should have a permission to access it. You can use the permission key in the route group to define the permission.

In above route definition, the permission is posts.export key and it parent is tools.data-synchronize. You can define the permission in the permissions.php of your plugin.

return [
    [
        'name' => 'Export Posts',
        'flag' => 'posts.export',
        'parent_flag' => 'tools.data-synchronize',
    ],
];

Now you can navigate to http://your-domain/tools/data-synchronize/export/posts to export posts.

Add exporter to Export/Import Data panel section

Panel Section

To add the exporter to the Export/Import Data panel section, you can use the beforeRendering method of the PanelSectionManager class to register the exporter into the panel section.

use ACTCMS\Base\Facades\PanelSectionManager;
use ACTCMS\Base\PanelSections\PanelSectionItem;
use ACTCMS\DataSynchronize\PanelSections\ExportPanelSection;

public function boot(): void
{
    // ...

    PanelSectionManager::setGroupId('data-synchronize')->beforeRendering(function () {
        PanelSectionManager::default()
            ->registerItem(
                ExportPanelSection::class,
                fn () => PanelSectionItem::make('posts')
                    ->setTitle('Posts')
                    ->withDescription('Export post data to CSV or Excel file.')
                    ->withPriority(120)
                    ->withRoute('tools.data-synchronize.export.posts.index')
                    ->withPermission('posts.export')
            );
    });
    
    // ...
}

You can see the exporter in the Export/Import Data panel section.

Panel Section

Importer

There are two ways to create an importer.

Importer

Create an importer using the command

You can use the php artisan data-synchronize:make:importer command to create an importer.

php artisan data-synchronize:make:importer PostImporter

Manually create an importer

This is how an importer should look like, below is an example of a PostImporter class.

<?php

namespace ACTCMS\Blog\Importers;

use ACTCMS\Blog\Models\Post;
use ACTCMS\DataSynchronize\Importer\ImportColumn;
use ACTCMS\DataSynchronize\Importer\Importer;

class PostImporter extends Importer
{
    public function chunkSize(): int
    {
        return 1000;
    }

    public function label(): string
    {
        return 'Posts';
    }
    
    public function getValidateUrl(): string
    {
        return route('tools.data-synchronize.import.posts.validate');
    }

    public function getImportUrl(): string
    {
        return route('tools.data-synchronize.import.posts.store');
    }

    public function getDownloadExampleUrl(): ?string
    {
        return route('tools.data-synchronize.import.posts.download-example');
    }

    public function columns(): array
    {
        return [
            ImportColumn::make('name')->rules(['required', 'string']),
            ImportColumn::make('description')->rules(['required', 'string']),
            ImportColumn::make('created_at')->rules(['required', 'string']),
        ];
    }
    
    public function examples(): array
    {
        return [
            [
                'name' => 'Post 1',
                'description' => 'Description 1',
                'created_at' => '2021-01-01 00:00:00',
            ],
            [
                'name' => 'Post 2',
                'description' => 'Description 2',
                'created_at' => '2021-01-02 00:00:00',
            ],
        ];
    }

    public function handle(array $data): int
    {
        $total = 0;
        
        foreach ($data as $item) {
            Post::create($item);
            $total++;
        }
        
        return $total;
    }
}

This is how to use the importer in a controller.

<?php

namespace ACTCMS\Blog\Http\Controllers;

use ACTCMS\DataSynchronize\Http\Controllers\ImportController;
use ACTCMS\DataSynchronize\Importer\Importer;

class ImportPostController extends ImportController
{
    protected function getImporter(): Importer
    {
        return PostImporter::make();
    }
}

And then register the route in your routes file.

use ACTCMS\Base\Facades\AdminHelper;

AdminHelper::registerRoutes(function () {
    Route::prefix('tools/data-synchronize')->name('tools.data-synchronize.')->group(function () {
        Route::group(['prefix' => 'import/posts', 'as' => 'import.posts.', 'permission' => 'posts.import'], function () {
            Route::get('/', [ImportPostController::class, 'index'])->name('index');
            Route::post('', [ImportPostController::class, 'store'])->name('store');
            Route::post('validate', [ImportPostController::class, 'validateData'])->name('validate');
            Route::get('download-example', [ImportPostController::class, 'downloadExample'])->name('download-example');
        });
    });
});

Each importer route should have a permission to access it. You can use the permission key in the route group to define the permission.

In above route definition, the permission is posts.import key and it parent is tools.data-synchronize. You can define the permission in the permissions.php of your plugin.

return [
    [
        'name' => 'Import Posts',
        'flag' => 'posts.import',
        'parent_flag' => 'tools.data-synchronize',
    ],
];

Now you can navigate to http://your-domain/tools/data-synchronize/import/posts to import posts.

Add importer to Export/Import Data panel section

To add the importer to the Export/Import Data panel section, you can use the beforeRendering method of the PanelSectionManager class to register the importer into the panel section.

use ACTCMS\Base\Facades\PanelSectionManager;
use ACTCMS\Base\PanelSections\PanelSectionItem;
use ACTCMS\DataSynchronize\PanelSections\ImportPanelSection;

public function boot(): void
{
    // ...

    PanelSectionManager::setGroupId('data-synchronize')->beforeRendering(function () {
        PanelSectionManager::default()
            ->registerItem(
                ImportPanelSection::class,
                fn () => PanelSectionItem::make('posts')
                    ->setTitle('Posts')
                    ->withDescription('Import post data from CSV or Excel file.')
                    ->withPriority(120)
                    ->withRoute('tools.data-synchronize.import.posts.index')
                    ->withPermission('posts.import')
            );
    });
    
    // ...
}

You can see the importer in the Export/Import Data panel section.

Import/Export data from command line

Screenshot 2024-11-26 at 20 31 23 Screenshot 2024-11-26 at 04 30 45

actcmsvn/data-synchronize 适用场景与选型建议

actcmsvn/data-synchronize 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 63 次下载、GitHub Stars 达 0, 最近一次更新时间为 2024 年 11 月 28 日, 在 PHP 生态内属于活跃度较高的组件。

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

围绕 actcmsvn/data-synchronize 我们能提供哪些服务?
定制开发 / 二次开发

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

  • 总下载量: 63
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 17
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

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

其他信息

  • 授权协议: Unknown
  • 更新时间: 2024-11-28