Permalink
Browse files

use released version of Plack and removed Builder

  • Loading branch information...
1 parent f118511 commit d18ed555882c98033b3f4065ff5f96e73a482642 @dann committed Oct 14, 2009
Showing with 27 additions and 223 deletions.
  1. +2 −1 Makefile.PL
  2. +25 −25 lib/PlackX/Engine.pm
  3. +0 −58 lib/PlackX/Engine/Builder.pm
  4. +0 −67 lib/PlackX/Engine/Builder/Declare.pm
  5. +0 −71 t/declare.t
  6. +0 −1 t/engine.t
View
@@ -3,8 +3,9 @@ name 'PlackX-Engine';
all_from 'lib/PlackX/Engine.pm';
requires(
- "Plack" => 0,
"Class::Accessor::Fast" => 0,
+ "Plack" => 0,
+ "Plack::Request" => 0,
);
test_requires(
'Test::More' => 0,
View
@@ -3,11 +3,12 @@ use strict;
use warnings;
use 5.008_001;
use Plack::Loader;
-use PlackX::Engine::Builder;
+use Plack::Builder;
+use PlackX::Engine::Util;
use Carp ();
use base qw/Class::Accessor::Fast/;
-__PACKAGE__->mk_accessors(qw/server middlewares request_handler/);
+__PACKAGE__->mk_accessors(qw/server middlewares request_handler request_class/);
our $VERSION = '0.01';
@@ -27,13 +28,14 @@ sub new {
sub _init {
my $self = shift;
- PlackX::Engine::Util::load_class($self->{request_class});
+ PlackX::Engine::Util::load_class( $self->{request_class} );
}
sub run {
my $self = shift;
Carp::croak 'server is required' unless $self->{server};
- Carp::croak '{server}->{module} is required' unless $self->{server}->{module};
+ Carp::croak '{server}->{module} is required'
+ unless $self->{server}->{module};
my $server_instance
= $self->_build_server_instance( $self->{server}->{module},
@@ -69,18 +71,10 @@ sub _build_app {
sub _wrap_with_middlewares {
my ( $self, $request_handler ) = @_;
- my $builder = PlackX::Engine::Builder->new;
-
- # orz. this code should be moved to PlackX::Engine::Builder
- for my $middleware ( @{ $self->{middlewares}} ) {
- my $middleware_name = $middleware->{module};
- $builder->add_middleware(
- $middleware_name,
- sub {
- $middleware_name->wrap( @{ $middleware->{args} || [] },
- $_[0] );
- }
- );
+ my $builder = Plack::Builder->new;
+ for my $middleware ( @{ $self->{middlewares} } ) {
+ $builder->add_middleware( $middleware->{module},
+ %{ $middleware->{opts} || {} } );
}
$builder->to_app($request_handler);
}
@@ -98,10 +92,12 @@ __END__
=head1 NAME
-PlackX::Engine -
+PlackX::Engine - simple request wrapper for Plack
=head1 SYNOPSIS
+* case1: as standalone
+
use PlackX::Engine;
use Plack::Response;
@@ -127,25 +123,29 @@ PlackX::Engine -
{ module => "Plack::Middleware::AccessLog::Timed" },
{ module => "Plack::Middleware::Static" }
],
+ request_class => 'Plack::Request', # you can use request class
}
);
-
+
$engine->run;
+* case2: as psgi handler builder
+ just create engine and return psgi_handler
+
+ my $psgi_handler = $engine->psgi_handler;
+
+
=head1 DESCRIPTION
-PlackX::Engine is
+PlackX::Engine is the simple request wrapper for Plack.
+You want to wrap psgi env with request and response if you make application with Plack.
+You don't need to wrap psgi env with the request and finaize response if you use this module.
=head1 SOURCE AVAILABILITY
This source is in Github:
- http://github.com/dann/
-
-=head1 CONTRIBUTORS
-
-Many thanks to:
-
+ http://github.com/dann/p5-plackx-engine
=head1 AUTHOR
@@ -1,58 +0,0 @@
-package PlackX::Engine::Builder;
-use strict;
-use warnings;
-use PlackX::Engine::Util;
-use base qw/Class::Accessor::Fast/;
-__PACKAGE__->mk_accessors(qw/middlewares/);
-
-sub new {
- my ( $class, $middlewares ) = @_;
- $middlewares ||= [];
- my $self = bless { middlewares => $middlewares }, $class;
- return $self;
-}
-
-sub add_middleware {
- my ( $self, $middleware_name, $middleware ) = @_;
- PlackX::Engine::Util::load_class($middleware_name);
- push @{ $self->{middlewares} }, $middleware;
-}
-
-sub to_app {
- my ( $self, $app ) = @_;
- for my $mw ( reverse @{ $self->{middlewares} } ) {
- $app = $mw->($app);
- }
- $app;
-}
-
-1;
-
-__END__
-
-=encoding utf-8
-
-=head1 NAME
-
-PlackX::Engine::Builder - request handler builder
-
-=head1 SYNOPSIS
-
- use PlackX::Engine::Util;
-
-=head1 DESCRIPTION
-
-PlackX::Engine::Builder is request handler builder
-
-=head1 AUTHOR
-
-Takatoshi Kitano E<lt>kitano.tk@gmail.comE<gt>
-
-=head1 SEE ALSO
-
-=head1 LICENSE
-
-This library is free software; you can redistribute it and/or modify
-it under the same terms as Perl itself.
-
-=cut
@@ -1,67 +0,0 @@
-package PlackX::Engine::Builder::Declare;
-use strict;
-use PlackX::Engine::Builder;
-
-sub import {
- my $caller = caller;
-
- no strict 'refs';
- no warnings 'redefine';
-
- *{ $caller . '::builder' } = \&builder;
- *{ $caller . '::enable' } = \&enable;
-}
-
-sub builder(&) {
- my $block = shift;
-
- my $builder = PlackX::Engine::Builder->new;
- no warnings 'redefine';
-
- local *enable = enable_middleware($builder);
-
- my $app = $block->();
- $builder->to_app($app);
-}
-
-sub enable_middleware {
- my $builder = shift;
-
- return sub {
- my ( $middleware, @args ) = @_;
- $builder->add_middleware( $middleware,
- sub { $middleware->wrap( @args, $_[0] ); } );
- };
-}
-
-1;
-
-__END__
-
-
-=encoding utf-8
-
-=head1 NAME
-
-PlackX::Engine::Builder::Declare - the DSL for PlackX::Engine::Builder.
-
-=head1 SYNOPSIS
-
- use PlackX::Engine::Builder::Declare;
-
-=head1 DESCRIPTION
-
-PlackX::Engine::Builder::Declare is the DSL for PlackX::Engine::Builder.
-
-=head1 AUTHOR
-
-Takatoshi Kitano E<lt>kitano.tk@gmail.comE<gt>
-
-=head1 SEE ALSO
-
-=head1 LICENSE
-
-This library is free software; you can redistribute it and/or modify
-it under the same terms as Perl itself.
-
-=cut
View
@@ -1,71 +0,0 @@
-use strict;
-use Test::Base;
-use PlackX::Engine::Builder::Declare;
-
-filters {
- app => 'eval',
- env => 'yaml',
- headers => 'yaml',
-};
-
-plan tests => 1 * blocks;
-
-run {
- my $block = shift;
- my $handler = builder sub {
- enable 'Plack::Middleware::ContentLength';
- $block->app;
- };
- my $res = $handler->($block->env);
- is_deeply $res->[1], $block->headers;
-};
-
-__END__
-
-=== 200 response
---- app
-sub { [ 200, [ 'Content-Type' => 'text/plain' ], [ 'OK' ] ] }
---- env
-REQUEST_METHOD: GET
---- headers
-- Content-Type
-- text/plain
-- Content-Length
-- 2
-
-=== 304 no entity header
---- app
-sub {
- [ 304, [ ETag => 'Foo' ], [] ];
-}
---- env
-REQUEST_METHOD: GET
---- headers
-- ETag
-- Foo
-
-=== 200 not calculatable
---- app
-sub {
- my $body = "Hello World";
- open my $fh, "<", \$body;
- [ 200, [ 'Content-Type' => 'text/plain' ], $fh ];
-}
---- env
-REQUEST_METHOD: GET
---- headers
-- Content-Type
-- text/plain
-
-=== 200 with C-L
---- app
-sub {
- [ 200, [ 'Content-Type' => 'text/plain', 'Content-Length' => 11 ], [ "Hello World" ] ];
-}
---- env
-REQUEST_METHOD: GET
---- headers
-- Content-Type
-- text/plain
-- Content-Length
-- 11
View
@@ -2,7 +2,6 @@ use strict;
use Test::Base;
use PlackX::Engine;
use Plack::Response;
-use PlackX::Engine::Builder::Declare;
filters {
app => 'eval',

0 comments on commit d18ed55

Please sign in to comment.