なんちゃってフレームワーク

10分で作るフレームワーク。テンプレートメソッドデザインパターンバージョン。
3分っていいたかったけど、10分かかった。

package Possy;
 
use strict;
use warnings;
 
use base qw/Class::Accessor/;
use UNIVERSAL::require;
use String::CamelCase;
use CGI;
__PACKAGE__->mk_accessors(qw/action_clas template_dir/);
 
sub new {
    my $class = shift;
    my $self = shift;
 
    $self = {
        action_class => $self->{action_class} || 'Possy::Action',
        template_dir => $self->{template_dir} || './../tt',
    };
 
    bless $self ,$class;
 
}
 
sub process {
    my $self    = shift;
    my $action = String::CamelCase::camelize( shift || 'index' ) ;
 
    eval {
        my $module = $self->{action_class} . "::" . $action;
        $module->require or die "NOT FOUNT $action PAGE:[$@]";
        my $cgi = CGI->new();
        my $stash = {};
        my $possy = $module->new(  { possy => { template_dir => $self->{template_dir} } });
        $possy->prepare( $cgi , $stash );
        my $to = $possy->process( $cgi , $stash );
        $possy->out( $cgi , $stash , $to );
 
    };
 
    if( $@ ) {
        print "Content-type:text/html \n\n";
        print "ERROR:\n$@";
    }
}
 
 
1;
=head1 NAME
 
Possy - Yet Another Web Framework
 
=head1 SYNOPSYS
 
    use Possy;
    my $possy = Possy->new( { 
        action_class => 'MyApp::Action' ,
        template_dir => File::Spec->catfile( $FindBin::Bin .'/../tt' ),
    } );
    
    $possy->process( $action_name );
 
=head2 DESCRIPTION
 
オレオレフレームワークを使って10分で作成するブログの紹介とかじゃなかった、10分で作ったフレームワーク。
目的は、以外と適当でも動くもんだねハハハ用
 
=head1 TODO
 
-Config周りが適当
 
-拡張性微妙
 
=cut
package Possy::Action;

use strict;
use warnings;
use Data::Dumper;
use FindBin;
use File::Spec;
use Template;


sub new {
    my $class = shift;
    my $self = shift;

    $self = {
        possy => $self->{possy} || {} ,
    };

    bless $self ,$class;


}

sub prepare {}
sub process {}
sub out{
    my $self = shift;
    my $cgi = shift;
    my $stash = shift;
    my $to = shift;

    my $tt = Template->new({
        INCLUDE_PATH => $self->{possy}{template_dir} ,
    }) || die "$Template::ERROR\n";

    print $cgi->header("text/html; charset=utf-8");
    $stash->{cgi} = $cgi;
    $tt->process( $to . '.tt' , $stash ) || die $tt->error() ;

}
sub dumper {
    my $self = shift;
    warn Dumper shift;
}

1;
package MyApp::Action::Index;

use strict;
use warnings;
use base qw/Possy::Action/;


sub process {
    my $self    = shift;
    my $cgi     = shift;
    my $stash   = shift;


    return 'index';

}

1;

bin/index.cgi

#!/usr/bin/perl 

use strict;
use warnings;
use FindBin;
use File::Spec;
use lib File::Spec->catfile( $FindBin::Bin , "./../lib" );
use CGI;
use Possy;

my $cgi = new CGI;

my $possy = Possy->new( {
    action_class => 'MyApp::Action' ,
    template_dir => File::Spec->catfile( $FindBin::Bin .'/../tt' ),
} );

$possy->process( $cgi->param('q') );

1;

tt/index.tt

Hello Index.