File: Handler.pm

package info (click to toggle)
movabletype-opensource 5.1.4%2Bdfsg-4%2Bdeb7u3
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 32,996 kB
  • sloc: perl: 197,285; php: 62,405; sh: 166; xml: 117; makefile: 83; sql: 32
file content (113 lines) | stat: -rw-r--r-- 2,592 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# Movable Type (r) Open Source (C) 2001-2012 Six Apart, Ltd.
# This program is distributed under the terms of the
# GNU General Public License, version 2.
#
# $Id$
package MT::Template::Handler;
use strict;

sub EL_CODE  {0}
sub EL_TYPE  {1}
sub EL_SUPER {2}

sub new {
    my $class = shift;
    return bless [@_], $class;
}

sub code {
    my $handler = shift;
    return $handler->[ EL_CODE() ] = shift if @_;
    return $handler->[ EL_CODE() ];
}

sub type {
    my $handler = shift;
    return $handler->[ EL_TYPE() ] = shift if @_;
    return $handler->[ EL_TYPE() ];
}

sub super {
    my $handler = shift;
    return $handler->[ EL_SUPER() ] = shift if @_;
    return $handler->[ EL_SUPER() ];
}

sub values { @{ $_[0] } }

sub invoke {
    my $self = shift;
    my ( $ctx, $args, $cond ) = @_;
    my $code = $self->[ EL_CODE() ];
    unless ( ref $code ) {
        $code = $self->[ EL_CODE() ] = MT->handler_to_coderef($code);
        return unless $code;
        $self->[ EL_CODE() ] = $code;
    }
    local $ctx->{__stash}{__handler} = $self;
    return $code->( $ctx, $args, $cond );
}

sub invoke_super {
    my $self = shift;
    my ( $ctx, $args, $cond ) = @_;
    my $super = $self->super;
    return unless defined $super;
    if ( !ref $super || ref $super ne 'MT::Template::Handler' ) {
        $super = $self->[ EL_SUPER() ] = MT::Template::Handler->new(@$super);
    }
    my $tag = lc $ctx->stash('tag');
    local $ctx->{__handlers}{$tag} = $super;
    $super->invoke(@_);
}

1;
__END__

=head1 NAME

MT::Template::Handler - Movable Type Template Handler

=head1 SYNOPSIS

    use MT::Template::Handler;
    my $hdlr = MT::Template::Handler->new(
        \&_hdlr_foo,    # handler coderef
        1,              # type of handler
        \$orig_hdlr,    # ref to parent handler
    ); 

    $hdlr->invoke( $ctx, $args, $cond );
    $hdlr->invoke_super( $ctx, $args, $cond );

=head1 USAGE

=head2 MT::Template::Handler->new

=head2 $hdlr->invoke

invoke this handler. takes $ctx, $args, $cond for handling context.

=head2 $hdlr->invoke_super

invoke parent handler. takes $ctx, $args, $cond for handling context.

=head1 DATA ACCESS METHODS

=head2 $hdlr->code

set/get handlers coderef. it's able to set Movable Type handler string (e.g. $MyPlugin::MyPlugin::Module::foo) too.

=head2 $hdlr->type

set/get handler type.

=head2 $hdlr->super

set/get super handler of this handler. it must be either object of I<MT::Template::Handler> or arrayref as C<[ $code, $type, $super ]>.

=head2 $hdlr->values

convinience method. returns array of three values above.

=cut