Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
added commands attribute to Mojolicious
  • Loading branch information
kraih committed Apr 29, 2012
1 parent 7488633 commit 79042c9
Show file tree
Hide file tree
Showing 7 changed files with 70 additions and 14 deletions.
3 changes: 2 additions & 1 deletion Changes
@@ -1,6 +1,7 @@
This file documents the revision history for Perl extension Mojolicious.

2.92 2012-04-29
2.92 2012-04-30
- Added commands attribute to Mojolicious.
- Improved documentation.
- Improved tests.
- Fixed attribute namespace selector bugs in Mojo::DOM::CSS.
Expand Down
16 changes: 14 additions & 2 deletions lib/Mojolicious.pm
Expand Up @@ -14,6 +14,7 @@ use Mojolicious::Types;
use Scalar::Util qw/blessed weaken/;

# "Robots don't have any emotions, and sometimes that makes me very sad."
has commands => sub { Mojolicious::Commands->new };
has controller_class => 'Mojolicious::Controller';
has mode => sub { ($ENV{MOJO_MODE} || 'development') };
has plugins => sub { Mojolicious::Plugins->new };
Expand Down Expand Up @@ -204,10 +205,10 @@ sub start {
$ENV{MOJO_EXE} ||= (caller)[1];

# We are the application
$ENV{MOJO_APP} = ref $class ? $class : $class->new;
my $self = $ENV{MOJO_APP} = ref $class ? $class : $class->new;

# Start!
Mojolicious::Commands->start(@_);
$self->commands->start(@_);
}

sub startup { }
Expand Down Expand Up @@ -249,6 +250,17 @@ Take a look at our excellent documentation in L<Mojolicious::Guides>!
L<Mojolicious> inherits all attributes from L<Mojo> and implements the
following new ones.
=head2 C<commands>
my $commands = $app->commands;
$app = $app->commands(Mojolicious::Commands->new);
Command line interface for your application, defaults to a
L<Mojolicious::Commands> object.
# Add another namespace to search for commands
push @{$app->commands->namespaces}, 'MyApp::Command';
=head2 C<controller_class>
my $class = $app->controller_class;
Expand Down
7 changes: 5 additions & 2 deletions lib/Mojolicious/Commands.pm
Expand Up @@ -125,7 +125,7 @@ sub start {
sub start_app {
my $self = shift;
$ENV{MOJO_APP} = shift;
$self->start(@_);
$self->new->app->start(@_);
}

sub _command {
Expand All @@ -138,7 +138,7 @@ sub _command {

=head1 NAME
Mojolicious::Commands - Commands
Mojolicious::Commands - Command line interface
=head1 SYNOPSIS
Expand Down Expand Up @@ -303,6 +303,9 @@ Short usage message shown before listing available commands.
Namespaces to search for available commands, defaults to
C<Mojolicious::Command> and C<Mojo::Command>.
# Add another namespace to search for commands
push @{$commands->namespaces}, 'MyApp::Command';
=head1 METHODS
L<Mojolicious::Commands> inherits all methods from L<Mojo::Command> and
Expand Down
2 changes: 0 additions & 2 deletions lib/Mojolicious/Plugins.pm
Expand Up @@ -3,8 +3,6 @@ use Mojo::Base 'Mojo::EventEmitter';

use Mojo::Util 'camelize';

# "Who would have thought Hell would really exist?
# And that it would be in New Jersey?"
has namespaces => sub { ['Mojolicious::Plugin'] };

sub emit_hook {
Expand Down
42 changes: 35 additions & 7 deletions t/mojolicious/commands.t
@@ -1,6 +1,6 @@
use Mojo::Base -strict;

use Test::More tests => 7;
use Test::More tests => 11;

use FindBin;
use lib "$FindBin::Bin/lib";
Expand Down Expand Up @@ -31,15 +31,43 @@ is ref Mojolicious::Commands->run('psgi'), 'CODE', 'right reference';
# Start application
{
local $ENV{MOJO_APP_LOADER} = 1;
local $ENV{MOJO_APP} = 'MojoliciousTest';
is ref Mojolicious::Commands->start, 'MojoliciousTest', 'right class';
local $ENV{MOJO_APP};
is ref Mojolicious::Commands->start_app('MojoliciousTest'),
'MojoliciousTest', 'right class';
}
{
local $ENV{MOJO_APP_LOADER} = 1;
is ref Mojolicious::Commands->start_app('MojoliciousTest'),
'MojoliciousTest', 'right class';
local $ENV{MOJO_APP} = 'MojoliciousTest';
is ref Mojolicious::Commands->start, 'MojoliciousTest', 'right class';
}

# Start application with command
is ref Mojolicious::Commands->start_app(MojoliciousTest => 'psgi'), 'CODE',
'right reference';
{
local $ENV{MOJO_APP};
is ref Mojolicious::Commands->start_app(MojoliciousTest => 'psgi'), 'CODE',
'right reference';
}
{
local $ENV{MOJO_APP} = 'MojoliciousTest';
is ref Mojolicious::Commands->start('psgi'), 'CODE', 'right reference';
}

# Start application with application specific command
my $app;
{
local $ENV{MOJO_APP_LOADER} = 1;
$app = Mojolicious::Commands->start_app('MojoliciousTest');
}
is $app->start('test_command'), 'works!', 'right result';
$app = undef;
{
local $ENV{MOJO_APP_LOADER} = 1;
local $ENV{MOJO_APP} = 'MojoliciousTest';
$app = Mojolicious::Commands->start;
}
is $app->start('test_command'), 'works!', 'right result';
{
local $ENV{MOJO_APP};
is(Mojolicious::Commands->start_app(MojoliciousTest => 'test_command'),
'works!', 'right result');
}
3 changes: 3 additions & 0 deletions t/mojolicious/lib/MojoliciousTest.pm
Expand Up @@ -22,6 +22,9 @@ sub startup {
push @{$self->static->classes}, 'MojoliciousTest';
push @{$self->renderer->classes}, 'MojoliciousTest';

# Application specific commands
push @{$self->commands->namespaces}, 'MojoliciousTest::Command';

# Plugins in custom namespace
unshift @{$self->plugins->namespaces}, $self->routes->namespace . '::Plugin';
$self->plugin('test-some_plugin2');
Expand Down
11 changes: 11 additions & 0 deletions t/mojolicious/lib/MojoliciousTest/Command/test_command.pm
@@ -0,0 +1,11 @@
package MojoliciousTest::Command::test_command;
use Mojo::Base 'Mojo::Command';

# "Who would have thought Hell would really exist?
# And that it would be in New Jersey?"
has description => "Test command.\n";
has usage => "usage: $0 test_command";

sub run { return 'works!' }

1;

0 comments on commit 79042c9

Please sign in to comment.