alter skin loading precedence (layoutset path search always starts from toip except...
[catagits/Reaction.git] / lib / Reaction / UI / Skin.pm
1 package Reaction::UI::Skin;
2
3 use Reaction::Class;
4
5 # declaring dependencies
6 use Reaction::UI::LayoutSet;
7 use Reaction::UI::RenderingContext;
8 use File::ShareDir;
9
10 use aliased 'Path::Class::Dir';
11
12 class Skin which {
13
14   has '_layout_set_cache'   => (is => 'ro', default => sub { {} });
15   has '_widget_class_cache'   => (is => 'ro', default => sub { {} });
16
17   has 'name' => (is => 'ro', isa => 'Str', required => 1);
18   has 'skin_dir' => (is => 'rw', isa => Dir, lazy_fail => 1);
19
20   has 'widget_search_path' => (
21     is => 'rw', isa => 'ArrayRef', requred => 1, default => sub { [] }
22   );
23
24   has 'view' => (
25     is => 'ro', required => 1, weak_ref => 1,
26     handles => [ qw(layout_set_class) ],
27   );
28
29   has 'super' => (
30     is => 'rw', isa => Skin, required => 0, predicate => 'has_super',
31   );
32
33   sub BUILD {
34     my ($self, $args) = @_;
35     $self->_find_skin_dir($args);
36     $self->_load_skin_config($args);
37   }
38
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);
49     confess "No such skin base directory ${base}"
50       unless -d $base;
51     $self->skin_dir($base);
52   };
53
54   implements '_load_skin_config' => as {
55     my ($self, $args) = @_;
56     my $base = $self->skin_dir;
57     my $lst = sub { (ref $_[0] eq 'ARRAY') ? $_[0] : [$_[0]] };
58     my @files = (
59       $args->{skin_base_dir}->file('defaults.conf'), $base->file('skin.conf')
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}) {
69       my $super = $self->new(
70         name => $super_name,
71         view => $self->view,
72         skin_base_dir => $args->{skin_base_dir},
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 {
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;
82     }
83   }
84
85   implements 'create_layout_set' => as {
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) = @_;
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,
96                top_skin => $top_skin,
97              );
98     }
99     $tried = [ @{$tried}, $self->our_path_for_type('layout') ];
100     if ($self->has_super) {
101       return $self->super->_create_layout_set($name, $tried, $top_skin);
102     }
103     confess "Couldn't find layout set file for ${name}, tried "
104             .join(', ', @$tried);
105   };
106
107   implements 'layout_set_args_for' => as {
108     my ($self, $name) = @_;
109     return (
110       name => $name,
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(
120       '.', $layout, $self->view->layout_set_file_extension
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) = @_;
140     return $self->skin_dir->subdir($type)
141   };
142
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
174 };
175
176 1;