File Coverage

lib/Kwiki/Config.pm
Criterion Covered Total %
statement 1 3 33.3
branch n/a
condition n/a
subroutine 1 1 100.0
pod n/a
total 2 4 50.0


line stmt bran cond sub pod time code
1             package Kwiki::Config;
2 1     1   4439 use Spoon::Config -Base;
  0            
  0            
3             use mixin 'Kwiki::Installer';
4              
5             const class_id => 'config';
6             const class_title => 'Kwiki Configuration';
7             const config_file => 'config.yaml';
8             field script_name => '';
9             const default_path => [ 'config' ];
10             field path => [];
11             field plugins_file => '';
12              
13             sub init {
14             $self->add_path(@{$self->default_path});
15             $self->add_file($self->config_file);
16             }
17              
18             sub paired_arguments { qw(-plugins) }
19             sub new {
20             my ($args, @configs) = $self->parse_arguments(@_);
21             $self = super(@configs);
22             if (my $plugins_file = $args->{-plugins}) {
23             $self->add_plugins_file($plugins_file);
24             $self->plugins_file($plugins_file);
25             }
26             return $self;
27             }
28              
29             sub add_plugins_file {
30             my $plugins_file = shift;
31             return unless -f $plugins_file;
32             $self->add_config(
33             {
34             plugin_classes => [ $self->read_plugins($plugins_file) ],
35             }
36             );
37             }
38              
39             sub read_plugins {
40             my $plugins_file = io(shift);
41             my @plugins = grep {
42             s/^([\+\-]?[\w\:]+)\s*$/$1/;
43             } $plugins_file->slurp;
44             return @plugins unless grep /^[\+\-]/, @plugins or not @plugins;
45             my $filename = $plugins_file->filename;
46             die "Can't create plugins list"
47             unless -e "../$filename";
48             my $updir = io->updir->chdir;
49             my @parent_plugins = $self->read_plugins($filename);
50             for (@plugins) {
51             my $remove = $_;
52             $remove =~ s/^\-// or next;
53             @parent_plugins = grep {$_ ne $remove} @parent_plugins;
54             }
55             my %have;
56             @have{@parent_plugins} = ('1') x @parent_plugins;
57             return @parent_plugins, grep {
58             not /^\-/ and do {
59             s/^\+//;
60             not $have{$_};
61             }
62             } @plugins;
63             }
64              
65             sub default_classes {
66             (
67             cgi_class => 'Kwiki::CGI',
68             command_class => 'Kwiki::Command',
69             config_class => 'Kwiki::Config',
70             cookie_class => 'Kwiki::Cookie',
71             css_class => 'Kwiki::CSS',
72             files_class => 'Kwiki::Files',
73             formatter_class => 'Kwiki::Formatter',
74             headers_class => 'Spoon::Headers',
75             hooks_class => 'Spoon::Hooks',
76             hub_class => 'Kwiki::Hub',
77             javascript_class => 'Kwiki::Javascript',
78             pages_class => 'Kwiki::Pages',
79             preferences_class => 'Kwiki::Preferences',
80             registry_class => 'Kwiki::Registry',
81             template_class => 'Kwiki::Template::TT2',
82             users_class => 'Kwiki::Users',
83             )
84             }
85              
86             sub add_plugin {
87             push @{$self->plugin_classes}, shift;
88             }
89              
90             sub change_plugin {
91             my ($new_class, $old_class) = @_;
92             my $pattern = $old_class || do {
93             my $temp = $new_class;
94             $temp =~ s/^\w+:://;
95             '\w+::' . $temp;
96             };
97             my $plugins = $self->plugin_classes;
98             for (@$plugins) {
99             last if s/$pattern/$new_class/;
100             }
101             }
102              
103             sub add_file {
104             my $file = shift
105             or return;
106             my $file_path = '';
107             for (@{$self->path}) {
108             $file_path = "$_/$file", last
109             if -f "$_/$file";
110             }
111             return unless $file_path;
112             my $hash = $self->hash_from_file($file_path);
113             for my $key (keys %$hash) {
114             next if defined $self->{$key};
115             field $key;
116             $self->{$key} = $hash->{$key};
117             }
118             }
119              
120             sub add_path {
121             splice @{$self->path}, 0, 0, @_;
122             }
123              
124             sub get_packed_files {
125             my @return;
126             my @packed = super;
127             while (my ($name, $content) = splice(@packed, 0, 2)) {
128             if ($name =~ /^(plugins|config\.yaml)$/) {
129             next if -f $name;
130             }
131             push @return, $name, $content;
132             }
133             @return;
134             }
135              
136             __DATA__