evispa/object-migration

对象版本之间的迁移,可以通过注解进行配置。

dev-master 2013-09-12 22:19 UTC

This package is auto-updated.

Last update: 2024-09-07 21:31:12 UTC


README

构建状态: Build Status

概述

根据版本注解将对象迁移到不同的版本。

例如,假设我们有两个类版本,第二个版本将公共字段名称从 "slug" 更改为 "id"

class V1
{
    public $slug;
}

class V2
{
    public $id;
}

然后我们可以使用版本注解装饰这些类,并创建到新版本的新迁移方法

use Evispa\ObjectMigration\Annotations as Api;

/**
 * @Api\Version("object.v1")
 */
class V1
{
    public $slug;
}

/**
 * @Api\Version("object.v2")
 */
class V2
{
    public $id;

    /**
     * @Api\Migration(from="V1")
     */
    public static function fromV1(V1 $other, $options) {
        $obj = new self();

        $obj->id = $other->slug;

        return $obj;
    }
}

使用 VersionConverter 工具检查对象版本或将对象迁移到其他可用的版本

use Doctrine\Common\Annotations\AnnotationReader;
use Evispa\ObjectMigration\VersionReader;
use Evispa\ObjectMigration\VersionConverter;

$converter = new VersionConverter(new VersionReader(new AnnotationReader()), 'V2');

// create v1 object

$v1 = new V1();
$v1->slug = "TEST";

// migrate to another version

$v2 = $converter->migrateFrom($v1);

$this->assertTrue($v2 instanceof V2); // true
$this->assertEquals("TEST", $v2->id); // true

要求

此库将使用

  • doctrine/annotations 进行注解解析
  • clue/graph 进行版本迁移搜索

安装

此库可以通过 composer 简单安装

composer require evispa/object-migration

或直接将其添加到您的 composer.json 文件中。