1 package Reaction::UI::Skin;
5 # declaring dependencies
6 use Reaction::UI::LayoutSet;
7 use Reaction::UI::RenderingContext;
10 use aliased 'Path::Class::Dir';
14 has '_layout_set_cache' => (is => 'ro', default => sub { {} });
15 has '_widget_class_cache' => (is => 'ro', default => sub { {} });
17 has 'name' => (is => 'ro', isa => 'Str', required => 1);
18 has 'skin_dir' => (is => 'rw', isa => Dir, lazy_fail => 1);
20 has 'widget_search_path' => (
21 is => 'rw', isa => 'ArrayRef', requred => 1, default => sub { [] }
25 is => 'ro', required => 1, weak_ref => 1,
26 handles => [ qw(layout_set_class) ],
30 is => 'rw', isa => Skin, required => 0, predicate => 'has_super',
34 my ($self, $args) = @_;
35 $self->_find_skin_dir($args);
36 $self->_load_skin_config($args);
39 implements '_find_skin_dir' => as {
40 my ($self, $args) = @_;
41 my $skin_name = $self->name;
42 if ($skin_name =~ s!^/(.*?)/!!) {
44 $args->{skin_base_dir} =
45 Dir->new(File::ShareDir::dist_dir($dist))
48 my $base = $args->{skin_base_dir}->subdir($skin_name);
49 confess "No such skin base directory ${base}"
51 $self->skin_dir($base);
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]] };
59 $args->{skin_base_dir}->file('defaults.conf'), $base->file('skin.conf')
61 # we get [ { $file => $conf }, ... ]
62 my %cfg = (map { %{(values %{$_})[0]} }
63 @{Config::Any->load_files({
64 files => [ grep { -e $_ } @files ],
68 if (my $super_name = $cfg{extends}) {
69 my $super = $self->new(
72 skin_base_dir => $args->{skin_base_dir},
76 if (exists $cfg{widget_search_path}) {
77 $self->widget_search_path($lst->($cfg{widget_search_path}));
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;
85 implements 'create_layout_set' => as {
86 my ($self, $name) = @_;
87 $self->_create_layout_set($name, [], $self);
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),
96 top_skin => $top_skin,
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);
103 confess "Couldn't find layout set file for ${name}, tried "
104 .join(', ', @$tried);
107 implements 'layout_set_args_for' => as {
108 my ($self, $name) = @_;
112 ($self->has_super ? (next_skin => $self->super) : ()),
113 $self->view->layout_set_args_for($name),
117 implements 'layout_path_for' => as {
118 my ($self, $layout) = @_;
119 my $file_name = join(
120 '.', $layout, $self->view->layout_set_file_extension
122 my $path = $self->our_path_for_type('layout')
124 return (-e $path ? $path : undef);
127 implements 'search_path_for_type' => as {
128 my ($self, $type) = @_;
130 $self->our_path_for_type($type),
132 ? @{$self->super->search_path_for_type($type)}
138 implements 'our_path_for_type' => as {
139 my ($self, $type) = @_;
140 return $self->skin_dir->subdir($type)
143 implements 'full_widget_search_path' => as {
146 @{$self->widget_search_path},
147 ($self->has_super ? $self->super->full_widget_search_path : ())
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 {
157 my @search_path = $self->full_widget_search_path;
158 my @haystack = map {join('::', $_, $widget_type)} @search_path;
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);
165 my $ok = eval { Class::MOP::load_class($class) };
166 confess("Failed to load widget '${class}': $@") if $@;
169 confess "Couldn't locate widget '${widget_type}' for layout "
170 ."'${\$layout_set->name}': tried: ".join(", ", @haystack);