22
33namespace Maatwebsite \Excel \Concerns ;
44
5+ use Maatwebsite \Excel \Events \AfterBatch ;
6+ use Maatwebsite \Excel \Events \AfterChunk ;
57use Maatwebsite \Excel \Events \AfterImport ;
68use Maatwebsite \Excel \Events \AfterSheet ;
79use Maatwebsite \Excel \Events \BeforeExport ;
@@ -17,34 +19,25 @@ trait RegistersEventListeners
1719 */
1820 public function registerEvents (): array
1921 {
22+ $ listenersClasses = [
23+ BeforeExport::class => 'beforeExport ' ,
24+ BeforeWriting::class => 'beforeWriting ' ,
25+ BeforeImport::class => 'beforeImport ' ,
26+ AfterImport::class => 'afterImport ' ,
27+ AfterBatch::class => 'afterBatch ' ,
28+ AfterChunk::class => 'afterChunk ' ,
29+ ImportFailed::class => 'importFailed ' ,
30+ BeforeSheet::class => 'beforeSheet ' ,
31+ AfterSheet::class => 'afterSheet ' ,
32+ ];
2033 $ listeners = [];
2134
22- if (method_exists ($ this , 'beforeExport ' )) {
23- $ listeners [BeforeExport::class] = [static ::class, 'beforeExport ' ];
24- }
25-
26- if (method_exists ($ this , 'beforeWriting ' )) {
27- $ listeners [BeforeWriting::class] = [static ::class, 'beforeWriting ' ];
28- }
29-
30- if (method_exists ($ this , 'beforeImport ' )) {
31- $ listeners [BeforeImport::class] = [static ::class, 'beforeImport ' ];
32- }
33-
34- if (method_exists ($ this , 'afterImport ' )) {
35- $ listeners [AfterImport::class] = [static ::class, 'afterImport ' ];
36- }
37-
38- if (method_exists ($ this , 'importFailed ' )) {
39- $ listeners [ImportFailed::class] = [static ::class, 'importFailed ' ];
40- }
41-
42- if (method_exists ($ this , 'beforeSheet ' )) {
43- $ listeners [BeforeSheet::class] = [static ::class, 'beforeSheet ' ];
44- }
45-
46- if (method_exists ($ this , 'afterSheet ' )) {
47- $ listeners [AfterSheet::class] = [static ::class, 'afterSheet ' ];
35+ foreach ($ listenersClasses as $ class => $ name ) {
36+ // Method names are case insensitive in php
37+ if (method_exists ($ this , $ name )) {
38+ // Allow methods to not be static
39+ $ listeners [$ class ] = [$ this , $ name ];
40+ }
4841 }
4942
5043 return $ listeners ;
0 commit comments