| 1 | package Sash::Plugin::Factory; | ||
| 2 | use strict; | ||
| 3 | use warnings; | ||
| 4 | |||
| 5 | use Carp; | ||
| 6 | |||
| 7 | my $_registry; | ||
| 8 | my $_using_plugin; | ||
| 9 | |||
| 10 | sub _load_registry { | ||
| 11 | my $class = shift; | ||
| 12 | |||
| 13 | # Find out where the registry has been installed. | ||
| 14 | my $base = ( grep { -d "$_/Sash/Plugin/registry" } @INC )[0]; | ||
| 15 | |||
| 16 | foreach ( <$base/Sash/Plugin/registry/*.pl*> ) { | ||
| 17 | do "$_"; | ||
| 18 | croak $@ if $@; | ||
| 19 | } | ||
| 20 | |||
| 21 | } | ||
| 22 | |||
| 23 | sub set_registry_item { | ||
| 24 | my $class = shift; | ||
| 25 | my $key = shift; | ||
| 26 | my $value = shift; | ||
| 27 | |||
| 28 | croak __PACKAGE__ . '->set_registry_item requires both key and value' | ||
| 29 | unless defined $key && defined $value; | ||
| 30 | |||
| 31 | $_registry->{$key} = $value; | ||
| 32 | |||
| 33 | return; | ||
| 34 | } | ||
| 35 | |||
| 36 | sub get_registry_item { | ||
| 37 | my $class = shift; | ||
| 38 | my $key = shift; | ||
| 39 | |||
| 40 | croak __PACKAGE__ . '->get_registry_item requires a key to lookup' unless defined $key; | ||
| 41 | |||
| 42 | return $_registry->{$key}; | ||
| 43 | } | ||
| 44 | |||
| 45 | sub get_registry_hash { | ||
| 46 | my $class = shift; | ||
| 47 | |||
| 48 | return $_registry; | ||
| 49 | } | ||
| 50 | |||
| 51 | sub get_plugin { | ||
| 52 | my $class = shift; | ||
| 53 | return $_using_plugin; | ||
| 54 | } | ||
| 55 | |||
| 56 | sub get_plugin_command_class { | ||
| 57 | my $class = shift; | ||
| 58 | return "${_using_plugin}::Command"; | ||
| 59 | } | ||
| 60 | |||
| 61 | sub get_plugin_command_hash { | ||
| 62 | my $class = shift; | ||
| 63 | |||
| 64 | my $plugin_class = $class->get_plugin; | ||
| 65 | my $command_class = "${plugin_class}::Command"; | ||
| 66 | |||
| 67 | return sort keys %{$command_class->get_command_hash}; | ||
| 68 | } | ||
| 69 | |||
| 70 | # This is a pass through method to the concrete implementation. | ||
| 71 | sub get_class { | ||
| 72 | my $class = shift; | ||
| 73 | my $args = shift; #hashref | ||
| 74 | |||
| 75 | croak __PACKAGE__ . '->get_instance: Invalid Arguments - missing hash ref' unless ref $args eq 'HASH'; | ||
| 76 | croak __PACKAGE__ . '->get_instance: Invalid Arguments - undefined endpoint or vendor' | ||
| 77 | unless defined $args->{endpoint} || defined $args->{vendor}; | ||
| 78 | |||
| 79 | $class->_load_registry; | ||
| 80 | |||
| 81 | my $key = ( $args->{endpoint} ) ? $args->{endpoint} : $args->{vendor}; | ||
| 82 | my $plugin_class = $class->get_registry_item( $key ); | ||
| 83 | |||
| 84 | croak __PACKAGE__ . "->get_class: Plugin class identified by $key not found." unless $plugin_class; | ||
| 85 | |||
| 86 | eval "use $plugin_class;"; | ||
| 87 | croak $@ if $@; | ||
| 88 | |||
| 89 | $plugin_class->enable( $args ); | ||
| 90 | |||
| 91 | # Keep track of which plugin we are using | ||
| 92 | $_using_plugin = $plugin_class; | ||
| 93 | |||
| 94 | return $plugin_class; | ||
| 95 | } | ||
| 96 | |||
| 97 | 1; |