[Catalyst-commits] r7725 - in Catalyst-Runtime/5.80/branches/moose: lib lib/Catalyst lib/Catalyst/DispatchType t

konobi at dev.catalyst.perl.org konobi at dev.catalyst.perl.org
Thu May 8 19:42:41 BST 2008


Author: konobi
Date: 2008-05-08 19:42:41 +0100 (Thu, 08 May 2008)
New Revision: 7725

Modified:
   Catalyst-Runtime/5.80/branches/moose/lib/Catalyst.pm
   Catalyst-Runtime/5.80/branches/moose/lib/Catalyst/Action.pm
   Catalyst-Runtime/5.80/branches/moose/lib/Catalyst/ActionChain.pm
   Catalyst-Runtime/5.80/branches/moose/lib/Catalyst/DispatchType.pm
   Catalyst-Runtime/5.80/branches/moose/lib/Catalyst/DispatchType/Chained.pm
   Catalyst-Runtime/5.80/branches/moose/lib/Catalyst/DispatchType/Default.pm
   Catalyst-Runtime/5.80/branches/moose/lib/Catalyst/DispatchType/Index.pm
   Catalyst-Runtime/5.80/branches/moose/lib/Catalyst/DispatchType/Path.pm
   Catalyst-Runtime/5.80/branches/moose/lib/Catalyst/DispatchType/Regex.pm
   Catalyst-Runtime/5.80/branches/moose/lib/Catalyst/Dispatcher.pm
   Catalyst-Runtime/5.80/branches/moose/lib/Catalyst/Engine.pm
   Catalyst-Runtime/5.80/branches/moose/lib/Catalyst/Exception.pm
   Catalyst-Runtime/5.80/branches/moose/lib/Catalyst/Log.pm
   Catalyst-Runtime/5.80/branches/moose/lib/Catalyst/Model.pm
   Catalyst-Runtime/5.80/branches/moose/lib/Catalyst/Request.pm
   Catalyst-Runtime/5.80/branches/moose/lib/Catalyst/Response.pm
   Catalyst-Runtime/5.80/branches/moose/lib/Catalyst/Stats.pm
   Catalyst-Runtime/5.80/branches/moose/lib/Catalyst/View.pm
   Catalyst-Runtime/5.80/branches/moose/t/live_engine_request_parameters.t
Log:
* Updated Catalyst::Request and Catalyst::Response to have sensible defaults for attributes
* Updated Catalyst.pm to reflect sensible defaults
* Worked through some TODO items added by groditi
* Fixed up a test that was testing against object internals rather than accessors


Modified: Catalyst-Runtime/5.80/branches/moose/lib/Catalyst/Action.pm
===================================================================
--- Catalyst-Runtime/5.80/branches/moose/lib/Catalyst/Action.pm	2008-05-08 18:36:35 UTC (rev 7724)
+++ Catalyst-Runtime/5.80/branches/moose/lib/Catalyst/Action.pm	2008-05-08 18:42:41 UTC (rev 7725)
@@ -49,15 +49,15 @@
     my ( $self, $c ) = @_;
     #Moose todo: grrrrrr. this is no good. i don't know enough about it to
     # debug it though. why can't we just call the accessor?
-    local $c->{namespace} = $self->namespace;
-    return $c->execute( $self->class, $self );
+    #local $c->{namespace} = $self->namespace;
+    #return $c->execute( $self->class, $self );
 
     #believed to be equivalent:
-    #my $orig = $c->namespace;
-    #$c->namespace($self->namespace);
-    #my $ret = $c->execute( $self->class, $self );
-    #$c->namespace($orig);
-    #return $ret;
+    my $orig = $c->namespace;
+    $c->namespace($self->namespace);
+    my $ret = $c->execute( $self->class, $self );
+    $c->namespace($orig);
+    return $ret;
 }
 
 sub execute {

Modified: Catalyst-Runtime/5.80/branches/moose/lib/Catalyst/ActionChain.pm
===================================================================
--- Catalyst-Runtime/5.80/branches/moose/lib/Catalyst/ActionChain.pm	2008-05-08 18:36:35 UTC (rev 7724)
+++ Catalyst-Runtime/5.80/branches/moose/lib/Catalyst/ActionChain.pm	2008-05-08 18:42:41 UTC (rev 7725)
@@ -57,6 +57,7 @@
     return $self->new({ %$final, chain => $actions });
 }
 
+no Moose;
 __PACKAGE__->meta->make_immutable;
 1;
 

Modified: Catalyst-Runtime/5.80/branches/moose/lib/Catalyst/DispatchType/Chained.pm
===================================================================
--- Catalyst-Runtime/5.80/branches/moose/lib/Catalyst/DispatchType/Chained.pm	2008-05-08 18:36:35 UTC (rev 7724)
+++ Catalyst-Runtime/5.80/branches/moose/lib/Catalyst/DispatchType/Chained.pm	2008-05-08 18:42:41 UTC (rev 7725)
@@ -328,6 +328,7 @@
    
 }
 
+no Moose;
 __PACKAGE__->meta->make_immutable;
 
 =head1 USAGE

Modified: Catalyst-Runtime/5.80/branches/moose/lib/Catalyst/DispatchType/Default.pm
===================================================================
--- Catalyst-Runtime/5.80/branches/moose/lib/Catalyst/DispatchType/Default.pm	2008-05-08 18:36:35 UTC (rev 7724)
+++ Catalyst-Runtime/5.80/branches/moose/lib/Catalyst/DispatchType/Default.pm	2008-05-08 18:42:41 UTC (rev 7725)
@@ -61,6 +61,7 @@
 
 =cut
 
+no Moose;
 __PACKAGE__->meta->make_immutable;
 
 1;

Modified: Catalyst-Runtime/5.80/branches/moose/lib/Catalyst/DispatchType/Index.pm
===================================================================
--- Catalyst-Runtime/5.80/branches/moose/lib/Catalyst/DispatchType/Index.pm	2008-05-08 18:36:35 UTC (rev 7724)
+++ Catalyst-Runtime/5.80/branches/moose/lib/Catalyst/DispatchType/Index.pm	2008-05-08 18:42:41 UTC (rev 7725)
@@ -69,6 +69,7 @@
 
 =cut
 
+no Moose;
 __PACKAGE__->meta->make_immutable;
 
 1;

Modified: Catalyst-Runtime/5.80/branches/moose/lib/Catalyst/DispatchType/Path.pm
===================================================================
--- Catalyst-Runtime/5.80/branches/moose/lib/Catalyst/DispatchType/Path.pm	2008-05-08 18:36:35 UTC (rev 7724)
+++ Catalyst-Runtime/5.80/branches/moose/lib/Catalyst/DispatchType/Path.pm	2008-05-08 18:42:41 UTC (rev 7725)
@@ -140,6 +140,7 @@
 
 =cut
 
+no Moose;
 __PACKAGE__->meta->make_immutable;
 
 1;

Modified: Catalyst-Runtime/5.80/branches/moose/lib/Catalyst/DispatchType/Regex.pm
===================================================================
--- Catalyst-Runtime/5.80/branches/moose/lib/Catalyst/DispatchType/Regex.pm	2008-05-08 18:36:35 UTC (rev 7724)
+++ Catalyst-Runtime/5.80/branches/moose/lib/Catalyst/DispatchType/Regex.pm	2008-05-08 18:42:41 UTC (rev 7725)
@@ -161,6 +161,7 @@
 
 =cut
 
+no Moose;
 __PACKAGE__->meta->make_immutable;
 
 1;

Modified: Catalyst-Runtime/5.80/branches/moose/lib/Catalyst/DispatchType.pm
===================================================================
--- Catalyst-Runtime/5.80/branches/moose/lib/Catalyst/DispatchType.pm	2008-05-08 18:36:35 UTC (rev 7724)
+++ Catalyst-Runtime/5.80/branches/moose/lib/Catalyst/DispatchType.pm	2008-05-08 18:42:41 UTC (rev 7725)
@@ -71,6 +71,7 @@
 
 =cut
 
+no Moose;
 __PACKAGE__->meta->make_immutable;
 
 1;

Modified: Catalyst-Runtime/5.80/branches/moose/lib/Catalyst/Dispatcher.pm
===================================================================
--- Catalyst-Runtime/5.80/branches/moose/lib/Catalyst/Dispatcher.pm	2008-05-08 18:36:35 UTC (rev 7724)
+++ Catalyst-Runtime/5.80/branches/moose/lib/Catalyst/Dispatcher.pm	2008-05-08 18:42:41 UTC (rev 7725)
@@ -171,10 +171,11 @@
 
     no warnings 'recursion';
 
-    #moose todo: reaching inside another object is bad
-    local $c->request->{arguments} = \@args;
+    my $orig_args = $c->request->arguments();
+    $c->request->arguments(\@args);
     $action->dispatch( $c );
-
+    $c->request->arguments($orig_args);
+    
     return $c->state;
 }
 
@@ -282,7 +283,6 @@
         unshift @args, $arg;
     }
 
-    #Moose todo: This seems illegible, even if efficient.
     s/%([0-9A-Fa-f]{2})/chr(hex($1))/eg for grep { defined } @{$req->captures||[]};
 
     $c->log->debug( 'Path is "' . $req->match . '"' )
@@ -531,6 +531,7 @@
     return @loaded;
 }
 
+no Moose;
 __PACKAGE__->meta->make_immutable;
 
 =head2 meta

Modified: Catalyst-Runtime/5.80/branches/moose/lib/Catalyst/Engine.pm
===================================================================
--- Catalyst-Runtime/5.80/branches/moose/lib/Catalyst/Engine.pm	2008-05-08 18:36:35 UTC (rev 7724)
+++ Catalyst-Runtime/5.80/branches/moose/lib/Catalyst/Engine.pm	2008-05-08 18:42:41 UTC (rev 7725)
@@ -130,9 +130,6 @@
         # Don't show body parser in the dump
         delete $c->req->{_body};
 
-        # Don't show response header state in dump
-        delete $c->res->{_finalized_headers};
-
         my @infos;
         my $i = 0;
         for my $dump ( $c->dump_these ) {

Modified: Catalyst-Runtime/5.80/branches/moose/lib/Catalyst/Exception.pm
===================================================================
--- Catalyst-Runtime/5.80/branches/moose/lib/Catalyst/Exception.pm	2008-05-08 18:36:35 UTC (rev 7724)
+++ Catalyst-Runtime/5.80/branches/moose/lib/Catalyst/Exception.pm	2008-05-08 18:42:41 UTC (rev 7725)
@@ -71,6 +71,7 @@
     extends($CATALYST_EXCEPTION_CLASS || 'Catalyst::Exception::Base');
 }
 
-Catalyst::Exception->meta->make_immutable;
+no Moose;
+__PACKAGE__->meta->make_immutable;
 
 1;

Modified: Catalyst-Runtime/5.80/branches/moose/lib/Catalyst/Log.pm
===================================================================
--- Catalyst-Runtime/5.80/branches/moose/lib/Catalyst/Log.pm	2008-05-08 18:36:35 UTC (rev 7724)
+++ Catalyst-Runtime/5.80/branches/moose/lib/Catalyst/Log.pm	2008-05-08 18:42:41 UTC (rev 7725)
@@ -99,6 +99,9 @@
     print STDERR @_;
 }
 
+no Moose;
+__PACKAGE__->meta->make_immutable();
+
 1;
 
 __END__

Modified: Catalyst-Runtime/5.80/branches/moose/lib/Catalyst/Model.pm
===================================================================
--- Catalyst-Runtime/5.80/branches/moose/lib/Catalyst/Model.pm	2008-05-08 18:36:35 UTC (rev 7724)
+++ Catalyst-Runtime/5.80/branches/moose/lib/Catalyst/Model.pm	2008-05-08 18:42:41 UTC (rev 7725)
@@ -3,6 +3,9 @@
 use Moose;
 extends qw/Catalyst::Component/;
 
+no Moose;
+__PACKAGE__->meta->make_immutable();
+
 =head1 NAME
 
 Catalyst::Model - Catalyst Model base class

Modified: Catalyst-Runtime/5.80/branches/moose/lib/Catalyst/Request.pm
===================================================================
--- Catalyst-Runtime/5.80/branches/moose/lib/Catalyst/Request.pm	2008-05-08 18:36:35 UTC (rev 7724)
+++ Catalyst-Runtime/5.80/branches/moose/lib/Catalyst/Request.pm	2008-05-08 18:42:41 UTC (rev 7725)
@@ -6,6 +6,7 @@
 use URI::http;
 use URI::https;
 use URI::QueryParam;
+use HTTP::Headers;
 
 use Moose;
 
@@ -26,6 +27,9 @@
   is      => 'rw',
   isa     => 'HTTP::Headers',
   handles => [qw(content_encoding content_length content_type header referer user_agent)],
+  default => sub { HTTP::Headers->new() },
+  required => 1,
+  lazy => 1,
 );
 
 has _context => (
@@ -54,7 +58,7 @@
 
 before uploads => sub {
   my ($self) = @_;
-  $self->_context->prepare_body;
+  #$self->_context->prepare_body;
 };
 
 has parameters => (
@@ -66,7 +70,7 @@
 
 before parameters => sub {
   my ($self, $params) = @_;
-  $self->_context->prepare_body();
+  #$self->_context->prepare_body();
   if ( $params && !ref $params ) {
     $self->_context->log->warn(
         "Attempt to retrieve '$params' with req->params(), " .

Modified: Catalyst-Runtime/5.80/branches/moose/lib/Catalyst/Response.pm
===================================================================
--- Catalyst-Runtime/5.80/branches/moose/lib/Catalyst/Response.pm	2008-05-08 18:36:35 UTC (rev 7724)
+++ Catalyst-Runtime/5.80/branches/moose/lib/Catalyst/Response.pm	2008-05-08 18:42:41 UTC (rev 7725)
@@ -1,16 +1,20 @@
 package Catalyst::Response;
 
 use Moose;
+use HTTP::Headers;
 
-has cookies   => (is => 'rw');
-has body      => (is => 'rw');
+has cookies   => (is => 'rw', default => sub { {} });
+has body      => (is => 'rw', default => '');
 has location  => (is => 'rw');
-has status    => (is => 'rw');
+has status    => (is => 'rw', default => 200);
+has finalized_headers => (is => 'rw', default => 0);
 has headers   => (
   is      => 'rw',
   handles => [qw(content_encoding content_length content_type header)],
+  default => sub { HTTP::Headers->new() },
+  required => 1,
+  lazy => 1,
 );
-
 has _context => (
   is => 'rw',
   weak_ref => 1,

Modified: Catalyst-Runtime/5.80/branches/moose/lib/Catalyst/Stats.pm
===================================================================
--- Catalyst-Runtime/5.80/branches/moose/lib/Catalyst/Stats.pm	2008-05-08 18:36:35 UTC (rev 7724)
+++ Catalyst-Runtime/5.80/branches/moose/lib/Catalyst/Stats.pm	2008-05-08 18:42:41 UTC (rev 7725)
@@ -120,6 +120,9 @@
     return $visitor->getResult;
 } 
 
+no Moose;
+__PACKAGE__->meta->make_immutable();
+
 1;
 
 __END__

Modified: Catalyst-Runtime/5.80/branches/moose/lib/Catalyst/View.pm
===================================================================
--- Catalyst-Runtime/5.80/branches/moose/lib/Catalyst/View.pm	2008-05-08 18:36:35 UTC (rev 7724)
+++ Catalyst-Runtime/5.80/branches/moose/lib/Catalyst/View.pm	2008-05-08 18:42:41 UTC (rev 7725)
@@ -64,4 +64,7 @@
 
 =cut
 
+no Moose;
+__PACKAGE__->meta->make_immutable();
+
 1;

Modified: Catalyst-Runtime/5.80/branches/moose/lib/Catalyst.pm
===================================================================
--- Catalyst-Runtime/5.80/branches/moose/lib/Catalyst.pm	2008-05-08 18:36:35 UTC (rev 7724)
+++ Catalyst-Runtime/5.80/branches/moose/lib/Catalyst.pm	2008-05-08 18:42:41 UTC (rev 7725)
@@ -29,14 +29,14 @@
 
 BEGIN { require 5.008001; }
 
-has stack => (is => 'rw');
-has stash => (is => 'rw');
-has state => (is => 'rw');
+has stack => (is => 'rw', default => sub { [] });
+has stash => (is => 'rw', default => sub { {} });
+has state => (is => 'rw', default => 0);
 has stats => (is => 'rw');
 has action => (is => 'rw');
-has counter => (is => 'rw');
-has request => (is => 'rw');
-has response => (is => 'rw');
+has counter => (is => 'rw', default => sub { {} });
+has request => (is => 'rw', default => sub { $_[0]->request_class->new({}) }, required => 1, lazy => 1);
+has response => (is => 'rw', default => sub { $_[0]->response_class->new({}) }, required => 1, lazy => 1);
 has namespace => (is => 'rw');
 
 
@@ -46,9 +46,16 @@
 
 # Laziness++
 *comp = \&component;
-*req  = \&request;
-*res  = \&response;
 
+sub req {
+    # carp "the use of req() is deprecated in favour of request()";
+    my $self = shift; return $self->request(@_);
+}
+sub res {
+    # carp "the use of res() is deprecated in favour of response()";
+    my $self = shift; return $self->response(@_);
+}
+
 # For backwards compatibility
 *finalize_output = \&finalize_body;
 
@@ -330,7 +337,7 @@
 
 =cut
 
-sub forward { my $c = shift; $c->dispatcher->forward( $c, @_ ) }
+sub forward { my $c = shift; no warnings 'recursion'; $c->dispatcher->forward( $c, @_ ) }
 
 =head2 $c->detach( $action [, \@arguments ] )
 
@@ -375,14 +382,16 @@
 around stash => sub {
     my $orig = shift;
     my $c = shift;
+
+    my $orig_stash = $c->$orig();
     if (@_) {
         my $stash = @_ > 1 ? {@_} : $_[0];
         croak('stash takes a hash or hashref') unless ref $stash;
         foreach my $key ( keys %$stash ) {
-            $c->$orig()->{$key} = $stash->{$key};
+            $orig_stash->{$key} = $stash->{$key};
         }
     }
-    return $c->$orig();
+    return $orig_stash;
 };
 
 =head2 $c->error
@@ -1429,9 +1438,8 @@
 
     my $response = $c->response; #accessor calls can add up?
 
-    # Moose TODO: Maybe this should be an attribute too?
     # Check if we already finalized headers
-    return if $response->{_finalized_headers};
+    return if $response->finalized_headers;
 
     # Handle redirects
     if ( my $location = $response->redirect ) {
@@ -1478,7 +1486,7 @@
     $c->engine->finalize_headers( $c, @_ );
 
     # Done
-    $response->{_finalized_headers} = 1;
+    $response->finalized_headers(1);
 }
 
 =head2 $c->finalize_output
@@ -1548,8 +1556,10 @@
     }
 
     $COUNT++;
-    #todo: reuse coderef from can
-    $class->log->_flush() if $class->log->can('_flush');
+    
+    if(my $coderef = $class->log->can('_flush')){
+        $class->log->$coderef();
+    }
     return $status;
 }
 
@@ -1563,50 +1573,23 @@
 sub prepare {
     my ( $class, @arguments ) = @_;
 
-    #moose todo: context_class as attr with default
+    # XXX
+    # After the app/ctxt split, this should become an attribute based on something passed
+    # into the application.
     $class->context_class( ref $class || $class ) unless $class->context_class;
-    #Moose TODO: if we make empty containers the defaults then that can be
-    #handled by the context class itself instead of having this here
-    my $c = $class->context_class->new(
-        {
-            counter => {},
-            stack   => [],
-            request => $class->request_class->new(
-                {
-                    arguments        => [],
-                    body_parameters  => {},
-                    cookies          => {},
-                    headers          => HTTP::Headers->new,
-                    parameters       => {},
-                    query_parameters => {},
-                    secure           => 0,
-                    captures         => [],
-                    uploads          => {}
-                }
-            ),
-            response => $class->response_class->new(
-                {
-                    body    => '',
-                    cookies => {},
-                    headers => HTTP::Headers->new(),
-                    status  => 200
-                }
-            ),
-            stash => {},
-            state => 0
-        }
-    );
+   
+    my $c = $class->context_class->new({});
 
+    # For on-demand data
+    $c->request->_context($c);
+    $c->response->_context($c);
+
     #surely this is not the most efficient way to do things...
     $c->stats($class->stats_class->new)->enable($c->use_stats);
     if ( $c->debug ) {
         $c->res->headers->header( 'X-Catalyst' => $Catalyst::VERSION );            
     }
 
-    # For on-demand data
-    $c->request->_context($c);
-    $c->response->_context($c);
-
     #XXX reuse coderef from can
     # Allow engine to direct the prepare flow (for POE)
     if ( $c->engine->can('prepare') ) {

Modified: Catalyst-Runtime/5.80/branches/moose/t/live_engine_request_parameters.t
===================================================================
--- Catalyst-Runtime/5.80/branches/moose/t/live_engine_request_parameters.t	2008-05-08 18:36:35 UTC (rev 7724)
+++ Catalyst-Runtime/5.80/branches/moose/t/live_engine_request_parameters.t	2008-05-08 18:42:41 UTC (rev 7725)
@@ -32,7 +32,7 @@
     ok( eval '$creq = ' . $response->content, 'Unserialize Catalyst::Request' );
     isa_ok( $creq, 'Catalyst::Request' );
     is( $creq->method, 'GET', 'Catalyst::Request method' );
-    is_deeply( $creq->{parameters}, $parameters,
+    is_deeply( $creq->parameters, $parameters,
         'Catalyst::Request parameters' );
 }
 
@@ -43,7 +43,7 @@
     ok( $response->is_success, 'Response Successful 2xx' );
     is( $response->content_type, 'text/plain', 'Response Content-Type' );
     ok( eval '$creq = ' . $response->content );
-    is $creq->{parameters}->{q}, 'foo+bar', '%2b not double decoded';
+    is $creq->parameters->{q}, 'foo+bar', '%2b not double decoded';
 }
 
 {
@@ -53,7 +53,7 @@
     ok( $response->is_success, 'Response Successful 2xx' );
     is( $response->content_type, 'text/plain', 'Response Content-Type' );
     ok( eval '$creq = ' . $response->content );
-    is $creq->{parameters}->{q}, 'foo=bar', '= not ignored';
+    is $creq->parameters->{q}, 'foo=bar', '= not ignored';
 }
 
 {
@@ -84,10 +84,10 @@
     ok( eval '$creq = ' . $response->content, 'Unserialize Catalyst::Request' );
     isa_ok( $creq, 'Catalyst::Request' );
     is( $creq->method, 'POST', 'Catalyst::Request method' );
-    is_deeply( $creq->{parameters}, $parameters,
+    is_deeply( $creq->parameters, $parameters,
         'Catalyst::Request parameters' );
     is_deeply( $creq->arguments, [qw(a b)], 'Catalyst::Request arguments' );
-    is_deeply( $creq->{uploads}, {}, 'Catalyst::Request uploads' );
+    is_deeply( $creq->uploads,   {}, 'Catalyst::Request uploads' );
     is_deeply( $creq->cookies,   {}, 'Catalyst::Request cookie' );
 }
 
@@ -109,7 +109,7 @@
 
     ok( my $response = request($request), 'Request' );
     ok( eval '$creq = ' . $response->content, 'Unserialize Catalyst::Request' );
-    is_deeply( $creq->{parameters}, $parameters, 'Catalyst::Request parameters' );
+    is_deeply( $creq->parameters, $parameters, 'Catalyst::Request parameters' );
 }
 
 # raw query string support
@@ -129,11 +129,11 @@
     
     ok( my $response = request($request), 'Request' );
     ok( eval '$creq = ' . $response->content, 'Unserialize Catalyst::Request' );
-    is( $creq->{uri}->query, 'query+string', 'Catalyst::Request POST query_string' );
+    is( $creq->uri->query, 'query+string', 'Catalyst::Request POST query_string' );
     is( $creq->query_keywords, 'query string', 'Catalyst::Request query_keywords' );
-    is_deeply( $creq->{parameters}, $parameters, 'Catalyst::Request parameters' );
+    is_deeply( $creq->parameters, $parameters, 'Catalyst::Request parameters' );
     
     ok( $response = request('http://localhost/dump/request/a/b?x=1&y=1&z=1'), 'Request' );
     ok( eval '$creq = ' . $response->content, 'Unserialize Catalyst::Request' );
-    is( $creq->{uri}->query, 'x=1&y=1&z=1', 'Catalyst::Request GET query_string' );
+    is( $creq->uri->query, 'x=1&y=1&z=1', 'Catalyst::Request GET query_string' );
 }




More information about the Catalyst-commits mailing list