1# $Id: /mirror/gungho/lib/Gungho/Component/Setup.pm 41086 2008-02-01T16:49:41.285355Z lestrrat  $
2#
3# Copyright (c) 2007 Daisuke Maki <daisuke@endeworks.jp>
4# All rights reserved.
5
6package Gungho::Component::Setup;
7use strict;
8use warnings;
9use base qw(Gungho::Component);
10use Config::Any;
11
12my @INTERNAL_PARAMS             = qw(bootstrap_finished setup_finished config);
13my @CONFIGURABLE_PARAMS         = qw(user_agent);
14my %CONFIGURABLE_PARAM_DEFAULTS = (
15    map { ($_ => 0) } @CONFIGURABLE_PARAMS
16);
17
18__PACKAGE__->mk_classdata($_) for (
19    qw(log provider handler engine is_running features observer),
20    @INTERNAL_PARAMS,
21    @CONFIGURABLE_PARAMS,
22);
23
24sub new
25{
26    warn "Gungho::new() has been deprecated in favor of Gungho->run()";
27    my $class = shift;
28    $class->bootstrap(@_);
29    return $class;
30}
31
32sub run
33{
34    my $c = shift;
35    $c->bootstrap(@_);
36    $c->is_running(1);
37
38    my $banner = sprintf(<<EOBANNER, eval('$Gungho' . '::VERSION'), join(', ', map { my $name = $_; $name =~ s/Gungho::Component:://; $name } @Gungho::ISA ) );
39Starting $c
40Gungho Version: %s
41Components: %s
42EOBANNER
43    $c->log->info($_) for split(/\n/, $banner);
44    $c->engine->run($c);
45}
46
47sub bootstrap
48{
49    my $c = shift;
50
51    return $c if $c->bootstrap_finished();
52
53    my $config = $c->load_config($_[0]);
54    if (exists $ENV{GUNGHO_DEBUG}) {
55        $config->{debug} = $ENV{GUNGHO_DEBUG};
56    }
57
58    $c->config($config);
59
60    for my $key (@CONFIGURABLE_PARAMS) {
61        $c->$key( $config->{$key} || $CONFIGURABLE_PARAM_DEFAULTS{$key} );
62    }
63
64    if (! $config->{user_agent}) {
65        warn "No user agent specified. You should specify one today!";
66        $c->user_agent( "Gungho/$Gungho::VERSION (http://code.google.com/p/gungho-crawler/wiki/Index)");
67    }
68    $c->features({});
69
70    my $components = $c->config->{components} || [];
71    push @$components, 'Core';
72    Gungho->load_components(@$components);
73    $c->bootstrap_finished(1);
74    $c->setup;
75
76    return $c;
77}
78
79sub load_config
80{
81    my $c = shift;
82    my $config = shift;
83
84    if ($config && ! ref $config) {
85        my $filename = $config;
86        # In the future, we may support multiple configs, but for now
87        # just load a single file via Config::Any
88
89        my $list = Config::Any->load_files( {
90            files => [ $filename ],
91            use_ext => 1,
92        } );
93        ($config) = $list->[0]->{$filename};
94    }
95
96    if (! $config) {
97        Carp::croak("Could not load config");
98    }
99
100    if (ref $config ne 'HASH') {
101        Carp::croak("Gungho expectes config that can be decoded to a HASH");
102    }
103
104    return $config;
105}
106
107
1081;
109
110__END__
111
112=head1 NAME
113
114Gungho::Component::Setup - Routines To Setup Gungho
115
116=head2 SYNOPSIS
117
118  # Internal Use only
119
120=head1 METHODS
121
122=head2 new
123
124Only here to announce its deprecation. Don't use. Use run()
125
126=head2 bootstrap
127
128Bootstraps Gungho.
129
130=head2 run
131
132Sets up, configures, and starts Gungho
133
134=head2 load_config
135
136Loads the given config
137
138=cut
139