X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=lib%2FCatalyst%2FEngine.pm;h=0c0b8cc411b8700ecae47766dc8e265fdfbfca9c;hb=3245f607a5d5494a4e7b4e8b06a63891b87893e5;hp=1a2d3eed4817e8955c3bc45bf4a07c0b70db801c;hpb=6ddb9f0109fba570f50a54a74057f81bf8628f12;p=catagits%2FCatalyst-Runtime.git diff --git a/lib/Catalyst/Engine.pm b/lib/Catalyst/Engine.pm index 1a2d3ee..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 @@ -193,10 +199,6 @@ sub finalize { return $status; } -=item $c->finalize_output - -alias to finalize_body - =item $c->finalize_body Finalize body. @@ -404,12 +406,18 @@ sub prepare { cookies => {}, headers => HTTP::Headers->new, parameters => {}, + secure => 0, snippets => [], uploads => {} } ), response => Catalyst::Response->new( - { cookies => {}, headers => HTTP::Headers->new, status => 200 } + { + body => '', + cookies => {}, + headers => HTTP::Headers->new, + status => 200 + } ), stash => {}, state => 0 @@ -425,10 +433,10 @@ sub prepare { } $c->prepare_request($engine); - $c->prepare_path; + $c->prepare_connection; $c->prepare_headers; $c->prepare_cookies; - $c->prepare_connection; + $c->prepare_path; $c->prepare_action; my $method = $c->req->method || ''; @@ -593,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. @@ -627,6 +668,7 @@ Setup. sub setup { my $self = shift; + $self->retrieve_components; $self->setup_components; if ( $self->debug ) { my $name = $self->config->{name} || 'Application'; @@ -643,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 @@ -692,8 +730,8 @@ Returns a hashref containing all your data. sub stash { my $self = shift; - if ( $_[0] ) { - my $stash = $_[1] ? {@_} : $_[0]; + if ( @_ ) { + my $stash = @_ > 1 ? {@_} : $_[0]; while ( my ( $key, $val ) = each %$stash ) { $self->{stash}->{$key} = $val; }