add mailmap
[p5sagit/Class-C3-Componentised.git] / lib / Class / C3 / Componentised.pm
CommitLineData
d288ce53 1package Class::C3::Componentised;
2
20169807 3=head1 NAME
4
5Class::C3::Componentised
6
7=head1 DESCRIPTION
8
9Load mix-ins or components to your C3-based class.
10
11=head1 SYNOPSIS
12
13 package MyModule;
14
15 use strict;
16 use warnings;
17
18 use base 'Class::C3::Componentised';
19
0c205e9c 20 sub component_base_class { "MyModule::Component" }
20169807 21
22 package main;
23
0c205e9c 24 MyModule->load_components( qw/Foo Bar/ );
b34a4025 25 # Will load MyModule::Component::Foo and MyModule::Component::Bar
0c205e9c 26
27=head1 DESCRIPTION
28
29This will inject base classes to your module using the L<Class::C3> method
30resolution order.
31
32Please note: these are not plugins that can take precedence over methods
33declared in MyModule. If you want something like that, consider
34L<MooseX::Object::Pluggable>.
20169807 35
36=head1 METHODS
37
38=cut
39
d288ce53 40use strict;
41use warnings;
42
3a4635fb 43# This will prime the Class::C3 namespace (either by loading it proper on 5.8
44# or by installing compat shims on 5.10+). A user might have a reasonable
45# expectation that using Class::C3::<something> will give him access to
46# Class::C3 itself, and this module has been providing this historically.
47# Therefore leaving it in indefinitely.
0b8e135a 48use MRO::Compat;
3a4635fb 49
f8b4872f 50use Carp ();
91e80be9 51use List::Util ();
d288ce53 52
c17e86b8 53our $VERSION = '1.001000';
54
55$VERSION = eval $VERSION if $VERSION =~ /_/; # numify for warning-free dev releases
d288ce53 56
eac9b176 57my $invalid_class = qr/(?: \b:\b | \:{3,} | \:\:$ )/x;
58
20169807 59=head2 load_components( @comps )
d288ce53 60
20169807 61Loads the given components into the current module. If a module begins with a
62C<+> character, it is taken to be a fully qualified class name, otherwise
63C<< $class->component_base_class >> is prepended to it.
d288ce53 64
20169807 65Calling this will call C<Class::C3::reinitialize>.
66
67=cut
d288ce53 68
69sub load_components {
70 my $class = shift;
3a4635fb 71 $class->_load_components( map {
72 /^\+(.*)$/
73 ? $1
74 : join ('::', $class->component_base_class, $_)
75 } grep { $_ !~ /^#/ } @_
76 );
d288ce53 77}
78
20169807 79=head2 load_own_components( @comps )
80
5e54b45d 81Similar to L<load_components>, but assumes every class is C<"$class::$comp">.
20169807 82
83=cut
84
d288ce53 85sub load_own_components {
86 my $class = shift;
3a4635fb 87 $class->_load_components( map { "${class}::$_" } grep { $_ !~ /^#/ } @_ );
d288ce53 88}
89
90sub _load_components {
3a4635fb 91 my $class = shift;
92 return unless @_;
93
94 $class->ensure_class_loaded($_) for @_;
95 $class->inject_base($class => @_);
20169807 96 Class::C3::reinitialize();
d288ce53 97}
98
20169807 99=head2 load_optional_components
d288ce53 100
20169807 101As L<load_components>, but will silently ignore any components that cannot be
102found.
d288ce53 103
20169807 104=cut
d288ce53 105
20169807 106sub load_optional_components {
107 my $class = shift;
3a4635fb 108 $class->_load_components( grep
109 { $class->load_optional_class( $_ ) }
110 ( map
111 { /^\+(.*)$/
112 ? $1
113 : join ('::', $class->component_base_class, $_)
114 }
115 grep { $_ !~ /^#/ } @_
116 )
117 );
20169807 118}
d288ce53 119
20169807 120=head2 ensure_class_loaded
121
122Given a class name, tests to see if it is already loaded or otherwise
123defined. If it is not yet loaded, the package is require'd, and an exception
124is thrown if the class is still not loaded.
125
126 BUG: For some reason, packages with syntax errors are added to %INC on
127 require
128=cut
129
20169807 130sub ensure_class_loaded {
131 my ($class, $f_class) = @_;
132
3a4635fb 133 no strict 'refs';
134
135 # ripped from Class::Inspector for speed
136 # note that the order is important (faster items are first)
137 return if ${"${f_class}::VERSION"};
138
139 return if @{"${f_class}::ISA"};
140
141 my $file = (join ('/', split ('::', $f_class) ) ) . '.pm';
142 return if $INC{$file};
143
144 for ( keys %{"${f_class}::"} ) {
145 return if ( *{"${f_class}::$_"}{CODE} );
146 }
147
3a4635fb 148 # require always returns true on success
15b7d164 149 # ill-behaved modules might very well obliterate $_
150 eval { local $_; require($file) } or do {
3a4635fb 151
eac9b176 152 $@ = "Invalid class name '$f_class'" if $f_class =~ $invalid_class;
3a4635fb 153
20169807 154 if ($class->can('throw_exception')) {
155 $class->throw_exception($@);
156 } else {
f8b4872f 157 Carp::croak $@;
20169807 158 }
3a4635fb 159 };
160
161 return;
20169807 162}
d288ce53 163
20169807 164=head2 ensure_class_found
d288ce53 165
20169807 166Returns true if the specified class is installed or already loaded, false
b34a4025 167otherwise.
168
169Note that the underlying mechanism (Class::Inspector->installed()) used by this
170sub will not, at the time of writing, correctly function when @INC includes
171coderefs. Since PAR relies upon coderefs in @INC, this function should be
172avoided in modules that are likely to be included within a PAR.
d288ce53 173
20169807 174=cut
d288ce53 175
20169807 176sub ensure_class_found {
3a4635fb 177 #my ($class, $f_class) = @_;
178 require Class::Inspector;
179 return Class::Inspector->loaded($_[1]) ||
180 Class::Inspector->installed($_[1]);
20169807 181}
d288ce53 182
d288ce53 183
184=head2 inject_base
185
20169807 186Does the actual magic of adjusting @ISA on the target module.
187
188=cut
d288ce53 189
20169807 190sub inject_base {
3a4635fb 191 my $class = shift;
192 my $target = shift;
193
e6b8b400 194 mro::set_mro($target, 'c3');
195
196 for my $comp (reverse @_) {
91e80be9 197 my $apply = do {
198 no strict 'refs';
199 sub { unshift ( @{"${target}::ISA"}, $comp ) };
200 };
e6b8b400 201 unless ($target eq $comp || $target->isa($comp)) {
91e80be9 202 our %APPLICATOR_FOR;
203 if (my $apply_class
204 = List::Util::first { $APPLICATOR_FOR{$_} } @{mro::get_linear_isa($comp)}
205 ) {
206 $APPLICATOR_FOR{$apply_class}->_apply_component_to_class($comp,$target,$apply);
207 } else {
208 $apply->();
e6b8b400 209 }
210 }
211 }
20169807 212}
d288ce53 213
078742b1 214=head2 load_optional_class
215
216Returns a true value if the specified class is installed and loaded
217successfully, throws an exception if the class is found but not loaded
218successfully, and false if the class is not installed
219
220=cut
221
222sub load_optional_class {
223 my ($class, $f_class) = @_;
3a4635fb 224
225 # ensure_class_loaded either returns a () (*not* true) or throws
226 eval {
227 $class->ensure_class_loaded($f_class);
228 1;
229 } && return 1;
230
dfb3a821 231 my $err = $@; # so we don't lose it
3a4635fb 232
eac9b176 233 if ($f_class =~ $invalid_class) {
234 $err = "Invalid class name '$f_class'";
235 }
236 else {
237 my $fn = quotemeta( (join ('/', split ('::', $f_class) ) ) . '.pm' );
238 return 0 if ($err =~ /Can't locate ${fn} in \@INC/ );
3a4635fb 239 }
eac9b176 240
241 if ($class->can('throw_exception')) {
3a4635fb 242 $class->throw_exception($err);
078742b1 243 }
dfb3a821 244 else {
3a4635fb 245 die $err;
dfb3a821 246 }
078742b1 247}
248
025a7b58 249=head1 AUTHORS
d288ce53 250
025a7b58 251Matt S. Trout and the L<DBIx::Class team|DBIx::Class/CONTRIBUTORS>
20169807 252
253Pulled out into seperate module by Ash Berlin C<< <ash@cpan.org> >>
d288ce53 254
025a7b58 255Optimizations and overall bolt-tightening by Peter "ribasushi" Rabbitson
256C<< <ribasushi@cpan.org> >>
257
258=head1 COPYRIGHT
259
260Copyright (c) 2006 - 2011 the Class::C3::Componentised L</AUTHORS> as listed
261above.
262
d288ce53 263=head1 LICENSE
264
265You may distribute this code under the same terms as Perl itself.
20169807 266
267=cut
268
2691;