/
Class.pm
319 lines (246 loc) · 8.86 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
# 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 |
| =: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 support.
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=.
=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;
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, %_registeredAttrs, %_ISA, %_WITH );
# BEGIN Install wrappers for Moo's has/with/extends to record basic object information. Works only when $ENV{FOSWIKI_ASSERTS} is true.
sub _fw_has {
my $target = shift;
my ($attr) = @_;
#say STDERR "Registering attr $attr on $target";
push @{ $_registeredAttrs{$target}{list} },
{ attr => $attr, options => [ @_[ 1 .. $#_ ] ] };
}
sub _fw_with {
my $target = shift;
#say STDERR "$target WITH ", join( ", ", @_ );
push @{ $_WITH{$target} }, @_;
}
sub _fw_extends {
my $target = shift;
#say STDERR "$target EXTENDS ", join( ", ", @_ );
push @{ $_ISA{$target} }, @_;
}
if ( $ENV{FOSWIKI_ASSERTS} ) {
# Moo doesn't provide a clean way to get all object's attributes. The only
# really clean way to distinguish between a key on object's hash and an
# attribute is to record what is passed to Moo's sub 'has'. Since Moo
# generates it for each class separately (as well as other 'keywords') and
# since Moo::Role does it on its own too then the only really clean way to
# catch everything is to tap into Moo's guts. And the best way to do so is
# to intercept calls to _install_tracked() as this sub is used to register
# every single Moo-generated code ref. Though this is a hacky way on its own
# but the rest approaches seem to be even more hacky and no doubt
# unreliable.
foreach my $module (qw(Moo Moo::Role)) {
my $ns = Foswiki::getNS($module);
my $_install_tracked = *{ $ns->{'_install_tracked'} }{CODE};
_inject_code(
$module,
'_install_tracked',
sub {
my $ovCode;
my $target = $_[0];
my $codeName = $_[1];
my $ovSubName = "_fw_" . $_[1];
$ovCode = __PACKAGE__->can($ovSubName);
if ($ovCode) {
#say STDERR "Installing wrapper $codeName on $target";
my $origCode = $_[2];
$_[2] = sub {
$ovCode->( $target, @_ );
goto &$origCode;
};
}
goto &$_install_tracked;
}
);
}
}
# END of has/with/extends wrappers.
sub import {
my ($class) = shift;
my $target = caller;
# Define options we would provide for classes.
my %options = (
callbacks => { use => 0, },
app => { use => 0, },
);
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 {
$class->_apply_roles;
};
require feature;
feature->import($featureSet);
namespace::clean->import(
-cleanee => $target,
-except => \@noNsClean,
);
@_ = ( $class, @p );
goto &Moo::import;
}
sub _getAllAttrs {
foreach my $class (@_) {
my @classAttrs;
if ( defined $_registeredAttrs{$class} ) {
if ( defined $_registeredAttrs{$class}{cached} ) {
# Skip the class if already cached.
next;
}
if ( defined $_registeredAttrs{$class}{list} ) {
push @classAttrs,
map { $_->{attr} } @{ $_registeredAttrs{$class}{list} };
}
}
if ( defined $_ISA{$class} ) {
push @classAttrs, _getAllAttrs( @{ $_ISA{$class} } );
}
if ( defined $_WITH{$class} ) {
push @classAttrs, _getAllAttrs( @{ $_WITH{$class} } );
}
$_registeredAttrs{$class}{cached} = \@classAttrs;
}
return map { @{ $_registeredAttrs{$_}{cached} } } @_;
}
sub getClassAttributes {
my $class = shift;
#require Data::Dumper;
#say STDERR Data::Dumper->Dump(
# [ \%_registeredAttrs, \%_ISA, \%_WITH ],
# [qw(%_registeredAttrs %_ISA %_WITH)]
#);
return _getAllAttrs($class);
}
# 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 roles ",
# join( ", ", @{ $_assignedRoles{$target} } ), " to $target";
push @{ $_WITH{$target} }, @{ $_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' );
}
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.