Added support for ~ prefix to plugins and action classes
Moritz Onken [Sun, 7 Jun 2009 12:28:53 +0000 (12:28 +0000)]
Changes
TODO
lib/Catalyst.pm
lib/Catalyst/Controller.pm
lib/Catalyst/Dispatcher.pm
lib/Catalyst/Utils.pm
t/aggregate/live_component_controller_action_action.t
t/lib/TestApp/Controller/Action/Action.pm

diff --git a/Changes b/Changes
index 73321fe..969f9fb 100644 (file)
--- a/Changes
+++ b/Changes
@@ -1,5 +1,8 @@
 # This file documents the revision history for Perl extension Catalyst.
 
+        - Use ~ as prefix for plugins or action classes which are located in MyApp::Plugin / MyApp::Action 
+
+
 5.80005 2009-06-06 14:40:00
 
   Behaviour changes:
diff --git a/TODO b/TODO
index fcade74..6e237a1 100644 (file)
--- a/TODO
+++ b/TODO
@@ -1,6 +1,6 @@
 TODO for brach namespace_handling_refactor:
 
-- refactor code in 
+- refactor code in
   * Catalyst::Dispatcher::get_containers           # No Idea
   * Catalyst::Dispatcher::dispatch_type            # DONE
 
index 426cbb3..877c53c 100644 (file)
@@ -2502,8 +2502,8 @@ the plugin name does not begin with C<Catalyst::Plugin::>.
 
         $class->_plugins( {} ) unless $class->_plugins;
         $plugins ||= [];
-        
-        my @plugins = Catalyst::Utils::resolve_namespace('Catalyst::Plugin', @$plugins);
+                
+        my @plugins = Catalyst::Utils::resolve_namespace($class . '::Plugin', 'Catalyst::Plugin', @$plugins);
         
         for my $plugin ( reverse @plugins ) {
             Class::MOP::load_class($plugin);
index 6924405..68e8b42 100644 (file)
@@ -390,7 +390,8 @@ sub _parse_PathPrefix_attr {
 
 sub _parse_ActionClass_attr {
     my ( $self, $c, $name, $value ) = @_;
-    $value = Catalyst::Utils::resolve_namespace($self->_action_class, $value);
+    my $appname = $self->_application;
+    $value = Catalyst::Utils::resolve_namespace($appname . '::Action', $self->_action_class, $value);
     return ( 'ActionClass', $value );
 }
 
index 5df4526..99c46bc 100644 (file)
@@ -640,10 +640,10 @@ sub _load_dispatch_types {
     my ( $self, @types ) = @_;
 
     my @loaded;
-
     # Preload action types
     for my $type (@types) {
-        my $class = Catalyst::Utils::resolve_namespace('Catalyst::DispatchType', $type);
+        # first param is undef because we cannot get the appclass
+        my $class = Catalyst::Utils::resolve_namespace(undef, 'Catalyst::DispatchType', $type);
         
         eval { Class::MOP::load_class($class) };
         Catalyst::Exception->throw( message => qq/Couldn't load "$class"/ )
@@ -666,8 +666,9 @@ of course it's being used.)
 
 sub dispatch_type {
     my ($self, $name) = @_;
-
-    $name = Catalyst::Utils::resolve_namespace('Catalyst::DispatchType', $name);
+    
+    # first param is undef because we cannot get the appclass
+    $name = Catalyst::Utils::resolve_namespace(undef, 'Catalyst::DispatchType', $name);
 
     for (@{ $self->_dispatch_types }) {
         return $_ if ref($_) eq $name;
index 76f0733..e791b4a 100644 (file)
@@ -392,10 +392,11 @@ Method which adds the namespace for plugins and actions.
 
 
 sub resolve_namespace {
+    my $appnamespace = shift;
     my $namespace = shift;
     my @classes = @_;
     return String::RewritePrefix->rewrite(
-        { '' => $namespace.'::', '+' => '' }, @classes,
+        { '' => $namespace.'::', '+' => '', '~' => $appnamespace . '::' }, @classes,
       );
 }
 
index ea52e78..1e11b18 100644 (file)
@@ -10,7 +10,7 @@ our $iters;
 
 BEGIN { $iters = $ENV{CAT_BENCH_ITERS} || 1; }
 
-use Test::More tests => 28 * $iters;
+use Test::More tests => 42 * $iters;
 use Catalyst::Test 'TestApp';
 
 if ( $ENV{CAT_BENCHMARK} ) {
@@ -106,5 +106,46 @@ sub run_tests {
             'Content is a serialized Catalyst::Request'
         );
     }
+    
+    {
+        ok( my $response = request('http://localhost/action_action_five'),
+            'Request' );
+        ok( $response->is_success, 'Response Successful 2xx' );
+        is( $response->content_type, 'text/plain', 'Response Content-Type' );
+        is( $response->header('X-Catalyst-Action'),
+            'action_action_five', 'Test Action' );
+        is(
+            $response->header('X-Test-Class'),
+            'TestApp::Controller::Action::Action',
+            'Test Class'
+        );
+        is( $response->header('X-Action'), 'works' );
+        like(
+            $response->content,
+            qr/^bless\( .* 'Catalyst::Request' \)$/s,
+            'Content is a serialized Catalyst::Request'
+        );
+    }
+    
+
+    {
+        ok( my $response = request('http://localhost/action_action_six'),
+            'Request' );
+        ok( $response->is_success, 'Response Successful 2xx' );
+        is( $response->content_type, 'text/plain', 'Response Content-Type' );
+        is( $response->header('X-Catalyst-Action'),
+            'action_action_six', 'Test Action' );
+        is(
+            $response->header('X-Test-Class'),
+            'TestApp::Controller::Action::Action',
+            'Test Class'
+        );
+        is( $response->header('X-TestAppActionTestMyAction'), 'MyAction works' );
+        like(
+            $response->content,
+            qr/^bless\( .* 'Catalyst::Request' \)$/s,
+            'Content is a serialized Catalyst::Request'
+        );
+    }
 
 }
index 787bb8d..5049427 100644 (file)
@@ -3,6 +3,8 @@ package TestApp::Controller::Action::Action;
 use strict;
 use base 'TestApp::Controller::Action';
 
+__PACKAGE__->config( actions => { action_action_five => { ActionClass => '+Catalyst::Action::TestBefore' } } );
+
 sub action_action_one : Global : ActionClass('TestBefore') {
     my ( $self, $c ) = @_;
     $c->res->header( 'X-Action', $c->stash->{test} );
@@ -25,4 +27,15 @@ sub action_action_four : Global : MyAction('TestMyAction') {
     $c->forward('TestApp::View::Dump::Request');
 }
 
+sub action_action_five : Global {
+    my ( $self, $c ) = @_;
+    $c->res->header( 'X-Action', $c->stash->{test} );
+    $c->forward('TestApp::View::Dump::Request');
+}
+
+sub action_action_six : Global : ActionClass('~TestMyAction') {
+    my ( $self, $c ) = @_;
+    $c->forward('TestApp::View::Dump::Request');
+}
+
 1;