File Coverage

blib/lib/Mojolicious/Lite.pm
Criterion Covered Total %
statement 44 44 100.0
branch 3 4 75.0
condition 2 3 66.6
subroutine 34 36 94.4
pod n/a
total 83 87 95.4


line stmt bran cond sub pod time code
1             package Mojolicious::Lite;
2 43     124   200121 use Mojo::Base 'Mojolicious';
  43         124  
  43         286  
3              
4             # "Bender: Bite my shiny metal ass!"
5 43     124   333 use Mojo::File qw(path);
  43         128  
  43         2067  
6 43     124   317 use Mojo::UserAgent::Server;
  43         103  
  43         395  
7 43     124   265 use Mojo::Util qw(monkey_patch);
  43         132  
  43         5419  
8              
9             sub import {
10              
11             # Remember executable for later
12 57   66 138   1342 $ENV{MOJO_EXE} ||= (caller)[1];
13              
14             # Reuse home directory if possible
15 57 50       432 local $ENV{MOJO_HOME} = path($ENV{MOJO_EXE})->dirname->to_string unless $ENV{MOJO_HOME};
16              
17             # Initialize application class
18 57         253 my $caller = caller;
19 43     124   385 no strict 'refs';
  43         143  
  43         28141  
20 57         135 push @{"${caller}::ISA"}, 'Mojolicious';
  57         1019  
21              
22             # Generate moniker based on filename
23 57         256 my $moniker = path($ENV{MOJO_EXE})->basename('.pl', '.pm', '.t');
24 57         635 my $app = shift->new(moniker => $moniker);
25              
26             # Initialize routes without namespaces
27 57         339 my $routes = $app->routes->namespaces([]);
28 57         209 $app->static->classes->[0] = $app->renderer->classes->[0] = $caller;
29              
30             # The Mojolicious::Lite DSL
31 57         146 my $root = $routes;
32 57         156 for my $name (qw(any get options patch post put websocket)) {
33 396     477   10397 monkey_patch $caller, $name, sub { $routes->$name(@_) }
        396      
        396      
        477      
        558      
        639      
        720      
        801      
        28      
        18      
        18      
        16      
        16      
        0      
        0      
34 399         1561 }
35 57     671   354 monkey_patch($caller, $_, sub {$app}) for qw(new app);
  185     671   48903  
        629      
36 57     404   343 monkey_patch $caller, del => sub { $routes->delete(@_) };
  2     323   10  
37             monkey_patch $caller, group => sub (&) {
38 3     243   16 (my $old, $root) = ($root, $routes);
        162      
39 3         10 shift->();
40 3         12 ($routes, $root) = ($root, $old);
41 57         354 };
42             monkey_patch $caller,
43 19     97   822 helper => sub { $app->helper(@_) },
        97      
44 26     75   616 hook => sub { $app->hook(@_) },
        36      
45 41     53   17716 plugin => sub { $app->plugin(@_) },
        53      
46 57     45   543 under => sub { $routes = $root->under(@_) };
  17     45   103  
47              
48             # Make sure there's a default application for testing
49 57 100       648 Mojo::UserAgent::Server->app($app) unless Mojo::UserAgent::Server->app;
50              
51             # Lite apps are strict!
52 57         192 unshift @_, 'Mojo::Base', '-strict';
53 57         533 goto &Mojo::Base::import;
54             }
55              
56             1;
57              
58             =encoding utf8
59              
60             =head1 NAME
61              
62             Mojolicious::Lite - Micro real-time web framework
63              
64             =head1 SYNOPSIS
65              
66             # Automatically enables "strict", "warnings", "utf8" and Perl 5.16 features
67             use Mojolicious::Lite -signatures;
68              
69             # Route with placeholder
70             get '/:foo' => sub ($c) {
71             my $foo = $c->param('foo');
72             $c->render(text => "Hello from $foo.");
73             };
74              
75             # Start the Mojolicious command system
76             app->start;
77              
78             =head1 DESCRIPTION
79              
80             L is a tiny domain specific language built around L, made up of only about a dozen Perl
81             functions.
82              
83             On Perl 5.20+ you can also use a C<-signatures> flag to enable support for L
84             signatures|perlsub/"Signatures">.
85              
86             use Mojolicious::Lite -signatures;
87              
88             get '/:foo' => sub ($c) {
89             my $foo = $c->param('foo');
90             $c->render(text => "Hello from $foo.");
91             };
92              
93             app->start;
94              
95             See L for more!
96              
97             =head1 GROWING
98              
99             While L will give you a detailed introduction to growing a L prototype
100             into a well-structured L application, here we have collected a few snippets that illustrate very well just
101             how similar both of them are.
102              
103             =head2 Routes
104              
105             The functions L, L and friends all have equivalent methods.
106              
107             # Mojolicious::Lite
108             get '/foo' => sub ($c) {
109             $c->render(text => 'Hello World!');
110             };
111              
112             # Mojolicious
113             sub startup ($self) {
114            
115             my $routes = $self->routes;
116             $routes->get('/foo' => sub ($c) {
117             $c->render(text => 'Hello World!');
118             });
119             }
120              
121             =head2 Application
122              
123             The application object you can access with the function L is the first argument passed to the C
124             method.
125              
126             # Mojolicious::Lite
127             app->max_request_size(16777216);
128              
129             # Mojolicious
130             sub startup ($self) {
131             $self->max_request_size(16777216);
132             }
133              
134             =head2 Plugins
135              
136             Instead of the L function you just use the method L.
137              
138             # Mojolicious::Lite
139             plugin 'Config';
140              
141             # Mojolicious
142             sub startup ($self) {
143             $self->plugin('Config');
144             }
145              
146             =head2 Helpers
147              
148             Similar to plugins, instead of the L function you just use the method L.
149              
150             # Mojolicious::Lite
151             helper two => sub ($c) {
152             return 1 + 1;
153             };
154              
155             # Mojolicious
156             sub startup ($self) {
157             $self->helper(two => sub ($c) {
158             return 1 + 1;
159             });
160             }
161              
162             =head2 Under
163              
164             Instead of sequential function calls, we can use methods to build a tree with nested routes, that much better
165             illustrates how routes work internally.
166              
167             # Mojolicious::Lite
168             under '/foo';
169             get '/bar' => sub ($c) {...};
170              
171             # Mojolicious
172             sub startup ($self) {
173              
174             my $routes = $self->routes;
175             my $foo = $routes->under('/foo');
176             $foo->get('/bar' => sub ($c) {...});
177             }
178              
179             =head1 FUNCTIONS
180              
181             L implements the following functions, which are automatically exported.
182              
183             =head2 any
184              
185             my $route = any '/:foo' => sub ($c) {...};
186             my $route = any '/:foo' => sub ($c) {...} => 'name';
187             my $route = any '/:foo' => {foo => 'bar'} => sub ($c) {...};
188             my $route = any '/:foo' => [foo => qr/\w+/] => sub ($c) {...};
189             my $route = any ['GET', 'POST'] => '/:foo' => sub ($c) {...};
190             my $route = any ['GET', 'POST'] => '/:foo' => [foo => qr/\w+/] => sub ($c) {...};
191             my $route = any ['GET', 'POST'] => '/:foo' => (agent => qr/Firefox/) => sub ($c) {...};
192              
193             Generate route with L, matching any of the listed HTTP request methods or all. See
194             L and L for more information.
195              
196             =head2 app
197              
198             my $app = app;
199              
200             Returns the L application object, which is a subclass of L.
201              
202             # Use all the available attributes and methods
203             app->log->level('error');
204             app->defaults(foo => 'bar');
205              
206             =head2 del
207              
208             my $route = del '/:foo' => sub ($c) {...};
209             my $route = del '/:foo' => sub ($c) {...} => 'name';
210             my $route = del '/:foo' => {foo => 'bar'} => sub ($c) {...};
211             my $route = del '/:foo' => [foo => qr/\w+/] => sub ($c) {...};
212             my $route = del '/:foo' => (agent => qr/Firefox/) => sub ($c) {...};
213              
214             Generate route with L, matching only C requests. See
215             L and L for more information.
216              
217             =head2 get
218              
219             my $route = get '/:foo' => sub ($c) {...};
220             my $route = get '/:foo' => sub ($c) {...} => 'name';
221             my $route = get '/:foo' => {foo => 'bar'} => sub ($c) {...};
222             my $route = get '/:foo' => [foo => qr/\w+/] => sub ($c) {...};
223             my $route = get '/:foo' => (agent => qr/Firefox/) => sub ($c) {...};
224              
225             Generate route with L, matching only C requests. See
226             L and L for more information.
227              
228             =head2 group
229              
230             group {...};
231              
232             Start a new route group.
233              
234             =head2 helper
235              
236             helper foo => sub ($c, @args) {...};
237              
238             Add a new helper with L.
239              
240             =head2 hook
241              
242             hook after_dispatch => sub ($c) {...};
243              
244             Share code with L.
245              
246             =head2 options
247              
248             my $route = options '/:foo' => sub ($c) {...};
249             my $route = options '/:foo' => sub ($c) {...} => 'name';
250             my $route = options '/:foo' => {foo => 'bar'} => sub ($c) {...};
251             my $route = options '/:foo' => [foo => qr/\w+/] => sub ($c) {...};
252             my $route = options '/:foo' => (agent => qr/Firefox/) => sub ($c) {...};
253              
254             Generate route with L, matching only C requests. See
255             L and L for more information.
256              
257             =head2 patch
258              
259             my $route = patch '/:foo' => sub ($c) {...};
260             my $route = patch '/:foo' => sub ($c) {...} => 'name';
261             my $route = patch '/:foo' => {foo => 'bar'} => sub ($c) {...};
262             my $route = patch '/:foo' => [foo => qr/\w+/] => sub ($c) {...};
263             my $route = patch '/:foo' => (agent => qr/Firefox/) => sub ($c) {...};
264              
265             Generate route with L, matching only C requests. See
266             L and L for more information.
267              
268             =head2 plugin
269              
270             plugin SomePlugin => {foo => 23};
271              
272             Load a plugin with L.
273              
274             =head2 post
275              
276             my $route = post '/:foo' => sub ($c) {...};
277             my $route = post '/:foo' => sub ($c) {...} => 'name';
278             my $route = post '/:foo' => {foo => 'bar'} => sub ($c) {...};
279             my $route = post '/:foo' => [foo => qr/\w+/] => sub ($c) {...};
280             my $route = post '/:foo' => (agent => qr/Firefox/) => sub ($c) {...};
281              
282             Generate route with L, matching only C requests. See
283             L and L for more information.
284              
285             =head2 put
286              
287             my $route = put '/:foo' => sub ($c) {...};
288             my $route = put '/:foo' => sub ($c) {...} => 'name';
289             my $route = put '/:foo' => {foo => 'bar'} => sub ($c) {...};
290             my $route = put '/:foo' => [foo => qr/\w+/] => sub ($c) {...};
291             my $route = put '/:foo' => (agent => qr/Firefox/) => sub ($c) {...};
292              
293             Generate route with L, matching only C requests. See
294             L and L for more information.
295              
296             =head2 under
297              
298             my $route = under sub ($c) {...};
299             my $route = under '/:foo' => sub ($c) {...};
300             my $route = under '/:foo' => {foo => 'bar'};
301             my $route = under '/:foo' => [foo => qr/\w+/];
302             my $route = under '/:foo' => (agent => qr/Firefox/);
303             my $route = under [format => ['json', 'yaml']];
304              
305             Generate nested route with L, to which all following routes are automatically
306             appended. See L and L for more information.
307              
308             =head2 websocket
309              
310             my $route = websocket '/:foo' => sub ($c) {...};
311             my $route = websocket '/:foo' => sub ($c) {...} => 'name';
312             my $route = websocket '/:foo' => {foo => 'bar'} => sub ($c) {...};
313             my $route = websocket '/:foo' => [foo => qr/\w+/] => sub ($c) {...};
314             my $route = websocket '/:foo' => (agent => qr/Firefox/) => sub ($c) {...};
315              
316             Generate route with L, matching only WebSocket handshakes. See
317             L and L for more information.
318              
319             =head1 ATTRIBUTES
320              
321             L inherits all attributes from L.
322              
323             =head1 METHODS
324              
325             L inherits all methods from L.
326              
327             =head1 SEE ALSO
328              
329             L, L, L.
330              
331             =cut