added the ability to specify extra options for component loading
Matt S Trout [Sun, 11 Jun 2006 14:45:36 +0000 (14:45 +0000)]
r9838@cain (orig r4319):  bricas | 2006-06-07 18:44:07 +0000

lib/Catalyst.pm
t/unit_core_component_loading.t

index 09372ea..d4a70c3 100644 (file)
@@ -1801,19 +1801,25 @@ sub setup_actions { my $c = shift; $c->dispatcher->setup_actions( $c, @_ ) }
 
 =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 ) {
index a01edf0..ffd20a5 100644 (file)
@@ -1,6 +1,7 @@
 # 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;
@@ -119,3 +120,42 @@ foreach (keys %$complist) {
 }
 
 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