1 package Class::C3::Componentised;
5 Class::C3::Componentised
9 Load mix-ins or components to your C3-based class.
18 use base 'Class::C3::Componentised';
20 sub component_base_class { "MyModule::Component" }
24 MyModule->load_components( qw/Foo Bar/ );
25 # Will load MyModule::Component::Foo and MyModule::Component::Bar
29 This will inject base classes to your module using the L<Class::C3> method
32 Please note: these are not plugins that can take precedence over methods
33 declared in MyModule. If you want something like that, consider
34 L<MooseX::Object::Pluggable>.
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.
53 our $VERSION = 1.0009;
55 my $invalid_class = qr/(?: \b:\b | \:{3,} | \:\:$ )/x;
57 =head2 load_components( @comps )
59 Loads the given components into the current module. If a module begins with a
60 C<+> character, it is taken to be a fully qualified class name, otherwise
61 C<< $class->component_base_class >> is prepended to it.
63 Calling this will call C<Class::C3::reinitialize>.
69 $class->_load_components( map {
72 : join ('::', $class->component_base_class, $_)
73 } grep { $_ !~ /^#/ } @_
77 =head2 load_own_components( @comps )
79 Similar to L<load_components>, but assumes every class is C<"$class::$comp">.
83 sub load_own_components {
85 $class->_load_components( map { "${class}::$_" } grep { $_ !~ /^#/ } @_ );
88 sub _load_components {
92 $class->ensure_class_loaded($_) for @_;
93 $class->inject_base($class => @_);
94 Class::C3::reinitialize();
97 =head2 load_optional_components
99 As L<load_components>, but will silently ignore any components that cannot be
104 sub load_optional_components {
106 $class->_load_components( grep
107 { $class->load_optional_class( $_ ) }
111 : join ('::', $class->component_base_class, $_)
113 grep { $_ !~ /^#/ } @_
118 =head2 ensure_class_loaded
120 Given a class name, tests to see if it is already loaded or otherwise
121 defined. If it is not yet loaded, the package is require'd, and an exception
122 is thrown if the class is still not loaded.
124 BUG: For some reason, packages with syntax errors are added to %INC on
128 sub ensure_class_loaded {
129 my ($class, $f_class) = @_;
133 # ripped from Class::Inspector for speed
134 # note that the order is important (faster items are first)
135 return if ${"${f_class}::VERSION"};
137 return if @{"${f_class}::ISA"};
139 my $file = (join ('/', split ('::', $f_class) ) ) . '.pm';
140 return if $INC{$file};
142 for ( keys %{"${f_class}::"} ) {
143 return if ( *{"${f_class}::$_"}{CODE} );
146 # require always returns true on success
147 # ill-behaved modules might very well obliterate $_
148 eval { local $_; require($file) } or do {
150 $@ = "Invalid class name '$f_class'" if $f_class =~ $invalid_class;
152 if ($class->can('throw_exception')) {
153 $class->throw_exception($@);
162 =head2 ensure_class_found
164 Returns true if the specified class is installed or already loaded, false
167 Note that the underlying mechanism (Class::Inspector->installed()) used by this
168 sub will not, at the time of writing, correctly function when @INC includes
169 coderefs. Since PAR relies upon coderefs in @INC, this function should be
170 avoided in modules that are likely to be included within a PAR.
174 sub ensure_class_found {
175 #my ($class, $f_class) = @_;
176 require Class::Inspector;
177 return Class::Inspector->loaded($_[1]) ||
178 Class::Inspector->installed($_[1]);
184 Does the actual magic of adjusting @ISA on the target module.
192 mro::set_mro($target, 'c3');
194 for my $comp (reverse @_) {
197 sub { unshift ( @{"${target}::ISA"}, $comp ) };
199 unless ($target eq $comp || $target->isa($comp)) {
202 = List::Util::first { $APPLICATOR_FOR{$_} } @{mro::get_linear_isa($comp)}
204 $APPLICATOR_FOR{$apply_class}->_apply_component_to_class($comp,$target,$apply);
212 =head2 load_optional_class
214 Returns a true value if the specified class is installed and loaded
215 successfully, throws an exception if the class is found but not loaded
216 successfully, and false if the class is not installed
220 sub load_optional_class {
221 my ($class, $f_class) = @_;
223 # ensure_class_loaded either returns a () (*not* true) or throws
225 $class->ensure_class_loaded($f_class);
229 my $err = $@; # so we don't lose it
231 if ($f_class =~ $invalid_class) {
232 $err = "Invalid class name '$f_class'";
235 my $fn = quotemeta( (join ('/', split ('::', $f_class) ) ) . '.pm' );
236 return 0 if ($err =~ /Can't locate ${fn} in \@INC/ );
239 if ($class->can('throw_exception')) {
240 $class->throw_exception($err);
249 Matt S. Trout and the L<DBIx::Class team|DBIx::Class/CONTRIBUTORS>
251 Pulled out into seperate module by Ash Berlin C<< <ash@cpan.org> >>
253 Optimizations and overall bolt-tightening by Peter "ribasushi" Rabbitson
254 C<< <ribasushi@cpan.org> >>
258 Copyright (c) 2006 - 2011 the Class::C3::Componentised L</AUTHORS> as listed
263 You may distribute this code under the same terms as Perl itself.