making $app be an instance, less symbol table hijacking, cache path_prefix and action...
Guillermo Roditi [Mon, 23 Jun 2008 21:19:43 +0000 (21:19 +0000)]
r17835@martha (orig r7766):  groditi | 2008-05-20 16:10:51 -0400

lib/Catalyst.pm
lib/Catalyst/Controller.pm
t/unit_controller_namespace.t
t/unit_core_action_for.t

index e22ff81..0083d0d 100644 (file)
@@ -89,21 +89,17 @@ sub import {
     # callers @ISA.
     return unless $class eq 'Catalyst';
 
-    my $caller = caller(0);
+    my $caller = caller();
+    return if $caller eq 'main';
+    my $meta = Moose::Meta::Class->initialize($caller);
+    #Moose->import({ into => $caller }); #do we want to do this?
 
-    #why does called have to ISA Catalyst and ISA Controller ?
-    #Convert test suite to not use the behavior where Myapp ISA Controller
-    # after that is done we can eliminate that little mess.
     unless ( $caller->isa('Catalyst') ) {
-        no strict 'refs';
-        if( $caller->can('meta') ){
-          my @superclasses = ($caller->meta->superclasses, $class, 'Catalyst::Controller');
-          #my @superclasses = ($caller->meta->superclasses, $class);
-          $caller->meta->superclasses(@superclasses);
-        } else {
-          push @{"$caller\::ISA"}, $class, 'Catalyst::Controller';
-          #push @{"$caller\::ISA"}, $class;
-        }
+        my @superclasses = ($meta->superclasses, $class, 'Catalyst::Controller');
+        $meta->superclasses(@superclasses);
+    }
+    unless( $meta->has_method('meta') ){
+        $meta->add_method(meta => sub { Moose::Meta::Class->initialize("${caller}") } );
     }
 
     $caller->arguments( [@arguments] );
@@ -926,7 +922,7 @@ EOF
     }
 
     # Add our self to components, since we are also a component
-    $class->components->{$class} = $class;
+    $class->components->{$class} = $class->setup_component($class);
 
     $class->setup_actions;
 
@@ -1936,7 +1932,7 @@ sub setup_component {
     Catalyst::Exception->throw(
         message =>
         qq/Couldn't instantiate component "$component", "COMPONENT() didn't return an object-like value"/
-    ) unless eval { $instance->can( 'can' ) };
+    ) unless blessed($instance);
 
     return $instance;
 }
@@ -1988,10 +1984,7 @@ sub setup_engine {
     if ( $ENV{MOD_PERL} ) {
 
         # create the apache method
-        {
-            no strict 'refs';
-            *{"$class\::apache"} = sub { shift->engine->apache };
-        }
+        $class->meta->add_method('apache' => sub { shift->engine->apache });
 
         my ( $software, $version ) =
           $ENV{MOD_PERL} =~ /^(\S+)\/(\d+(?:[\.\_]\d+)+)/;
@@ -2126,9 +2119,7 @@ sub setup_log {
 
     my $env_debug = Catalyst::Utils::env_value( $class, 'DEBUG' );
     if ( defined($env_debug) ? $env_debug : $debug ) {
-        no strict 'refs';
-        #Moose todo: dying to be made a bool attribute
-        *{"$class\::debug"} = sub { 1 };
+        $class->meta->add_method('debug' => sub { 1 });
         $class->log->debug('Debug messages enabled');
     }
 }
@@ -2152,9 +2143,7 @@ sub setup_stats {
 
     my $env = Catalyst::Utils::env_value( $class, 'STATS' );
     if ( defined($env) ? $env : ($stats || $class->debug ) ) {
-        no strict 'refs';
-        #Moose todo: dying to be made a bool attribute
-        *{"$class\::use_stats"} = sub { 1 };
+        $class->meta->add_method('use_stats' => sub { 1 });
         $class->log->debug('Statistics enabled');
     }
 }
index 29ffc81..a76337e 100644 (file)
@@ -12,7 +12,7 @@ use Class::Inspector;
 
 has path_prefix =>
     (
-     is => 'ro',
+     is => 'rw',
      isa => 'Str',
      init_arg => 'path',
      predicate => 'has_path_prefix',
@@ -20,7 +20,7 @@ has path_prefix =>
 
 has action_namespace =>
     (
-     is => 'ro',
+     is => 'rw',
      isa => 'Str',
      init_arg => 'namespace',
      predicate => 'has_action_namespace',
@@ -149,7 +149,8 @@ around action_namespace => sub {
     if( ref($self) ){
         return $self->$orig if $self->has_action_namespace;
     } else { 
-        # if the following won't change at runtime it should be lazy_building thing
+       warn "action_namespace called as class method";
+       # if the following won't change at runtime it should be lazy_building thing
         return $self->config->{namespace} if exists $self->config->{namespace};
     }
 
@@ -171,7 +172,9 @@ around action_namespace => sub {
         }
     }
 
-    return Catalyst::Utils::class2prefix(ref($self) || $self, $case_s) || '';
+    my $namespace = Catalyst::Utils::class2prefix(ref($self) || $self, $case_s) || '';
+    $self->$orig($namespace) if ref($self);
+    return $namespace;
 };
 
 #Once again, this is probably better written as a builder method
@@ -183,7 +186,9 @@ around path_prefix => sub {
     } else {
       return $self->config->{path} if exists $self->config->{path};
     }
-    return $self->action_namespace(@_);
+    my $namespace = $self->action_namespace(@_);
+    $self->$orig($namespace) if ref($self);
+    return $namespace;
 };
 
 
@@ -192,26 +197,18 @@ sub register_actions {
     my $class = ref $self || $self;
     #this is still not correct for some reason.
     my $namespace = $self->action_namespace($c);
-    my %methods;
-    if( $self->can('meta') ){
-      my $meta = $self->meta;
-      %methods = map{ $_->{code}->body => $_->{name} }
+    my $meta = $self->meta;
+    my %methods = map{ $_->{code}->body => $_->{name} }
         grep {$_->{class} ne 'Moose::Object'} #ignore Moose::Object methods
-          $meta->compute_all_applicable_methods;
-    } else { #until we are sure there's no moose stuff left...
-      $methods{ $self->can($_) } = $_
-        for @{ Class::Inspector->methods($class) || [] };
-    }
+            $meta->compute_all_applicable_methods;
+
 
     # Advanced inheritance support for plugins and the like
-    #to be modified to use meta->superclasses
     #moose todo: migrate to eliminate CDI compat
     my @action_cache;
-    {
-        no strict 'refs';
-        for my $isa ( @{"$class\::ISA"}, $class ) {
-            push @action_cache, @{ $isa->_action_cache }
-              if $isa->can('_action_cache');
+    for my $isa ( $meta->superclasses, $class ) {
+        if(my $coderef = $isa->can('_action_cache')){
+            push(@action_cache, @{ $isa->$coderef });
         }
     }
 
index 41acfe7..90b5665 100644 (file)
@@ -19,6 +19,6 @@ BEGIN {
   sub config { {} };
 }
 
-is(MyApp::Controller::Foo->action_namespace('Stub'), 'foo');
+is(MyApp::Controller::Foo->COMPONENT->action_namespace('Stub'), 'foo');
 
-is(MyApp::Controller::Root->action_namespace('Stub'), '');
+is(MyApp::Controller::Root->COMPONENT->action_namespace('Stub'), '');
index 71772f8..fbff715 100644 (file)
@@ -12,7 +12,7 @@ plan tests => 3;
 
 use_ok('TestApp');
 
-is(TestApp->action_for('global_action')->code, TestApp->can('global_action'),
+is(TestApp->component('TestApp')->action_for('global_action')->code, TestApp->can('global_action'),
    'action_for on appclass ok');
 
 is(TestApp->controller('Args')->action_for('args')->code,