gitprep / mojo / lib / Mojo.pm /
Newer Older
188 lines | 4.204kb
copy gitweblite soruce code
root authored on 2012-11-23
1
package Mojo;
2
use Mojo::Base -base;
3

            
upgraded Mojolicious to v3.7...
Yuki Kimoto authored on 2013-01-28
4
# "Professor: These old Doomsday devices are dangerously unstable. I'll rest
5
#             easier not knowing where they are."
copy gitweblite soruce code
root authored on 2012-11-23
6
use Carp 'croak';
7
use Mojo::Home;
8
use Mojo::Log;
9
use Mojo::Transaction::HTTP;
10
use Mojo::UserAgent;
11
use Scalar::Util 'weaken';
12

            
13
has home => sub { Mojo::Home->new };
14
has log  => sub { Mojo::Log->new };
15
has ua   => sub {
16
  my $self = shift;
17

            
update Mojolicious to 4.57
Yuki Kimoto authored on 2013-12-02
18
  my $ua = Mojo::UserAgent->new;
19
  weaken $ua->server->app($self)->{app};
copy gitweblite soruce code
root authored on 2012-11-23
20
  weaken $self;
update Mojolicious and added...
Yuki Kimoto authored on 2013-03-20
21
  $ua->on(error => sub { $self->log->error($_[1]) });
copy gitweblite soruce code
root authored on 2012-11-23
22

            
23
  return $ua;
24
};
25

            
26
sub new {
27
  my $self = shift->SUPER::new(@_);
28

            
update Mojolicious and added...
Yuki Kimoto authored on 2013-03-20
29
  # Check if we have a log directory
update Mojolicious to 4.57
Yuki Kimoto authored on 2013-12-02
30
  my $home = $self->home;
31
  $home->detect(ref $self) unless @{$home->parts};
copy gitweblite soruce code
root authored on 2012-11-23
32
  $self->log->path($home->rel_file('log/mojo.log'))
33
    if -w $home->rel_file('log');
34

            
35
  return $self;
36
}
37

            
38
sub build_tx { Mojo::Transaction::HTTP->new }
39

            
40
sub config { shift->_dict(config => @_) }
41

            
42
sub handler { croak 'Method "handler" not implemented in subclass' }
43

            
44
sub _dict {
45
  my ($self, $name) = (shift, shift);
46

            
47
  # Hash
48
  my $dict = $self->{$name} ||= {};
49
  return $dict unless @_;
50

            
51
  # Get
52
  return $dict->{$_[0]} unless @_ > 1 || ref $_[0];
53

            
54
  # Set
55
  %$dict = (%$dict, %{ref $_[0] ? $_[0] : {@_}});
56

            
57
  return $self;
58
}
59

            
60
1;
61

            
update Mojolicious to 4.57
Yuki Kimoto authored on 2013-12-02
62
=encoding utf8
63

            
copy gitweblite soruce code
root authored on 2012-11-23
64
=head1 NAME
65

            
66
Mojo - Duct tape for the HTML5 web!
67

            
68
=head1 SYNOPSIS
69

            
upgraded Mojolicious to v3.7...
Yuki Kimoto authored on 2013-01-28
70
  package MyApp;
copy gitweblite soruce code
root authored on 2012-11-23
71
  use Mojo::Base 'Mojo';
72

            
73
  # All the complexities of CGI, PSGI, HTTP and WebSockets get reduced to a
74
  # single method call!
75
  sub handler {
76
    my ($self, $tx) = @_;
77

            
78
    # Request
79
    my $method = $tx->req->method;
80
    my $path   = $tx->req->url->path;
81

            
82
    # Response
83
    $tx->res->code(200);
84
    $tx->res->headers->content_type('text/plain');
85
    $tx->res->body("$method request for $path!");
86

            
87
    # Resume transaction
88
    $tx->resume;
89
  }
90

            
91
=head1 DESCRIPTION
92

            
93
Mojo provides a flexible runtime environment for Perl real-time web
94
frameworks. It provides all the basic tools and helpers needed to write
upgraded Mojolicious to v3.7...
Yuki Kimoto authored on 2013-01-28
95
simple web applications and higher level web frameworks, such as
copy gitweblite soruce code
root authored on 2012-11-23
96
L<Mojolicious>.
97

            
update Mojolicious to 4.57
Yuki Kimoto authored on 2013-12-02
98
See L<Mojolicious::Guides> for more!
copy gitweblite soruce code
root authored on 2012-11-23
99

            
100
=head1 ATTRIBUTES
101

            
102
L<Mojo> implements the following attributes.
103

            
update Mojolicious and added...
Yuki Kimoto authored on 2013-03-20
104
=head2 home
copy gitweblite soruce code
root authored on 2012-11-23
105

            
106
  my $home = $app->home;
107
  $app     = $app->home(Mojo::Home->new);
108

            
109
The home directory of your application, defaults to a L<Mojo::Home> object
110
which stringifies to the actual path.
111

            
112
  # Generate portable path relative to home directory
113
  my $path = $app->home->rel_file('data/important.txt');
114

            
update Mojolicious and added...
Yuki Kimoto authored on 2013-03-20
115
=head2 log
copy gitweblite soruce code
root authored on 2012-11-23
116

            
117
  my $log = $app->log;
118
  $app    = $app->log(Mojo::Log->new);
119

            
120
The logging layer of your application, defaults to a L<Mojo::Log> object.
121

            
122
  # Log debug message
123
  $app->log->debug('It works!');
124

            
update Mojolicious and added...
Yuki Kimoto authored on 2013-03-20
125
=head2 ua
copy gitweblite soruce code
root authored on 2012-11-23
126

            
127
  my $ua = $app->ua;
128
  $app   = $app->ua(Mojo::UserAgent->new);
129

            
upgraded Mojolicious to v3.7...
Yuki Kimoto authored on 2013-01-28
130
A full featured HTTP user agent for use in your applications, defaults to a
131
L<Mojo::UserAgent> object. Note that this user agent should not be used in
132
plugins, since non-blocking requests that are already in progress will
133
interfere with new blocking ones.
copy gitweblite soruce code
root authored on 2012-11-23
134

            
135
  # Perform blocking request
update Mojolicious to 4.57
Yuki Kimoto authored on 2013-12-02
136
  say $app->ua->get('example.com')->res->body;
copy gitweblite soruce code
root authored on 2012-11-23
137

            
138
=head1 METHODS
139

            
140
L<Mojo> inherits all methods from L<Mojo::Base> and implements the following
141
new ones.
142

            
update Mojolicious and added...
Yuki Kimoto authored on 2013-03-20
143
=head2 new
copy gitweblite soruce code
root authored on 2012-11-23
144

            
145
  my $app = Mojo->new;
146

            
147
Construct a new L<Mojo> application. Will automatically detect your home
update Mojolicious to 4.57
Yuki Kimoto authored on 2013-12-02
148
directory if necessary and set up logging to C<log/mojo.log> if there's a
149
C<log> directory.
copy gitweblite soruce code
root authored on 2012-11-23
150

            
update Mojolicious and added...
Yuki Kimoto authored on 2013-03-20
151
=head2 build_tx
copy gitweblite soruce code
root authored on 2012-11-23
152

            
153
  my $tx = $app->build_tx;
154

            
155
Transaction builder, defaults to building a L<Mojo::Transaction::HTTP>
156
object.
157

            
update Mojolicious and added...
Yuki Kimoto authored on 2013-03-20
158
=head2 config
copy gitweblite soruce code
root authored on 2012-11-23
159

            
update Mojolicious 4.07
Yuki Kimoto authored on 2013-06-03
160
  my $hash = $app->config;
161
  my $foo  = $app->config('foo');
162
  $app     = $app->config({foo => 'bar'});
163
  $app     = $app->config(foo => 'bar');
copy gitweblite soruce code
root authored on 2012-11-23
164

            
165
Application configuration.
166

            
update Mojolicious 4.07
Yuki Kimoto authored on 2013-06-03
167
  # Remove value
168
  my $foo = delete $app->config->{foo};
copy gitweblite soruce code
root authored on 2012-11-23
169

            
update Mojolicious and added...
Yuki Kimoto authored on 2013-03-20
170
=head2 handler
copy gitweblite soruce code
root authored on 2012-11-23
171

            
upgraded Mojolicious to v3.7...
Yuki Kimoto authored on 2013-01-28
172
  $app->handler(Mojo::Transaction::HTTP->new);
copy gitweblite soruce code
root authored on 2012-11-23
173

            
174
The handler is the main entry point to your application or framework and will
175
be called for each new transaction, which will usually be a
176
L<Mojo::Transaction::HTTP> or L<Mojo::Transaction::WebSocket> object. Meant to
177
be overloaded in a subclass.
178

            
179
  sub handler {
180
    my ($self, $tx) = @_;
181
    ...
182
  }
183

            
184
=head1 SEE ALSO
185

            
186
L<Mojolicious>, L<Mojolicious::Guides>, L<http://mojolicio.us>.
187

            
188
=cut