From: Christian Hansen Date: Wed, 27 Apr 2005 21:47:43 +0000 (+0000) Subject: Added support for non C::Base components to live in MyApp namespace X-Git-Tag: 5.7099_04~1420 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?p=catagits%2FCatalyst-Runtime.git;a=commitdiff_plain;h=3245f607a5d5494a4e7b4e8b06a63891b87893e5 Added support for non C::Base components to live in MyApp namespace --- diff --git a/Build.PL b/Build.PL index 7f08e12..7518026 100644 --- a/Build.PL +++ b/Build.PL @@ -16,7 +16,7 @@ my $build = Module::Build->new( 'HTTP::Request' => 0, 'HTTP::Response' => 0, 'LWP::UserAgent' => 0, - 'Module::Pluggable::Fast' => 0.14, + 'Module::Pluggable::Fast' => 0.15, 'Path::Class' => 0, 'Template' => 0, 'Text::ASCIITable' => 0, diff --git a/lib/Catalyst/Dispatcher.pm b/lib/Catalyst/Dispatcher.pm index 32ccd67..33d497f 100644 --- a/lib/Catalyst/Dispatcher.pm +++ b/lib/Catalyst/Dispatcher.pm @@ -144,6 +144,13 @@ sub forward { $c->log->debug($error) if $c->debug; return 0; } + + unless ( UNIVERSAL::isa( $class, 'Catalyst::Base' ) ) { + my $error = qq/Can't forward to "$class". Class is not a Catalyst component./; + $c->error($error); + $c->log->debug($error) if $c->debug; + return 0; + } my $method = shift || 'process'; diff --git a/lib/Catalyst/Engine.pm b/lib/Catalyst/Engine.pm index de0bbc3..0c0b8cc 100644 --- a/lib/Catalyst/Engine.pm +++ b/lib/Catalyst/Engine.pm @@ -80,18 +80,24 @@ Regex search for a component. =cut sub component { - my ( $c, $name ) = @_; + my $c = shift; - if ( my $component = $c->components->{$name} ) { - return $component; - } + if ( @_ ) { - else { - for my $component ( keys %{ $c->components } ) { - return $c->components->{$component} if $component =~ /$name/i; + my $name = shift; + + if ( my $component = $c->components->{$name} ) { + return $component; + } + + else { + for my $component ( keys %{ $c->components } ) { + return $c->components->{$component} if $component =~ /$name/i; + } } } + return sort keys %{ $c->components }; } =item $c->error @@ -595,6 +601,39 @@ Prepare uploads. sub prepare_uploads { } +=item $c->retrieve_components + +Retrieve Components. + +=cut + +sub retrieve_components { + my $self = shift; + + my $class = ref $self || $self; + eval <<""; + package $class; + import Module::Pluggable::Fast + name => '_components', + search => [ + '$class\::Controller', '$class\::C', + '$class\::Model', '$class\::M', + '$class\::View', '$class\::V' + ], + require => 1; + + if ( my $error = $@ ) { + chomp $error; + die qq/Couldn't load components "$error"/; + } + + $self->components( {} ); + + for my $component ( $self->_components ) { + $self->components->{$component} = $component; + } +} + =item $c->run Starts the engine. @@ -629,6 +668,7 @@ Setup. sub setup { my $self = shift; + $self->retrieve_components; $self->setup_components; if ( $self->debug ) { my $name = $self->config->{name} || 'Application'; @@ -645,38 +685,34 @@ Setup components. sub setup_components { my $self = shift; - # Components - my $class = ref $self || $self; - eval <<""; - package $class; - import Module::Pluggable::Fast - name => '_components', - search => [ - '$class\::Controller', '$class\::C', - '$class\::Model', '$class\::M', - '$class\::View', '$class\::V' - ]; + my @components; + for my $component ( keys %{ $self->components } ) { - if ( my $error = $@ ) { - chomp $error; - die qq/Couldn't load components "$error"/; - } + unless ( UNIVERSAL::isa( $component, 'Catalyst::Base' ) ) { + next; + } - $self->components( {} ); - my @comps; - for my $comp ( $self->_components($self) ) { - $self->components->{ ref $comp } = $comp; - push @comps, $comp; - } + my $instance; + + eval { $instance = $component->new($self) }; + + if ( $@ ) { + die( qq/Couldn't instantiate "$component", "$@"/ ); + } + + $self->components->{$component} = $instance; + push @components, $component; + } + my $t = Text::ASCIITable->new( { hide_HeadRow => 1, hide_HeadLine => 1 } ); $t->setCols('Class'); $t->setColWidth( 'Class', 75, 1 ); - $t->addRow($_) for keys %{ $self->components }; + $t->addRow($_) for sort keys %{ $self->components }; $self->log->debug( 'Loaded components', $t->draw ) if ( @{ $t->{tbl_rows} } && $self->debug ); - $self->setup_actions( [ $self, @comps ] ); + $self->setup_actions( [ $self, @components ] ); } =item $c->state