]> git.decadent.org.uk Git - maypole.git/blob - lib/Maypole/Application.pm
3336c1f73cd7d1da68d9fbbb1c6677856e41b302
[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.08';
11
12 sub import {
13     my ( $self, @plugins ) = @_;
14     my $caller = caller(0);
15     no strict 'refs';
16     push @{"${caller}::ISA"}, $self;
17     my $autosetup=0;
18     foreach (sort @plugins) {
19         if    (/^\-Setup$/) { $autosetup++; }
20         elsif (/^\-Debug$/) {
21             *{"$caller\::debug"} = sub { 1 };
22             warn "Debugging enabled";
23         }
24         elsif (/^-.*$/) { warn "Unknown flag: $_" }
25         else {
26             # The plugin caller should be our application class
27             eval "package $caller; require Maypole::Plugin::$_";
28             if ($@) { warn qq(Loading plugin "Maypole::Plugin::$_" failed: $@) }
29             else {
30                 warn "Loaded plugin: Maypole::Plugin::$_" if $caller->debug;
31                 push @{"${caller}::ISA"}, "Maypole::Plugin::$_";
32             }
33         }
34     }
35
36     $caller->config(Maypole::Config->new);
37     $caller->setup() if $autosetup;
38 }
39
40 if ( $ENV{MOD_PERL} ) {
41     Apache::MVC->require or die "Loading Apache frontend failed: $@";
42     push @ISA, 'Apache::MVC';
43 }
44 else {
45     CGI::Maypole->require or die "Loading CGI frontend failed: $@";
46     push @ISA, 'CGI::Maypole';
47 }
48
49 1;
50
51 =head1 NAME
52
53 Maypole::Application - Universal Maypole Frontend
54
55 =head1 SYNOPSIS
56
57     use Maypole::Application;
58
59     use Maypole::Application qw(Config::YAML);
60
61     use Maypole::Application qw(-Debug Config::YAML -Setup);
62
63     use Maypole::Application qw(Config::YAML Loader -Setup -Debug);
64
65 =head1 DESCRIPTION
66
67 This is a universal frontend for mod_perl1, mod_perl2 and CGI.
68
69 You can omit the Maypole::Plugin:: prefix from plugins.
70 So Maypole::Plugin::Config::YAML becomes Config::YAML.
71
72     use Maypole::Application qw(Config::YAML);
73
74 You can also set special flags like -Setup and -Debug.
75
76     use Maypole::Application qw(-Debug Config::YAML -Setup);
77
78 The position of plugins and flags in the chain is important,
79 because they are loaded/executed in the same order they appear.
80
81 =head2 -Setup
82
83     use Maypole::Application qw(-Setup);
84
85 is equivalent to
86
87     use Maypole::Application;
88     MyApp->setup;
89
90 Note that no options are passed to C<setup()>. You must ensure that the
91 required model config parameters are set in C<MyApp-E<gt>config>. See
92 L<Maypole::Config> for more information.
93
94 =head2 -Debug
95
96     use Maypole::Application qw(-Debug);
97
98 is equivalent to
99
100     use Maypole::Application;
101     sub debug { 1 }
102
103 =head1 AUTHOR
104
105 Sebastian Riedel, C<sri@oook.de>
106 Idea by Marcus Ramberg, C<marcus@thefeed.no>
107
108 =head1 LICENSE
109
110 You may distribute this code under the same terms as Perl itself.
111
112 =cut