Migrate objects to different versions based on version annotations.
For example, let's say we have two class versions, the second one modifies public field name from "slug" to "id":
class V1
{
public $slug;
}
class V2
{
public $id;
}
We can then decorate these classes with version annotations, and create migration method to the new version:
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;
}
}
Use VersionConverter tool to check object version or migrate object to another available version:
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
This library will use:
- doctrine/annotations for annotation parsing
- clue/graph for version migrations search
This library can be easily installed via composer
composer require evispa/object-migration
or just add it to your composer.json
file directly.