complete MooseX::Types port
[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 {
c964124f 86 my ($self, $name, $tried) = @_;
8a293e2e 87 if (my $path = $self->layout_path_for($name)) {
88 return $self->layout_set_class->new(
89 $self->layout_set_args_for($name),
90 source_file => $path,
91 );
92 }
c964124f 93 $tried = [ @{$tried||[]}, $self->our_path_for_type('layout') ];
8a293e2e 94 if ($self->has_super) {
c964124f 95 return $self->super->create_layout_set(
96 $name,
97 $tried
98 );
8a293e2e 99 }
c964124f 100 confess "Couldn't find layout set file for ${name}, tried "
101 .join(', ', @$tried);
8a293e2e 102 };
103
104 implements 'layout_set_args_for' => as {
105 my ($self, $name) = @_;
106 return (
107 name => $name,
8a293e2e 108 skin => $self,
109 ($self->has_super ? (next_skin => $self->super) : ()),
110 $self->view->layout_set_args_for($name),
111 );
112 };
113
114 implements 'layout_path_for' => as {
115 my ($self, $layout) = @_;
116 my $file_name = join(
349a57a4 117 '.', $layout, $self->view->layout_set_file_extension
8a293e2e 118 );
119 my $path = $self->our_path_for_type('layout')
120 ->file($file_name);
121 return (-e $path ? $path : undef);
122 };
123
124 implements 'search_path_for_type' => as {
125 my ($self, $type) = @_;
126 return [
127 $self->our_path_for_type($type),
128 ($self->has_super
129 ? @{$self->super->search_path_for_type($type)}
130 : ()
131 )
132 ];
133 };
134
135 implements 'our_path_for_type' => as {
136 my ($self, $type) = @_;
8b498574 137 return $self->skin_dir->subdir($type)
8a293e2e 138 };
139
a2a5872b 140 implements 'full_widget_search_path' => as {
141 my ($self) = @_;
142 return (
143 @{$self->widget_search_path},
144 ($self->has_super ? $self->super->full_widget_search_path : ())
145 );
146 };
147
148 implements 'widget_class_for' => as {
149 my ($self, $layout_set) = @_;
150 my $base = $self->blessed;
151 my $widget_type = $layout_set->widget_type;
152 return $self->_widget_class_cache->{$widget_type} ||= do {
153
154 my @search_path = $self->full_widget_search_path;
155 my @haystack = map {join('::', $_, $widget_type)} @search_path;
156
157 foreach my $class (@haystack) {
158 #if the class is already loaded skip the call to Installed etc.
159 return $class if Class::MOP::is_class_loaded($class);
160 next unless Class::Inspector->installed($class);
161
162 my $ok = eval { Class::MOP::load_class($class) };
163 confess("Failed to load widget '${class}': $@") if $@;
164 return $class;
165 }
166 confess "Couldn't locate widget '${widget_type}' for layout "
167 ."'${\$layout_set->name}': tried: ".join(", ", @haystack);
168 };
169 };
170
8a293e2e 171};
172
1731;