-
Notifications
You must be signed in to change notification settings - Fork 38
/
Extensions.pm
969 lines (764 loc) · 26.1 KB
/
Extensions.pm
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
# See bottom of file for license and copyright information
package Foswiki::Extensions;
=begin TML
---++!! Class Foswiki::Extensions
[[https://foswiki.org/Development/OONewPluginModel][Foswiki OONewPluginModel topic]]
could serve as a temporary explanasion of why this module extists and what
functionality it is expected to provide.
=cut
use File::Spec ();
use IO::Dir ();
use Devel::Symdump ();
use Scalar::Util qw(blessed);
use Assert;
use Try::Tiny;
use Data::Dumper;
use Foswiki::Exception;
# Constants for topological sorting.
use constant NODE_TEMP_MARK => 0;
use constant NODE_PERM_MARK => 1;
use constant NODE_DISABLED => -1;
use Foswiki::Class qw(app callbacks);
extends qw(Foswiki::Object);
# This is the version to be matched agains extension's API version declaration.
# Extension is considered valid only if it's API version is no higher than
# $VERSION.
use version 0.77; our $VERSION = version->declare("2.99.0");
# The minimal API version this module supports. If an extension declares API
# version lower than this it gets rejected.
our $MIN_VERSION = version->declare("2.99.0");
# --- Static data registered upon extension's module load and to be parsed when
# extensions are built.
# NOTE All data stored in globals is raw and must be revalidated before used.
our @extModules
; # List of the extension modules in the order they were registered with registerExtModule().
our %registeredModules; # Modules registered with registerExtModule().
our %extSubClasses; # Subclasses registered by extensions.
our %extDeps; # Module dependecies; defines the order of extensions.
our %extTags; # Tags registered by extensions.
our %extCallbacks; # Callbacks registered by extensions.
our %pluggables; # Pluggable methods
our %plugMethods; # Extension registered plug methods.
# --- END of static data declarations
has extensions => (
is => 'rw',
lazy => 1,
builder => 'prepareExtensions',
);
has extSubDirs => (
is => 'rw',
lazy => 1,
builder => 'prepareExtSubDirs',
);
=begin TML
---++ ObjectAttribute extPrefix => string
Extension modules name prefix. Used by =normalizeExtName()= method and for locating extension by their =.pm= files.
Default: Foswiki::Extension.
=cut
has extPrefix => (
is => 'ro',
default => 'Foswiki::Extension',
);
=begin TML
---++ ObjectAttribute dependecies => hashref
Keys: extension module names
Values: list of extensions modules required by the key's module.
=cut
has dependencies => (
is => 'rw',
lazy => 1,
builder => 'prepareDependencies',
);
=begin TML
---++ ObjectAttribute orderedList => arrayref
List of extensions presorted to confirm with their dependencies.
=cut
has orderedList => (
is => 'ro',
lazy => 1,
clearer => 1,
builder => 'prepareOrderedList',
);
=begin TML
---++ ObjectAttribute registeredClasses => hashref
Map of core classes into list of overriding subclasses.
=cut
has registeredClasses => (
is => 'ro',
lazy => 1,
clearer => 1,
builder => 'prepareRegisteredClasses',
);
has registeredMethods => (
is => 'rw',
lazy => 1,
clearer => 1,
builder => 'prepareRegisteredMethods',
);
has _errors => (
is => 'rw',
lazy => 1,
default => sub { [] },
);
# Hashref of disabled extensions. Keys are extension names, values – reason
# descriptions.
has disabledExtensions => (
is => 'ro',
lazy => 1,
clearer => 1,
builder => 'prepareDisabledExtensions',
);
sub BUILD {
my $this = shift;
$this->loadExtensions;
}
sub normalizeExtName {
my $this = shift;
my ($extName) = @_;
unless ( $extName =~ /::/ ) {
# Attempt to load en extension by its short name.
$extName = $this->extPrefix . "::" . $extName;
}
return $extName;
}
sub extEnabled {
my $this = shift;
my ($ext) = @_;
my $extName = $this->normalizeExtName($ext);
return defined $this->disabledExtensions->{$extName} ? undef : $extName;
}
sub extObject {
my $this = shift;
my ($ext) = @_;
my $extName = $this->normalizeExtName($ext);
return $this->extensions->{$extName};
}
sub isBadVersion {
my $this = shift;
my ($extName) = @_;
return "Extension module $extName not a subclass of Foswiki::Extension"
unless $extName->isa('Foswiki::Extension');
my @apiScalar = grep { /::API_VERSION$/ } Devel::Symdump->scalars($extName);
return "No \$API_VERSION scalar defined in $extName"
unless @apiScalar;
my $api_ver = Foswiki::fetchGlobal( '$' . $apiScalar[0] );
return "Failed to fetch \$API_VERSION"
unless defined $api_ver;
return
"Declared API version "
. $api_ver
. " is lower than supported "
. $MIN_VERSION
if $api_ver < $MIN_VERSION;
return
"Declared API version "
. $api_ver
. " is higher than supported "
. $VERSION
if $api_ver > $VERSION;
return '';
}
sub _loadExtModule {
my $this = shift;
my ($extModule) = @_;
return if isRegistered($extModule);
try {
Foswiki::load_class($extModule);
registerExtModule($extModule);
}
catch {
Foswiki::Exception::Ext::Load->rethrow(
$_,
extension => $extModule,
reason => Foswiki::Exception::errorStr($_),
);
};
}
sub _loadFromSubDir {
my $this = shift;
my ($subDir) = @_;
my $extDir =
IO::Dir->new(
File::Spec->catdir( $subDir, split( /::/, $this->extPrefix ) ) );
Foswiki::Exception::FileOp->throw(
file => $subDir,
op => "opendir",
) unless defined $extDir;
while ( my $dirEntry = $extDir->read ) {
next if -d $dirEntry || $dirEntry !~ /\.pm$/;
# SMELL $dirEntry is tainted but this we must take care of later.
my $extModule;
try {
if ( $dirEntry =~
/^(\w+)\.pm$/a ) # We match against ASCII symbols only.
{
$extModule = $this->normalizeExtName($1);
$this->_loadExtModule($extModule);
}
else {
# SMELL Bad extension file name, shall we do something about it?
# Note that loggins isn't possible yet. But we can rely upon
# server logging perhaps.
Foswiki::Exception::Ext::BadName->throw(
extension => $dirEntry );
}
}
catch {
# We don't really fail upon extension load because this isn't fatal
# in neither way. What bad could unloaded extension cause?
push @{ $this->_errors },
Foswiki::Exception::Ext::Load->transmute( $_, 1,
extension => $extModule );
say STDERR "Extension $extModule problem: \n",
Foswiki::Exception::errorStr( $this->_errors->[-1] );
};
}
}
sub loadExtensions {
my $this = shift;
foreach my $extDir ( @{ $this->extSubDirs } ) {
$this->_loadFromSubDir($extDir);
}
}
sub initialize {
my $this = shift;
# Register macro tag handlers for enabled extensions.
foreach my $tag ( keys %extTags ) {
if ( $this->extEnabled( $extTags{$tag}{extension} ) ) {
my $handler = $extTags{$tag}{class} // $extTags{$tag}{extension};
$this->app->macros->registerTagHandler( $tag, $handler );
}
}
# Register callback handlers for enabled extensions.
foreach my $cbName ( keys %extCallbacks ) {
foreach my $cbData ( @{ $extCallbacks{$cbName} } ) {
$this->registerCallback(
$cbName,
\&_cbDispatch,
{
extension => $cbData->{extension},
app => $this->app,
userCode => $cbData->{code},
}
);
}
}
}
sub _cbDispatch {
my $cbObj = shift; # The object initiated the callback.
my %params = @_;
my $app = $params{data}{app};
my $extObj = $app->extensions->extObject( $params{data}{extension} );
return $params{data}{userCode}->( $extObj, $cbObj, $params{params} );
}
sub _extVisit {
my $this = shift;
my %params = @_;
my $marked = $params{marked};
my $depHash = $params{depHash};
my $extName = $params{extName};
my $visitPath = $params{_visitPath} // [];
my @list;
if (
defined $marked->{$extName}
&& ( $marked->{$extName} == NODE_TEMP_MARK
|| $marked->{$extName} == NODE_DISABLED )
)
{
state $nType = {
&NODE_TEMP_MARK => "Circular dependecy found for",
&NODE_DISABLED => "Disabled extension",
};
my $disableMsg =
$nType->{ $marked->{$extName} } . " "
. $extName
. " in dependecy chain: "
. join( " -> ", @$visitPath, $extName );
# Disable all problematic extensions.
foreach my $disabledExt (@$visitPath) {
$marked->{$disabledExt} = NODE_DISABLED;
$this->disableExtension( $disabledExt, $disableMsg );
}
# Don't override the original disable message!
if ( $marked->{$extName} == NODE_TEMP_MARK ) {
$this->disableExtension( $extName, $disableMsg );
$marked->{$extName} = NODE_DISABLED;
}
return ();
}
unless ( $marked->{$extName} ) {
$marked->{$extName} = NODE_TEMP_MARK;
my @subList;
foreach my $depExt ( @{ $depHash->{$extName} } ) {
@subList = $this->_extVisit(
marked => $marked,
depHash => $depHash,
extName => $depExt,
_visitPath => [ @$visitPath, $extName ],
_level => ( $params{_level} // 0 ) + 1,
);
push @list, @subList;
}
unless ( $marked->{$extName} == NODE_DISABLED ) {
$marked->{$extName} = NODE_PERM_MARK;
push @list, $extName;
}
}
return @list;
}
sub _topoSort {
my $this = shift;
my ( $order, $depHash ) = @_;
# Marked nodes:
# undef – not visited yet.
# defined – NODE_* constants.
my %marked;
my @list;
foreach my $node (@$order) {
# Support manually disabled extensions.
$marked{$node} = NODE_DISABLED unless $this->extEnabled($node);
# At this stage there must be no temporary marks.
Foswiki::Exception::Fatal->throw(
text => "Temp. mark for node $node is impossible here" )
if defined $marked{$node} && $marked{$node} == NODE_TEMP_MARK;
next if $marked{$node};
push @list,
$this->_extVisit(
marked => \%marked,
depHash => $depHash,
extName => $node
);
}
return @list;
}
sub prepareOrderedList {
my $this = shift;
my @orderedExtList =
$this->_topoSort( [ map { $this->normalizeExtName($_) } @extModules ],
$this->dependencies );
return \@orderedExtList;
}
sub prepareExtensions {
my $this = shift;
my $app = $this->app;
my $extensions = {};
foreach my $ext ( @{ $this->orderedList } ) {
$extensions->{$ext} = $app->create($ext);
}
return $extensions;
}
sub prepareExtSubDirs {
my $this = shift;
my $extLibs = $this->app->env->{FOSWIKI_EXTLIBS};
my @extPath;
if ( defined $extLibs ) {
push @extPath, split /:/, $extLibs;
}
else {
my $fwPath = $this->app->env->{FOSWIKI_LIBS};
# If the env is not set guess by Foswiki.pm module.
$fwPath //= ( File::Spec->splitpath( $INC{'Foswiki.pm'} ) )[1];
push @extPath, $fwPath;
}
# Add extra extension subdirs to @INC but make sure not to duplicate with
# existins entries.
my %incDirs = map { File::Spec->rel2abs($_) => 1 } @INC;
foreach my $pth (@extPath) {
push @INC, $pth unless $incDirs{ File::Spec->rel2abs($pth) };
}
return \@extPath;
}
=begin TML
---++ ObjectMethod disableExtension( $extName, $reason )
Marks extension =$extName= as disable because of =$reason=.
=cut
sub disableExtension {
my $this = shift;
my ( $extName, $reason ) = @_;
ASSERT(
defined $extName,
"Undefined extension name in call to "
. ref($this)
. "::disableExtension method"
);
ASSERT(
defined $reason,
"Undefined reason in call to "
. ref($this)
. "::disableExtension method"
);
#$this->app->logger->warn("Disabling $extName because of: $reason");
$this->disabledExtensions->{ $this->normalizeExtName($extName) } = $reason;
}
=begin TML
---++ ObjectMethod mapClass($class) => $replacement
Maps a core class name into replacement class name.
=cut
sub mapClass {
my $this = shift;
my ($class) = @_;
$class = ref($class) || $class;
my $replClass = $this->registeredClasses->{$class};
return $replClass || $class;
}
=begin TML
---++ ObjectMethod prepareDisabledExtensions => \%disabled
Returns extensions disabled for this installation or host. %disabled hash keys
are extension names, values are text reasons for disabling the extension.
*NOTE* Extension =Foswiki::Extension::Empty= is hard coded into the list of
disabled extensions because its purpose is to be a template for developing
functional extensions.
=cut
sub prepareDisabledExtensions {
my $this = shift;
my $env = $this->app->env;
my $envVar = 'FOSWIKI_DISABLED_EXTENSIONS';
my $envDisabled = $env->{$envVar} // '';
my %disabled;
if ( my $reftype = ref($envDisabled) ) {
Foswiki::Exception::Fatal->throw(
text => "Environment variable $envVar is a ref to "
. $reftype
. " but ARRAY or scalar string expected" )
unless $reftype eq 'ARRAY';
}
else {
$envDisabled = [ split /,/, $envDisabled ];
}
# Never enable extension Empty. It's purpose is to serve as a template only.
push @$envDisabled, 'Empty';
%disabled =
map {
$this->normalizeExtName($_) =>
"Disabled by $envVar environment variable."
} @$envDisabled;
foreach my $ext (@extModules) {
my $extMod = $this->normalizeExtName($ext);
my $reason;
unless ( $disabled{$extMod} ) {
# Disable API-incompatible modules
$reason = $this->isBadVersion($extMod);
if ($reason) {
$disabled{$extMod} = $reason;
}
}
}
return \%disabled;
}
sub prepareDependencies {
my $this = shift;
my %nDeps; # Normalized dependecy hash.
foreach my $ext ( keys %extDeps ) {
my $extName = $this->normalizeExtName($ext);
my @deps = map { $this->normalizeExtName($_) } @{ $extDeps{$ext} };
push @{ $nDeps{$extName} }, @deps;
}
return \%nDeps;
}
# Build mapping of core classes into overriding classes based on the ordered
# extension list.
sub prepareRegisteredClasses {
my $this = shift;
my %classMap;
my %ext2class;
foreach my $coreClass ( keys %extSubClasses ) {
foreach my $registration ( @{ $extSubClasses{$coreClass} } ) {
my $extName = $this->extEnabled( $registration->{extension} );
next unless $extName;
if ( defined $ext2class{$extName}{$coreClass} ) {
# That's not something we'd tolerate.
$this->disableExtension( $extName,
"$extName attepted double-registration for core class $coreClass"
);
next;
}
$ext2class{$extName}{$coreClass} = $registration->{subClass};
}
}
# Build inheritance order. Use reverse to conform with the way overriden
# methods are getting called.
my %inheritance;
foreach my $extName ( reverse @{ $this->orderedList } ) {
foreach my $coreClass ( keys %{ $ext2class{$extName} } ) {
push @{ $inheritance{$coreClass} },
$ext2class{$extName}{$coreClass};
}
}
# Build actual replacement classes.
foreach my $coreClass ( keys %inheritance ) {
$classMap{$coreClass} =
Moo::Role->create_class_with_roles( $coreClass,
@{ $inheritance{$coreClass} } );
}
return \%classMap;
}
sub prepareRegisteredMethods {
my $this = shift;
my %normPlugs;
# Rebuild %plugMethods using normalized extension names.
foreach my $ext ( keys %plugMethods ) {
my $extMod = $this->normalizeExtName($ext);
foreach my $target ( keys %{ $plugMethods{$ext} } ) {
foreach my $method ( keys %{ $plugMethods{$ext}{$target} } ) {
foreach
my $where ( keys %{ $plugMethods{$ext}{$target}{$method} } )
{
Foswiki::Exception::Fatal->throw(
text => "Duplicate registration of "
. $where
. " method "
. $target . "::"
. $method . " in "
. $extMod )
if defined $normPlugs{$extMod}{$target}{$method}{$where};
$normPlugs{$extMod}{$target}{$method}{$where} =
$plugMethods{$ext}{$target}{$method}{$where};
}
}
}
}
my %methodMap;
foreach my $ext ( @{ $this->orderedList } ) {
foreach my $target ( keys %{ $normPlugs{$ext} } ) {
foreach my $method ( keys %{ $normPlugs{$ext}{$target} } ) {
foreach
my $where ( keys %{ $normPlugs{$ext}{$target}{$method} } )
{
#say STDERR "Recording registered ", $where,
# " plug method ", $method, " on ", $target,
# " for extension ", $ext;
push @{ $methodMap{$target}{$method}{$where} },
{
extension => $ext,
code => $normPlugs{$ext}{$target}{$method}{$where},
};
}
}
}
}
return \%methodMap;
}
sub _execMethodList {
my $this = shift;
my ( $mList, $callParams ) = @_;
# Remember the original argument list pointer to avoid plugin modification.
# SMELL Shouldn't it be a list of protected keys?
my $origArgs = $callParams->{args};
# SMELL Use of the logger may cause issues if logger declares a pluggable
# method or methods.
my $logger = $this->app->logger;
my $extensions = $this->extensions;
my $restart;
do {
$restart = 0;
my $lastIteration = 0;
my ( $mIdx, $mEntry );
values @$mList; # Explicitly reset previous iterations over this array.
while ( !$lastIteration && ( ( $mIdx, $mEntry ) = each @$mList ) ) {
try {
$mEntry->{code}
->( $extensions->{ $mEntry->{extension} }, $callParams );
if ( $callParams->{args} != $origArgs ) {
$logger->warn(
"Extension ",
$mEntry->{extension},
" attempted to replace argument list in parameters hash."
);
$callParams->{args} = $origArgs;
}
}
catch {
my $e = Foswiki::Exception::Fatal->transmute( $_, 0 );
if ( $e->isa('Foswiki::Exception::Ext::Flow') ) {
if ( $e->isa('Foswiki::Exception::Ext::Last') ) {
$callParams->{rc} = $e->rc if $e->has_rc;
$callParams->{execAborted} =
{ extension => $mEntry->{extension}, };
$lastIteration = 1;
}
elsif ( $e->isa('Foswiki::Exception::Ext::Restart') ) {
$callParams->{execRestarted} =
{ extension => $mEntry->{extension}, };
$lastIteration = $restart = 1;
}
else {
Foswiki::Exception::Fatal->throw(
text => "Cannot handle flow control exception "
. ref($e)
. " thrown by "
. $mEntry->{extension} );
}
}
else {
$callParams->{execFailed} = {
extension => $mEntry->{extension},
exception => $e,
};
$e->rethrow;
}
};
# NOTE If any code would be needed at this point it must remember
# about $lastIteration.
}
} while ($restart);
}
sub _callPluggable {
my $this = shift;
my ( $target, $method, %params ) = @_;
my $origCode = $pluggables{$target}{$method};
ASSERT( defined $origCode,
"Pluggable method $method for $target is not defined" );
my $wantArray = wantarray;
my $callParams = {
wantarray => $wantArray,
class => $target,
method => $method,
args => $params{args},
object => $params{object},
};
my $registeredMethods = $this->registeredMethods;
if ( defined $registeredMethods->{$target}{$method} ) {
my $m = $registeredMethods->{$target}{$method};
foreach my $stage (qw(before around after)) {
$callParams->{stage} = $stage;
$this->_execMethodList( $m->{$stage}, $callParams );
if ( $stage eq 'before' ) {
# Remove errorneous rc set by an extension on `before' stage.
delete $callParams->{rc} if exists $callParams->{rc};
}
elsif ( $stage eq 'around' ) {
unless ( exists $callParams->{rc} ) {
if ($wantArray) {
$callParams->{rc} =
[ $origCode->( $params{object}, @{ $params{args} } )
];
}
elsif ( defined $wantArray ) {
$callParams->{rc} =
$origCode->( $params{object}, @{ $params{args} } );
}
else {
# As no return value is expected we don't set the rc key.
$origCode->( $params{object}, @{ $params{args} } );
}
}
}
}
if ($wantArray) {
return @{ $callParams->{rc} };
}
elsif ( defined $wantArray ) {
return $callParams->{rc};
}
return;
}
# When no extension registered for this method call the original directly.
return $origCode->( $params{object}, @{ $params{args} } );
}
# Universal methods supporting static, on class, and object calls.
sub extName {
shift if ref( $_[0] ) && $_[0]->isa('Foswiki::Extensions');
my ($extName) = @_;
my $name = Foswiki::fetchGlobal( "\$" . $extName . "::NAME" );
unless ($name) {
( $name = $extName ) =~ s/^Foswiki::Extension:://;
}
return $name // '';
}
=begin TML
---++ Static methods
=cut
sub registerSubClass {
my ( $extModule, $class, $subClass ) = @_;
push @{ $extSubClasses{$class} },
{
extension => $extModule,
subClass => $subClass
};
}
sub registerExtModule {
my ($extModule) = @_;
push @extModules, $extModule;
$registeredModules{$extModule} = 1;
}
sub registerExtTagHandler {
my ( $extModule, $tagName, $tagClass ) = @_;
$extTags{$tagName} = {
extension => $extModule,
( defined $tagClass ? ( class => $tagClass ) : () ),
};
}
sub registerExtCallback {
my ( $extModule, $cbName, $cbCode ) = @_;
push @{ $extCallbacks{$cbName} },
{
extension => $extModule,
code => $cbCode,
};
}
sub registerDeps {
my $extModule = shift;
#say STDERR "Registering dependencies for module ", $extModule, ":",
# join( ",", @_ );
push @{ $extDeps{$extModule} }, @_;
}
sub registerPluggable {
my ( $target, $method, $code ) = @_;
ASSERT( ref($code) eq 'CODE' ) if DEBUG;
Foswiki::Exception::Fatal->throw(
text => "Attempt to register duplicate pluggable method "
. $method
. " for class "
. $target )
if defined $pluggables{$target}{$method};
#say STDERR "Registering pluggable method $method for $target";
$pluggables{$target}{$method} = $code;
Foswiki::Class::_inject_code(
$target, $method,
sub {
my $obj = shift;
if ( $obj->_has__appObj ) {
return $obj->__appObj->extensions->_callPluggable(
$target,
$method,
args => \@_,
object => $obj,
);
}
return $code->( $obj, @_ );
}
);
}
sub registerPlugMethod {
my ( $extModule, $where, $pluggableMethod, $code ) = @_;
ASSERT(
$where =~ /^(?:before|after|around)$/,
"Unknown plug method type $where"
);
$pluggableMethod =~ /^(.+)::([^:]+)$/;
my ( $target, $method ) = ( $1, $2 );
$plugMethods{$extModule}{$target}{$method}{$where} = $code;
}
sub isRegistered {
my ($extModule) = @_;
return $registeredModules{$extModule} // 0;
}
=begin TML
---++ SEE ALSO
=Foswiki::Extension::Empty=, =Foswiki::Class=.
=cut
1;
__END__
Foswiki - The Free and Open Source Wiki, http://foswiki.org/
Copyright (C) 2016 Foswiki Contributors. Foswiki Contributors
are listed in the AUTHORS file in the root of this distribution.
NOTE: Please extend that file, not this notice.
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version. For
more details read LICENSE in the root of this distribution.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
As per the GPL, removal of this notice is prohibited.