]> git.decadent.org.uk Git - maypole.git/blob - lib/Maypole/Application.pm
f05379d87c8ab8035118aac9ff6fb8dc44b86d62
[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 @ISA;
10 our $VERSION = '2.11';
11
12 sub import {
13     my ( $class, @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     # inheritance may already be set up in a multi-app mod_perl environment
30     push @ISA, $frontend unless __PACKAGE__->isa( $frontend );
31
32     my $autosetup=0;
33     my @plugin_modules;
34     {
35         foreach (@plugins) {
36             if    (/^\-Setup$/) { $autosetup++; }
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, $class;
59     $caller->config(Maypole::Config->new);
60     $caller->config->masonx({}) if $masonx;
61     $caller->setup() if $autosetup;
62 }
63
64 1;
65
66 =head1 NAME
67
68 Maypole::Application - Universal Maypole Frontend
69
70 =head1 SYNOPSIS
71
72     use Maypole::Application;
73
74     use Maypole::Application qw(Config::YAML);
75
76     use Maypole::Application qw(-Debug Config::YAML -Setup);
77
78     use Maypole::Application qw(Config::YAML Loader -Setup -Debug);
79
80     use Maypole::Application qw(-Debug2 MasonX AutoUntaint);
81
82 =head1 DESCRIPTION
83
84 This is a universal frontend for mod_perl1, mod_perl2, HTML::Mason and CGI.
85
86 You can omit the Maypole::Plugin:: prefix from plugins.
87 So Maypole::Plugin::Config::YAML becomes Config::YAML.
88
89     use Maypole::Application qw(Config::YAML);
90
91 You can also set special flags like -Setup and -Debug.
92
93     use Maypole::Application qw(-Debug Config::YAML -Setup);
94
95 The position of plugins and flags in the chain is important,
96 because they are loaded/executed in the same order they appear.
97
98 =head2 -Setup
99
100     use Maypole::Application qw(-Setup);
101
102 is equivalent to
103
104     use Maypole::Application;
105     MyApp->setup;
106
107 Note that no options are passed to C<setup()>. You must ensure that the
108 required model config parameters are set in C<MyApp-E<gt>config>. See
109 L<Maypole::Config> for more information.
110
111 =head2 -Debug
112
113     use Maypole::Application qw(-Debug);
114
115 is equivalent to
116
117     use Maypole::Application;
118     sub debug { 1 }
119
120 You can specify a higher debug level by saying C<-Debug2> etc. 
121
122 =head1 AUTHOR
123
124 Sebastian Riedel, C<sri@oook.de>
125 Idea by Marcus Ramberg, C<marcus@thefeed.no>
126
127 =head1 LICENSE
128
129 You may distribute this code under the same terms as Perl itself.
130
131 =cut