| 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 | ## | ||
| 13 | ## Adapted from Web::Reactor::Actions::Decor | ||
| 14 | ## Decor application machinery core | ||
| 15 | ## Copyright (c) 2014-2022 Vladi Belperchinov-Shabanski "Cade" | ||
| 16 | ## <cade@noxrun.com> <cade@bis.bg> <cade@cpan.org> | ||
| 17 | ## http://cade.noxrun.com | ||
| 18 | ## | ||
| 19 | ## LICENSE: GPLv2 | ||
| 20 | ## https://github.com/cade-vs/perl-decor | ||
| 21 | ## | ||
| 22 | ############################################################################## | ||
| 23 | package Web::Reactor::Actions::Alt; | ||
| 24 | use strict; | ||
| 25 | use Exception::Sink; | ||
| 26 | use Web::Reactor::Actions; | ||
| 27 | use Data::Dumper; | ||
| 28 | |||
| 29 | use parent 'Web::Reactor::Actions'; | ||
| 30 | |||
| 31 | # calls an action (function) by name | ||
| 32 | # args: | ||
| 33 | # name -- function/action name | ||
| 34 | # %args -- array used as named hash arguments | ||
| 35 | # args hash keys: | ||
| 36 | # ARGS -- hash reference of attributes/arguments passed to the action | ||
| 37 | # returns: | ||
| 38 | # result text to be replaced in output | ||
| 39 | sub call | ||
| 40 | { | ||
| 41 | my $self = shift; | ||
| 42 | |||
| 43 | my $name = lc shift; | ||
| 44 | my %args = @_; | ||
| 45 | |||
| 46 | my $reo = $self->get_reo(); | ||
| 47 | |||
| 48 | if( $name !~ /^[a-z_\-0-9]+$/ ) | ||
| 49 | { | ||
| 50 | $reo->log( "error: invalid action name [$name] expected ALPHANUMERIC" ); | ||
| 51 | return undef; | ||
| 52 | } | ||
| 53 | |||
| 54 | my $cr = $self->__load_action_file( $name ); | ||
| 55 | |||
| 56 | if( ! $cr ) | ||
| 57 | { | ||
| 58 | $reo->log( "error: cannot load action [$name]" ); | ||
| 59 | return undef; | ||
| 60 | } | ||
| 61 | |||
| 62 | my $data = $cr->( $reo, %args ); | ||
| 63 | |||
| 64 | return $data; | ||
| 65 | } | ||
| 66 | |||
| 67 | sub __load_action_file | ||
| 68 | { | ||
| 69 | my $self = shift; | ||
| 70 | |||
| 71 | my $name = shift; | ||
| 72 | |||
| 73 | my $reo = $self->get_reo(); | ||
| 74 | my $cfg = $self->get_cfg(); | ||
| 75 | |||
| 76 | return $self->{ 'ACTIONS_CODE_CACHE' }{ $name } if exists $self->{ 'ACTIONS_CODE_CACHE' }{ $name }; | ||
| 77 | |||
| 78 | my $dirs = $cfg->{ 'ACTIONS_DIRS' } || [ $cfg->{ 'APP_ROOT' } . '/actions' ]; | ||
| 79 | my $pkgs = $cfg->{ 'ACTIONS_PKGS' } || 'reactor::actions::'; | ||
| 80 | |||
| 81 | my $found; | ||
| 82 | for my $dir ( @$dirs ) | ||
| 83 | { | ||
| 84 | my $file = "$dir/$name.pm"; # TODO: subdirs? | ||
| 85 | next unless -e $file; | ||
| 86 | $found = $file; | ||
| 87 | last; | ||
| 88 | } | ||
| 89 | |||
| 90 | return undef unless $found; | ||
| 91 | |||
| 92 | my $ap = $pkgs . $name; | ||
| 93 | |||
| 94 | eval | ||
| 95 | { | ||
| 96 | delete $INC{ $found }; | ||
| 97 | require $found; | ||
| 98 | }; | ||
| 99 | |||
| 100 | if( ! $@ ) | ||
| 101 | { | ||
| 102 | $reo->log_debug( "status: load action ok: $ap [$found]" ); | ||
| 103 | my $cr = $self->{ 'ACTIONS_CODE_CACHE' }{ $name } = \&{ "${ap}::main" }; # call/function reference | ||
| 104 | return $cr; | ||
| 105 | } | ||
| 106 | elsif( $@ =~ /Can't locate $found/) | ||
| 107 | { | ||
| 108 | $reo->log( "error: action not found: $ap [$found]" ); | ||
| 109 | } | ||
| 110 | else | ||
| 111 | { | ||
| 112 | $reo->log( "error: load action failed: $ap: $@ [$found]" ); | ||
| 113 | } | ||
| 114 | |||
| 115 | return undef; | ||
| 116 | } | ||
| 117 | |||
| 118 | ############################################################################## | ||
| 119 | 1; | ||
| 120 | ###EOF######################################################################## | ||
| 121 |