=head2 $c->setup_components
-Sets up components.
+Sets up components. Specify a C<setup_components> config option to pass additional options
+directly to L<Module::Pluggable>. To add additional search paths, specify a key named
+C<search_extra> as an array reference. Items in the array beginning with C<::> will have the
+application class name prepended to them.
=cut
sub setup_components {
my $class = shift;
+ my @paths = qw( ::Controller ::C ::Model ::M ::View ::V );
+ my $config = $class->config->{ setup_components };
+ my $extra = delete $config->{ search_extra } || [];
+
+ push @paths, @$extra;
+
my $locator = Module::Pluggable::Object->new(
- search_path => [
- "${class}::Controller", "${class}::C",
- "${class}::Model", "${class}::M",
- "${class}::View", "${class}::V"
- ],
+ search_path => [ map { s/^(?=::)/$class/; $_; } @paths ],
+ %$config
);
for my $component ( sort { length $a <=> length $b } $locator->plugins ) {
# 2 initial tests, and 6 per component in the loop below
# (do not forget to update the number of components in test 3 as well)
-use Test::More tests => 2 + 6 * 24;
+# 4 extra tests for the loading options
+use Test::More tests => 2 + 6 * 24 + 4;
use strict;
use warnings;
}
rmtree($libdir);
+
+# test extra component loading options
+
+$appclass = 'ExtraOptions';
+push @components, { type => 'View', prefix => 'Extra', name => 'Foo' };
+
+foreach my $component (@components) {
+ make_component_file($component->{type},
+ $component->{prefix},
+ $component->{name});
+}
+
+eval qq(
+package $appclass;
+use Catalyst;
+__PACKAGE__->config->{ setup_components } = {
+ search_extra => [ '::Extra' ],
+ except => [ "${appclass}::Controller::Foo" ]
+};
+__PACKAGE__->setup;
+);
+
+can_ok( $appclass, 'components');
+
+$complist = $appclass->components;
+
+is(scalar keys %$complist, 24+1, "Correct number of components loaded");
+
+my $seen_foo_controller = 0;
+my $seen_extra = 0;
+foreach (keys %$complist) {
+ $seen_foo_controller++ if $_ eq "${appclass}::Controller::Foo";
+ $seen_extra++ if $_ eq "${appclass}::Extra::Foo";
+}
+
+is( $seen_foo_controller, 0, 'Controller::Foo was skipped' );
+is( $seen_extra, 1, 'Extra::Foo was loaded' );
+
+rmtree($libdir);
\ No newline at end of file