From: Matt S Trout Date: Sun, 11 Jun 2006 14:45:36 +0000 (+0000) Subject: added the ability to specify extra options for component loading X-Git-Tag: 5.7099_04~516 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?p=catagits%2FCatalyst-Runtime.git;a=commitdiff_plain;h=18de900e2fecac0ce4e6d3056d4e31bb6e71d81d added the ability to specify extra options for component loading r9838@cain (orig r4319): bricas | 2006-06-07 18:44:07 +0000 --- diff --git a/lib/Catalyst.pm b/lib/Catalyst.pm index 09372ea..d4a70c3 100644 --- a/lib/Catalyst.pm +++ b/lib/Catalyst.pm @@ -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 config option to pass additional options +directly to L. To add additional search paths, specify a key named +C 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 ) { diff --git a/t/unit_core_component_loading.t b/t/unit_core_component_loading.t index a01edf0..ffd20a5 100644 --- a/t/unit_core_component_loading.t +++ b/t/unit_core_component_loading.t @@ -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