diff --git a/Changes b/Changes index 4097b58c30..226d1c2dee 100644 --- a/Changes +++ b/Changes @@ -1,5 +1,9 @@ This file documents the revision history for Perl extension Mojo. +0.991234 2009-07-03 00:00:00 + - Added prepare/finalize methods to Mojolicious. + - Updated generator scripts to play nice with MM->parse_version. + 0.991233 2009-07-01 00:00:00 - Rewrote Mojo::Client::process_local to use the new state machine. - Added Server and X-Powered-By headers. diff --git a/lib/Mojo.pm b/lib/Mojo.pm index e8cf808391..af01ef6275 100755 --- a/lib/Mojo.pm +++ b/lib/Mojo.pm @@ -18,7 +18,7 @@ __PACKAGE__->attr(home => (default => sub { Mojo::Home->new })); __PACKAGE__->attr(log => (default => sub { Mojo::Log->new })); # Oh, so they have internet on computers now! -our $VERSION = '0.991233'; +our $VERSION = '0.991234'; sub new { my $self = shift->SUPER::new(@_); diff --git a/lib/Mojo/Script/Generate/App.pm b/lib/Mojo/Script/Generate/App.pm index 8277911b59..0d4f9fdf5d 100644 --- a/lib/Mojo/Script/Generate/App.pm +++ b/lib/Mojo/Script/Generate/App.pm @@ -116,8 +116,6 @@ use warnings; use base 'Mojo'; -our $VERSION = '0.1'; - sub handler { my ($self, $tx) = @_; diff --git a/lib/Mojo/Script/Generate/Makefile.pm b/lib/Mojo/Script/Generate/Makefile.pm index 9b43420f65..47046e887d 100644 --- a/lib/Mojo/Script/Generate/Makefile.pm +++ b/lib/Mojo/Script/Generate/Makefile.pm @@ -83,9 +83,6 @@ WriteMakefile( NAME => '<%= $class %>', VERSION_FROM => 'lib/<%= $path %>', AUTHOR => 'A Good Programmer ', - ($ExtUtils::MakeMaker::VERSION >= 6.3002 - ? ('LICENSE' => 'perl') - : ()), EXE_FILES => ['bin/<%= $name %>'], PREREQ_PM => { 'Mojo' => '0.9003' }, test => {TESTS => 't/*.t t/*/*.t t/*/*/*.t'} diff --git a/lib/Mojolicious.pm b/lib/Mojolicious.pm index 8d7bedd737..5c5be94503 100644 --- a/lib/Mojolicious.pm +++ b/lib/Mojolicious.pm @@ -97,6 +97,9 @@ sub dispatch { elsif ($e) { $self->static->serve_404($c) } } +# This will run for every request after dispatch +sub finalize { } + # Bite my shiny metal ass! sub handler { my ($self, $tx) = @_; @@ -104,8 +107,17 @@ sub handler { # Start timer my $start = [Time::HiRes::gettimeofday()]; - # Build context and dispatch - $self->dispatch($self->build_ctx($tx)); + # Context + my $c = $self->build_ctx($tx); + + # Prepare + $self->prepare($c); + + # Dispatch unless we already have a response code + $self->dispatch($c) unless $c->res->code; + + # Finalize + $self->finalize($c); # End timer my $elapsed = sprintf '%f', @@ -116,6 +128,9 @@ sub handler { return $tx; } +# This will run for every request before dispatch +sub prepare { } + # This will run once at startup sub startup { } @@ -206,10 +221,18 @@ For example in production mode, C will be called. $mojo->dispatch($c); +=head2 C + + $mojo->finalize($c); + =head2 C $tx = $mojo->handler($tx); +=head2 C + + $mojo->prepare($c); + =head2 C $mojo->startup($tx); diff --git a/lib/Mojolicious/Script/Generate/App.pm b/lib/Mojolicious/Script/Generate/App.pm index df73bd891d..84e048d52f 100644 --- a/lib/Mojolicious/Script/Generate/App.pm +++ b/lib/Mojolicious/Script/Generate/App.pm @@ -140,8 +140,6 @@ use warnings; use base 'Mojolicious'; -our $VERSION = '0.1'; - # This method will run once at server start sub startup { my $self = shift; diff --git a/t/mojolicious/app.t b/t/mojolicious/app.t index 90642f7bf8..89db38f2f5 100644 --- a/t/mojolicious/app.t +++ b/t/mojolicious/app.t @@ -5,7 +5,7 @@ use strict; use warnings; -use Test::More tests => 54; +use Test::More tests => 76; use FindBin; use lib "$FindBin::Bin/lib"; @@ -28,6 +28,8 @@ $client->process_local('MojoliciousTest', $tx); is($tx->res->code, 500); is($tx->res->headers->server, 'Mojo (Perl)'); is($tx->res->headers->header('X-Powered-By'), 'Mojo (Perl)'); +is($tx->res->headers->header('X-Finalized'), 'works!'); +is($tx->res->headers->header('X-Prepared'), 'works!'); like($tx->res->body, qr/Missing right curly/); # Foo::test @@ -37,6 +39,8 @@ is($tx->res->code, 200); is($tx->res->headers->header('X-Bender'), 'Kiss my shiny metal ass!'); is($tx->res->headers->server, 'Mojo (Perl)'); is($tx->res->headers->header('X-Powered-By'), 'Mojo (Perl)'); +is($tx->res->headers->header('X-Finalized'), 'works!'); +is($tx->res->headers->header('X-Prepared'), 'works!'); like($tx->res->body, qr/\/bar\/test/); # Foo::index @@ -46,6 +50,8 @@ is($tx->res->code, 200); is($tx->res->headers->content_type, 'text/html'); is($tx->res->headers->server, 'Mojo (Perl)'); is($tx->res->headers->header('X-Powered-By'), 'Mojo (Perl)'); +is($tx->res->headers->header('X-Finalized'), 'works!'); +is($tx->res->headers->header('X-Prepared'), 'works!'); like($tx->res->body, qr/\n23Hello Mojo from the template \/foo! He/); # Foo::Bar::index @@ -55,6 +61,8 @@ is($tx->res->code, 200); is($tx->res->headers->content_type, 'text/html'); is($tx->res->headers->server, 'Mojo (Perl)'); is($tx->res->headers->header('X-Powered-By'), 'Mojo (Perl)'); +is($tx->res->headers->header('X-Finalized'), 'works!'); +is($tx->res->headers->header('X-Prepared'), 'works!'); like($tx->res->body, qr/Hello Mojo from the other template \/foo-bar!/); # Foo::templateless @@ -64,6 +72,8 @@ $client->process_local('MojoliciousTest', $tx); is($tx->res->code, 200); is($tx->res->headers->server, 'Mojo (Perl)'); is($tx->res->headers->header('X-Powered-By'), 'Mojo (Perl)'); +is($tx->res->headers->header('X-Finalized'), 'works!'); +is($tx->res->headers->header('X-Prepared'), 'works!'); like($tx->res->body, qr/Hello Mojo from a templateless renderer!/); # MojoliciousTest2::Foo::test @@ -73,6 +83,8 @@ is($tx->res->code, 200); is($tx->res->headers->header('X-Bender'), 'Kiss my shiny metal ass!'); is($tx->res->headers->server, 'Mojo (Perl)'); is($tx->res->headers->header('X-Powered-By'), 'Mojo (Perl)'); +is($tx->res->headers->header('X-Finalized'), 'works!'); +is($tx->res->headers->header('X-Prepared'), 'works!'); like($tx->res->body, qr/\/test2/); # MojoliciousTestController::index @@ -82,6 +94,8 @@ is($tx->res->code, 200); is($tx->res->headers->header('X-Bender'), 'Kiss my shiny metal ass!'); is($tx->res->headers->server, 'Mojo (Perl)'); is($tx->res->headers->header('X-Powered-By'), 'Mojo (Perl)'); +is($tx->res->headers->header('X-Finalized'), 'works!'); +is($tx->res->headers->header('X-Prepared'), 'works!'); like($tx->res->body, qr/No class works!/); # 404 @@ -90,6 +104,8 @@ $client->process_local('MojoliciousTest', $tx); is($tx->res->code, 404); is($tx->res->headers->server, 'Mojo (Perl)'); is($tx->res->headers->header('X-Powered-By'), 'Mojo (Perl)'); +is($tx->res->headers->header('X-Finalized'), 'works!'); +is($tx->res->headers->header('X-Prepared'), 'works!'); like($tx->res->body, qr/File Not Found/); # Static file /hello.txt in a production mode @@ -101,6 +117,8 @@ is($tx->res->code, 200); is($tx->res->headers->content_type, 'text/plain'); is($tx->res->headers->server, 'Mojo (Perl)'); is($tx->res->headers->header('X-Powered-By'), 'Mojo (Perl)'); +is($tx->res->headers->header('X-Finalized'), 'works!'); +is($tx->res->headers->header('X-Prepared'), 'works!'); like($tx->res->content->file->slurp, qr/Hello Mojo from a static file!/); $ENV{MOJO_MODE} = $backup; @@ -122,6 +140,8 @@ is($tx->res->headers->content_length, $stat->size, 'Content-Length is set correctly'); is($tx->res->headers->server, 'Mojo (Perl)'); is($tx->res->headers->header('X-Powered-By'), 'Mojo (Perl)'); +is($tx->res->headers->header('X-Finalized'), 'works!'); +is($tx->res->headers->header('X-Prepared'), 'works!'); like($tx->res->content->file->slurp, qr/Hello Mojo from a development static file!/); $ENV{MOJO_MODE} = $backup; @@ -134,6 +154,8 @@ $client->process_local('MojoliciousTest', $tx); is($tx->res->code, 304, 'Setting If-Modified-Since triggers 304'); is($tx->res->headers->server, 'Mojo (Perl)'); is($tx->res->headers->header('X-Powered-By'), 'Mojo (Perl)'); +is($tx->res->headers->header('X-Finalized'), 'works!'); +is($tx->res->headers->header('X-Prepared'), 'works!'); $ENV{MOJO_MODE} = $backup; # Make sure we can override attributes with constructor arguments diff --git a/t/mojolicious/lib/MojoliciousTest.pm b/t/mojolicious/lib/MojoliciousTest.pm index 05778116ea..723c3aff1a 100644 --- a/t/mojolicious/lib/MojoliciousTest.pm +++ b/t/mojolicious/lib/MojoliciousTest.pm @@ -14,6 +14,10 @@ sub development_mode { $self->static->root($self->home->rel_dir('t/mojolicious/public_dev')); } +sub finalize { $_[1]->res->headers->header('X-Finalized' => 'works!') } + +sub prepare { $_[1]->res->headers->header('X-Prepared' => 'works!') } + sub production_mode { my $self = shift;