-
-
Notifications
You must be signed in to change notification settings - Fork 1.9k
/
RegistersCustomConcerns.php
39 lines (34 loc) 路 1.1 KB
/
RegistersCustomConcerns.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
<?php
namespace Maatwebsite\Excel;
use Maatwebsite\Excel\Events\AfterSheet;
use Maatwebsite\Excel\Events\BeforeExport;
use Maatwebsite\Excel\Events\BeforeSheet;
use Maatwebsite\Excel\Events\BeforeWriting;
use Maatwebsite\Excel\Events\Event;
trait RegistersCustomConcerns
{
/**
* @var array
*/
private static $eventMap = [
BeforeWriting::class => Writer::class,
BeforeExport::class => Writer::class,
BeforeSheet::class => Sheet::class,
AfterSheet::class => Sheet::class,
];
/**
* @param string $concern
* @param callable $handler
* @param string $event
*/
public static function extend(string $concern, callable $handler, string $event = BeforeWriting::class)
{
/** @var HasEventBus $delegate */
$delegate = static::$eventMap[$event] ?? BeforeWriting::class;
$delegate::listen($event, function (Event $event) use ($concern, $handler) {
if ($event->appliesToConcern($concern)) {
$handler($event->getConcernable(), $event->getDelegate());
}
});
}
}