Skip to content

Commit

Permalink
LAZILY INITIALIZE ALL THE THINGS
Browse files Browse the repository at this point in the history
  • Loading branch information
kraih committed Nov 29, 2016
1 parent 303018b commit b30fe9c
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 28 deletions.
6 changes: 3 additions & 3 deletions Changes
@@ -1,10 +1,10 @@

7.11 2016-11-29
7.11 2016-11-30
- Added EXPERIMENTAL close_idle_connections method to Mojo::Server::Daemon.
- Improved one_tick method in Mojo::IOLoop to protect from recursion, similar
to the start method.
- Improved log attribute in Mojolicious making it easier to override default
settings. (jberger)
- Improved log, renderer, routes and static attributes in Mojolicious, making
it easier to override default settings. (jberger, sri)
- Fixed bug in Mojo::Server::Prefork where workers would accept keep-alive
requests after a graceful shutdown had already been initiated.
- Fixed bugs in Mojo::Util and Mojo::Asset::File where incomplete writes would
Expand Down
56 changes: 31 additions & 25 deletions lib/Mojolicious.pm
Expand Up @@ -38,11 +38,28 @@ has log => sub {
return $mode eq 'development' ? $log : $log->level('info');
};
has mode => sub { $ENV{MOJO_MODE} || $ENV{PLACK_ENV} || 'development' };
has moniker => sub { Mojo::Util::decamelize ref shift };
has plugins => sub { Mojolicious::Plugins->new };
has renderer => sub { Mojolicious::Renderer->new };
has routes => sub { Mojolicious::Routes->new };
has secrets => sub {
has moniker => sub { Mojo::Util::decamelize ref shift };
has plugins => sub { Mojolicious::Plugins->new };
has renderer => sub {
my $renderer = Mojolicious::Renderer->new;
push @{$renderer->paths}, shift->home->rel_dir('templates');
return $renderer;
};
has routes => sub {
my $self = shift;

# Hide controller attributes/methods
my $r = Mojolicious::Routes->new;
$r->hide(qw(app continue cookie every_cookie every_param));
$r->hide(qw(every_signed_cookie finish flash helpers match on param));
$r->hide(qw(redirect_to render render_later render_maybe render_to_string));
$r->hide(qw(rendered req res respond_to send session signed_cookie stash));
$r->hide(qw(tx url_for validation write write_chunk));

# Default to controller and application namespace
return $r->namespaces(["@{[ref $self]}::Controller", ref $self]);
};
has secrets => sub {
my $self = shift;

# Warn developers about insecure default
Expand All @@ -51,8 +68,12 @@ has secrets => sub {
# Default to moniker
return [$self->moniker];
};
has sessions => sub { Mojolicious::Sessions->new };
has static => sub { Mojolicious::Static->new };
has sessions => sub { Mojolicious::Sessions->new };
has static => sub {
my $static = Mojolicious::Static->new;
push @{$static->paths}, shift->home->rel_dir('public');
return $static;
};
has types => sub { Mojolicious::Types->new };
has validator => sub { Mojolicious::Validator->new };

Expand Down Expand Up @@ -151,20 +172,6 @@ sub hook { shift->plugins->on(@_) }
sub new {
my $self = shift->SUPER::new(@_);

my $home = $self->home;
push @{$self->renderer->paths}, $home->rel_dir('templates');
push @{$self->static->paths}, $home->rel_dir('public');

# Default to controller and application namespace
my $r = $self->routes->namespaces(["@{[ref $self]}::Controller", ref $self]);

# Hide controller attributes/methods
$r->hide(qw(app continue cookie every_cookie every_param));
$r->hide(qw(every_signed_cookie finish flash helpers match on param));
$r->hide(qw(redirect_to render render_later render_maybe render_to_string));
$r->hide(qw(rendered req res respond_to send session signed_cookie stash));
$r->hide(qw(tx url_for validation write write_chunk));

$self->plugin($_)
for qw(HeaderCondition DefaultHelpers TagHelpers EPLRenderer EPRenderer);

Expand Down Expand Up @@ -649,10 +656,9 @@ requests indiscriminately, for a full list of available hooks see L</"HOOKS">.
my $app = Mojolicious->new(moniker => 'foo_bar');
my $app = Mojolicious->new({moniker => 'foo_bar'});
Construct a new L<Mojolicious> application and call L</"startup">. Will
automatically detect your home directory. Also sets up the renderer, static file
server, a default set of plugins and an L</"around_dispatch"> hook with the
default exception handling.
Construct a new L<Mojolicious> application and call L</"startup">. Also sets up
a default set of plugins and an L</"around_dispatch"> hook with the default
exception handling.
=head2 plugin
Expand Down

0 comments on commit b30fe9c

Please sign in to comment.