| 1 | package Net::FreeIPA::API::Function; | ||
| 2 | $Net::FreeIPA::API::Function::VERSION = '3.0.3'; | ||
| 3 | use strict; | ||
| 4 | use warnings; | ||
| 5 | |||
| 6 | use base qw(Exporter); | ||
| 7 | |||
| 8 | our $AUTOLOAD; | ||
| 9 | |||
| 10 | use Net::FreeIPA::API::Convert qw(process_args); | ||
| 11 | use Net::FreeIPA::API::Magic qw(retrieve); | ||
| 12 | |||
| 13 | my @all = Net::FreeIPA::API::Magic::all_command_names(); | ||
| 14 | |||
| 15 | # Make all functions individually exportable | ||
| 16 | our @EXPORT_OK = @all; | ||
| 17 | |||
| 18 | # Support :all tag | ||
| 19 | our %EXPORT_TAGS = ( | ||
| 20 | all => \@all, | ||
| 21 | ); | ||
| 22 | |||
| 23 | # Make an export tag for each method-class, e.g. :user | ||
| 24 | foreach my $cmd (@all) { | ||
| 25 | my $class = $cmd; | ||
| 26 | $class =~ s/_.*$//; | ||
| 27 | $EXPORT_TAGS{$class} = [] if ! defined($EXPORT_TAGS{$class}); | ||
| 28 | push(@{$EXPORT_TAGS{$class}}, $cmd); | ||
| 29 | }; | ||
| 30 | |||
| 31 | # | ||
| 32 | # AUTOLOAD a function with the exact commandname, returns C<_api_function> call | ||
| 33 | # | ||
| 34 | sub AUTOLOAD | ||
| 35 | { | ||
| 36 | my $called = $AUTOLOAD; | ||
| 37 | |||
| 38 | # Don't mess with garbage collection! | ||
| 39 | return if $called =~ m{DESTROY}; | ||
| 40 | |||
| 41 | my $called_orig = $called; | ||
| 42 | $called =~ s{^.*::}{}; | ||
| 43 | |||
| 44 | my ($cmd, $fail) = retrieve($called); | ||
| 45 | |||
| 46 | if ($fail) { | ||
| 47 | die "Unknown Net::FreeIPA::API method: $called failed $fail (from original $called_orig)"; | ||
| 48 | } else { | ||
| 49 | # Run the expected method. | ||
| 50 | # AUTOLOAD with glob assignment and goto defines the autoloaded method | ||
| 51 | # (so they are only autoloaded once when they are first called), | ||
| 52 | # but that breaks inheritance. | ||
| 53 | |||
| 54 | # The method name is in the name attribute | ||
| 55 | return process_args($cmd, @_); | ||
| 56 | } | ||
| 57 | } | ||
| 58 | |||
| 59 | 1; |