定制 dittto/doctrine-entity-factories 二次开发

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

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

dittto/doctrine-entity-factories

Composer 安装命令:

composer require dittto/doctrine-entity-factories

包简介

Allows doctrine to pull entities from factories instead of just reflection

README 文档

README

With it's usage of simple PHP objects for entities, Doctrine is a very easy-to-use ORM. It instantiates these entities using reflection. If you want to use entities that rely on another object via dependency injection, however, then you're stuck with setter injection.

This code allows you to pull new entities from a factory instead, allowing constructor injection and cleaner code.

How to use

First up, install this plugin.

composer require dittto/doctrine-entity-factories

Next, let's create a factory for our entity. These needs to implement EntityFactoryInterface.

<?php
namespace App\Entities\Factories;

use App\Entities\TestEntity;
use Dittto\DoctrineEntityFactories\Doctrine\ORM\Mapping\EntityFactoryInterface;
use Illuminate\Contracts\Validation\Validator;

class TestEntityFactory implements EntityFactoryInterface
{
    private $validator;

    public function __construct(Validator $validator)
    {
        $this->validator = $validator;
    }

    public function getEntity()
    {
        return new TestEntity($this->validator);
    }
}

The more-common framework to use Doctrine with is Symfony, but you can also use Doctrine with Laravel. The following are instructions for how to use it with both.

With Laravel

To use Doctrine with Laravel, you can use this helpful plugin:

composer require laravel-doctrine/orm:1.3.*
php artisan vendor:publish --tag="config"

We're going to use a custom provider and one that exists within this plugin. The custom one is as follows:

<?php
namespace App\Providers;

use App\Entities\Factories\TestEntityFactoryInterface;
use App\Entities\TestEntity;
use Dittto\DoctrineEntityFactories\Doctrine\ORM\Mapping\EntityFactoryAware;
use Dittto\DoctrineEntityFactories\Doctrine\ORM\Provider\AbstractEntityFactoryServiceProvider;

class EntityFactoryServiceProvider extends AbstractEntityFactoryServiceProvider
{
    public function registerEntityFactories(EntityFactoryAware $entityFactoryRegister)
    {
        $entityFactoryRegister->addEntityFactory(
            TestEntity::class,
            new TestEntityFactory($this->app->make('hash'))
        );
    }
}

This provider defers all objects. You can also convert that new TestEntityFactory into another object easily by extending register() and provides().

Next, we'll add the providers to the main app config:

<?php
// config/app.php
return [
'providers' => [
        App\Providers\DoctrineServiceProvider::class,
        \Dittto\DoctrineEntityFactories\Doctrine\ORM\Provider\DoctrineServiceProvider::class,
    ],
];

Lastly, we'll need to alter the doctrine config to use our plugin:

<?php
return [
    'managers' => [
        'default' => [
            'meta' => env('DOCTRINE_METADATA', 'yaml_with_entity_factories'),
        ]
    ]
];

With Symfony

As with most things with Symfony, we can use config for pretty much most things.

To start with, we're going to want to tell Symfony to use our metadata factory instead of the default Doctrine one:

# app/config/config.yml
doctrine:
    orm:
        entity_managers:
            default:
                class_metadata_factory_name: Dittto\DoctrineEntityFactories\Doctrine\ORM\Mapping\ClassMetadataFactoryWithEntityFactories

If you're using multiple entity managers, this may change a bit, but you'll probably also have the understanding to make this work for your code.

Laravel, above, allows us to easily load additional code via it's service providers. For Symfony, we're going to take a slightly different approach and decorate Doctrine's EntityManager. The aim of both of these approaches is the same - to add our entity factories to it before Doctrine starts creating entities.

For Symfony, we're going to add some new services:

services:
    dittto.doctrine_entity_factories.entity_factory_manager_decorator:
        public: false
        class: Dittto\DoctrineEntityFactories\Doctrine\ORM\Decorator\EntityFactoryManagerDecorator
        decorates: doctrine.orm.default_entity_manager
        arguments: [ "@dittto.doctrine_entity_factories.entity_factory_manager_decorator.inner" ]
        calls:
            - [addEntityFactory, ['App\Entities\TestEntity', '@app.entities.factory.test']]

    app.entities.factory.test:
        class: App\Entities\Factories\Testfactory
        arguments: ['@validator']

It's here we'll use the calls definition to add in as many entity factories as we require. The first field is the full class name of the entity to be created by the factory.

The namespaces here look non-standard for Symfony, but that's purely to tie into the example above.

Testing

This plugin comes with it's own tests. To run these, clone the code and navigate to the directory. Then run the following:

composer install
./vendor/bin/phpunit

dittto/doctrine-entity-factories 适用场景与选型建议

dittto/doctrine-entity-factories 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 6.3k 次下载、GitHub Stars 达 3, 最近一次更新时间为 2017 年 08 月 21 日, 在 PHP 生态内属于活跃度较高的组件。

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

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

围绕 dittto/doctrine-entity-factories 我们能提供哪些服务?
定制开发 / 二次开发

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

  • 总下载量: 6.3k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 3
  • 点击次数: 19
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 3
  • Watchers: 0
  • Forks: 3
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2017-08-21