File Coverage

blib/lib/Hub/Base/Registry.pm
Criterion Covered Total %
statement 49 71 69.0
branch 9 28 32.1
condition 2 6 33.3
subroutine 7 9 77.7
pod 4 4 100.0
total 71 118 60.1


line stmt bran cond sub pod time code
1             package Hub::Base::Registry;
2 1     1   6 use strict;
  1         2  
  1         49  
3 1     1   7 use Hub qw/:lib :config/;
  1         2  
  1         6  
4             our $VERSION = '4.00043';
5             our @EXPORT = qw//;
6             our @EXPORT_OK = qw/ROOT_KEYS/;
7              
8 1     1   6 use constant ROOT_KEYS => qw(sys conf session cgi);
  1         1  
  1         948  
9              
10             # ------------------------------------------------------------------------------
11             # new - Constructor
12             # ------------------------------------------------------------------------------
13              
14             sub new {
15 1     1 1 2 my $self = shift;
16 1   33     6 my $classname = ref($self) || $self;
17 1         3 $self = bless {}, $classname;
18 1         13 tie %$self, 'Hub::Knots::TiedObject', 'Hub::Knots::FileSystem';
19 1         5 return $self;
20             }#new
21              
22             # ------------------------------------------------------------------------------
23             # run - Main action method
24             # run \&callback_subroutine
25             # ------------------------------------------------------------------------------
26              
27             sub run {
28 0     0 1 0 my $self = shift;
29 0 0       0 croak "Illegal call to instance method" unless ref($self);
30 0         0 my $sub = shift;
31 0 0       0 unless(defined &$sub) {
32 0         0 croak "$0: Callback subroutine ($sub) not defined";
33             }
34 0         0 $self->bootstrap();
35 0         0 my $ret = &$sub(@_);
36 0         0 my $err = $@;
37 0         0 $self->finish();
38 0 0       0 croak $err if $err;
39 0         0 return $ret;
40             }#run
41              
42             # ------------------------------------------------------------------------------
43             # bootstrap - Bootstrap the scope for this thread
44             # ------------------------------------------------------------------------------
45              
46             sub bootstrap {
47 1     1   2 my $self = shift;
48 1 50       14 croak "Illegal call to instance method" unless ref($self);
49             # System environment
50 1         3 my @argv = (@ARGV);
51             # Remove virtual containers
52 1         3 do {delete $$self{"/$_"}} for qw(sys/.* conf session cgi);
  4         24  
53 1         9 $$self{'/sys'} = {}; # Clear
54 1         7 $$self{'/sys/OPTS'} = Hub::cmdopts(\@argv);
55 1         18 $$self{'/sys/ARGV'} = \@argv;
56 1         9 $$self{'/sys/ENV'} = Hub::cpref(\%ENV);
57 1   33     8 $$self{'/sys/ENV/WORKING_DIR'} ||= cwd();
58             # Configuration
59 1         14 my $conf = Hub::CONF_BASE;
60 1 50       10 if ($Hub::TAG_MAP{'Webapp'}) {
61 0         0 $conf = Hub::merge($conf, Hub::CONF_WEBAPP, -overwrite, -copy)
62             }
63 1         30 my $conf_file = Hub::bestof($$Hub{'/sys/ENV/CONF_FILE'}, '.conf');
64 1 50       32 if (-e $conf_file) {
65 0         0 my $hf = Hub::mkinst('HashFile', $conf_file);
66 0         0 $conf = Hub::merge($conf, $hf->get_data(), -overwrite, -copy);
67             }
68 1         13 $$self{'/conf'} = $conf;
69             # Directories encountered first have prescedence
70 1         4 my @root_keys = ();
71 1         6 foreach my $var ('/sys/ENV/BASE_DIR', '/sys/ENV/WORKING_DIR') {
72 2         10 my $dir = $$self{$var};
73 2 100       13 if (defined $dir) {
74 1         178 chdir $dir;
75 1         13 push @root_keys, $self->_root_overlay($dir);
76             }
77             }
78             # Clear
79 1         4 foreach my $k (keys %$self) {
80 16 50       20 unless (grep { $_ eq $k } (@root_keys, ROOT_KEYS)) {
  288         392  
81 0         0 delete $$self{$k};
82             }
83             }
84             # Our dot directory is the working (not base) directory
85 1         11 $$self{'.'} = Hub::mkhandler($$self{'/sys/ENV/WORKING_DIR'});
86             }#bootstrap
87              
88             sub _root_overlay {
89 1     1   6 my ($self, $dir) = @_;
90 1 50       24 return () unless defined $dir;
91 1         15 my @root_files = Hub::readdir($dir);
92 1         6 foreach my $name (@root_files) {
93 14 50       43 next if grep {$_ eq $name} ROOT_KEYS; # don't overwrite the above
  56         130  
94 14         76 my $init = $$self{$name}; # creates the handler
95             }
96 1         12 return @root_files;
97             }
98              
99             # ------------------------------------------------------------------------------
100             # finish - The caller's script has completed
101             # ------------------------------------------------------------------------------
102              
103             sub finish {
104 1     1 1 3 my $self = shift;
105 1 50       11 croak "Illegal call to instance method" unless ref($self);
106             }#finish
107              
108             # ------------------------------------------------------------------------------
109             # resolve - Return a scalar value of a node
110             # resolve $key
111             # Because some nodes (like files) can be pointers to certain resources, this
112             # method will return their final contents.
113             # ------------------------------------------------------------------------------
114              
115             sub resolve {
116 0     0 1   my $self = shift;
117 0 0         croak "Illegal call to instance method" unless ref($self);
118 0 0         my $key = shift or confess "Provide a hash key";
119 0           my $unresolved = $$self{$key};
120 0 0         if (UNIVERSAL::isa($unresolved, 'Hub::Data::File')) {
121 0           return $unresolved->get_content();
122             } else {
123 0           return $unresolved;
124             }
125             }#resolve
126              
127             # ------------------------------------------------------------------------------
128             1;
129              
130             __END__