Jifty.pm

filter: Statementstmt 23.0% 32 / 139 Branchbran 3.8% 2 / 52 Conditioncond 0.0% 0 / 18 MC/DCmcdc 0.0% 0 / 11 Subroutinesub 44.0% 11 / 25 Podpod 100% 15 / 15 totaltotal 23.0% 60 / 260 CC 34 SCAR 38.9
CC
34
Cov
16%
new
182.0
setup_database_connection
42.0
config
12.0
CRAP 48.7
« Jifty/Test/WWW/Mechanize.pm ↑ Summary Jifty/Test.pm »
1 113 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
2 113 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 113 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 package Jifty;
6 113 ▶use Data::UUID;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 113 ▶use Class::Trigger;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 113 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
10 # Work around the fact that Time::Local caches TZ on first require
11 113 local $ENV{'TZ'} = "GMT";
12 113 require Time::Local;
13  
14 # Declare early to make sure Jifty::Record::schema_version works
15 113 $Jifty::VERSION = '1.50430';
16 }
17  
18 =head1 NAME
19  
20 Jifty - an application framework
21  
22 =head1 SYNOPSIS
23  
24 # Object containing lots of web related goodies...
25 my $web = Jifty->web;
26 my $request = Jifty->web->request;
27 my $response = Jifty->web->response;
28 my $link = Jifty->web->link( label => _('W00t'), url => '/whatsit' );
29  
30 # Retrieve information from your application's etc/config.yml file.
31 my $config = Jifty->config;
32  
33 # Retrieve the Jifty::DBI handle
34 my $handle = Jifty->handle;
35  
36 # Load an application class, very handy in plugins
37 my $class = Jifty->app_class('Model', 'Foo');
38 my $foo = $class->new;
39 $foo->create( frobnicate => 42 );
40  
41 # Configure information related to your application's actions
42 my $api = Jifty->api;
43  
44 # Retrieve general information about Mason
45 my $handler = Jifty->handler;
46  
47 =head1 DESCRIPTION
48  
49 Yet another web framework.
50  
51 =head2 What's cool about Jifty? (Buzzwords)
52  
53 =over 4
54  
55 =item DRY (Don't Repeat Yourself)
56  
57 Jifty tries not to make you say things more than once.
58  
59 =item Full-stack
60  
61 Out of the proverbial box, Jifty comes with one way to do everything
62 you should need to do: One database mapper, one templating system, one
63 web services layer, one AJAX toolkit, one set of handlers for
64 standalone or FastCGI servers. We work hard to make all the bits play
65 well together, so you don't have to.
66  
67 =item Continuations
68  
69 With Jifty, it's easy to let the user go off and do something else,
70 like fill out a wizard, look something up in the help system or go
71 twiddle their preferences and come right back to where they were.
72  
73 =item Form-based dispatch
74  
75 This is one of the things that Jifty does that we've not seen anywhere
76 else. Jifty owns your form rendering and processing. This means you
77 never need to write form handling logic. All you say is "I want an
78 input for this argument here" and Jifty takes care of the rest. (Even
79 autocomplete and validation)
80  
81 =item A Pony
82  
83 Jifty is the only web application framework that comes with a pony.
84  
85 =back
86  
87 =head2 Introduction
88  
89 If this is your first time using Jifty, L<Jifty::Manual::Tutorial> is
90 probably a better place to start.
91  
92 =cut
93  
94  
95 113 ▶use base qw/Jifty::Object/;
Subroutine BEGIN: called CC=1 SCAR=0.0
96 113 ▶use Jifty::Everything;
Subroutine BEGIN: called CC=1 SCAR=0.0
97  
98 6 ▶use vars qw/$HANDLE $CONFIG $LOGGER $HANDLER $API $CLASS_LOADER $WEB @PLUGINS/;
Subroutine BEGIN: called CC=1 SCAR=0.0
99  
100 =head1 METHODS
101  
102 =head2 new PARAMHASH
103  
104 This class method instantiates a new C<Jifty> object. This object
105 deals with configuration files, logging and database handles for the
106 system. Before this method returns, it calls the application's C<start>
107 method (i.e. C<MyApp-E<gt>start>) to handle any application-specific startup.
108  
109 Most of the time, the server will call this for you to set up
110 your C<Jifty> object. If you are writing command-line programs that
111 want to use your libraries (as opposed to web services) you will need
112 to call this yourself.
113  
114 See L<Jifty::Config> for details on how to configure your Jifty
115 application.
116  
117 =head3 Arguments
118  
119 =over
120  
121 =item no_handle
122  
123 If this is set to true, Jifty will not create a L<Jifty::Handle> and
124 connect to a database. Only use this if you're about to drop the
125 database or do something extreme like that; most of Jifty expects the
126 handle to exist. Defaults to false.
127  
128 =item logger_component
129  
130 The name that Jifty::Logger will log under. If you don't specify anything
131 Jifty::Logger will log under the empty string. See L<Jifty::Logger> for
132 more information.
133  
134 =back
135  
136 =cut
137  
138 0 ▶sub new {
Subroutine new: not called CC=13 SCAR=52.0
Pod documented
139 0 my $ignored_class = shift;
140  
141 # Setup the defaults
142 0 my %args = (
143 no_handle => 0,
144 no_request => 0,
145 pre_init => 0,
146 logger_component => undef,
147 @_
148 );
149  
150 # Add the application's library path
151 0 push @INC, File::Spec->catdir(Jifty::Util->app_root, "lib");
152  
153 # Now that we've loaded the configuration, we can remove the temporary
154 # Jifty::DBI::Record baseclass for records and insert our "real" baseclass,
155 # which is likely Record::Cachable or Record::Memcached
156 0 @Jifty::Record::ISA = grep { $_ ne 'Jifty::DBI::Record' } @Jifty::Record::ISA;
157  
158 # Configure the base class used by Jifty models
159 0 my $record_base_class = Jifty->config->framework('Database')->{'RecordBaseClass'};
160 0 Jifty::Util->require( $record_base_class );
161 0 ▶ push @Jifty::Record::ISA, $record_base_class unless $record_base_class eq 'Jifty::Record';
Branch: 1 branch
BranchTrueFalseTotal
unless $record_base_class eq "Jifty::Record" 0 0 0
162  
163 # Configure the base class for Jifty::CAS
164 0 Jifty::CAS->setup;
165  
166 # Logger turn on
167 0 Jifty->logger( Jifty::Logger->new( $args{'logger_component'} ) );
168  
169 # Set up plugins
170 0 @PLUGINS = ();
171 0 my @plugins_to_load = @{Jifty->config->framework('Plugins')};
172 0 my $app_plugin = Jifty->app_class('Plugin');
173 # we are pushing prereq to plugin, hence the 3-part for.
174 0 ▶ for (my $i = 0; my $plugin = $plugins_to_load[$i]; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $plugin = $plugins_to_load[$i]) 0 0 0
175 0 my $is_prereq = delete $plugin->{_prereq};
176  
177 # Prepare to learn the plugin class name
178 0 my ($plugin_name) = keys %{$plugin};
179 0 my $class;
180  
181 # Is the plugin name a fully-qualified class name?
182 0 ▶ if ($plugin_name =~ /^(?:Jifty::Plugin|$app_plugin)::/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($plugin_name =~ /^(?:Jifty::Plugin|$app_plugin)::/) { } 0 0 0
183 # app-specific plugins use fully qualified names, Jifty plugins may
184 0 $class = $plugin_name;
185 }
186  
187 # otherwise, assume it's a short name, qualify it
188 else {
189 0 $class = "Jifty::Plugin::".$plugin_name;
190 }
191  
192 # avoid duplicating prereq plugins. we can't do this in the loop below
193 # because a plugin might prereq a plugin later in config.yml
194 0 ▶ if ($is_prereq) {
Branch: 1 branch
BranchTrueFalseTotal
if ($is_prereq) 0 0 0
195 0 my $this_class = qr/^(?:Jifty::Plugin::|\Q$app_plugin\E)?\Q$plugin_name\E$/;
196  
197 0 ▶ next if grep { $_ =~ $this_class } @plugins_to_load;
Branch: 1 branch
BranchTrueFalseTotal
if grep {$_ =~ /$this_class/;} @plugins_to_load 0 0 0
198  
199 # already loaded plugin objects
200 0 ▶ next if grep { ref($_) =~ $this_class } Jifty->plugins;
Branch: 1 branch
BranchTrueFalseTotal
if grep {ref($_) =~ /$this_class/;} "Jifty"->plugins 0 0 0
201 }
202  
203 # Load the plugin options
204 0 my %options = (%{ $plugin->{ $plugin_name } },
205 0 _pre_init => $args{'pre_init'} );
206  
207 # Load the plugin code
208 0 Jifty::Util->require($class);
209 0 Jifty::ClassLoader->new(base => $class)->require;
210  
211 # Initialize the plugin and mark the prerequisites for loading too
212 0 my $plugin_obj = $class->new(%options);
213 0 foreach my $name ($plugin_obj->prereq_plugins) {
214 0 push @plugins_to_load, {$name => {}, _prereq => 1};
215 }
216 }
217  
218 # Now that we have the config set up and loaded plugins,
219 # load the localization files.
220 0 Jifty::I18N->refresh();
221
222 # Get a classloader set up
223 0 my $class_loader = Jifty::ClassLoader->new(
224 base => Jifty->app_class,
225 );
226  
227 # Save the class loader for later reference
228 0 Jifty->class_loader($class_loader);
229 0 $class_loader->require;
230  
231 # Configure the request handler and action API handler
232 0 ▶ Jifty->handler(Jifty::Handler->new()) unless Jifty->handler;
Branch: 1 branch
BranchTrueFalseTotal
unless "Jifty"->handler 0 0 0
233 0 ▶ Jifty->api(Jifty::API->new()) unless Jifty->api;
Branch: 1 branch
BranchTrueFalseTotal
unless "Jifty"->api 0 0 0
234  
235 # We can only require view classes once we have our models and actions set.
236 0 $class_loader->require_views;
237  
238 # Let's get the database rocking and rolling
239 0 Jifty->setup_database_connection(%args);
240  
241 # Call the application's start method to let it do anything
242 # application specific for startup
243 0 my $app = Jifty->app_class;
244
245 # Run the App::start() method if it exists for app-specific initialization
246 $app->start
247 0 ▶ if not $args{no_handle} and $app->can('start');
Branch: 1 branch
BranchTrueFalseTotal
if not $args{"no_handle"} and $app->can("start") 0 0 0
Condition0%0/3
not $args{"no_handle"} and $app->can("start")
!ll&&!rl&&r
000
MC/DC0%0/2
not $args{"no_handle"} and $app->can("start")
not $args{"no_handle"} $app->can("start")
Truth table0%0/3
not(A) and B
A= not $args{"no_handle"}B= $app->can("start")
ABresult
0X0
100
111
248  
249 # Setup an empty request and response if we're not in a web environment
250 0 ▶ if ($args{no_request}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args{"no_request"}) 0 0 0
251 0 Jifty->web->request(Jifty::Request->new);
252 0 Jifty->web->response(Jifty::Response->new);
253 }
254  
255 # For plugins that want all the above initialization, but want to run before
256 # we begin serving requests
257 0 Jifty->call_trigger('post_init');
258 }
259  
260 # Explicitly destroy the classloader; if this happens during global
261 # destruction, there's a period of time where there's a bogus entry in
262 # @INC
263 6 ▶END {
Subroutine END: called CC=2 SCAR=7.6
264 6 ▶ Jifty->class_loader->DESTROY if Jifty->class_loader;
Branch: 1 branch
BranchTrueFalseTotal
if "Jifty"->class_loader 0 6 6
265 }
266  
267 =head2 config
268  
269 An accessor for the L<Jifty::Config> object that stores the
270 configuration for the Jifty application.
271  
272 =cut
273  
274 0 ▶sub config {
Subroutine config: not called CC=3 SCAR=24.8
Pod documented
275 0 my $class = shift;
276 0 ▶ $CONFIG = shift if (@_);
Branch: 1 branch
BranchTrueFalseTotal
if @_ 0 0 0
277 0 ▶ $CONFIG ||= Jifty::Config->new();
Condition0%0/3
$Jifty::CONFIG ||= "Jifty::Config"->new
l!l&&r!l&&!r
000
MC/DC0%0/2
$Jifty::CONFIG ||= "Jifty::Config"->new
$Jifty::CONFIG "Jifty::Config"->new
Truth table0%0/3
A ||= B
A= $Jifty::CONFIGB= "Jifty::Config"->new
ABresult
1X1
011
000
278 0 return $CONFIG;
279 }
280  
281 =head2 logger
282  
283 An accessor for our L<Jifty::Logger> object for the application.
284  
285 You probably aren't interested in this. See L</log> for information on how to
286 make log messages.
287  
288 =cut
289  
290 0 ▶sub logger {
Subroutine logger: not called CC=2 SCAR=17.9
Pod documented
291 0 my $class = shift;
292 0 ▶ $LOGGER = shift if (@_);
Branch: 1 branch
BranchTrueFalseTotal
if @_ 0 0 0
293 0 return $LOGGER;
294 }
295  
296 =head2 handler
297  
298 An accessor for our L<Jifty::Handler> object.
299  
300 This is another method that you usually don't want to mess with too much.
301 Most of the interesting web bits are handled by L</web>.
302  
303 =cut
304  
305 0 ▶sub handler {
Subroutine handler: not called CC=2 SCAR=17.9
Pod documented
306 0 my $class = shift;
307 0 ▶ $HANDLER = shift if (@_);
Branch: 1 branch
BranchTrueFalseTotal
if @_ 0 0 0
308 0 return $HANDLER;
309 }
310  
311 =head2 handle
312  
313 An accessor for the L<Jifty::Handle> object that stores the database
314 handle for the application.
315  
316 =cut
317  
318 0 ▶sub handle {
Subroutine handle: not called CC=2 SCAR=17.9
Pod documented
319 0 my $class = shift;
320 0 ▶ $HANDLE = shift if (@_);
Branch: 1 branch
BranchTrueFalseTotal
if @_ 0 0 0
321 0 return $HANDLE;
322 }
323  
324 =head2 api
325  
326 An accessor for the L<Jifty::API> object that publishes and controls
327 information about the application's L<Jifty::Action>s.
328  
329 =cut
330  
331 0 ▶sub api {
Subroutine api: not called CC=2 SCAR=17.9
Pod documented
332 0 my $class = shift;
333 0 ▶ $API = shift if (@_);
Branch: 1 branch
BranchTrueFalseTotal
if @_ 0 0 0
334 0 return $API;
335 }
336  
337 =head2 app_class(@names)
338  
339 Return Class in application space. For example C<app_class('Model', 'Foo')>
340 returns YourApp::Model::Foo.
341  
342 By the time you get it back, the class will have already been required
343  
344 Is you pass a hashref as the first argument, it will be treated as
345 configuration parameters. The only existing parameter is C<require>,
346 which defaults to true.
347  
348 =cut
349  
350 0 ▶sub app_class {
Subroutine app_class: not called CC=3 SCAR=24.8
Pod documented
351 0 shift;
352 0 ▶ my $args = (ref $_[0] ? shift : { require => 1 });
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 0 0 0
353 0 my $val = join('::', Jifty->config->framework('ApplicationClass'), @_);
354 0 ▶ Jifty::Util->try_to_require($val) if $args->{require};
Branch: 1 branch
BranchTrueFalseTotal
if $args->{"require"} 0 0 0
355 0 return $val;
356 }
357  
358 =head2 web
359  
360 An accessor for the L<Jifty::Web> object that the web interface uses.
361  
362 =cut
363  
364 0 ▶sub web {
Subroutine web: not called CC=2 SCAR=17.9
Pod documented
365 0 ▶ return $Jifty::WEB ||= Jifty::Web->new();
Condition0%0/3
$Jifty::WEB ||= "Jifty::Web"->new
l!l&&r!l&&!r
000
MC/DC0%0/2
$Jifty::WEB ||= "Jifty::Web"->new
$Jifty::WEB "Jifty::Web"->new
Truth table0%0/3
A ||= B
A= $Jifty::WEBB= "Jifty::Web"->new
ABresult
1X1
011
000
366 }
367  
368 =head2 plugins
369  
370 Returns a list of L<Jifty::Plugin> objects for this Jifty application.
371  
372 =cut
373  
374 0 ▶sub plugins {
Subroutine plugins: not called CC=2 SCAR=17.9
Pod documented
375 0 my $class = shift;
376 0 ▶ @PLUGINS = @_ if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 0 0 0
377 0 return @PLUGINS;
378 }
379  
380 =head2 find_plugin
381  
382 Find plugins by name.
383  
384 =cut
385  
386 0 ▶sub find_plugin {
Subroutine find_plugin: not called CC=2 SCAR=17.9
Pod documented
387 0 my $self = shift;
388 0 my $name = shift;
389  
390 0 my @plugins = grep { $_->isa($name) } Jifty->plugins;
391 0 ▶ return wantarray ? @plugins : $plugins[0];
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
392 }
393  
394 =head2 class_loader
395  
396 An accessor for the L<Jifty::ClassLoader> object that stores the loaded
397 classes for the application.
398  
399 =cut
400  
401 6 ▶sub class_loader {
Subroutine class_loader: called CC=2 SCAR=7.1
Pod documented
402 6 my $class = shift;
403 6 ▶ $CLASS_LOADER = shift if (@_);
Branch: 1 branch
BranchTrueFalseTotal
if @_ 0 6 6
404 6 return $CLASS_LOADER;
405 }
406  
407 =head2 setup_database_connection
408  
409 Set up our database connection. Optionally takes a paramhash with a
410 single argument. This method is automatically called by L</new>.
411  
412 =over
413  
414 =item no_handle
415  
416 Defaults to false. If true, Jifty won't try to set up a database handle
417  
418 =item pre_init
419  
420 Defaults to false. If true, plugins are notified that this is a
421 pre-init, any trigger registration in C<init()> should not happen
422 during this stage. Note that model mixins' C<register_triggers> is
423 unrelated to this.
424  
425 =back
426  
427  
428 If C<no_handle> is set or our application's config file is missing a C<Database> configuration
429 section or I<has> a C<SkipDatabase: 1> directive in its framework configuration, does nothing.
430  
431 =cut
432  
433 0 ▶sub setup_database_connection {
Subroutine setup_database_connection: not called CC=6 SCAR=37.4
Pod documented
434 0 my $self = shift;
435 0 my %args = (no_handle => 0,
436 check_opts => {},
437 @_);
438  
439 # Don't setup the database connection if we're told not to
440 0 ▶ unless ( $args{'no_handle'}
Branch: 1 branch
BranchTrueFalseTotal
unless ($args{"no_handle"} or "Jifty"->config->framework("SkipDatabase") or not "Jifty"->config->framework("Database")) 0 0 0
Condition0%0/6
$args{"no_handle"} or "Jifty"->config->framework("SkipDatabase") or not "Jifty"->config->framework("Database")
l!l&&r!l&&!r
000
$args{"no_handle"} or "Jifty"->config->framework("SkipDatabase")
l!l&&r!l&&!r
000
MC/DC0%0/3
$args{"no_handle"} or "Jifty"->config->framework("SkipDatabase") or not "Jifty"->config->framework("Database")
$args{"no_handle"} "Jifty"->config->framework("SkipDatabase") not "Jifty"->config->framework("Database")
Truth table0%0/4
A or B or not(C)
A= $args{"no_handle"}B= "Jifty"->config->framework("SkipDatabase")C= not "Jifty"->config->framework("Database")
ABCresult
1XX1
01X1
0011
0000
441 or Jifty->config->framework('SkipDatabase')
442 or not Jifty->config->framework('Database') )
443 {
444  
445 # Load the application's database handle and connect
446 0 my $handle_class = Jifty->app_class("Handle");
447 0 Jifty::Util->require( $handle_class );
448 0 Jifty->handle( $handle_class->new );
449 0 Jifty->handle->connect();
450  
451 # Clean out any stale Cache::Memcached connections
452 0 ▶ $Jifty::DBI::Record::Memcached::MEMCACHED->disconnect_all
Branch: 1 branch
BranchTrueFalseTotal
if $Jifty::DBI::Record::Memcached::MEMCACHED 0 0 0
453 if $Jifty::DBI::Record::Memcached::MEMCACHED;
454  
455 # Make sure the app version matches the database version
456 0 Jifty->handle->check_schema_version(%{$args{'check_opts'}})
457 0 ▶ unless $args{'no_version_check'};
Branch: 1 branch
BranchTrueFalseTotal
unless $args{"no_version_check"} 0 0 0
458 }
459 }
460  
461  
462 =head2 app_instance_id
463  
464 Returns a globally unique id for this instance of this jifty
465 application. This value is generated the first time it's accessed
466  
467 =cut
468  
469 0 ▶sub app_instance_id {
Subroutine app_instance_id: not called CC=2 SCAR=17.9
Pod documented
470 0 my $self = shift;
471 0 my $app_instance_id = Jifty::Model::Metadata->load("application_instance_uuid");
472 0 ▶ unless ($app_instance_id) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($app_instance_id) 0 0 0
473 0 require Data::UUID;
474 0 $app_instance_id = Data::UUID->new->create_str();
475 0 Jifty::Model::Metadata->store(application_instance_uuid => $app_instance_id );
476 }
477 0 return $app_instance_id;
478 }
479  
480 =head2 background SUB
481  
482 Forks a background process, and ensures that database connections and
483 sockets are not shared with the parent process.
484  
485 =cut
486  
487 0 ▶sub background {
Subroutine background: not called CC=2 SCAR=17.9
Pod documented
488 0 my $class = shift;
489 0 my $sub = shift;
490 0 ▶ if (my $pid = fork) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $pid = fork) { } 0 0 0
491 0 return $pid;
492 } else {
493 0 close STDOUT;
494 0 close STDIN;
495 # XXX: make $Jifty::SERVER close client sockets if exists
496 0 Jifty->handle->dbh->{InactiveDestroy} = 1;
497 0 Jifty->setup_database_connection();
498 0 $sub->();
499 0 exit;
500 }
501 }
502  
503 =head2 admin_mode
504  
505 Returns true if the application is in admin mode. This should be used instead
506 of C<< Jifty->config->framework('AdminMode') >>.
507  
508 =cut
509  
510 0 ▶sub admin_mode {
Subroutine admin_mode: not called CC=2 SCAR=17.9
Pod documented
511 0 ▶ return Jifty->config->framework('AdminMode')
Condition0%0/3
'Jifty'->config->framework('AdminMode') || 'Jifty'->config->framework('SetupMode')
l!l&&r!l&&!r
000
MC/DC0%0/2
'Jifty'->config->framework('AdminMode') || 'Jifty'->config->framework('SetupMode')
'Jifty'->config->framework('AdminMode') 'Jifty'->config->framework('SetupMode')
Truth table0%0/3
A || B
A= 'Jifty'->config->framework('AdminMode')B= 'Jifty'->config->framework('SetupMode')
ABresult
1X1
011
000
512 || Jifty->config->framework('SetupMode');
513 }
514  
515 =head1 SEE ALSO
516  
517 L<http://jifty.org>, L<Jifty::Manual::Tutorial>, L<Jifty::Everything>, L<Jifty::Config>, L<Jifty::Handle>, L<Jifty::Logger>, L<Jifty::Handler>, L<Jifty::Web>, L<Jifty::API>, L<Jifty::Plugin>, L<Jifty::ClassLoader>
518  
519 =head1 AUTHORS
520  
521 Jesse Vincent, Alex Vandiver and David Glasser.
522  
523 =head1 LICENSE
524  
525 Jifty is Copyright 2005-2010 Best Practical Solutions, LLC.
526 Jifty is distributed under the same terms as Perl itself.
527  
528  
529  
530 =cut
531  
532 6 1;
« Jifty/Test/WWW/Mechanize.pm ↑ Summary Jifty/Test.pm »