]> git.decadent.org.uk Git - maypole.git/blob - lib/Maypole/Application.pm
tidied up inheritance in Mp::App - bugs 12923 & 14120
[maypole.git] / lib / Maypole / Application.pm
1 package Maypole::Application;
2
3 use strict;
4 use warnings;
5 use UNIVERSAL::require;
6 use Maypole;
7 use Maypole::Config;
8
9 our $VERSION = '2.11';
10
11 sub import {
12     shift; # not interested in this - we manipulate the caller's @ISA directly
13     my @plugins = @_;
14     my $caller = caller(0);
15     
16     my $frontend = 'Apache::MVC' if $ENV{MOD_PERL};
17     
18     my $masonx;
19     if ( grep { /^MasonX$/ } @plugins )
20     {
21         $masonx++;
22         @plugins = grep { ! /^MasonX$/ } @plugins;
23         $frontend = 'MasonX::Maypole';
24     }
25     
26     $frontend ||= 'CGI::Maypole';
27     
28     $frontend->require or die "Loading $frontend frontend failed: $@";
29
30     my $autosetup=0;
31     my $autoinit=0;
32     my @plugin_modules;
33     {
34         foreach (@plugins) {
35             if    (/^\-Setup$/) { $autosetup++; }
36             elsif (/^\-Init$/)  { $autoinit++ }
37             elsif (/^\-Debug(\d*)$/) {
38                 my $d = $1 || 1;
39                 no strict 'refs';
40                 *{"$caller\::debug"} = sub { $d };
41                 warn "Debugging (level $d) enabled for $caller";
42             }
43             elsif (/^-.*$/) { warn "Unknown flag: $_" }
44             else {
45                 my $plugin = "Maypole::Plugin::$_";
46                 if ($plugin->require) {
47                     push @plugin_modules, "Maypole::Plugin::$_";
48                     warn "Loaded plugin: $plugin for $caller"
49                         if $caller->can('debug') && $caller->debug;
50                 } else {
51                     die qq(Loading plugin "$plugin" for $caller failed: )
52                         . $UNIVERSAL::require::ERROR;
53                 }
54             }
55         }
56     }
57     no strict 'refs';
58     push @{"${caller}::ISA"}, @plugin_modules, $frontend;
59     $caller->config(Maypole::Config->new);
60     $caller->config->masonx({}) if $masonx;
61     $caller->setup() if $autosetup;
62     $caller->init() if $autosetup && $autoinit;
63 }
64
65 1;
66
67 =head1 NAME
68
69 Maypole::Application - Universal Maypole Frontend
70
71 =head1 SYNOPSIS
72
73     use Maypole::Application;
74
75     use Maypole::Application qw(Config::YAML);
76
77     use Maypole::Application qw(-Debug Config::YAML -Setup);
78
79     use Maypole::Application qw(Config::YAML Loader -Setup -Debug);
80
81     use Maypole::Application qw(-Debug2 MasonX AutoUntaint);
82
83 =head1 DESCRIPTION
84
85 This is a universal frontend for mod_perl1, mod_perl2, HTML::Mason and CGI.
86
87 You can omit the Maypole::Plugin:: prefix from plugins.
88 So Maypole::Plugin::Config::YAML becomes Config::YAML.
89
90     use Maypole::Application qw(Config::YAML);
91
92 You can also set special flags like -Setup, -Debug and -Init.
93
94     use Maypole::Application qw(-Debug Config::YAML -Setup);
95
96 The position of plugins and flags in the chain is important,
97 because they are loaded/executed in the same order they appear.
98
99 =head2 -Setup
100
101     use Maypole::Application qw(-Setup);
102
103 is equivalent to
104
105     use Maypole::Application;
106     MyApp->setup;
107
108 Note that no options are passed to C<setup()>. You must ensure that the
109 required model config parameters are set in C<MyApp-E<gt>config>. See
110 L<Maypole::Config> for more information.
111
112 =head2 -Init
113
114     use Maypole::Application qw(-Setup -Init);
115     
116 is equivalent to
117
118     use Maypole::Application;
119     MyApp->setup;
120     MyApp->init;
121     
122 Note that the C<-Setup> flag is required for the C<-Init> flag to work.
123
124 In persistent environments (e.g. C<mod_perl>), it is useful to call C<init> 
125 once in the parent server, rather than at the beginning of the first request
126 to each child server, in order to share the view code loaded during C<init>. 
127 Note that you must supply all the config data to your app before calling 
128 C<setup> and C<init>, probably by using one of the C<Maypole::Plugin::Config::*> 
129 plugins.
130
131 =head2 -Debug
132
133     use Maypole::Application qw(-Debug);
134
135 is equivalent to
136
137     use Maypole::Application;
138     sub debug { 1 }
139
140 You can specify a higher debug level by saying C<-Debug2> etc. 
141
142 =head1 AUTHOR
143
144 Sebastian Riedel, C<sri@oook.de>
145 Idea by Marcus Ramberg, C<marcus@thefeed.no>
146
147 =head1 LICENSE
148
149 You may distribute this code under the same terms as Perl itself.
150
151 =cut