Skip to content

Commit

Permalink
Add 'bundled' attribute to Mojolicious::Static to permit examining, o…
Browse files Browse the repository at this point in the history
…verriding, or disabling any or all bundled files. Resolves #1094
  • Loading branch information
lindleyw committed Jul 25, 2017
1 parent 051aeab commit 2305883
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 6 deletions.
24 changes: 18 additions & 6 deletions lib/Mojolicious/Static.pm
Expand Up @@ -11,10 +11,21 @@ use Mojo::Util 'md5_sum';

has classes => sub { ['main'] };
has paths => sub { [] };

# Bundled files
my $PUBLIC = Mojo::Home->new(Mojo::Home->new->mojo_lib_dir)
->child('Mojolicious', 'resources', 'public');
has bundled => sub {

# Bundled files
my $public = Mojo::Home->new(Mojo::Home->new->mojo_lib_dir)
->child('Mojolicious', 'resources', 'public');
return {
@{
$public->list_tree->map(
sub {
$_->to_rel($public)->to_string, $_->realpath;
}
)
}
};
};

sub dispatch {
my ($self, $c) = @_;
Expand Down Expand Up @@ -48,8 +59,9 @@ sub file {
# Search DATA
if (my $asset = $self->_get_data_file($rel)) { return $asset }

# Search bundled files
return $self->_get_file(path($PUBLIC, split('/', $rel))->to_string);
# Search bundled or special files
return undef unless exists $self->bundled->{$rel};
return $self->_get_file($self->bundled->{$rel});
}

sub is_fresh {
Expand Down
3 changes: 3 additions & 0 deletions t/mojolicious/production_app.t
Expand Up @@ -33,6 +33,9 @@ is $t->app, $t->app->commands->app, 'applications are equal';
is $t->app->static->file('hello.txt')->slurp,
"Hello Mojo from a static file!\n", 'right content';
is $t->app->static->file('does_not_exist.html'), undef, 'no file';
isnt $t->app->static->file('mojo/jquery/jquery.js'), undef, 'find built-in jQuery';
delete $t->app->static->bundled->{'mojo/jquery/jquery.js'};
is $t->app->static->file('mojo/jquery/jquery.js'), undef, 'suppress built-in jQuery';
is $t->app->moniker, 'mojolicious_test', 'right moniker';

# Default namespaces
Expand Down

0 comments on commit 2305883

Please sign in to comment.