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