Skip to content

Commit

Permalink
slightly better encapsulation
Browse files Browse the repository at this point in the history
  • Loading branch information
kraih committed Oct 28, 2015
1 parent e4040bd commit 0914cab
Show file tree
Hide file tree
Showing 9 changed files with 62 additions and 47 deletions.
4 changes: 3 additions & 1 deletion Changes
@@ -1,6 +1,8 @@

6.26 2015-10-27
6.26 2015-10-28
- Renamed built-in templates with more descriptive names.
- Added warmup method to Mojolicious::Renderer.
- Added warmup method to Mojolicious::Static.

6.25 2015-10-21
- Deprecated Mojo::Message::Request::proxy with boolean and string arguments
Expand Down
2 changes: 1 addition & 1 deletion lib/Mojo/Message.pm
Expand Up @@ -390,7 +390,7 @@ C<MOJO_MAX_LINE_SIZE> environment variable or C<8192> (8KB).
Maximum message size in bytes, defaults to the value of the
C<MOJO_MAX_MESSAGE_SIZE> environment variable or C<16777216> (16MB). Setting
the value to C<0> will allow messages of indefinite size. Note that increasing
this value can also drastically increase memory usage, should you for example
this value can also drastically increase memory usage, should you for example,
attempt to parse an excessively large message body with the L</"body_params">,
L</"dom"> or L</"json"> methods.
Expand Down
2 changes: 1 addition & 1 deletion lib/Mojo/Template.pm
Expand Up @@ -350,7 +350,7 @@ automatically enabled.
%% Replaced with "%", useful for generating templates
Escaping behavior can be reversed with the L</"auto_escape"> attribute, this is
the default in L<Mojolicious> C<.ep> templates for example.
the default in L<Mojolicious> C<.ep> templates, for example.
<%= Perl expression, replaced with XML escaped result %>
<%== Perl expression, replaced with result %>
Expand Down
6 changes: 3 additions & 3 deletions lib/Mojolicious.pm
Expand Up @@ -183,7 +183,7 @@ sub plugin {

sub start {
my $self = shift;
$_->_warmup for $self->static, $self->renderer;
$_->warmup for $self->static, $self->renderer;
return $self->commands->run(@_ ? @_ : @ARGV);
}

Expand Down Expand Up @@ -264,8 +264,8 @@ in the chain and a call to L</"dispatch"> the last, yours will be in between.
...
});
This is a very powerful hook and should not be used lightly, it allows you for
example to customize application-wide exception handling, consider it the
This is a very powerful hook and should not be used lightly, it allows you to,
for example, customize application-wide exception handling, consider it the
sledgehammer in your toolbox. (Passed a callback leading to the next hook and
the default controller object)
Expand Down
6 changes: 3 additions & 3 deletions lib/Mojolicious/Guides/Cookbook.pod
Expand Up @@ -453,7 +453,7 @@ nested closures that often result from continuation-passing style.
=head2 Timers

Timers, another primary feature of the event loop, are created with
L<Mojo::IOLoop/"timer"> and can for example be used to delay rendering of a
L<Mojo::IOLoop/"timer"> and can, for example, be used to delay rendering of a
response, and unlike C<sleep>, won't block any other requests that might be
processed concurrently.

Expand Down Expand Up @@ -789,7 +789,7 @@ can be combined to solve some of hardest problems in web development.
=head2 Event loops

Internally the L<Mojo::IOLoop> event loop can use multiple reactor backends,
L<EV> for example will be automatically used if possible. Which in turn allows
L<EV> for example, will be automatically used if possible. Which in turn allows
other event loops like L<AnyEvent> to just work.

use Mojolicious::Lite;
Expand Down Expand Up @@ -823,7 +823,7 @@ Who actually controls the event loop backend is not important.
});
say $cv->recv;

You could for example just embed the built-in web server into an L<AnyEvent>
You could, for example, just embed the built-in web server into an L<AnyEvent>
application.

use Mojolicious::Lite;
Expand Down
4 changes: 2 additions & 2 deletions lib/Mojolicious/Guides/Rendering.pod
Expand Up @@ -214,7 +214,7 @@ alternatives.
=head2 Rendering to strings

Sometimes you might want to use the rendered result directly instead of
generating a response, for example to send emails, this can be done with
generating a response, for example, to send emails, this can be done with
L<Mojolicious::Controller/"render_to_string">.

my $html = $c->render_to_string('mail');
Expand Down Expand Up @@ -679,7 +679,7 @@ action could do.
@@ index.html.ep
% debug 'Hello from a template!';

Helpers can also accept template blocks as last argument, this for example
Helpers can also accept template blocks as last argument, this for example,
allows very pleasant to use tag helpers and filters. Wrapping the helper result
into a L<Mojo::ByteStream> object can prevent accidental double escaping.

Expand Down
6 changes: 3 additions & 3 deletions lib/Mojolicious/Guides/Routing.pod
Expand Up @@ -550,7 +550,7 @@ authentication.
$auth->get('/blackjack')->to('hideout#blackjack');

Broken dispatch chains can be continued by calling the method
L<Mojolicious::Controller/"continue">, this allows for example non-blocking
L<Mojolicious::Controller/"continue">, this allows for example, non-blocking
operations to finish before reaching the next dispatch cycle.

my $maybe = $r->under('/maybe' => sub {
Expand Down Expand Up @@ -588,7 +588,7 @@ automatically detected and stored in the stash value C<format>.
# /foo.txt -> {controller => 'foo', action => 'bar', format => 'txt'}
$r->get('/foo')->to('foo#bar');

This for example allows multiple templates in different formats to share the
This for example, allows multiple templates in different formats to share the
same action code. Restrictive placeholders can also be used to limit the
allowed formats.

Expand Down Expand Up @@ -950,7 +950,7 @@ applications under a domain and/or prefix.
=head2 Embed applications

For a litte more power you can also embed applications by using them instead of
a controller. This allows for example the use of the L<Mojolicious::Lite>
a controller. This allows for example, the use of the L<Mojolicious::Lite>
domain specific language in normal L<Mojolicious> controllers.

# Controller
Expand Down
49 changes: 28 additions & 21 deletions lib/Mojolicious/Renderer.pm
Expand Up @@ -178,7 +178,7 @@ sub template_name {
return undef unless my $format = $options->{format};
$template .= ".$format";

$self->_warmup unless $self->{templates};
$self->warmup unless $self->{templates};

# Variants
my $handler = $options->{handler};
Expand Down Expand Up @@ -207,6 +207,23 @@ sub template_path {
return undef;
}

sub warmup {
my $self = shift;

my ($index, $templates) = @$self{qw(index templates)} = ({}, {});

# Handlers for templates
s/\.(\w+)$// and push @{$templates->{$_}}, $1
for map { sort @{Mojo::Home->new($_)->list_files} } @{$self->paths},
$TEMPLATES;

# Handlers and classes for DATA templates
for my $class (reverse @{$self->classes}) {
$index->{$_} = $class for my @keys = sort keys %{data_section $class};
s/\.(\w+)$// and unshift @{$templates->{$_}}, $1 for reverse @keys;
}
}

sub _extends {
my ($self, $stash) = @_;
my $layout = delete $stash->{layout};
Expand All @@ -229,23 +246,6 @@ sub _render_template {
return undef;
}

sub _warmup {
my $self = shift;

my ($index, $templates) = @$self{qw(index templates)} = ({}, {});

# Handlers for templates
s/\.(\w+)$// and push @{$templates->{$_}}, $1
for map { sort @{Mojo::Home->new($_)->list_files} } @{$self->paths},
$TEMPLATES;

# Handlers and classes for DATA templates
for my $class (reverse @{$self->classes}) {
$index->{$_} = $class for my @keys = sort keys %{data_section $class};
s/\.(\w+)$// and unshift @{$templates->{$_}}, $1 for reverse @keys;
}
}

1;

=encoding utf8
Expand Down Expand Up @@ -286,9 +286,10 @@ Renderer cache, defaults to a L<Mojo::Cache> object.
Classes to use for finding templates in C<DATA> sections with L<Mojo::Loader>,
first one has the highest precedence, defaults to C<main>. Only files with
exactly two extensions will be used, like C<index.html.ep>. Note that these
classes need to have already been loaded and added during application startup
for templates to be detected.
exactly two extensions will be used, like C<index.html.ep>. Note that for
templates to be detected, these classes need to have already been loaded and
added before L</"warmup"> is called, which usually happens during application
startup.
# Add another class with templates in DATA section
push @{$renderer->classes}, 'Mojolicious::Plugin::Fun';
Expand Down Expand Up @@ -448,6 +449,12 @@ C<variant> and C<handler> values, usually used by handlers.
Build a full template path for an options hash reference with C<template>,
C<format>, C<variant> and C<handler> values, usually used by handlers.
=head2 warmup
$renderer->warmup;
Prepare templates from L</"classes"> for future use.
=head1 SEE ALSO
L<Mojolicious>, L<Mojolicious::Guides>, L<http://mojolicio.us>.
Expand Down
30 changes: 18 additions & 12 deletions lib/Mojolicious/Static.pm
Expand Up @@ -113,6 +113,14 @@ sub serve_asset {
return $res->content->asset($asset->start_range($start)->end_range($end));
}

sub warmup {
my $self = shift;
my $index = $self->{index} = {};
for my $class (reverse @{$self->classes}) {
$index->{$_} = $class for keys %{data_section $class};
}
}

sub _epoch { Mojo::Date->new(shift)->epoch }

sub _get_data_file {
Expand All @@ -121,7 +129,7 @@ sub _get_data_file {
# Protect files without extensions and templates with two extensions
return undef if $rel !~ /\.\w+$/ || $rel =~ /\.\w+\.\w+$/;

$self->_warmup unless $self->{index};
$self->warmup unless $self->{index};

# Find file
return undef
Expand All @@ -135,14 +143,6 @@ sub _get_file {
return -f $path && -r _ ? Mojo::Asset::File->new(path => $path) : undef;
}

sub _warmup {
my $self = shift;
my $index = $self->{index} = {};
for my $class (reverse @{$self->classes}) {
$index->{$_} = $class for keys %{data_section $class};
}
}

1;

=encoding utf8
Expand Down Expand Up @@ -177,9 +177,9 @@ L<Mojolicious::Static> implements the following attributes.
Classes to use for finding files in C<DATA> sections with L<Mojo::Loader>,
first one has the highest precedence, defaults to C<main>. Only files with
exactly one extension will be used, like C<index.html>. Note that these classes
need to have already been loaded and added during application startup for files
to be detected.
exactly one extension will be used, like C<index.html>. Note that for files to
be detected, these classes need to have already been loaded and added before
L</"warmup"> is called, which usually happens during application startup.
# Add another class with static files in DATA section
push @{$static->classes}, 'Mojolicious::Plugin::Fun';
Expand Down Expand Up @@ -264,6 +264,12 @@ this method does not protect from traversing to parent directories.
Serve a L<Mojo::Asset::File> or L<Mojo::Asset::Memory> object with C<Range>,
C<If-Modified-Since> and C<If-None-Match> support.
=head2 warmup
$static->warmup;
Prepare static files from L</"classes"> for future use.
=head1 SEE ALSO
L<Mojolicious>, L<Mojolicious::Guides>, L<http://mojolicio.us>.
Expand Down

0 comments on commit 0914cab

Please sign in to comment.