| 1 | ############################################################################## | ||
| 2 | ## | ||
| 3 | ## Web::Reactor application machinery | ||
| 4 | ## 2013 (c) Vladi Belperchinov-Shabanski "Cade" | ||
| 5 | ## <cade@bis.bg> <cade@biscom.net> <cade@cpan.org> | ||
| 6 | ## | ||
| 7 | ## LICENSE: GPLv2 | ||
| 8 | ## | ||
| 9 | ############################################################################## | ||
| 10 | package Web::Reactor::Actions::Native; | ||
| 11 | use strict; | ||
| 12 | use Exception::Sink; | ||
| 13 | use Carp; | ||
| 14 | use Web::Reactor::Actions; | ||
| 15 | use Data::Dumper; | ||
| 16 | |||
| 17 | our @ISA = qw( Web::Reactor::Actions ); | ||
| 18 | |||
| 19 | # calls an action (function) by name | ||
| 20 | # args: | ||
| 21 | # name -- function/action name | ||
| 22 | # %args -- array used as named hash arguments | ||
| 23 | # args hash keys: | ||
| 24 | # ARGS -- hash reference of attributes/arguments passed to the action | ||
| 25 | # returns: | ||
| 26 | # result text to be replaced in output | ||
| 27 | sub call | ||
| 28 | { | ||
| 29 | my $self = shift; | ||
| 30 | |||
| 31 | my $name = lc shift; | ||
| 32 | my %args = @_; | ||
| 33 | |||
| 34 | my $ap = $self->__find_act_pkg( $name ); | ||
| 35 | |||
| 36 | # print STDERR Dumper( $name, $ap, \%{ $ap } ); | ||
| 37 | |||
| 38 | if( ! $ap ) | ||
| 39 | { | ||
| 40 | confess "action package for action name [$name] not found"; | ||
| 41 | return undef; | ||
| 42 | } | ||
| 43 | |||
| 44 | # FIXME: move to global error/log reporting | ||
| 45 | print STDERR "reactor::actions::call [$name] action package found [$ap]\n"; | ||
| 46 | |||
| 47 | my $cr = \&{ "${ap}::main" }; # call/function reference | ||
| 48 | |||
| 49 | my $text; | ||
| 50 | |||
| 51 | $text = $cr->( $self->{ 'REO_REACTOR' }, \%args ); | ||
| 52 | |||
| 53 | # print STDERR "reactor::actions::call result: $text\n"; | ||
| 54 | |||
| 55 | return $text; | ||
| 56 | } | ||
| 57 | |||
| 58 | sub __find_act_pkg | ||
| 59 | { | ||
| 60 | my $self = shift; | ||
| 61 | |||
| 62 | my $name = lc shift; | ||
| 63 | |||
| 64 | my $act_cache = $self->{ 'ACT_PKG_CACHE' }; | ||
| 65 | |||
| 66 | return $act_cache->{ $name } if exists $act_cache->{ $name }; | ||
| 67 | |||
| 68 | my $app_name = lc $self->{ 'ENV' }{ 'APP_NAME' }; | ||
| 69 | my $dirs = $self->{ 'ENV' }{ 'LIB_DIRS' } || []; | ||
| 70 | if( @$dirs == 0 ) | ||
| 71 | { | ||
| 72 | my $app_root = $self->{ 'ENV' }{ 'APP_ROOT' }; | ||
| 73 | boom "missing APP_ROOT" unless -d $app_root; # FIXME: function? get_app_root() | ||
| 74 | $dirs = [ "$app_root/lib" ]; # FIXME: 'act' actions ? | ||
| 75 | } | ||
| 76 | |||
| 77 | my @asl = @{ $self->{ 'ENV' }{ 'ACTIONS_SETS' } || [] }; | ||
| 78 | @asl = ( $app_name, "Base", "Core" ) unless @asl > 0; | ||
| 79 | |||
| 80 | # action package | ||
| 81 | for my $asl ( @asl ) | ||
| 82 | { | ||
| 83 | my $ap = 'Web::Reactor::Actions::' . $asl . '::' . $name; | ||
| 84 | |||
| 85 | # print STDERR "testing action: $ap\n"; | ||
| 86 | my $fn = $ap; | ||
| 87 | $fn =~ s/::/\//g; | ||
| 88 | $fn .= '.pm'; | ||
| 89 | eval | ||
| 90 | { | ||
| 91 | require $fn; | ||
| 92 | }; | ||
| 93 | if( ! $@ ) | ||
| 94 | { | ||
| 95 | print STDERR "LOADED! action: $ap\n"; | ||
| 96 | $act_cache->{ $name } = $ap; | ||
| 97 | return $ap; | ||
| 98 | } | ||
| 99 | elsif( $@ =~ /Can't locate $fn/) | ||
| 100 | { | ||
| 101 | print STDERR "NOT FOUND: action: $ap: $fn\n"; | ||
| 102 | } | ||
| 103 | else | ||
| 104 | { | ||
| 105 | print STDERR "ERROR LOADING: action: $ap: $@\n"; | ||
| 106 | } | ||
| 107 | |||
| 108 | |||
| 109 | next; | ||
| 110 | =pod | ||
| 111 | my $fn = $ap; | ||
| 112 | $fn =~ s/::/\//g; | ||
| 113 | # paths | ||
| 114 | for my $p ( @$dirs ) | ||
| 115 | { | ||
| 116 | my $ffn = "$p/$fn.pm"; | ||
| 117 | |||
| 118 | print STDERR "looking for file, action: $ap --> $fn --> $ffn\n"; | ||
| 119 | |||
| 120 | next unless -e $ffn; | ||
| 121 | # FIXME: check require status! | ||
| 122 | print STDERR "FOUND! action: $ap --> $fn --> $ffn\n"; | ||
| 123 | require $ffn; | ||
| 124 | print STDERR "LOADED! action: $ap --> $fn --> $ffn\n"; | ||
| 125 | # print "FOUND ", %INC; | ||
| 126 | $act_cache->{ $name } = $ap; | ||
| 127 | return $ap; | ||
| 128 | } | ||
| 129 | =cut | ||
| 130 | } | ||
| 131 | |||
| 132 | return undef; | ||
| 133 | } | ||
| 134 | |||
| 135 | ############################################################################## | ||
| 136 | 1; | ||
| 137 | ###EOF######################################################################## | ||
| 138 |