asika/object-metadata

PHP 全局对象元数据管理

1.0 2024-01-23 07:24 UTC

This package is auto-updated.

Last update: 2024-08-23 08:56:23 UTC


README

GitHub GitHub Workflow Status Packagist Downloads Packagist Version

Object Metadata 是一个帮助开发者管理自定义全局对象元数据的包。此包使用 WeakMap 来控制数据与对象实例之间的映射。

安装

composer require asika/object-metadata

入门

基本用法

use Asika\ObjectMetadata\ObjectMetadata;

// Create any objects
$obj = new ArticleEntity();

// Get global main instance
$meta = ObjectMetadata::getInstance();

// Set custom metadata
$meta->set($obj, 'foo', 'Hello');

// Now you can get the data everywhere if this object still exists and not destruct yet
ObjectMetadata::getInstance()->get($obj, 'foo'); // Hello

// Available methods
$meta->get($obj, 'key');
$meta->set($obj, 'key', 'value');
$meta->has($obj, 'key');
$meta->remove($obj, 'key');
$meta->getMetadata($obj, 'key'); // array
$meta->setMetadata($obj, 'key', $data);

使用包装器

$obj = new ArticleEntity();
$meta = ObjectMetadata::getInstance();

$metaWrapper = $meta->wrapper($obj);
$metaWrapper->set('foo', 'Hello');

$metaWrapper->get('key');
$metaWrapper->has('key');
$metaWrapper->remove('key');
$metaWrapper->all();

// Array Access
$metaWrapper['key'] = 'value';

// If object destructed, getting metadata will be NULL
unset($obj);

$metaWrapper->get('foo'); // NULL

作用域

ObjectMetadata 能够分离不同的作用域。

$meta = ObjectMetadata::getInstance('main'); // Main scope

$appMeta = ObjectMetadata::getInstance('app');

$dbMeta = ObjectMetadata::getInstance('db');

实际用途是什么

此包的有用案例是我们可以将某些实体对象或值对象转换为富对象。例如,如果一个 ORM 使用数据映射模式,它们的实体对象将是一个贫血对象,可能不会保留 ORM 实例。

$item = new Article();

$item = $orm->createOne($item);

// This item will only contains pure data

如果 ORM 使用此包作为实体元数据,我们可以将 Article 实体转换为富对象,并具有获取其他对象的能力。

$orm->on('entity.prepare', function (object $entity, ORM $orm) {
    ObjectMetadata::getInstance('db')->set($entity, 'orm', $orm);
});

class Article 
{
    // ...

    // Article can use ObjectMetadata to get ORM instance.
    public function getComments() {
        $orm = ObjectMetadata::getInstance('db')->get($this, 'orm');
        
        return $orm->from(Comment::class)
            ->where('article_id', $this->getId())
            ->all();
    }
}

// Now we can test it
$article = $orm->createEntity(Article::class);

$item = $orm->createOne($item);

// Article is able to call ORM to get another items from DB
$item->getComments();
$item->getAuthor();
$item->getTags();