| 1 | package Mason::PluginManager; | ||
| 2 | $Mason::PluginManager::VERSION = '2.23'; | ||
| 3 | use Carp; | ||
| 4 | use Log::Any qw($log); | ||
| 5 | use Mason::Moose; | ||
| 6 | use Mason::Util qw(can_load uniq); | ||
| 7 | |||
| 8 | my ( %apply_plugins_cache, %final_subclass_seen ); | ||
| 9 | |||
| 10 | # CLASS METHODS | ||
| 11 | # | ||
| 12 | |||
| 13 | our $depth; | ||
| 14 | our %visited; | ||
| 15 | my $max_depth = 16; | ||
| 16 | |||
| 17 | method process_top_plugin_specs ($class: $plugin_specs) { | ||
| 18 | local $depth = 0; | ||
| 19 | local %visited = (); | ||
| 20 | |||
| 21 | my @positive_plugin_specs = grep { !/^\-/ } @$plugin_specs; | ||
| 22 | my @negative_plugin_specs = map { substr( $_, 1 ) } grep { /^\-/ } @$plugin_specs; | ||
| 23 | push( @positive_plugin_specs, '@Default' ); | ||
| 24 | my %exclude_plugin_modules = | ||
| 25 | map { ( $_, 1 ) } $class->process_plugin_specs( \@negative_plugin_specs ); | ||
| 26 | |||
| 27 | my @modules = | ||
| 28 | grep { !$exclude_plugin_modules{$_} } $class->process_plugin_specs( \@positive_plugin_specs ); | ||
| 29 | |||
| 30 | return @modules; | ||
| 31 | } | ||
| 32 | |||
| 33 | method process_plugin_specs ($class: $plugin_specs) { | ||
| 34 | local $depth = $depth + 1; | ||
| 35 | local %visited = %visited; | ||
| 36 | die ">$max_depth levels deep in process_plugins_list (plugin cycle?)" if $depth >= $max_depth; | ||
| 37 | croak 'plugins must be an array reference' unless ref($plugin_specs) eq 'ARRAY'; | ||
| 38 | my @modules = ( uniq( map { $class->process_plugin_spec($_) } @$plugin_specs ) ); | ||
| 39 | return @modules; | ||
| 40 | } | ||
| 41 | |||
| 42 | method process_plugin_spec ($class: $plugin_spec) { | ||
| 43 | my $module = $class->plugin_spec_to_module($plugin_spec); | ||
| 44 | my @modules = !$visited{$module}++ ? $module->expand_to_plugins : (); | ||
| 45 | return @modules; | ||
| 46 | } | ||
| 47 | |||
| 48 | method plugin_spec_to_module ($class: $plugin_spec) { | ||
| 49 | my $module = | ||
| 50 | substr( $plugin_spec, 0, 1 ) eq '+' ? ( substr( $plugin_spec, 1 ) ) | ||
| 51 | : substr( $plugin_spec, 0, 1 ) eq '@' | ||
| 52 | ? ( "Mason::PluginBundle::" . substr( $plugin_spec, 1 ) ) | ||
| 53 | : "Mason::Plugin::$plugin_spec"; | ||
| 54 | return can_load($module) | ||
| 55 | ? $module | ||
| 56 | : die "could not load '$module' for plugin spec '$plugin_spec'"; | ||
| 57 | } | ||
| 58 | |||
| 59 | method apply_plugins_to_class ($class: $base_subclass, $name, $plugins) { | ||
| 60 | my $subclass; | ||
| 61 | my $key = join( ",", $base_subclass, @$plugins ); | ||
| 62 | return $apply_plugins_cache{$key} if defined( $apply_plugins_cache{$key} ); | ||
| 63 | |||
| 64 | my $final_subclass; | ||
| 65 | my @roles = map { $_->get_roles_for_mason_class($name) } @$plugins; | ||
| 66 | if (@roles) { | ||
| 67 | my $meta = Moose::Meta::Class->create_anon_class( | ||
| 68 | superclasses => [$base_subclass], | ||
| 69 | roles => \@roles, | ||
| 70 | cache => 1 | ||
| 71 | ); | ||
| 72 | $final_subclass = $meta->name; | ||
| 73 | $meta->add_method( 'meta' => sub { $meta } ) | ||
| 74 | if !$final_subclass_seen{$final_subclass}++; | ||
| 75 | } | ||
| 76 | else { | ||
| 77 | $final_subclass = $base_subclass; | ||
| 78 | } | ||
| 79 | $log->debugf( "apply_plugins - base_subclass=%s, name=%s, plugins=%s, roles=%s - %s", | ||
| 80 | $base_subclass, $name, $plugins, \@roles, $final_subclass ) | ||
| 81 | if $log->is_debug; | ||
| 82 | |||
| 83 | $final_subclass->meta->make_immutable if $final_subclass->can('meta'); | ||
| 84 | |||
| 85 | $apply_plugins_cache{$key} = $final_subclass; | ||
| 86 | return $final_subclass; | ||
| 87 | } | ||
| 88 | |||
| 89 | __PACKAGE__->meta->make_immutable(); | ||
| 90 | |||
| 91 | 1; |