定制 rekalogika/reconstitutor 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

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

rekalogika/reconstitutor

Composer 安装命令:

composer require rekalogika/reconstitutor

包简介

A thin layer above Doctrine events to help you reconstitute/hydrate your entities. The most common example being handling file uploads, but also many other purposes. It lets you augment Doctrine's hydration with your logic in a concise and expressive class.

README 文档

README

This library provides a thin layer that sits above Doctrine events to help you reconstitute/hydrate your entities. It lets you augment Doctrine's hydration with your logic in a concise and expressive class.

Full documentation: rekalogika.dev/reconstitutor

After Doctrine hydrates an object from the database, this framework gives you the control to hydrate additional properties not handled by Doctrine, without having to deal with the peculiarities of Doctrine events and Unit of Work. Then, after Doctrine persists the changes to the database, it lets you do similarly with the properties.

The most common case of this type of tasks is for handling file uploads, of which many specialized libraries have already been written. But plenty of other cases exist:

  • A lazy-loading proxy that fetches the real resource using an API call.
  • Linking objects that are managed by different object managers, or non-Doctrine entities.

These days, we usually call the process hydration. Reconstitution is the term used by Eric Evans in "Domain-Driven Design: Tackling Complexity in the Heart of Software".

Features

  • Streamlines a very specific, yet very common use case of Doctrine events.
  • Simple declaration in a class. You can create a reconstitutor class to handle the reconstitution of a specific entity class, entities that implement a specific interface, entities in a class hierarchy, or those with a specific PHP attribute.
  • Our abstract classes provide get() and set() methods as a convenience. They let you work with the properties directly, bypassing getters and setters. It is the best practice in reconstitutions as it frees you to have business logic in the getters and setters.
  • The get() and set() methods are forwarders to a custom implementation of Symfony's PropertyAccessorInterface. Therefore, you can use the same exceptions defined in PropertyAccessorInterface.
  • Abstracts all the peculiarities of Doctrine events, unit of work, database transactions, and proxy, so you don't have to find out and deal with them yourself.
  • It asks your reconstitutor to save only after Doctrine has successfully saved the object.
  • Does not rely on Doctrine seeing the object being dirty before flush()-ing. i.e. your entities don't have to modify a Doctrine-managed property —like $lastUpdated— just to make sure the correct Doctrine event will be fired.
  • Handles transactions. In a transaction, it will only ask your reconstitutor to save or remove after the transaction has been successfully committed.

Example

A very simple file upload handling:

use Rekalogika\Reconstitutor\AbstractClassReconstitutor;
use Symfony\Component\HttpFoundation\File\File;
use Symfony\Component\HttpFoundation\File\UploadedFile;

/**
 * @extends AbstractClassReconstitutor<Order>
 */
final class OrderReconstitutor extends AbstractClassReconstitutor
{
    /**
     * The class that this reconstitutor manages. It can also be a super class
     * or an interface.
     */
    public static function getClass(): string
    {
        return Order::class;
    }

    /**
     * When the object is being saved, we check if the paymentReceipt has been
     * just uploaded. If it is, we save it to a file.
     * 
     * Note: in Symfony, an uploaded file is represented by an instance of
     * `UploadedFile`, otherwise it will be a `File` object.
     */
    public function onSave(object $order): void
    {
        $path = sprintf('/tmp/payment_receipt/%s', $order->getId());

        $file = $this->get($order, 'paymentReceipt');

        if ($file instanceof UploadedFile) {
            file_put_contents($path, $file->getContent());
            $this->set($order, 'paymentReceipt', new File($path));
        }
    }

    /**
     * When the object is being loaded from the database, we check if the
     * supposed payment receipt is already saved. If it is, then we load the
     * file to the property.
     */
    public function onLoad(object $order): void
    {
        $path = sprintf('/tmp/payment_receipt/%s', $order->getId());

        if (file_exists($path)) {
            $file = new File($path);
        } else {
            $file = null;
        }

        $this->set($order, 'paymentReceipt', $file);
    }

    /**
     * If the order is being removed, we remove the associated payment receipt
     * here.
     */
    public function onRemove(object $order): void
    {
        $path = sprintf('/tmp/payment_receipt/%s', $order->getId());

        if (file_exists($path)) {
            unlink($path);
        }
    }
}

Documentation

rekalogika.dev/reconstitutor

License

MIT

Contributing

Issues and pull requests should be filed in the GitHub repository rekalogika/reconstitutor.

rekalogika/reconstitutor 适用场景与选型建议

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

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

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

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

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

GitHub 信息

  • Stars: 5
  • Watchers: 2
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2023-09-03