File Coverage

blib/lib/Wrangler/PluginManager.pm
Criterion Covered Total %
statement 6 50 12.0
branch 0 34 0.0
condition n/a
subroutine 2 7 28.5
pod 0 5 0.0
total 8 96 8.3


line stmt bran cond sub pod time code
1             package Wrangler::PluginManager;
2              
3 1     1   4 use strict;
  1         1  
  1         34  
4 1     1   3 use warnings;
  1         1  
  1         579  
5              
6             our @plugins;
7             our $phase_plugins;
8             our %enabled;
9             sub load_plugins {
10 0     0 0   my $wrangler = shift;
11 0 0         die 'Wrangler::PluginManager has a functional interface but needs the $wrangler obj for plugins' unless $wrangler;
12              
13 0           my %seen = ();
14             # this is modeled after Padre::PluginManager::load_plugins
15 0           foreach my $inc (@INC) {
16 0           my $dir = File::Spec->catdir( $inc, 'Wrangler', 'Plugin' );
17 0 0         next unless -d $dir;
18              
19 0           Wrangler::debug("Wrangler::PluginManager::load_plugins: readdir $dir");
20 0 0         opendir(my $dirh, $dir) or die "Wrangler::PluginManager::load_plugins: opendir $dir failed: $!";
21 0 0         my @files = readdir($dirh) or die "Wrangler::PluginManager::load_plugins: readdir $dir failed: $!";
22 0 0         closedir($dirh) or die "Wrangler::PluginManager::load_plugins: closedir $dir failed: $!";
23              
24 0           foreach (@files) {
25 0 0         next unless s/\.pm$//;
26 0           my $module = "Wrangler::Plugin::$_";
27 0 0         next if $seen{$module}++;
28              
29 0           Wrangler::debug("Wrangler::PluginManager::load_plugins: module:$module in $dir");
30 0           eval "use $module ();";
31 0 0         Wrangler::debug(" eval 'use' module failed:\n $@") if $@;
32              
33             # Wrangler::debug(' version: '. ${ $module . '::VERSION' }) if ${ $module . '::VERSION' };
34              
35 0 0         next unless $module->can('plugin_phases');
36              
37 0 0         my $plugin = $module->new($wrangler) or next;
38              
39 0 0         next unless ref($plugin) =~ /^Wrangler::Plugin::/;
40              
41 0           push(@plugins,$plugin);
42             }
43             }
44              
45             # call plugins for phase "wrangler_startup"
46 0 0         if( my $plugins_ref = Wrangler::PluginManager::plugins('wrangler_startup') ){
47 0           for my $plugin (@$plugins_ref){
48             # print "PluginManager::load_plugins and call startup: $plugin\n";
49 0           $plugin->wrangler_startup();
50             }
51             }
52             }
53              
54             # loading plugin simply prepares the obj, by calling new, which shouldn't do much
55             # plugs then are actually 'enabled' later on, by calling enable_plugin(), which
56             # calls the Plugin's enable_plugin and pushes the pos into @enabled
57             sub enable_plugin {
58 0     0 0   Wrangler::debug("Enable Plugin '$_[0]'");
59 0           $enabled{$_[0]} = 1;
60 0           $Wrangler::Config::settings{'plugins'}{$_[0]} = 1;
61 0           return 1;
62             }
63             sub disable_plugin {
64 0 0   0 0   return 0 unless defined($Wrangler::Config::settings{'plugins'}{$_[0]});
65 0           Wrangler::debug("Disable Plugin '$_[0]'");
66 0           $Wrangler::Config::settings{'plugins'}{$_[0]} = 0;
67 0           return 1;
68             }
69             sub is_enabled {
70             # Wrangler::debug("PluginManager::is_enabled: @_");
71 0     0 0   return $Wrangler::Config::settings{'plugins'}{$_[0]};
72             }
73              
74              
75             sub plugins {
76 0     0 0   my $phase = shift;
77              
78 0 0         return unless $Wrangler::Config::settings{'plugins'};
79              
80 0 0         return \@plugins unless $phase;
81              
82             # in case we got a phase, return only plugins for this phase
83 0 0         unless($phase_plugins){
84 0           for(@plugins){
85 0 0         next unless $Wrangler::Config::settings{'plugins'}{ $_->plugin_name };
86 0           for my $phase (keys %{ $_->plugin_phases() }){
  0            
87 0           push(@{ $phase_plugins->{$phase} }, $_);
  0            
88             }
89             }
90             # require Data::Dumper;
91             # Wrangler::debug("Done building optimisation hash:".Data::Dumper::Dumper(\@plugins, $phase_plugins));
92             }
93              
94 0           return $phase_plugins->{$phase};
95             }
96              
97             1;