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