Reverting back to old behavior for components
[catagits/Catalyst-Runtime.git] / lib / Catalyst / Engine.pm
index cbc5896..f74fa73 100644 (file)
@@ -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
@@ -179,12 +185,13 @@ sub finalize {
         $c->finalize_error;
     }
 
-    if ( !$c->response->body_length && $c->response->status !~ /^(1|3)\d\d$/ ) {
+    if ( !$c->response->body && $c->response->status !~ /^(1|3)\d\d$/ ) {
         $c->finalize_error;
     }
 
-    if ( $c->response->body_length && !$c->response->content_length ) {
-        $c->response->content_length( $c->response->body_length );
+    if ( $c->response->body && !$c->response->content_length ) {
+        use bytes;    # play safe with a utf8 aware perl
+        $c->response->content_length( length $c->response->body );
     }
 
     my $status = $c->finalize_headers;
@@ -192,10 +199,6 @@ sub finalize {
     return $status;
 }
 
-=item $c->finalize_output
-
-alias to finalize_body
-
 =item $c->finalize_body
 
 Finalize body.
@@ -400,20 +403,20 @@ sub prepare {
         request => Catalyst::Request->new(
             {
                 arguments  => [],
-                body       => undef,
                 cookies    => {},
                 headers    => HTTP::Headers->new,
                 parameters => {},
+                secure     => 0,
                 snippets   => [],
                 uploads    => {}
             }
         ),
         response => Catalyst::Response->new(
-            { 
-                body       => undef,
-                cookies    => {},
-                headers    => HTTP::Headers->new,
-                status     => 200
+            {
+                body    => '',
+                cookies => {},
+                headers => HTTP::Headers->new,
+                status  => 200
             }
         ),
         stash => {},
@@ -598,6 +601,35 @@ 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"/;
+    }
+
+    return $self->_components;
+}
+
 =item $c->run
 
 Starts the engine.
@@ -675,7 +707,7 @@ sub setup_components {
     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 );
 
@@ -697,8 +729,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;
         }