if( !ref $name ) {
# is it the exact name?
- return $comps->{ $name } if exists $comps->{ $name };
+ return $c->_filter_component( $comps->{ $name }, @args )
+ if exists $comps->{ $name };
# perhaps we just omitted "MyApp"?
my $composed = ( ref $c || $c ) . "::${name}";
- return $comps->{ $composed } if exists $comps->{ $composed };
+ return $c->_filter_component( $comps->{ $composed }, @args )
+ if exists $comps->{ $composed };
# search all of the models, views and controllers
my( $comp ) = $c->_comp_search_prefixes( $name, qw/Model M Controller C View V/ );
my $query = ref $name ? $name : qr{$name}i;
my @result = grep { m{$query} } keys %{ $c->components };
- return @result if ref $name;
+ return map { $c->_filter_component( $_, @args ) } @result if ref $name;
if( $result[ 0 ] ) {
+ $c->log->warn( qq(Found results for "${name}" using regexp fallback.) );
$c->log->warn( 'Relying on the regexp fallback behavior for component resolution' );
$c->log->warn( 'is unreliable and unsafe. You have been warned' );
- return $result[ 0 ];
+ return $c->_filter_component( $result[ 0 ], @args );
}
# I would expect to return an empty list here, but that breaks back-compat
-use Test::More tests => 14;
+use Test::More tests => 22;
use strict;
use warnings;
is(MyApp->comp('Model'), 'MyApp::M::Model', 'Single part return ok');
-# regexp fallback
-{
- my $warnings = 0;
- no warnings 'redefine';
- local *Catalyst::Log::warn = sub { $warnings++ };
-
- is(MyApp->comp('::M::'), 'MyApp::M::Model', 'Regex return ok');
- ok( $warnings, 'regexp fallback for comp() warns' );
-}
-
is_deeply([ MyApp->comp() ], \@complist, 'Empty return ok');
# Is this desired behaviour?
is_deeply( [ MyApp->comp('MyApp::V::View$') ], [ 'MyApp::V::View' ], 'Explicit return ok');
is_deeply( [ MyApp->comp('MyApp::C::Controller$') ], [ 'MyApp::C::Controller' ], 'Explicit return ok');
is_deeply( [ MyApp->comp('MyApp::M::Model$') ], [ 'MyApp::M::Model' ], 'Explicit return ok');
+
+ # a couple other varieties for regexp fallback
+ is_deeply( [ MyApp->comp('M::Model') ], [ 'MyApp::M::Model' ], 'Explicit return ok');
+
+ {
+ my $warnings = 0;
+ no warnings 'redefine';
+ local *Catalyst::Log::warn = sub { $warnings++ };
+
+ is_deeply( [ MyApp->comp('::M::Model') ], [ 'MyApp::M::Model' ], 'Explicit return ok');
+ ok( $warnings, 'regexp fallback warnings' );
+
+ $warnings = 0;
+ is_deeply( [ MyApp->comp('Mode') ], [ 'MyApp::M::Model' ], 'Explicit return ok');
+ ok( $warnings, 'regexp fallback warnings' );
+
+ $warnings = 0;
+ is(MyApp->comp('::M::'), 'MyApp::M::Model', 'Regex return ok');
+ ok( $warnings, 'regexp fallback for comp() warns' );
+ }
+
}
# multiple returns
is_deeply( scalar MyApp->comp( qr{DNE} ), 0, 'no results for failed search' );
}
+
+#checking @args passed to ACCEPT_CONTEXT
+{
+ my $args;
+
+ no warnings;
+ *MyApp::M::Model::ACCEPT_CONTEXT = sub { my ($self, $c, @args) = @_; $args= \@args};
+
+ MyApp->component('MyApp::M::Model', qw/foo bar/);
+ is_deeply($args, [qw/foo bar/], 'args passed to ACCEPT_CONTEXT ok');
+
+ MyApp->component('M::Model', qw/foo2 bar2/);
+ is_deeply($args, [qw/foo2 bar2/], 'args passed to ACCEPT_CONTEXT ok');
+
+ MyApp->component('Mode', qw/foo3 bar3/);
+ is_deeply($args, [qw/foo3 bar3/], 'args passed to ACCEPT_CONTEXT ok');
+}
+
is_deeply( [ MyApp->controller( qr{Dummy\::Model$} ) ], [ 'MyApp::Controller::Model::Dummy::Model' ], 'regexp controller ok' );
is_deeply( [ MyApp->model( qr{Dum{2}y} ) ], [ 'MyApp::Model::Dummy::Model' ], 'regexp model ok' );
- # ACCEPT_CONTEXT w/ qr{}
+ # object w/ qr{}
is_deeply( [ MyApp->model( qr{Test} ) ], [ MyApp->components->{'MyApp::Model::Test::Object'} ], 'Object returned' );
{
no warnings 'redefine';
local *Catalyst::Log::warn = sub { $warnings++ };
- # ACCEPT_CONTEXT w/ regexp fallback
+ # object w/ regexp fallback
is_deeply( [ MyApp->model( 'Test' ) ], [ MyApp->components->{'MyApp::Model::Test::Object'} ], 'Object returned' );
ok( $warnings, 'regexp fallback warnings' );
}