Skip to content

Commit

Permalink
added moniker attribute to Mojolicious
Browse files Browse the repository at this point in the history
  • Loading branch information
kraih committed Jan 6, 2013
1 parent 6d6f87e commit 17f4b7a
Show file tree
Hide file tree
Showing 9 changed files with 25 additions and 19 deletions.
1 change: 1 addition & 0 deletions Changes
Expand Up @@ -2,6 +2,7 @@
3.73 2013-01-06
- Deprecated Mojolicious::Commands->start in favor of
Mojolicious::Commands->start_app.
- Added moniker attribute to Mojolicious.
- Added after_render hook.
- Improved name detection in Mojolicious::Plugin::Config.
- Improved documentation.
Expand Down
1 change: 1 addition & 0 deletions lib/Mojo/Server.pm
Expand Up @@ -17,6 +17,7 @@ sub new {

sub build_app {
my ($self, $app) = @_;
local $ENV{MOJO_EXE};
return $app->new unless my $e = Mojo::Loader->new->load($app);
die ref $e ? $e : qq{Couldn't find application class "$app".\n};
}
Expand Down
11 changes: 11 additions & 0 deletions lib/Mojolicious.pm
Expand Up @@ -4,6 +4,7 @@ use Mojo::Base 'Mojo';
# "Fry: Shut up and take my money!"
use Carp 'croak';
use Mojo::Exception;
use Mojo::Util 'decamelize';
use Mojolicious::Commands;
use Mojolicious::Controller;
use Mojolicious::Plugins;
Expand All @@ -21,6 +22,7 @@ has commands => sub {
};
has controller_class => 'Mojolicious::Controller';
has mode => sub { $ENV{MOJO_MODE} || 'development' };
has moniker => sub { decamelize ref shift };
has plugins => sub { Mojolicious::Plugins->new };
has renderer => sub { Mojolicious::Renderer->new };
has routes => sub { Mojolicious::Routes->new };
Expand Down Expand Up @@ -272,6 +274,15 @@ Right before calling C<startup> and mode specific methods, L<Mojolicious>
will pick up the current mode, name the log file after it and raise the log
level from C<debug> to C<info> if it has a value other than C<development>.
=head2 C<moniker>
my $moniker = $app->moniker;
$app = $app->moniker('foo_bar');
Moniker of this application, often used as default filename for configuration
files and the like, defaults to decamelizing the application class with
L<Mojo::Util/"decamelize">.
=head2 C<plugins>
my $plugins = $app->plugins;
Expand Down
7 changes: 6 additions & 1 deletion lib/Mojolicious/Lite.pm
Expand Up @@ -2,7 +2,7 @@ package Mojolicious::Lite;
use Mojo::Base 'Mojolicious';

# "Bender: Bite my shiny metal ass!"
use File::Basename 'dirname';
use File::Basename qw(basename dirname);
use File::Spec::Functions 'catdir';
use Mojo::UserAgent;
use Mojo::Util 'monkey_patch';
Expand All @@ -22,6 +22,11 @@ sub import {
push @{"${caller}::ISA"}, 'Mojo';
my $app = shift->new;

# Moniker
my $moniker = basename $ENV{MOJO_EXE};
$moniker =~ s/\.(?:pl|t)$//i;
$app->moniker($moniker);

# Initialize routes
my $routes = $app->routes->namespaces([]);

Expand Down
18 changes: 2 additions & 16 deletions lib/Mojolicious/Plugin/Config.pm
@@ -1,9 +1,7 @@
package Mojolicious::Plugin::Config;
use Mojo::Base 'Mojolicious::Plugin';

use File::Basename 'basename';
use File::Spec::Functions 'file_name_is_absolute';
use Mojo::Util 'decamelize';

sub load {
my ($self, $file, $conf, $app) = @_;
Expand Down Expand Up @@ -36,18 +34,7 @@ sub register {

# Config file
my $file = $conf->{file} || $ENV{MOJO_CONFIG};
unless ($file) {

# Executable
if ($app->isa('Mojolicious::Lite')) { $file = basename $ENV{MOJO_EXE} }

# Class
else { $file = decamelize ref $app }

# Replace ".pl" and ".t" with default extension
$file =~ s/\.(?:pl|t)$//i;
$file .= '.' . ($conf->{ext} || 'conf');
}
$file ||= $app->moniker . '.' . ($conf->{ext} || 'conf');

# Mode specific config file
my $mode = $file =~ /^(.*)\.([^.]+)$/ ? join('.', $1, $app->mode, $2) : '';
Expand Down Expand Up @@ -113,8 +100,7 @@ The application object can be accessed via C<$app> or the C<app> function,
L<strict>, L<warnings>, L<utf8> and Perl 5.10 features are automatically
enabled. You can extend the normal configuration file C<myapp.conf> with
C<mode> specific ones like C<myapp.$mode.conf>. A default configuration
filename will be generated by decamelizing the application class with
L<Mojo::Util/"decamelize"> or from the application filename.
filename will be generated from the value of L<Mojolicious/"moniker">.
The code of this plugin is a good example for learning to build new plugins,
you're welcome to fork it.
Expand Down
3 changes: 1 addition & 2 deletions lib/Mojolicious/Plugin/JSONConfig.pm
Expand Up @@ -73,8 +73,7 @@ preprocesses its input with L<Mojo::Template>.
The application object can be accessed via C<$app> or the C<app> function. You
can extend the normal config file C<myapp.json> with C<mode> specific ones
like C<myapp.$mode.json>. A default configuration filename will be generated
by decamelizing the application class with L<Mojo::Util/"decamelize"> or from
the application filename.
from the value of L<Mojolicious/"moniker">.
The code of this plugin is a good example for learning to build new plugins,
you're welcome to fork it.
Expand Down
1 change: 1 addition & 0 deletions t/mojolicious/app.t
Expand Up @@ -46,6 +46,7 @@ is_deeply $t->app->commands->namespaces,
is $t->app, $t->app->commands->app, 'applications are equal';
is $t->app->static->file('hello.txt')->slurp,
"Hello Mojo from a development static file!\n", 'right content';
is $t->app->moniker, 'mojolicious_test', 'right moniker';

# Plugin::Test::SomePlugin2::register (security violation)
$t->get_ok('/plugin-test-some_plugin2/register')->status_isnt(500)
Expand Down
1 change: 1 addition & 0 deletions t/mojolicious/lite_app.t
Expand Up @@ -523,6 +523,7 @@ 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';

# GET /☃
$t->get_ok('/☃')->status_is(200)->content_is('/%E2%98%83/%E2%98%83');
Expand Down
1 change: 1 addition & 0 deletions t/mojolicious/production_app.t
Expand Up @@ -34,6 +34,7 @@ is_deeply $t->app->commands->namespaces,
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->moniker, 'mojolicious_test', 'right moniker';

# Plugin::Test::SomePlugin2::register (security violation)
$t->get_ok('/plugin-test-some_plugin2/register')->status_isnt(500)
Expand Down

0 comments on commit 17f4b7a

Please sign in to comment.