Permalink
Browse files

added build_controller method to Mojolicious

  • Loading branch information...
1 parent 418faa6 commit 1f94c39a2bd1ab273a9e80cf5ab93549b992f9c4 @kraih committed Mar 8, 2014
Showing with 54 additions and 25 deletions.
  1. +1 −0 Changes
  2. +33 −16 lib/Mojolicious.pm
  3. +2 −1 lib/Mojolicious/Routes.pm
  4. +2 −1 t/mojolicious/app.t
  5. +1 −1 t/mojolicious/layouted_lite_app.t
  6. +15 −6 t/mojolicious/lite_app.t
View
@@ -1,5 +1,6 @@
4.88 2014-03-08
+ - Added build_controller method to Mojolicious.
- Improved Mojo::Server::Daemon to handle setuid/setgid errors more
gracefully.
- Improved Mojo::Server::Prefork to handle lock file errors more gracefully.
View
@@ -55,11 +55,29 @@ sub AUTOLOAD {
# Call helper with fresh controller
croak qq{Can't locate object method "$method" via package "$package"}
unless my $helper = $self->renderer->helpers->{$method};
- return $self->controller_class->new(app => $self)->$helper(@_);
+ return $self->build_controller->$helper(@_);
}
sub DESTROY { }
+sub build_controller {
+ my ($self, $tx) = @_;
+ $tx ||= $self->build_tx;
+
+ # Embedded application
+ my $stash = {};
+ if (my $sub = $tx->can('stash')) { ($stash, $tx) = ($tx->$sub, $tx->tx) }
+ $stash->{'mojo.secrets'} //= $self->secrets;
+
+ # Build default controller
+ %$stash = (%$stash, %{$self->defaults});
+ my $c
+ = $self->controller_class->new(app => $self, stash => $stash, tx => $tx);
+ weaken $c->{app};
+
+ return $c;
+}
+
sub build_tx {
my $self = shift;
my $tx = Mojo::Transaction::HTTP->new;
@@ -102,18 +120,7 @@ sub dispatch {
}
sub handler {
- my ($self, $tx) = @_;
-
- # Embedded application
- my $stash = {};
- if (my $sub = $tx->can('stash')) { ($stash, $tx) = ($tx->$sub, $tx->tx) }
- $stash->{'mojo.secrets'} //= $self->secrets;
-
- # Build default controller
- %$stash = (%$stash, %{$self->defaults});
- my $c
- = $self->controller_class->new(app => $self, stash => $stash, tx => $tx);
- weaken $c->{$_} for qw(app tx);
+ my $self = shift;
# Dispatcher has to be last in the chain
++$self->{dispatch}
@@ -122,11 +129,13 @@ sub handler {
unless $self->{dispatch};
# Process with chain
+ my $c = $self->build_controller(@_);
+ weaken $c->{tx};
$self->plugins->emit_chain(around_dispatch => $c);
# Delayed response
$self->log->debug('Nothing has been rendered, expecting delayed response.')
- unless $tx->is_writing;
+ unless $c->tx->is_writing;
}
sub helper {
@@ -528,12 +537,20 @@ Validate parameters, defaults to a L<Mojolicious::Validator> object.
L<Mojolicious> inherits all methods from L<Mojo> and implements the following
new ones.
+=head2 build_controller
+
+ my $c = $app->build_controller;
+ my $c = $app->build_controller(Mojo::Transaction::HTTP->new);
+ my $c = $app->build_controller(Mojolicious::Controller->new);
+
+Build default controller object with L</"defaults">, which is usually a
+L<Mojolicious::Controller> object.
+
=head2 build_tx
my $tx = $app->build_tx;
-Transaction builder, defaults to building a L<Mojo::Transaction::HTTP>
-object.
+Build L<Mojo::Transaction::HTTP> object and emit L</"after_build_tx"> hook.
=head2 defaults
@@ -333,7 +333,8 @@ Continue dispatch chain.
my $bool = $r->dispatch(Mojolicious::Controller->new);
-Match routes with L<Mojolicious::Routes::Match> and dispatch.
+Match routes with L<Mojolicious::Routes::Match> and dispatch, emits the hook
+L<Mojolicious/"around_action"> for every action.
=head2 hide
View
@@ -62,6 +62,7 @@ is $t->app->moniker, 'mojolicious_test', 'right moniker';
is $t->app->secrets->[0], $t->app->moniker, 'secret defaults to moniker';
is $t->app->renderer->template_handler(
{template => 'foo/bar/index', format => 'html'}), 'epl', 'right handler';
+is $t->app->build_controller->req->url, '', 'no URL';
# Missing methods and functions (AUTOLOAD)
eval { $t->app->missing };
@@ -70,7 +71,7 @@ like $@,
'right error';
eval { Mojolicious::missing() };
like $@, qr/^Undefined subroutine &Mojolicious::missing called/, 'right error';
-my $c = $t->app->controller_class->new;
+my $c = $t->app->build_controller;
eval { $c->missing };
like $@, qr/^Can't locate object method "missing" via package "@{[ref $c]}"/,
'right error';
@@ -109,7 +109,7 @@ get '/data' => {data => 0};
my $t = Test::Mojo->new;
# "0" content reassignment
-my $c = $t->app->controller_class->new;
+my $c = $t->app->build_controller;
$c->content(foo => '0');
is $c->content('foo'), '0', 'right content';
$c->content(foo => '1');
View
@@ -21,6 +21,13 @@ is $@, qq{Plugin "does_not_exist" missing, maybe you need to install it?\n},
# Default
app->defaults(default => 23);
+# Secret
+my $log = '';
+my $cb = app->log->on(message => sub { $log .= pop });
+is app->secrets->[0], app->moniker, 'secret defaults to moniker';
+like $log, qr/Your secret passphrase needs to be changed!!!/, 'right message';
+app->log->unsubscribe(message => $cb);
+
# Test helpers
helper test_helper => sub { shift->param(@_) };
helper test_helper2 => sub { shift->app->controller_class };
@@ -447,13 +454,15 @@ my $t = Test::Mojo->new;
# Application is already available
is $t->app->test_helper2, 'Mojolicious::Controller', 'right class';
-is $t->app, app->commands->app, 'applications are equal';
is $t->app->moniker, 'lite_app', 'right moniker';
-my $log = '';
-my $cb = $t->app->log->on(message => sub { $log .= pop });
-is $t->app->secrets->[0], $t->app->moniker, 'secret defaults to moniker';
-like $log, qr/Your secret passphrase needs to be changed!!!/, 'right message';
-$t->app->log->unsubscribe(message => $cb);
+is $t->app, app->build_controller->app->commands->app,
+ 'applications are equal';
+is $t->app->build_controller->req->url, '', 'no URL';
+is $t->app->build_controller->stash->{default}, 23, 'right value';
+is $t->app->build_controller($t->app->ua->build_tx(GET => '/foo'))->req->url,
+ '/foo', 'right URL';
+is $t->app->build_controller->render('index', handler => 'epl', partial => 1),
+ 'Just works!', 'right result';
# Unicode snowman
$t->get_ok('/☃')->status_is(200)

0 comments on commit 1f94c39

Please sign in to comment.