-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGlomuleHandler.pm
135 lines (98 loc) · 2.55 KB
/
GlomuleHandler.pm
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
package eTrevolution::GlomuleHandler;
use strict;
use vars qw( $core );
use base qw(Apache::Filter);
use Apache::Const -compile => qw(OK);
use Apache::Connection;
use APR::Table;
use constant BUFF_LEN => 1024;
use eTrevolution::eThreads::Core;
sub init {
my ($child_pool,$s) = @_;
$core = new eTrevolution::eThreads::Core;
return Apache::OK;
}
sub handler {
my $f = shift;
$core = new eTrevolution::eThreads::Core if (!$core);
my $data;
while ( $f->read( my $buffer, BUFF_LEN ) ) {
$data .= $buffer;
}
# short-curcuit if this is the end
if (!$data) {
return Apache::OK;
}
my $inst = $core->load_instance_from_notes($f->c);
$inst->{template} = $inst->new_object("Template");
$inst->{template}{value} = $data;
# -- walk the template to see what glomules we're using -- #
my $walker = $inst->new_object("Template::Walker");
foreach my $t (keys %{$core->{settings}{glomule_types}}) {
# -- register the walker -- #
$walker->register(
[ $t , sub { return &walk_glomule($t,$inst,@_); } ]
);
# -- and also register the handler -- #
$inst->{gholders}->register(
[ $t , sub { return &handle_glomule($t,$inst,@_); } ]
);
}
$walker->walk_template_tree(
$inst->{template}->get_tree
);
# -- now actually process the template -- #
if (0) {
use Data::Dumper;
open(DUMP,">/web/perl/eTrevolution/cached/dump");
print DUMP Data::Dumper->Dump([$inst->{template}->get_tree]);
close DUMP;
}
my $content;
$inst->{gholders}->handle_template_tree(
$inst->{template}->get_tree,
$content
);
$f->print($content);
$inst->DESTROY;
return Apache::OK;
}
sub walk_glomule {
my $type = shift;
my $inst = shift;
my $i = shift;
$core->check_rights_for_glomule($inst,$i->args->{glomule});
my $objname = $core->get_object_for_type($type);
if (!$objname) {
$core->bail("Couldn't find object name for $type");
}
my $rctx = $inst->new_object(
"GHolders::RegisterContext"
)->set($type.".".$i->args->{glomule});
my $g = $inst->new_object(
"Glomule::Type::".$objname,
$i->args->{glomule}
)->activate;
$g->connect_to_gholders($rctx);
if ( my $ref = $g->is_function( $i->args->{function} ) ) {
$ref->( $i->args );
} else {
$core->bail(
"Unknown glomule function: "
. $i->args->{glomule}
. "/"
. $i->args->{function}
);
}
}
sub handle_glomule {
my $type = shift;
my $inst = shift;
my $i = shift;
my $ctx = $inst->{gholders}->get_context;
$inst->{gholders}->set_context($type.".".$i->args->{glomule});
$inst->{gholders}->handle_template_tree($i,$_[0]);
$inst->{gholders}->set_context($ctx);
return undef;
}
1;