Skip to content

Commit

Permalink
Plack::Util::load_psgi("MyApp::PSGIRunner") would now DWIM.
Browse files Browse the repository at this point in the history
Also it uses require() instead of do().
  • Loading branch information
miyagawa committed Dec 16, 2009
1 parent 9d1f776 commit 7ca8a9c
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 3 deletions.
13 changes: 10 additions & 3 deletions lib/Plack/Util.pm
Expand Up @@ -93,12 +93,19 @@ sub foreach {
}
}

sub class_to_file {
my $class = shift;
$class =~ s!::!/!g;
$class . ".pm";
}

sub load_psgi {
my $file = shift;
my $stuff = shift;

my $app = do $file;
my $file = $stuff =~ /^[a-zA-Z0-9\_\:]+$/ ? class_to_file($stuff) : $stuff;
my $app = require $file;
return $app->to_app if $app and Scalar::Util::blessed($app) and $app->can('to_app');
return $app if $app and ref $app eq 'CODE' or overload::Method($app, '&{}');
return $app if $app and (ref $app eq 'CODE' or overload::Method($app, '&{}'));

if (my $e = $@ || $!) {
die "Can't load $file: $e";
Expand Down
11 changes: 11 additions & 0 deletions t/Plack-Util/Hello.pm
@@ -0,0 +1,11 @@
package Hello;

sub to_app {
return sub {
return [200, ['Content-Type', 'text/plain'], ['Hello']];
};
}

__PACKAGE__->to_app;


1 change: 1 addition & 0 deletions t/Plack-Util/hello.psgi
@@ -0,0 +1 @@
sub { return [200, ['Content-Type', 'text/plain'], ['Hello']] };
28 changes: 28 additions & 0 deletions t/Plack-Util/load.t
@@ -0,0 +1,28 @@
use strict;
use Plack::Util;
use Plack::Test;
use HTTP::Request::Common;
use Test::More;

{
my $app = Plack::Util::load_psgi("t/Plack-Util/hello.psgi");
ok $app;

test_psgi $app, sub {
is $_[0]->(GET "/")->content, "Hello";
};
}

{
use lib "t/Plack-Util";
my $app = Plack::Util::load_psgi("Hello");
ok $app;
test_psgi $app, sub {
is $_[0]->(GET "/")->content, "Hello";
};
}




done_testing;

0 comments on commit 7ca8a9c

Please sign in to comment.