root/lang/perl/Catalyst-Plugin-ConfigLoader-Multi/trunk/lib/Catalyst/Plugin/ConfigLoader/Multi.pm @ 9013

Revision 9013, 3.3 kB (checked in by tomyhero, 6 years ago)

lang/perl/Catalyst-ConfigLoader?-Multi : support $ENV{MYAPP_CONFIG_MULTI}

Line 
1package Catalyst::Plugin::ConfigLoader::Multi;
2
3use strict;
4use warnings;
5use base qw/Catalyst::Plugin::ConfigLoader/;
6use Config::Any;
7use Catalyst::Utils;
8use DirHandle;
9use NEXT;
10
11our $VERSION = 0.05;
12
13sub find_files {
14    my $c = shift;
15
16    my @files    = $c->SUPER::find_files();
17    my @my_files = $c->_find_my_files();
18
19    return ( @files, @my_files );
20}
21
22sub _find_my_files {
23    my $c = shift;
24    my ( $path_prefix, $extension ) = $c->get_config_path();
25
26    return [] if $extension;
27
28    my $prefix = Catalyst::Utils::appprefix( ref $c || $c );
29    my $path = $path_prefix;
30    $path =~ s{\/$prefix$}{};
31
32    my @extensions = @{ Config::Any->extensions };
33    my $suffix     = $c->get_config_local_suffix;
34    my @my_files   = ();
35    my $dh         = DirHandle->new($path);
36
37    my $config_key_name = uc($prefix) . '_CONFIG_MULTI';
38
39
40    while ( my $file = $dh->read() ) {
41        next unless $file =~ /^$prefix\_(.+)\.\w+$/ && $suffix ne $1;
42
43
44        if ( $ENV{ $config_key_name } ) {
45            push @my_files, (
46                map {
47                    (   "${path_prefix}_${1}.$_",)
48                    } @extensions
49            );
50        }
51        else {
52            push @my_files, (
53                map {
54                    (   "${path_prefix}_${1}.$_",
55                        "${path_prefix}_${1}_${suffix}.$_"
56                        )
57                    } @extensions
58            );
59        }
60    }
61
62    push @my_files ,  $ENV{ $config_key_name } if $ENV{ $config_key_name };
63
64    return @my_files;
65}
66
671;
68
69=head1 NAME
70
71Catalyst::Plugin::ConfigLoader::Multi - Catalyst Plugin for Multiple ConfigLoader
72
73=head1 SYNOPSIS
74
75    package MyApp;
76   
77    use strict;
78    use warnings;
79   
80    use Catalyst::Runtime '5.70';
81   
82   
83    use Catalyst qw/-Debug ConfigLoader::Multi/;
84   
85    our $VERSION = '0.01';
86   
87    __PACKAGE__->config( name => 'MyApp' );
88    # Do not forget add this.
89    __PACKAGE__->config( 'Plugin::ConfigLoader' => { file => __PACKAGE__->path_to('conf')  } );
90   
91    __PACKAGE__->setup;
92   
93    1;
94
95Your directory
96
97    %tree
98    /Users/tomyhero/work/MyApp/
99    |-- Changes
100    |-- Makefile.PL
101    |-- README
102    |-- conf
103    |   |-- my_app_local.yml
104    |   |-- myapp.yml
105    |   |-- myapp_bar.yml
106    |   `-- myapp_foo.yml
107    |-- lib
108    |   |-- MyApp
109    |   |   |-- Controller
110    |   |   |   `-- Root.pm
111    |   |   |-- Model
112    |   |   `-- View
113    |   `-- MyApp.pm
114    |-- root
115    |-- script
116    `-- t
117
118=head1 DESCRIPTION
119
120When a project is getting bigger and bigger , it is hard to organize a config
121file. So we create this plugin which divide a config file to multiple. Config
122files name must start your project prefix. such as 'myapp_' .
123
124 __PACKAGE__->config( 'Plugin::ConfigLoader' => { file => __PACKAGE__->path_to('conf')  } );
125
126Do not forget add this code to your MyApp.pm
127
128=head1 METHOD
129
130=head2 find_files
131
132override from ConfigLoader
133
134=head1 ENV
135
136you can specify local file with this setting.
137
138 $ENV{MYAPP_CONFIG_MULTI} = '/tmp/foo/your_own_local.yaml'
139
140=head1 SEE ALSO
141
142L<Catalyst::Plugin::ConfigLoader>
143
144=head1 AUTHOR
145
146Masahiro Funakoshi <masap@cpan.org>
147
148Yu-suke Amano
149
150Tomohiro Teranishi <tomohiro.teranishi@gmail.com>
151
152=head1 COPYRIGHT
153
154This library is free software; you can redistribute it and/or modify
155it under the same terms as Perl itself.
156
157=cut
Note: See TracBrowser for help on using the browser.