bedita/import-tools 问题修复 & 功能扩展

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

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

bedita/import-tools

Composer 安装命令:

composer require bedita/import-tools

包简介

Import Tools for data import with BEdita

README 文档

README

Github Actions codecov phpstan psalm Scrutinizer Code Quality image image

Installation

First, if vendor directory has not been created, you have to install composer dependencies using:

composer install

You can install this plugin into your CakePHP application using composer.

The recommended way to install composer packages is:

composer require bedita/import-tools

Commands

AnonymizeUsersCommand

This command provides a tool to anonymize users. Usage examples

// anonymize all users (except user by ID 1, admin)
$ bin/cake anonymize_users

// anonymize users by id 2,3,4
$ bin/cake anonymize_users --id 2,3,4

// anonymize all users except users by id 1,2,3
$ bin/cake anonymize_users --preserve 1,2,3

ImportCommand

This command provides a tool to import data from csv file.

Usage examples:

# basic
$ bin/cake import --file documents.csv --type documents
$ bin/cake import -f documents.csv -t documents

# dry-run
$ bin/cake import --file articles.csv --type articles --dryrun yes
$ bin/cake import -f articles.csv -t articles -d yes

# destination folder
$ bin/cake import --file news.csv --type news --parent my-folder-uname
$ bin/cake import -f news.csv -t news -p my-folder-uname

# translations
$ bin/cake import --file translations.csv --type translations
$ bin/cake import -f translations.csv -t translations

ImportProjectCommand

You could use this command when you are copying a remote database to a locale database, and after that you want to "adjust" applications and users. Applications and users in default datasource will be updated.

Before launching it, you should setup properly default and import datasources in config/app_local.php. Example:

'Datasources' => [
    // the target database
    'default' => [
        'className' => 'Cake\Database\Connection',
        'driver' => 'Cake\Database\Driver\Mysql',
        'host' => '***********',
        'port' => '***********',
        'username' => '***********',
        'password' => '***********',
        'database' => '***********',
        // ...
    ],
    // the remote database you want to you as source
    'import' => [
        'className' => 'Cake\Database\Connection',
        'driver' => 'Cake\Database\Driver\Mysql',
        'host' => '***********',
        'port' => '***********',
        'username' => '***********',
        'password' => '***********',
        'database' => '***********',
        // ...
    ],
],

Usage example:

$ bin/cake import_project

TranslateFileCommand

This command provides a tool to translate the content of a file from one language to another, using a translator service (i.e., set in config/app_local.php).

Translator service configuration example:

'Translators' => [
    'deepl' => [
        'name' => 'DeepL',
        'class' => '\BEdita\I18n\Deepl\Core\Translator',
        'options' => [
            'auth_key' => '************',
        ],
    ],
]

Usage example:

$ bin/cake translate_file \
  --input articles-en.txt \
  --output articles-it.txt \
  --from en \
  --to it \
  --translator deepl

TranslateObjectsCommand

This command provides a tool to translate the content of objects from one language to another, using a translator service (i.e., set in config/app_local.php, as described above).

Usage examples:

# basic
$ bin/cake translate_objects \
  --from en \
  --to it \
  --engine deepl

# dry-run
$ bin/cake translate_objects \
  --from en \
  --to it \
  --engine deepl \
  --dry-run yes

# limit
$ bin/cake translate_objects \
  --from en \
  --to it \
  --engine deepl \
  --limit 10

# status
$ bin/cake translate_objects \
  --from en \
  --to it \
  --engine deepl \
  --status draft

# type
$ bin/cake translate_objects \
  --from en \
  --to it \
  --engine deepl \
  --type articles

Utilities

You can find some utility classes in src/Utility folder.

CsvTrait

This trait provides readCsv method to progressively read a csv file line by line.

Usage example:

use BEdita\ImportTools\Utility\CsvTrait;

class MyImporter
{
    use CsvTrait;

    public function import(string $filename): void
    {
        foreach ($this->readCsv($filename) as $obj) {
            // process $obj
        }
    }
}

FileTrait

This trait provides readFileStream method to open "read-only" file stream (you can use local filesystem or adapter).

Usage example:

use BEdita\ImportTools\Utility\FileTrait;

class MyImporter
{
    use FileTrait;

    public function read(string $file): void
    {
        [$fh, $close] = $this->readFileStream($path);

        try {
            flock($fh, LOCK_SH);
            // do your stuff
        } finally {
            $close();
        }
    }
}

TreeTrait

This trait provides setParent method to save the parent for a specified entity.

Usage example:

use BEdita\ImportTools\Utility\TreeTrait;

class MyImporter
{
    use TreeTrait;

    public function import(string $filename, string $destination): void
    {
        foreach ($this->readCsv($filename) as $obj) {
            $this->setParent($obj, $destination);
        }
    }
}

Import

This class provides functions to import data from csv files into BEdita.

Public methods are:

  • saveObjects: read data from csv and save objects
  • saveObject: save a single object
  • saveTranslations: read data from csv and save translations
  • saveTranslation: save a single translation
  • translatedFields: get translated fields for a given object

Usage example:

use BEdita\ImportTools\Utility\Import;

class MyImporter
{
    public function import(string $filename, string $type, ?string $parent, ?bool $dryrun): void
    {
        $import = new Import($filename, $type, $parent, $dryrun);
        $import->saveObjects();
    }
}

bedita/import-tools 适用场景与选型建议

bedita/import-tools 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 8.2k 次下载、GitHub Stars 达 2, 最近一次更新时间为 2023 年 03 月 15 日, 在 PHP 生态内属于活跃度较高的组件。

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

围绕 bedita/import-tools 我们能提供哪些服务?
定制开发 / 二次开发

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

  • 总下载量: 8.2k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 2
  • 点击次数: 1
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 2
  • Watchers: 4
  • Forks: 1
  • 开发语言: PHP

其他信息

  • 授权协议: LGPL-3.0-or-later
  • 更新时间: 2023-03-15