Skip to content
Browse files

Dispatcher の分離

  • Loading branch information...
1 parent f05dc49 commit 1acf3e7854995afec569be28ed78207b138118e2 @cho45 committed
Showing with 42 additions and 61 deletions.
  1. +0 −21 lib/Blosxom.pm
  2. +2 −34 lib/Blosxom/Dispatcher.pm
  3. +37 −0 lib/Blosxom/Dispatcher/HTTP.pm
  4. +3 −6 script/server.pl
View
21 lib/Blosxom.pm
@@ -12,27 +12,6 @@ use base qw(Class::Accessor::Fast);
__PACKAGE__->mk_accessors(qw/config path flavour/);
-sub dispatch {
- my ($self, $req, $res) = @_;
- $self->{req} = $req;
-
- $req->path =~ /(.+?)(?:\.([^.]+))?$/;
- my ($path, $flavour) = ($1, $2);
- $path =~ s/index$//;
-
- $self->path($path);
- $self->flavour($flavour || $self->config->{flavour});
-
- $self->collect_entries;
- $self->sort_entries;
- $self->filter_entries;
-
- my ($content_type, $content) = $self->render_entries;
- $res->header("Content-Type" => $content_type);
- $res->content($content);
- $res;
-}
-
sub collect_entries {
my ($self) = @_;
$self->{entries} = Blosxom::Collector::FileSystem->new({ config => $self->config->{FileSystem} })->collect;
View
36 lib/Blosxom/Dispatcher.pm
@@ -1,41 +1,9 @@
package Blosxom::Dispatcher;
-
use strict;
use warnings;
-use UNIVERSAL::require;
-use Blosxom::Config;
-
-sub dispatch {
- my ($class, $req, $res) = @_;
- my $path = $req->path;
-
- my ($name, $method) = _translate($path);
-
- my $root = $class;
- $root =~ s/::.+?$//;
-
- my $controller = "$root\::Controller::$name";
- Blosxom::Config->logger->debug("Requested: $path\n");
- Blosxom::Config->logger->debug("Dispatch: $controller - $method\n");
- $controller->use;
- my $instance = $controller->new;
- $instance->$method($req, $res);
- $res;
-}
-
-sub _translate {
- my ($path) = @_;
- my $action = "default";
- if ($path =~ s{\.([^/]+)}{}) {
- $action = $1;
- }
-
- $path =~ s{/([a-z])([a-z]+)}{"::".uc($1).$2}eg;
- $path =~ s{/$}{::Index};
- $path =~ s{^::}{};
- ($path, $action);
-}
+use base qw(Class::Accessor::Fast);
+__PACKAGE__->mk_accessors(qw(config));
1;
__END__
View
37 lib/Blosxom/Dispatcher/HTTP.pm
@@ -0,0 +1,37 @@
+package Blosxom::Dispatcher::HTTP;
+
+use strict;
+use warnings;
+use base qw(Blosxom::Dispatcher);
+
+use Blosxom;
+
+sub dispatch {
+ my ($self, $req, $res) = @_;
+ my $blosxom = Blosxom->new({ config => $self->config });
+
+ $blosxom->{req} = $req;
+
+ $req->path =~ /(.+?)(?:\.([^.]+))?$/;
+ my ($path, $flavour) = ($1, $2);
+ $path =~ s/index$//;
+
+ $blosxom->path($path);
+ $blosxom->flavour($flavour || $self->config->{flavour});
+
+ $blosxom->collect_entries;
+ $blosxom->sort_entries;
+ $blosxom->filter_entries;
+
+ my ($content_type, $content) = $blosxom->render_entries;
+ $res->header("Content-Type" => $content_type);
+ $res->content($content);
+ $res;
+}
+
+
+1;
+__END__
+
+
+
View
9 script/server.pl
@@ -10,8 +10,7 @@
use HTTP::Engine;
-use Blosxom;
-use Blosxom::Dispatcher;
+use Blosxom::Dispatcher::HTTP;
HTTP::Engine->new(
interface => {
@@ -23,7 +22,7 @@
my $req = shift;
my $res = HTTP::Engine::Response->new;
- my $blosxom = Blosxom->new({
+ Blosxom::Dispatcher::HTTP->new({
config => {
flavour => "html",
template_dir => "template",
@@ -32,9 +31,7 @@
dir => "data"
},
}
- });
-
- $blosxom->dispatch($req, $res);
+ })->dispatch($req, $res);
}
}
)->run;

0 comments on commit 1acf3e7

Please sign in to comment.
Something went wrong with that request. Please try again.