Permalink
Browse files

implemented forward, detach, visit, go

  • Loading branch information...
1 parent 657880f commit ebcddeee222b06ab44187ac8c45eaa50cb924e86 @dann committed Jan 17, 2009
@@ -1,5 +1,5 @@
package Angelos::View::HTPro;
-use Mouse;
+use Angelos::Class;
use Angelos::Home;
use HTML::Template::Pro;
use Path::Class;
@@ -8,8 +8,6 @@ extends 'Angelos::View';
has 'TEMPLATE_EXTENSION' => ( +default => '.tmpl' );
has 'CONTENT_TYPE' => ( +default => 'text/html' );
-no Mouse;
-
sub _build_engine {
}
@@ -24,6 +22,4 @@ sub _render {
$out;
}
-__PACKAGE__->meta->make_immutable;
-
-1;
+__END_OF_CLASS__
View
@@ -43,6 +43,7 @@ requires 'Exception::Class';
requires 'File::Spec';
requires 'IO::Pager';
requires 'Class::Singleton';
+requires 'Params::Validate';
# components
requires 'Template';
@@ -68,7 +69,6 @@ requires 'File::HomeDir';
requires 'Devel::EvalContext';
# debug
-requires 'Devel::MemUsed';
requires 'Text::SimpleTable';
requires 'File::Find::Rule';
@@ -1,5 +1,5 @@
package Angelos::Console::Plugin::History;
-use Mouse::Role;
+use Angelos::Plugin;
use File::Slurp;
use Path::Class;
use File::HomeDir;
@@ -1,6 +1,8 @@
package Angelos::Context;
use Angelos::Class;
-use Carp ();
+use Angelos::Exceptions;
+use Angelos::Utils;
+use Params::Validate qw(SCALAR HASHREF);
with 'Angelos::Class::Pluggable';
@@ -45,8 +47,6 @@ has 'session' => (
isa => 'HTTP::Session',
);
-no Mouse;
-
sub req {
shift->request;
}
@@ -58,7 +58,10 @@ sub res {
sub view {
my ( $self, $view ) = @_;
my $v = $self->app->view($view);
- Carp::croak "view $view doesn't exist" unless $v;
+ Angelos::Exception::ComponentNotFound->throw(
+ level => 'error',
+ message => "view $view doesn't exist"
+ ) unless $v;
$v->context($self);
$v;
}
@@ -68,7 +71,8 @@ sub action {
$self->_match->params->{action};
}
-# TODO: should I extend HTTP::Engine::Response?
+# redirect_to( action => $action) ?
+# redirect_to( url => $url) ?
sub redirect {
my ( $self, $location, $status ) = @_;
unless ( $self->finished ) {
@@ -79,6 +83,58 @@ sub redirect {
}
}
+# forward(action=> $action)
+# forward(controller=> $controller, action => 'index', params=> aaa)
+sub forward {
+ my $self = shift;
+ my %forward_to = Params::Validate::validate(
+ @_,
+ { action => 1, # required
+ controller => { type => SCALAR },
+ #params => { type => HASHREF },
+ }
+ );
+ my $controller ||= $forward_to{controller};
+ $controller ||= caller;
+ my $short_controller_name
+ = Angelos::Utils::class2classsuffix($controller);
+ my $controller_instance = $self->app->controller($short_controller_name);
+ my $action = $forward_to{action};
+ my $params = $forward_to{params};
+ $controller_instance->$action( $self, $self->request, $params );
+}
+
+sub forward_with_filters {
+ my $self = shift;
+ my %forward_to = Params::Validate::validate(
+ @_,
+ { action => 1, # required
+ controller => { type => SCALAR },
+ #params => { type => HASHREF },
+ }
+ );
+ my $controller ||= $forward_to{controller};
+ $controller ||= caller;
+ my $short_controller_name
+ = Angelos::Utils::class2classsuffix($controller);
+ my $controller_instance = $self->app->controller($short_controller_name);
+ my $action = $forward_to{action};
+ my $params = $forward_to{params};
+ $controller_instance->_do_action( $self, $action, $params );
+}
+
+sub detach {
+ my ( $self, %forward_to ) = @_;
+ $self->forward(%forward_to);
+ Angelos::Exception::Detach->throw( message => 'detached' );
+}
+
+sub detach_with_filter {
+ my ( $self, %forward_to ) = @_;
+ $self->forward_with_filters(%forward_to);
+ Angelos::Exception::Detach->throw( message => 'detached' );
+}
+
__END_OF_CLASS__
__END__
@@ -2,6 +2,7 @@ package Angelos::Controller;
use Angelos::Class;
use Carp ();
use Angelos::Exceptions;
+use Exception::Class;
with( 'Angelos::Component', );
@@ -25,7 +26,7 @@ has 'after_filters' => (
}
);
-sub SETUP {}
+sub SETUP { }
sub _call_filters {
my ( $self, $filters, $context, $action, $params ) = @_;
@@ -43,30 +44,43 @@ sub _call_filters {
sub add_before_filter {
my ( $self, $filter ) = @_;
- Angelos::Exception::InvalidArgumentError->throw(message => "name key is required")
+ Angelos::Exception::InvalidArgumentError->throw(
+ message => "name key is required" )
unless $filter->{name};
push @{ $self->before_filters }, $filter;
}
sub add_after_filter {
my ( $self, $filter ) = @_;
- Angelos::Exception::InvalidArgumentError->throw(message => "name key is required")
+ Angelos::Exception::InvalidArgumentError->throw(
+ message => "name key is required" )
unless $filter->{name};
push @{ $self->after_filters }, $filter;
}
sub _do_action {
my ( $self, $context, $action, $params ) = @_;
- return if $context->finished; # already redirected
+ return if $context->finished; # already redirected
$self->_call_filters( $self->before_filters, $context, $action, $params );
- $self->ACTION($context, $action, $params);
+
+ eval { $self->ACTION( $context, $action, $params ); };
+ my $e;
+ if ( $e = Exception::Class->caught('Angelos::Exception::Detach') ) {
+ $self->log->( level => 'info', message => "Detached" );
+ }
+ else {
+ $e = Exception::Class->caught();
+ $self->log->( level => 'error', message => $e );
+ $e->rethrow;
+ }
+
$self->_call_filters( $self->after_filters, $context, $action, $params );
}
sub ACTION {
- my ($self, $context, $action, $params) = @_;
+ my ( $self, $context, $action, $params ) = @_;
$self->$action( $context, $params );
}
@@ -1,15 +0,0 @@
-package Angelos::Debug::MemoryUsage;
-use Mouse::Role;
-
-use Devel::MemUsed;
-our $memused = Devel::MemUsed->new;
-
-sub reset {
- $memused->reset;
-}
-
-sub show {
- print $memused . "\n";
-}
-
-1;
@@ -8,6 +8,7 @@ use Angelos::Context;
use Angelos::Component::Loader;
use Angelos::Middleware::Builder;
use Angelos::Exceptions;
+use Exception::Class;
extends 'Angelos::Engine::Base';
with 'Angelos::Class::Pluggable';
@@ -32,8 +33,6 @@ has 'component_loader' => (
}
);
-no Mouse;
-
sub build_request_handler {
my $self = shift;
my $request_handler = eval {
@@ -58,7 +57,7 @@ sub handle_request {
);
eval { $self->DISPATCH( $c, $req ); };
- if ( my $e = $@ ) {
+ if ( my $e = Exception::Class->caught() ) {
$self->HANDLE_EXCEPTION( $c, $e );
}
return $c->res;
@@ -59,6 +59,12 @@ BEGIN {
description => 'Component (MVC) not found'
},
+ 'Angelos::Exception::Detach' => {
+ isa => 'Angelos::Exception',
+ alias => 'detach',
+ description => 'detach'
+ },
+
);
}
@@ -4,20 +4,25 @@ use warnings;
use Log::Dispatch::Config;
use Log::Dispatch::Configurator::YAML;
use Angelos::Config;
+use base 'Class::Singleton';
-our $LOGGER;
-
-sub instance {
- my $class = shift;
- return $LOGGER if $LOGGER;
- my $config = Log::Dispatch::Configurator::YAML->new($class->_logger_conf_path);
+sub _new_instance {
+ my $class = shift;
+ my $self = bless {}, $class;
+ my $config
+ = Log::Dispatch::Configurator::YAML->new( $self->_logger_conf_path );
Log::Dispatch::Config->configure($config);
- $LOGGER = Log::Dispatch::Config->instance();
- $LOGGER;
+ $self->{logger} = Log::Dispatch::Config->instance();
+ return $self;
+}
+
+sub log {
+ my ( $self, %log ) = @_;
+ $log{level} ||= 'debug';
+ $self->{logger}->log(%log);
}
sub _logger_conf_path {
- my $class = shift;
Angelos::Config->logger_conf_path;
}
@@ -2,6 +2,7 @@
sub {
$_->match('/')->to( { controller => 'Root', action => 'index' } );
$_->match('/japanese')->to( { controller => 'Root', action => 'japanese' } );
+ $_->match('/forward')->to( { controller => 'Root', action => 'forward' } );
# $_->match('/middlewareunicode')
# ->to( { controller => 'MiddlewareUnicode', action => 'index' } );
# $_->resources('Book');
@@ -4,8 +4,7 @@ extends 'Angelos::Controller';
sub index {
my ($self, $c) = @_;
- warn 'coooooooooooool';
- $self->dump($c);
+ $self->log(level=>'info', message=> 'index method in Books controller');
$c->res->body('Hello world in Book controller');
$c->res->code(200);
}
@@ -1,5 +1,5 @@
package TestApp::Web::Controller::Root;
-use Mouse;
+use Angelos::Class;
use Angelos::Utils;
extends 'Angelos::Controller';
@@ -25,6 +25,14 @@ sub japanese {
warn $self->loc('Hello') . "\n";
}
-__PACKAGE__->meta->make_immutable;
+sub forward {
+ my ($self, $c, $params) = @_;
+ $c->forward(controller => 'Books', action => 'index');
+}
+
+sub detach {
+ my ($self, $c, $params) = @_;
+ $c->detach(controller => 'Books', action => 'index');
+}
-1;
+__END_OF_CLASS__
@@ -17,6 +17,10 @@
my $port = shift;
my $ua = LWP::UserAgent->new;
$ua->get("http://localhost:$port/");
+
+ $ua->get("http://localhost:$port/forward");
+
+ $ua->get("http://localhost:$port/detach");
},
server => sub {
my $port = shift;
@@ -1,5 +1,5 @@
package Angelos::JobQueue::Gearman::Client;
-use Mouse;
+use Angelos::Class;
use Gearman::Client;
use Storable qw( freeze );
@@ -17,8 +17,6 @@ has 'client' => (
}
);
-no Mouse;
-
sub BUILD {
my $self = shift;
$self->setup;
@@ -56,7 +54,6 @@ sub add_task {
$self->client->dispatch_background($gtask);
}
-__PACKAGE__->meta->make_immutable;
-
-1;
+__END_OF_CLASS__
+__END__
@@ -1,5 +1,5 @@
package Angelos::JobQueue::Gearman::Task;
-use Mouse;
+use Angelos::Class;
use String::CamelCase qw(decamelize);
use Angelos::Exceptions;
@@ -44,4 +44,4 @@ sub on_complete {
sub on_retry {
}
-1;
+__END_OF_CLASS__
Oops, something went wrong.

0 comments on commit ebcddee

Please sign in to comment.