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