3 sub _getglob { \*{$_[0]} }
4 sub _getstash { \%{"$_[0]::"} }
7 use warnings FATAL => 'all';
13 # inlined from Moo::_Utils - update that first.
16 (my $proto = $_[0]) =~ s/::/\//g;
17 return 1 if $INC{"${proto}.pm"};
18 # can't just ->can('can') because a sub-package Foo::Bar::Baz
19 # creates a 'Baz::' key in Foo::Bar's symbol table
20 return 1 if grep !/::$/, keys %{_getstash($_[0])||{}};
21 { local $@; require "${proto}.pm"; }
29 return if $INFO{$target}; # already exported into this package
30 # get symbol table reference
31 my $stash = do { no strict 'refs'; \%{"${target}::"} };
32 # install before/after/around subs
33 foreach my $type (qw(before after around)) {
34 *{_getglob "${target}::${type}"} = sub {
35 { local $@; require Class::Method::Modifiers; }
36 push @{$INFO{$target}{modifiers}||=[]}, [ $type => @_ ];
39 *{_getglob "${target}::requires"} = sub {
40 push @{$INFO{$target}{requires}||=[]}, @_;
42 *{_getglob "${target}::with"} = sub {
43 die "Only one role supported at a time by with" if @_ > 1;
44 $me->apply_role_to_package($target, $_[0]);
46 # grab all *non-constant* (ref eq 'SCALAR'/'REF') subs present
47 # in the symbol table and store their refaddrs (no need to forcibly
48 # inflate constant subs into real subs) - also add '' to here (this
50 @{$INFO{$target}{not_methods}={}}{
51 '', map { *$_{CODE}||() } grep !(ref =~ /^(?:SCALAR|REF)$/), values %$stash
54 $APPLIED_TO{$target} = { $target => undef };
57 sub apply_role_to_package {
58 my ($me, $to, $role) = @_;
62 die "This is apply_role_to_package" if ref($to);
63 die "${role} is not a Role::Tiny" unless my $info = $INFO{$role};
65 $me->_check_requires($to, $role, @{$info->{requires}||[]});
67 $me->_install_methods($to, $role);
69 $me->_install_modifiers($to, $info->{modifiers});
71 # only add does() method to classes and only if they don't have one
72 if (not $INFO{$to} and not $to->can('does')) {
73 *{_getglob "${to}::does"} = \&does_role;
76 # copy our role list into the target's
77 @{$APPLIED_TO{$to}||={}}{keys %{$APPLIED_TO{$role}}} = ();
80 sub apply_roles_to_object {
81 my ($me, $object, @roles) = @_;
82 die "No roles supplied!" unless @roles;
83 my $class = ref($object);
84 bless($object, $me->create_class_with_roles($class, @roles));
88 sub create_class_with_roles {
89 my ($me, $superclass, @roles) = @_;
91 die "No roles supplied!" unless @roles;
94 '__WITH__', $superclass, my $compose_name = join '__AND__', @roles
97 return $new_name if $COMPOSED{class}{$new_name};
99 foreach my $role (@roles) {
101 die "${role} is not a Role::Tiny" unless my $info = $INFO{$role};
105 { local $@; require mro; }
107 { local $@; require MRO::Compat; }
110 my @composable = map $me->_composable_package_for($_), reverse @roles;
112 *{_getglob("${new_name}::ISA")} = [ @composable, $superclass ];
114 my @info = map +($INFO{$_} ? $INFO{$_} : ()), @roles;
116 $me->_check_requires(
117 $new_name, $compose_name,
118 do { my %h; @h{map @{$_->{requires}||[]}, @info} = (); keys %h }
121 *{_getglob "${new_name}::does"} = \&does_role unless $new_name->can('does');
123 @{$APPLIED_TO{$new_name}||={}}{
124 map keys %{$APPLIED_TO{$_}}, @roles
127 $COMPOSED{class}{$new_name} = 1;
131 sub _composable_package_for {
132 my ($me, $role) = @_;
133 my $composed_name = 'Role::Tiny::_COMPOSABLE::'.$role;
134 return $composed_name if $COMPOSED{role}{$composed_name};
135 $me->_install_methods($composed_name, $role);
136 my $base_name = $composed_name.'::_BASE';
137 *{_getglob("${composed_name}::ISA")} = [ $base_name ];
138 my $modifiers = $INFO{$role}{modifiers}||[];
140 foreach my $modified (
141 do { my %h; @h{map $_->[1], @$modifiers} = (); keys %h }
143 push @mod_base, "sub ${modified} { shift->next::method(\@_) }";
147 eval(my $code = join "\n", "package ${base_name};", @mod_base);
148 die "Evaling failed: $@\nTrying to eval:\n${code}" if $@;
150 $me->_install_modifiers($composed_name, $modifiers);
151 $COMPOSED{role}{$composed_name} = 1;
152 return $composed_name;
155 sub _check_requires {
156 my ($me, $to, $name, @requires) = @_;
157 if (my @requires_fail = grep !$to->can($_), @requires) {
158 # role -> role, add to requires, role -> class, error out
159 if (my $to_info = $INFO{$to}) {
160 push @{$to_info->{requires}||=[]}, @requires_fail;
162 die "Can't apply ${name} to ${to} - missing ".join(', ', @requires_fail);
167 sub _concrete_methods_of {
168 my ($me, $role) = @_;
169 my $info = $INFO{$role};
170 $info->{methods} ||= do {
171 # grab role symbol table
172 my $stash = do { no strict 'refs'; \%{"${role}::"}};
173 my $not_methods = $info->{not_methods};
175 # grab all code entries that aren't in the not_methods list
177 my $code = *{$stash->{$_}}{CODE};
178 # rely on the '' key we added in import for "no code here"
179 exists $not_methods->{$code||''} ? () : ($_ => $code)
180 } grep !(ref($stash->{$_}) =~ /^(?:SCALAR|REF)$/), keys %$stash
185 sub methods_provided_by {
186 my ($me, $role) = @_;
187 die "${role} is not a Role::Tiny" unless my $info = $INFO{$role};
188 (keys %{$me->_concrete_methods_of($role)}, @{$info->{requires}||[]});
191 sub _install_methods {
192 my ($me, $to, $role) = @_;
194 my $info = $INFO{$role};
196 my $methods = $me->_concrete_methods_of($role);
198 # grab target symbol table
199 my $stash = do { no strict 'refs'; \%{"${to}::"}};
201 # determine already extant methods of target
204 +((ref($stash->{$_}) =~ /^(?:SCALAR|REF)$/) || (*{$stash->{$_}}{CODE})),
208 foreach my $i (grep !exists $has_methods{$_}, keys %$methods) {
210 *{_getglob "${to}::${i}"} = $methods->{$i};
214 sub _install_modifiers {
215 my ($me, $to, $modifiers) = @_;
216 if (my $info = $INFO{$to}) {
217 push @{$info->{modifiers}}, @{$modifiers||[]};
219 foreach my $modifier (@{$modifiers||[]}) {
220 $me->_install_single_modifier($to, @$modifier);
225 sub _install_single_modifier {
226 my ($me, @args) = @_;
227 Class::Method::Modifiers::install_modifier(@args);
231 my ($proto, $role) = @_;
232 return exists $APPLIED_TO{ref($proto)||$proto}{$role};
239 Role::Tiny - Roles. Like a nouvelle cusine portion size slice of Moose.
257 use Role::Tiny::With;
259 # bar gets imported, but not foo
268 C<Role::Tiny> is a minimalist role composition tool.
270 =head1 ROLE COMPOSITION
272 Role composition can be thought of as much more clever and meaningful multiple
273 inheritance. The basics of this implementation of roles is:
279 If a method is already defined on a class, that method will not be composed in
284 If a method that the role L</requires> to be implemented is not implemented,
285 role application will fail loudly.
289 Unlike L<Class::C3>, where the B<last> class inherited from "wins," role
290 composition is the other way around, where first wins. In a more complete
291 system (see L<Moose>) roles are checked to see if they clash. The goal of this
292 is to be much simpler, hence disallowing composition of multiple roles at once.
296 =head2 apply_role_to_package
298 Role::Tiny->apply_role_to_package('Some::Package', 'Some::Role');
300 Composes role with package. See also L<Role::Tiny::With>.
302 =head2 apply_roles_to_object
304 Role::Tiny->apply_roles_to_object($foo, qw(Some::Role1 Some::Role2));
306 Composes roles in order into object directly. Object is reblessed into the
309 =head2 create_class_with_roles
311 Role::Tiny->create_class_with_roles('Some::Base', qw(Some::Role1 Some::Role2));
313 Creates a new class based on base, with the roles composed into it in order.
314 New class is returned.
320 if (Role::Tiny::does_role($foo, 'Some::Role')) {
324 Returns true if class has been composed with role.
326 This subroutine is also installed as ->does on any class a Role::Tiny is
327 composed into unless that class already has an ->does method, so
329 if ($foo->does_role('Some::Role')) {
333 will work for classes but to test a role, one must use ::does_role directly
335 =head1 IMPORTED SUBROUTINES
339 requires qw(foo bar);
341 Declares a list of methods that must be defined to compose role.
348 Composes another role into the current role. Only one role may be composed in
349 at a time to allow the code to remain as simple as possible.
353 before foo => sub { ... };
355 See L<< Class::Method::Modifiers/before method(s) => sub { ... } >> for full
360 around foo => sub { ... };
362 See L<< Class::Method::Modifiers/around method(s) => sub { ... } >> for full
367 after foo => sub { ... };
369 See L<< Class::Method::Modifiers/after method(s) => sub { ... } >> for full
374 See L<Moo> for authors.
376 =head1 COPYRIGHT AND LICENSE
378 See L<Moo> for the copyright and license.