定制 randomstate/laravel-doctrine-entity-events 二次开发

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

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

randomstate/laravel-doctrine-entity-events

Composer 安装命令:

composer require randomstate/laravel-doctrine-entity-events

包简介

Easily hook in and fire native laravel events with laravel-doctrine

README 文档

README

This package provides a simple way to hook into Doctrine2 Entity events and remap them to native Laravel events.

Getting Started

  • Install using composer require randomstate/laravel-doctrine-entity-events
  • Add RandomState\LaravelDoctrineEntityEvents\LaravelDoctrineEntityEventsServiceProvider::class to the providers section of config/app.php

Usage

Configuration

Helper Method (recommended)

<?php

use RandomState\LaravelDoctrineEntityEvents\EventRedirector;

public class AppServiceProvider extends ServiceProvider {

    public function register() {
        EventRedirector::register(function(EventRedirector $redirector) {
           $redirector->redirect(MyEntity::class)
            ->postPersist(MyEntityWasCreated::class)
            ->postUpdate(MyEntityWasUpdated::class)
            ->postFlush() // falls back to default as no destination is provided
            ->default(SomethingHappenedToMyEntityEvent::class); 
        });
    }
}

Intercept Service Instantiation

<?php

use RandomState\LaravelDoctrineEntityEvents\EventRedirector;

public class AppServiceProvider extends ServiceProvider {

    public function register() {
        $this->app->resolving(EventRedirector::class, (function(EventRedirector $redirector) {
           $redirector->redirect(MyEntity::class)
            ->postPersist(MyEntityWasCreated::class)
            ->postUpdate(MyEntityWasUpdated::class)
            ->postFlush() // falls back to default as no destination is provided
            ->default(SomethingHappenedToMyEntityEvent::class); 
        }));
    }
}

Events

Every Laravel Event you specify as a destination will be supplied the entity and doctrine event arguments on creation. The entity is supplied as the first argument so you can conveniently ignore other event arguments when you are only interested in the entity itself.

<?php

class MyEntityWasCreated {
    
    public function __construct(MyEntity $entity, LifecycleEventArgs $eventArgs) {
        // do something
    }
    
    public function handle() {
        // do something
    }
    
}

Advanced Usage

If you need to customise the way the event is instantiated, supply a closure as your 'destination' when defining the redirects.

<?php

EventRedirector::register(function(EventRedirector $redirector) {
           $redirector->redirect(MyEntity::class)
            ->postPersist(MyEntityWasCreated::class)
            ->postUpdate(function(MyEntity $entity) {
                $mailer = app('mailer');
                event(new MyEntityWasUpdated($entity, $mailer)); // customised instantiation
            })
            ->postFlush() // falls back to default as no destination is provided
            ->default(SomethingHappenedToMyEntityEvent::class); 
        });

统计信息

  • 总下载量: 109
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 6
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2018-03-02

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固