misantron/dynamite 问题修复 & 功能扩展

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

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

misantron/dynamite

Composer 安装命令:

composer require misantron/dynamite

包简介

AWS DynamoDB data fixtures loader

README 文档

README

Build Status Code Coverage Packagist

Provide a simple way to manage and execute the loading of data fixtures for AWS DynamoDB storage.
Can use client from AWS PHP SDK or Async AWS under the hood.
Library code design is heavily inspired by doctrine/data-fixtures.

Install

The preferred way to install is through Composer. Run this command to install the latest stable version:

composer require --dev misantron/dynamite

Loading fixtures

Create table creation class

This feature is optional.
Fixture classes must implement Dynamite\TableInterface interface to be visible for a loader.

<?php

declare(strict_types=1);

namespace Fixtures;

use Dynamite\AbstractTable;
use Dynamite\Attribute\Groups;
use Dynamite\Enum\KeyTypeEnum;
use Dynamite\Enum\ProjectionTypeEnum;
use Dynamite\Enum\ScalarAttributeTypeEnum;
use Dynamite\TableInterface;
use Dynamite\Schema\Attribute;

#[Groups(['group1'])] // groups can be used optionally with console command
final class UsersTable extends AbstractTable implements TableInterface
{
    protected function configure(): void
    {
        $this
            ->setTableName('Users')
            ->addAttributes([
                new Attribute('Id', ScalarAttributeTypeEnum::String, KeyTypeEnum::Hash),
                new Attribute('Email', ScalarAttributeTypeEnum::String),
            ])
            ->addGlobalSecondaryIndex(
                'Emails',
                ProjectionTypeEnum::KeysOnly,
                'Email'
            )
            ->setProvisionedThroughput(1, 1)
        ;
    }
}

Create a fixture loading class

Fixture classes must implement Dynamite\FixtureInterface interface to be visible for a loader.

<?php

declare(strict_types=1);

namespace Fixtures;

use Dynamite\AbstractFixture;
use Dynamite\Attribute\Groups;
use Dynamite\FixtureInterface;
use Dynamite\Schema\Record;
use Dynamite\Schema\Value;

#[Groups(['group1'])] // groups can be used optionally with console command
final class UserFixtures extends AbstractFixture implements FixtureInterface
{
    protected function configure(): void
    {
        $this
            ->setTableName('Users')
            ->addRecords([
                new Record([
                    Value::stringValue('Id', 'e5502ec2-42a7-408b-9f03-f8e162b6257e'),
                    Value::stringValue('Email', 'john.doe@example.com'),
                    Value::boolValue('Active', true),
                ]),
                new Record([
                    Value::stringValue('Id', 'f0cf458c-4fc0-4dd8-ba5b-eca6dba9be63'),
                    Value::stringValue('Email', 'robert.smith@example.com'),
                    Value::boolValue('Active', true),
                ]),
            ])
        ;
    }
}

Tables and fixtures loading

It's possible to provide fixtures loading path:

<?php

declare(strict_types=1);

use Dynamite\Loader;
use Dynamite\Serializer\PropertyNameConverter;
use Symfony\Component\Serializer\Normalizer\BackedEnumNormalizer;
use Symfony\Component\Serializer\Normalizer\ObjectNormalizer;
use Symfony\Component\Serializer\Serializer;
use Symfony\Component\Validator\Mapping\Loader\AnnotationLoader;
use Symfony\Component\Validator\Validation;

$validator = Validation::createValidatorBuilder()
    ->addLoader(new AnnotationLoader())
    ->getValidator()
;
$serializer = new Serializer([
    new BackedEnumNormalizer(),
    new ObjectNormalizer(null, new PropertyNameConverter()),
]);

$loader = new Loader($validator, $serializer);
$loader->loadFromDirectory('/path/to/YourFixtures');

or loading each fixture or table class manually:

<?php

declare(strict_types=1);

$loader->addTable(new \App\Fixtures\UsersTable());
$loader->addFixture(new \App\Fixtures\UserFixtures());

Create tables and executing fixtures

To create database schema and load the fixtures in storage you should do the following:

<?php

declare(strict_types=1);

use Dynamite\Client;
use Dynamite\Executor;
use Dynamite\Loader;
use Dynamite\Serializer\PropertyNameConverter;
use Symfony\Component\Serializer\Normalizer\BackedEnumNormalizer;
use Symfony\Component\Serializer\Normalizer\ObjectNormalizer;
use Symfony\Component\Serializer\Serializer;
use Symfony\Component\Validator\Mapping\Loader\AnnotationLoader;
use Symfony\Component\Validator\Validation;

$validator = Validation::createValidatorBuilder()
    ->addLoader(new AnnotationLoader())
    ->getValidator()
;
$serializer = new Serializer([
    new BackedEnumNormalizer(),
    new ObjectNormalizer(null, new PropertyNameConverter()),
]);
$clientFactory = new ClientFactory($serializer);

$loader = new Loader($validator, $serializer);
$loader->loadFromDirectory('/path/to/YourFixtures');

$groups = ['group1']; // loading fixtures belong to the selected group only

$executor = new Executor($clientFactory->createAsyncAwsClient());
$executor->execute($loader->getFixtures($groups), $loader->getTables($groups));

Important! Each executor class comes with a purger class which executed before, drop tables and truncate data.

Load fixtures via console command

bin/console dynamite:fixtures:load --path path/to/fixtures

Debug logger

Execution process debug logs can be enabled by passing PSR-3 logger into executor:

<?php

declare(strict_types=1);

use Dynamite\Executor;

// PSR-3 compatible implementation of Psr\Log\LoggerInterface
$logger = new Logger();

$executor = new Executor($client, logger: $logger);

misantron/dynamite 适用场景与选型建议

misantron/dynamite 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 8.94k 次下载、GitHub Stars 达 2, 最近一次更新时间为 2022 年 06 月 02 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「php」 「dynamodb」 「data-fixtures」 「aws-sdk-php」 「async-aws」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

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

围绕 misantron/dynamite 我们能提供哪些服务?
定制开发 / 二次开发

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2022-06-02