Version 1.0006
[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
d2b0e111 43# see Makefile.PL for discussion on why we load both Class::C3 and MRO::Compat
d2b0e111 44use Class::C3 ();
0b8e135a 45use MRO::Compat;
20169807 46use Class::Inspector;
47use Carp;
d288ce53 48
ade50a21 49our $VERSION = 1.0006;
d288ce53 50
20169807 51=head2 load_components( @comps )
d288ce53 52
20169807 53Loads the given components into the current module. If a module begins with a
54C<+> character, it is taken to be a fully qualified class name, otherwise
55C<< $class->component_base_class >> is prepended to it.
d288ce53 56
20169807 57Calling this will call C<Class::C3::reinitialize>.
58
59=cut
d288ce53 60
61sub load_components {
62 my $class = shift;
b0dd4f7b 63 my @comp = map {
64 /^\+(.*)$/
65 ? $1
66 : join ('::', $class->component_base_class, $_)
67 }
68 grep { $_ !~ /^#/ } @_;
d288ce53 69 $class->_load_components(@comp);
d288ce53 70}
71
20169807 72=head2 load_own_components( @comps )
73
5e54b45d 74Similar to L<load_components>, but assumes every class is C<"$class::$comp">.
20169807 75
76=cut
77
d288ce53 78sub load_own_components {
79 my $class = shift;
80 my @comp = map { "${class}::$_" } grep { $_ !~ /^#/ } @_;
81 $class->_load_components(@comp);
82}
83
84sub _load_components {
85 my ($class, @comp) = @_;
86 foreach my $comp (@comp) {
20169807 87 $class->ensure_class_loaded($comp);
d288ce53 88 }
89 $class->inject_base($class => @comp);
20169807 90 Class::C3::reinitialize();
d288ce53 91}
92
20169807 93=head2 load_optional_components
d288ce53 94
20169807 95As L<load_components>, but will silently ignore any components that cannot be
96found.
d288ce53 97
20169807 98=cut
d288ce53 99
20169807 100sub load_optional_components {
101 my $class = shift;
20169807 102 my @comp = grep { $class->load_optional_class( $_ ) }
b0dd4f7b 103 map {
104 /^\+(.*)$/
105 ? $1
106 : join ('::', $class->component_base_class, $_)
107 }
20169807 108 grep { $_ !~ /^#/ } @_;
d288ce53 109
20169807 110 $class->_load_components( @comp ) if scalar @comp;
111}
d288ce53 112
20169807 113=head2 ensure_class_loaded
114
115Given a class name, tests to see if it is already loaded or otherwise
116defined. If it is not yet loaded, the package is require'd, and an exception
117is thrown if the class is still not loaded.
118
119 BUG: For some reason, packages with syntax errors are added to %INC on
120 require
121=cut
122
123#
124# TODO: handle ->has_many('rel', 'Class'...) instead of
125# ->has_many('rel', 'Some::Schema::Class'...)
126#
127sub ensure_class_loaded {
128 my ($class, $f_class) = @_;
129
130 croak "Invalid class name $f_class"
131 if ($f_class=~m/(?:\b:\b|\:{3,})/);
132 return if Class::Inspector->loaded($f_class);
c2bfa58a 133 my $file = $f_class . '.pm';
134 $file =~ s{::}{/}g;
135 eval { CORE::require($file) }; # require needs a bareword or filename
20169807 136 if ($@) {
137 if ($class->can('throw_exception')) {
138 $class->throw_exception($@);
139 } else {
140 croak $@;
141 }
142 }
143}
d288ce53 144
20169807 145=head2 ensure_class_found
d288ce53 146
20169807 147Returns true if the specified class is installed or already loaded, false
b34a4025 148otherwise.
149
150Note that the underlying mechanism (Class::Inspector->installed()) used by this
151sub will not, at the time of writing, correctly function when @INC includes
152coderefs. Since PAR relies upon coderefs in @INC, this function should be
153avoided in modules that are likely to be included within a PAR.
d288ce53 154
20169807 155=cut
d288ce53 156
20169807 157sub ensure_class_found {
158 my ($class, $f_class) = @_;
159 return Class::Inspector->loaded($f_class) ||
160 Class::Inspector->installed($f_class);
161}
d288ce53 162
d288ce53 163
164=head2 inject_base
165
20169807 166Does the actual magic of adjusting @ISA on the target module.
167
168=cut
d288ce53 169
20169807 170sub inject_base {
171 my ($class, $target, @to_inject) = @_;
172 {
173 no strict 'refs';
174 foreach my $to (reverse @to_inject) {
0c205e9c 175 unshift ( @{"${target}::ISA"}, $to )
20169807 176 unless ($target eq $to || $target->isa($to));
177 }
178 }
179
d91a39a9 180 mro::set_mro($target, 'c3');
20169807 181}
d288ce53 182
078742b1 183=head2 load_optional_class
184
185Returns a true value if the specified class is installed and loaded
186successfully, throws an exception if the class is found but not loaded
187successfully, and false if the class is not installed
188
189=cut
190
191sub load_optional_class {
192 my ($class, $f_class) = @_;
dfb3a821 193 eval { $class->ensure_class_loaded($f_class) };
194 my $err = $@; # so we don't lose it
195 if (! $err) {
078742b1 196 return 1;
197 }
dfb3a821 198 else {
199 my $fn = (join ('/', split ('::', $f_class) ) ) . '.pm';
200 if ($err =~ /Can't locate ${fn} in \@INC/ ) {
201 return 0;
202 }
203 else {
204 die $err;
205 }
206 }
078742b1 207}
208
d288ce53 209=head1 AUTHOR
210
20169807 211Matt S. Trout and the DBIx::Class team
212
213Pulled out into seperate module by Ash Berlin C<< <ash@cpan.org> >>
d288ce53 214
215=head1 LICENSE
216
217You may distribute this code under the same terms as Perl itself.
20169807 218
219=cut
220
2211;