Андрей (andy) wrote in changelog,
Андрей
andy
changelog

[ljcom] r10996: LJSUP-9792 (generalize m.livejournal.com...

Committer: ailyin
LJSUP-9792 (generalize m.livejournal.com's router/controller/response framework)
A   trunk/cgi-bin/LJ/Controller.pm
A   trunk/cgi-bin/LJ/Response/
A   trunk/cgi-bin/LJ/Response/Error.pm
A   trunk/cgi-bin/LJ/Response/Redirect.pm
A   trunk/cgi-bin/LJ/Response/Template.pm
A   trunk/cgi-bin/LJ/Response.pm
A   trunk/cgi-bin/LJ/Router.pm
Added: trunk/cgi-bin/LJ/Controller.pm
===================================================================
--- trunk/cgi-bin/LJ/Controller.pm	                        (rev 0)
+++ trunk/cgi-bin/LJ/Controller.pm	2011-09-13 07:30:02 UTC (rev 10996)
@@ -0,0 +1,20 @@
+package LJ::Controller;
+use strict;
+use warnings;
+
+use LJ::Response;
+
+sub new {
+    my ($class) = @_;
+    return bless {}, $class;
+}
+
+sub process {
+    # this is an abstract method that is supposed to return an LJ::Response
+}
+
+sub default_response {
+    return LJ::Response::Error->new;
+}
+
+1;

Added: trunk/cgi-bin/LJ/Response/Error.pm
===================================================================
--- trunk/cgi-bin/LJ/Response/Error.pm	                        (rev 0)
+++ trunk/cgi-bin/LJ/Response/Error.pm	2011-09-13 07:30:02 UTC (rev 10996)
@@ -0,0 +1,42 @@
+package LJ::Response::Error;
+use strict;
+use warnings;
+
+use base qw( LJ::Response );
+
+__PACKAGE__->mk_accessors( qw( status active_journal_username ) );
+
+sub output {
+    my ($self) = @_;
+
+    my $output = LJ::Widget::ErrorPage->render(
+        'error_type'              => $self->error_type,
+        'uri'                     => $self->uri,
+        'remote'                  => $self->remote,
+        'active_journal_username' => $self->active_journal_username,
+    );
+
+    LJ::Request->print($output);
+}
+
+sub error_type {
+    my ($self) = @_;
+    return $self->{'error_type'} || 'e404';
+}
+
+sub uri {
+    my ($self) = @_;
+    return $self->{'uri'} || LJ::Request->uri;
+}
+
+sub remote {
+    my ($self) = @_;
+    return $self->{'remote'} || LJ::get_remote();
+}
+
+sub http_status {
+    my ($self) = @_;
+    return $self->status || LJ::Request::NOT_FOUND;
+}
+
+1;

Added: trunk/cgi-bin/LJ/Response/Redirect.pm
===================================================================
--- trunk/cgi-bin/LJ/Response/Redirect.pm	                        (rev 0)
+++ trunk/cgi-bin/LJ/Response/Redirect.pm	2011-09-13 07:30:02 UTC (rev 10996)
@@ -0,0 +1,24 @@
+package LJ::Response::Redirect;+use strict;+use warnings;++use base qw( LJ::Response );++__PACKAGE__->mk_accessors( qw( where status ) );++sub output {+    my ($self) = @_;++    my $status = LJ::Request->redirect( $self->where );++    unless ( $self->status ) {+        $self->status($status);+    }+}++sub http_status {+    my ($self) = @_;+    return $self->status;+}++1;
\ No newline at end of file

Added: trunk/cgi-bin/LJ/Response/Template.pm
===================================================================
--- trunk/cgi-bin/LJ/Response/Template.pm	                        (rev 0)
+++ trunk/cgi-bin/LJ/Response/Template.pm	2011-09-13 07:30:02 UTC (rev 10996)
@@ -0,0 +1,39 @@
+package LJ::Response::Template;
+use strict;
+use warnings;
+
+use LJ::HTML::Template;
+use LJ::SiteScheme;
+
+use base qw( LJ::Response );
+__PACKAGE__->mk_accessors(
+    qw( path file params use_site_scheme site_scheme_params title )
+);
+
+sub output {
+    my ($self) = @_;
+
+    my $template = LJ::HTML::Template->new(
+        { use_expr => 1 }, # force HTML::Template::Pro with Expr support
+        filename => $self->path . $self->file,
+    );
+
+    $template->param( %{ $self->params || {} } );
+
+    my $output_body = $template->output;
+
+    my $final_output = '';
+    if ( $self->use_site_scheme ) {
+        $final_output = LJ::SiteScheme->render_page( {
+            'body'  => $output_body,
+            'title' => $self->title || '',
+            %{ $self->site_scheme_params || {} },
+        } );
+    } else {
+        $final_output = $output_body;
+    }
+
+    LJ::Request->print($final_output);
+}
+
+1;

Added: trunk/cgi-bin/LJ/Response.pm
===================================================================
--- trunk/cgi-bin/LJ/Response.pm	                        (rev 0)
+++ trunk/cgi-bin/LJ/Response.pm	2011-09-13 07:30:02 UTC (rev 10996)
@@ -0,0 +1,29 @@
+package LJ::Response;
+use strict;
+use warnings;
+
+use base qw( Class::Accessor );
+
+use LJ::Request;
+
+use LJ::Response::Error;
+use LJ::Response::Redirect;
+use LJ::Response::Template;
+
+
+sub new {
+    my ( $class, %args ) = @_;
+
+    return bless \%args, $class;
+}
+
+sub output {
+    # this is an abstract method that is supposed to use LJ::Request
+    # to output data
+}
+
+sub http_status {
+    return LJ::Request::OK;
+}
+
+1;

Added: trunk/cgi-bin/LJ/Router.pm
===================================================================
--- trunk/cgi-bin/LJ/Router.pm	                        (rev 0)
+++ trunk/cgi-bin/LJ/Router.pm	2011-09-13 07:30:02 UTC (rev 10996)
@@ -0,0 +1,18 @@
+package LJ::Router;
+use strict;
+use warnings;
+
+use LJ::Mob::Router;
+
+my @routers = qw(
+    LJ::Mob::Router
+);
+
+sub match_controller {
+    foreach my $router (@routers) {
+        $router->match_controller;
+        return if LJ::Request->notes('controller');
+    }
+}
+
+1;

Tags: andy, ljcom, pm
Subscribe
  • Post a new comment

    Error

    Anonymous comments are disabled in this journal

    default userpic

    Your reply will be screened

    Your IP address will be recorded 

  • 0 comments