/
Class.pm
344 lines (262 loc) · 9.61 KB
/
Class.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
# See bottom of file for license and copyright information
package Foswiki::Class;
use v5.14;
=begin TML
---+!! Module Foswiki::Class
This is a wrapper package for Moo and intended to be used as a replacement and
a shortcut for a bunch lines of code like:
<verbatim>
use v5.14;
use Moo;
use namespace::clean;
with qw(Foswiki::AppObject);
</verbatim>
The above could be replaced with a single line of:
<verbatim>
use Foswiki::Class qw(app);
</verbatim>
---++ Usage
A set of features is exported to the calling module is defined by =use=
parameter keywords. If no parameters defined then all it does is applies
=[[CPAN:Moo][Moo]]=, ':5.14'
[[http://perldoc.perl.org/feature.html#FEATURE-BUNDLES][feature]] bundle, and
cleans namespace with =[[CPAN:namespace::clean][namespace::clean]]=.
---++ Parameters
The following parameters are support by this module:
| *Parameter* | *Description* |
| =app= | Class being created will have =Foswiki::AppObject= role applied. |
| =callbacks= | Provide support for callbacks |
| =extension= | Declares class to be an extension. See =Foswiki::Extenion::Empty= for more information. |
| =extensible= | Makes class an extensible. |
| =:5.XX= | A string prefixed with colon is treated as a feature bundle name and passed over to the =feature= module as is. This allows to override the ':5.14' default. |
---++ Callbacks
When =callbacks= parameter is used:
<verbatim>
use Foswiki::Class qw(callbacks);
</verbatim>
a subroutine =callback_names= is exported into a class' namespace and
=Foswiki::Aux::Callbacks= role gets applied. =callback_names= accepts a list
and registers names from the list as callbacks supported by the class.
For example:
<verbatim>
package Foswiki::SomeClass;
use Foswiki::Class qw(app callbacks);
callback_names qw(callback1 callback2);
sub someMethod {
my $this = shift;
$this->callback('callback1', $cbParams);
}
</verbatim>
Here we get two callbacks registered: =Foswiki::SomeClass::callback1= and
=Foswiki::SomeClass::callback2=.
See =Foswiki::Aux::Callbacks=.
---++ Extensions
Extension support is provided by exporting subroutines =callbackHandler,
extBefore, extAfter, extClass, plugBefore, plugAround, plugAfter, tagHandler=.
See more in =Foswiki::Extension::Empty=.
---++ Extensible
A core class called extensible if it allows overriding one or more of it's
methods by extensions. This is a lightweight version of subclassing through
reimplementing or extending only key method(s).
See more in =Foswiki::Extension::Empty=.
=cut
# Naming conventions for this module:
# _install_something – functions that install feature `something' into the target module;
# _handler_someword - function which implements exported keyword `someword'
use Carp;
use Class::Method::Modifiers qw(install_modifier);
require Foswiki;
require Moo::Role;
require Moo;
require namespace::clean;
use B::Hooks::EndOfScope 'on_scope_end';
use constant DEFAULT_FEATURESET => ':5.14';
our @ISA = qw(Moo);
my %_assignedRoles;
sub import {
my ($class) = shift;
my $target = caller;
$SIG{__DIE__} = sub { Carp::confess(@_) };
# Define options we would provide for classes.
my %options = (
callbacks => { use => 0, },
app => { use => 0, },
extension => {
use => 0,
keywords =>
[qw(extClass extAfter extBefore plugBefore plugAfter plugAround)],
},
extensible => {
use => 0,
keywords => [qw(pluggable)],
},
);
my @p;
my @noNsClean = qw(meta);
my $featureSet = DEFAULT_FEATURESET;
while (@_) {
my $param = shift;
if ( $param =~ /^:/ ) {
$featureSet = $param;
next;
}
if ( exists $options{$param} ) {
my $opt = $options{$param};
$opt->{use} = 1;
#push @noNsClean, @{ $opt->{keywords} } if defined $opt->{keywords};
}
else {
push @p, $param;
}
}
foreach my $option ( grep { $options{$_}{use} } keys %options ) {
my $installer = __PACKAGE__->can("_install_$option");
die "INTERNAL:There is no installer for option $option"
unless defined $installer;
$installer->( $class, $target );
}
on_scope_end {
if ( $options{callbacks}{use} ) {
my $ns = Foswiki::getNS($target);
# Install BUILD method if callbacks feature requested.
# Otherwise Foswiki::Aux::Callbacks fails to apply cleanly.
unless ( defined $ns->{BUILD} && defined *{ $ns->{BUILD} }{CODE} ) {
say STDERR "Installing BUILD";
install_modifier( $target, fresh => BUILD => sub { } );
}
else {
say STDERR "BUILD exists for $target: ", $ns->{BUILD}, "//",
*{ $ns->{BUILD} }{CODE};
}
}
$class->_apply_roles;
};
require feature;
feature->import($featureSet);
namespace::clean->import(
-cleanee => $target,
-except => \@noNsClean,
);
@_ = ( $class, @p );
goto &Moo::import;
}
# Actually we're duplicating Moo::_install_coderef here in a way. But we better
# avoid using a module's internalls.
sub _inject_code {
my ( $target, $name, $code ) = @_;
Foswiki::getNS($target)->{$name} = $code;
}
sub _apply_roles {
my $class = shift;
foreach my $target ( keys %_assignedRoles ) {
say STDERR "Applying to $target: ", join ",",
map { "{$_}" } @{ $_assignedRoles{$target} };
Moo::Role->apply_roles_to_package( $target,
@{ $_assignedRoles{$target} } );
$class->_maybe_reset_handlemoose($target);
delete $_assignedRoles{$target};
}
}
sub _assign_role {
my ( $class, $role ) = @_;
push @{ $_assignedRoles{$class} }, $role;
}
sub _handler_callback_names {
my $target = caller;
Foswiki::Aux::Callbacks::registerCallbackNames( $target, @_ );
}
sub _install_callbacks {
my ( $class, $target ) = @_;
Foswiki::load_package('Foswiki::Aux::Callbacks');
_assign_role( $target, 'Foswiki::Aux::Callbacks' );
_inject_code( $target, "callback_names", *_handler_callback_names );
}
sub _install_app {
my ( $class, $target ) = @_;
Foswiki::load_package('Foswiki::AppObject');
_assign_role( $target, 'Foswiki::AppObject' );
}
sub _handler_plugBefore ($&) {
my $target = caller;
my ( $plug, $code ) = @_;
Foswiki::Extensions::registerPlugMethod( $target, 'before', $plug, $code );
}
sub _handler_plugAround ($&) {
my $target = caller;
my ( $plug, $code ) = @_;
Foswiki::Extensions::registerPlugMethod( $target, 'around', $plug, $code );
}
sub _handler_plugAfter ($&) {
my $target = caller;
my ( $plug, $code ) = @_;
Foswiki::Extensions::registerPlugMethod( $target, 'after', $plug, $code );
}
sub _handler_extClass ($$) {
my ( $class, $subClass ) = @_;
my $target = caller;
Foswiki::Extensions::registerSubClass( $target, $class, $subClass );
}
sub _handler_extAfter (@) {
my $target = caller;
Foswiki::Extensions::registerDeps( $target, @_ );
}
sub _handler_extBefore (@) {
my $target = caller;
Foswiki::Extensions::registerDeps( $_, $target ) foreach @_;
}
sub _handler_tagHandler ($;$) {
my $target = caller;
# Handler could be a class name doing Foswiki::Macro role or a sub to be
# installed as target's hadnling method.
my ( $tagName, $tagHandler ) = @_;
if ( ref($tagHandler) eq 'CODE' ) {
# If second argument is a code ref then we install method with the same
# name as macro name.
_inject_code( $target, $tagName, $tagHandler );
Foswiki::Extensions::registerExtTagHandler( $target, $tagName );
}
else {
Foswiki::Extensions::registerExtTagHandler( $target, $tagName,
$tagHandler );
}
}
sub _handler_callbackHandler ($&) {
my $target = caller;
Foswiki::Extensions::registerExtCallback( $target, @_ );
}
sub _install_extension {
my ( $class, $target ) = @_;
_inject_code( $target, 'plugBefore', \&_handler_plugBefore );
_inject_code( $target, 'plugAround', \&_handler_plugAround );
_inject_code( $target, 'plugAfter', \&_handler_plugAfter );
_inject_code( $target, 'extClass', \&_handler_extClass );
_inject_code( $target, 'extAfter', \&_handler_extAfter );
_inject_code( $target, 'extBefore', \&_handler_extBefore );
_inject_code( $target, 'tagHandler', \&_handler_tagHandler );
_inject_code( $target, 'callbackHandler', \&_handler_callbackHandler );
}
sub _handler_pluggable ($&) {
my $target = caller;
my ( $method, $code ) = @_;
Foswiki::Extensions::registerPluggable( $target, $method, $code );
}
sub _install_extensible {
my ( $class, $target ) = @_;
_assign_role( $target, 'Foswiki::Aux::_ExtensibleRole' );
_inject_code( $target, 'pluggable', \&_handler_pluggable );
}
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.