count code
[catagits/Reaction.git] / lib / Reaction / UI / Skin.pm
CommitLineData
8a293e2e 1package Reaction::UI::Skin;
2
3use Reaction::Class;
4
5# declaring dependencies
6use Reaction::UI::LayoutSet;
7use Reaction::UI::RenderingContext;
8b498574 8use File::ShareDir;
8a293e2e 9
10use aliased 'Path::Class::Dir';
11
12class Skin which {
13
14 has '_layout_set_cache' => (is => 'ro', default => sub { {} });
a2a5872b 15 has '_widget_class_cache' => (is => 'ro', default => sub { {} });
8a293e2e 16
8b498574 17 has 'name' => (is => 'ro', isa => 'Str', required => 1);
18 has 'skin_dir' => (is => 'rw', isa => Dir, lazy_fail => 1);
8a293e2e 19
a2a5872b 20 has 'widget_search_path' => (
21 is => 'rw', isa => 'ArrayRef', requred => 1, default => sub { [] }
22 );
349a57a4 23
8a293e2e 24 has 'view' => (
25 is => 'ro', required => 1, weak_ref => 1,
a2a5872b 26 handles => [ qw(layout_set_class) ],
8a293e2e 27 );
28
29 has 'super' => (
30 is => 'rw', isa => Skin, required => 0, predicate => 'has_super',
31 );
32
33 sub BUILD {
8b498574 34 my ($self, $args) = @_;
35 $self->_find_skin_dir($args);
36 $self->_load_skin_config($args);
8a293e2e 37 }
38
8b498574 39 implements '_find_skin_dir' => as {
40 my ($self, $args) = @_;
41 my $skin_name = $self->name;
42 if ($skin_name =~ s!^/(.*?)/!!) {
43 my $dist = $1;
44 $args->{skin_base_dir} =
45 Dir->new(File::ShareDir::dist_dir($dist))
46 ->subdir('skin');
47 }
48 my $base = $args->{skin_base_dir}->subdir($skin_name);
8a293e2e 49 confess "No such skin base directory ${base}"
50 unless -d $base;
8b498574 51 $self->skin_dir($base);
52 };
53
54 implements '_load_skin_config' => as {
55 my ($self, $args) = @_;
56 my $base = $self->skin_dir;
a2a5872b 57 my $lst = sub { (ref $_[0] eq 'ARRAY') ? $_[0] : [$_[0]] };
349a57a4 58 my @files = (
8b498574 59 $args->{skin_base_dir}->file('defaults.conf'), $base->file('skin.conf')
349a57a4 60 );
61 # we get [ { $file => $conf }, ... ]
62 my %cfg = (map { %{(values %{$_})[0]} }
63 @{Config::Any->load_files({
64 files => [ grep { -e $_ } @files ],
65 use_ext => 1,
66 })}
67 );
68 if (my $super_name = $cfg{extends}) {
349a57a4 69 my $super = $self->new(
8b498574 70 name => $super_name,
71 view => $self->view,
72 skin_base_dir => $args->{skin_base_dir},
349a57a4 73 );
74 $self->super($super);
75 }
76 if (exists $cfg{widget_search_path}) {
77 $self->widget_search_path($lst->($cfg{widget_search_path}));
78 } else {
a2a5872b 79 confess "No widget_search_path in defaults.conf or skin.conf"
80 ." and no search path provided from super skin"
81 unless $self->full_widget_search_path;
7c2bcb55 82 }
8a293e2e 83 }
84
85 implements 'create_layout_set' => as {
b419e52e 86 my ($self, $name) = @_;
87 $self->_create_layout_set($name, [], $self);
88 };
89
90 implements '_create_layout_set' => as {
91 my ($self, $name, $tried, $top_skin) = @_;
8a293e2e 92 if (my $path = $self->layout_path_for($name)) {
93 return $self->layout_set_class->new(
94 $self->layout_set_args_for($name),
95 source_file => $path,
b419e52e 96 top_skin => $top_skin,
8a293e2e 97 );
98 }
b419e52e 99 $tried = [ @{$tried}, $self->our_path_for_type('layout') ];
8a293e2e 100 if ($self->has_super) {
b419e52e 101 return $self->super->_create_layout_set($name, $tried, $top_skin);
8a293e2e 102 }
c964124f 103 confess "Couldn't find layout set file for ${name}, tried "
104 .join(', ', @$tried);
8a293e2e 105 };
106
107 implements 'layout_set_args_for' => as {
108 my ($self, $name) = @_;
109 return (
110 name => $name,
8a293e2e 111 skin => $self,
112 ($self->has_super ? (next_skin => $self->super) : ()),
113 $self->view->layout_set_args_for($name),
114 );
115 };
116
117 implements 'layout_path_for' => as {
118 my ($self, $layout) = @_;
119 my $file_name = join(
349a57a4 120 '.', $layout, $self->view->layout_set_file_extension
8a293e2e 121 );
122 my $path = $self->our_path_for_type('layout')
123 ->file($file_name);
124 return (-e $path ? $path : undef);
125 };
126
127 implements 'search_path_for_type' => as {
128 my ($self, $type) = @_;
129 return [
130 $self->our_path_for_type($type),
131 ($self->has_super
132 ? @{$self->super->search_path_for_type($type)}
133 : ()
134 )
135 ];
136 };
137
138 implements 'our_path_for_type' => as {
139 my ($self, $type) = @_;
8b498574 140 return $self->skin_dir->subdir($type)
8a293e2e 141 };
142
a2a5872b 143 implements 'full_widget_search_path' => as {
144 my ($self) = @_;
145 return (
146 @{$self->widget_search_path},
147 ($self->has_super ? $self->super->full_widget_search_path : ())
148 );
149 };
150
151 implements 'widget_class_for' => as {
152 my ($self, $layout_set) = @_;
153 my $base = $self->blessed;
154 my $widget_type = $layout_set->widget_type;
155 return $self->_widget_class_cache->{$widget_type} ||= do {
156
157 my @search_path = $self->full_widget_search_path;
158 my @haystack = map {join('::', $_, $widget_type)} @search_path;
159
160 foreach my $class (@haystack) {
161 #if the class is already loaded skip the call to Installed etc.
162 return $class if Class::MOP::is_class_loaded($class);
163 next unless Class::Inspector->installed($class);
164
165 my $ok = eval { Class::MOP::load_class($class) };
166 confess("Failed to load widget '${class}': $@") if $@;
167 return $class;
168 }
169 confess "Couldn't locate widget '${widget_type}' for layout "
170 ."'${\$layout_set->name}': tried: ".join(", ", @haystack);
171 };
172 };
173
8a293e2e 174};
175
1761;