App/Plugin/Manager.pm untested

filter: Statementstmt 0.0% 0 / 69 Branchbran 0.0% 0 / 14 Conditioncond 0.0% 0 / 6 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 0.0% 0 / 14 Podpod 0.0% 0 / 9 totaltotal 0.0% 0 / 116 CC 8 SCAR 22.2
CC
8
Cov
0%
scan
20.0
register_plugin
20.0
register_plugins
6.0
CRAP 9.2
1 package Gapp::App::Plugin::Manager;
2 {
3 $Gapp::App::Plugin::Manager::VERSION = '0.007';
4 }
5  
6 use Moose;
7 use MooseX::StrictConstructor;
8 use MooseX::SemiAffordanceAccessor;
9  
10  
11 use Gapp::App::Plugin::MetaFile;
12  
13  
14 has 'app' => (
15 is => 'rw',
16 isa => 'Gapp::App',
17 weak_ref => 1,
18 required => 1,
19 );
20  
21 has 'index' => (
22 is => 'ro',
23 isa => 'HashRef',
24 traits => [qw( Hash )],
25 writer => '_set_index',
26 init_arg => undef,
27 default => sub { { } },
28 handles => {
29 plugin_ids => 'keys',
30 },
31 );
32  
33 has 'meta_file_class' => (
34 is => 'rw',
35 isa => 'Str',
36 default => 'Gapp::App::Plugin::MetaFile',
37 );
38  
39 has 'search_paths' => (
40 writer => '_set_search_paths',
41 isa => 'ArrayRef',
42 default => sub { [ 'res/plugins'] },
43 traits => [qw( Array )],
44 handles => {
45 add_search_path => 'push',
46 search_paths => 'elements',
47 }
48 );
49  
50  
51 sub disable {
52 my ( $self, $id ) = @_;
53  
54 $self->index->{$id}{disabled} = 1;
55 }
56 sub enable {
57 my ( $self, $id ) = @_;
58 $self->index->{$id}{disabled} = 0;
59 }
60  
61 sub is_disabled {
62 my ( $self, $id ) = @_;
63 $self->index->{$id}{disabled};
64 }
65  
66 sub meta_files {
67 my ( $self ) = @_;
68 return map { $self->index->{$_}{meta_object} } $self->plugin_ids;
69 }
70  
71 sub plugin {
72 my ( $self, $id ) = @_;
73 $self->index->{$id}{plugin};
74 }
75  
76 sub set_search_paths {
77 my ( $self, @paths ) = @_;
78 $self->_set_search_paths( \@paths );
79 }
80  
81 sub scan {
82 my ( $self ) = @_;
83
84 my $index = $self->index;
85
86 # parse the meta file
87 my $meta_class = $self->meta_file_class;
88
89 for my $path ( $self->search_paths ) {
90
91 my $rxpath = quotemeta $path;
92
93
94 for my $f ( <$path/*> ) {
95 next if ! -d $f; # skip regular files
96
97 $f =~ s/^$rxpath(\\?\/)?//; # remove the path name from the begininng of the file
98
99 my $xml_path = "$path/$f/$f.xml";
100
101 # if the plugin xml file exists
102 if ( -e $xml_path ) {
103
104 # if the meta object has not been loaded, do it now
105 if ( $index->{$f} || ! $index->{$f}->{ meta_object } ) {
106
107 my $meta = $meta_class->new;
108 $meta->load_file( $xml_path );
109
110 $index->{$f}->{ meta_object } = $meta;
111 }
112
113 }
114
115
116 }
117 }
118
119 return $index;
120 }
121  
122 sub register_plugin {
123 my ( $self, $id ) = @_;
124
125 # don't register the plugin if it is disabled
126 return if $self->index->{$id}->{disabled};
127
128 my $meta = $self->index->{$id}->{ meta_object };
129
130 return if ! $meta;
131
132 # add path to lib
133 unshift @INC, $meta->dir . '/lib' if -d $meta->dir . '/lib';
134
135 my $class = $meta->class;
136
137 use Module::Load qw( load );
138 load "$class";
139
140 my $plugin = $class->new;
141
142 $self->index->{$id}->{ plugin } = $plugin;
143
144 $self->app->register_plugin( $id, $plugin );
145 }
146  
147 sub register_plugins {
148 my ( $self ) = @_;
149
150 $self->scan;
151
152 for ( $self->plugin_ids ) {
153
154 # register the plugin if it is not already and is not disabled
155 if ( ! $self->index->{$_}->{plugin} && ! $self->index->{$_}->{disabled} ) {
156
157 $self->register_plugin( $_ );
158
159 }
160
161 }
162 }
163  
164  
165  
166  
167  
168 1;