File Coverage

lib/Egg/Util.pm
Criterion Covered Total %
statement 15 153 9.8
branch 0 78 0.0
condition 0 71 0.0
subroutine 5 23 21.7
pod 14 14 100.0
total 34 339 10.0


line stmt bran cond sub pod time code
1             package Egg::Util;
2             #
3             # Masatoshi Mizuno E<lt>lusheE<64>cpan.orgE<gt>
4             #
5             # $Id: Util.pm 337 2008-05-14 12:30:09Z lushe $
6             #
7 37     37   229 use strict;
  37         60  
  37         2599  
8 37     37   180 use warnings;
  37         71  
  37         1256  
9 37     37   175 use Carp qw/ croak /;
  37         62  
  37         4137  
10 37     37   194 use base qw/ Egg::Base /;
  37         59  
  37         19371  
11 37     37   324116 use URI;
  37         267334  
  37         3102  
12              
13             our $VERSION= '3.01';
14              
15             sub page_title {
16 0     0 1   my $e= shift;
17 0 0 0       return ($e->stash->{page_title} ||= $e->config->{title}) unless @_;
18 0   0       $e->stash->{page_title}= shift || $e->config->{title};
19             }
20             sub debug {
21 0 0   0 1   $_[0]->flag->{-debug} || 0;
22             }
23             sub flag {
24 0     0 1   my $e = shift;
25 0   0       my $key= shift || return $e->global->{flag};
26 0           $key=~s{^\-} [];
27 0 0         $e->global->{flag}{ '-'. lc $key } || 0;
28             }
29             sub snip {
30 0     0 1   my $e= shift;
31 0 0         return $e->{snip} unless @_;
32 0 0         $e->{snip}= $_[0] ? ($_[1] ? [@_]: $_[0]): croak q{ I want argument. };
    0          
33             }
34             sub action {
35 0     0 1   my $e= shift;
36 0 0         return $e->{action} unless @_;
37 0 0         $e->{action}= $_[0] ? ($_[1] ? [@_]: $_[0]): croak q{ I want argument. };
    0          
38             }
39             sub stash {
40 0     0 1   my $e= shift;
41 0 0         return $e->{stash} unless @_;
42 0 0         return $e->{stash}{$_[0]} if @_ < 2;
43 0           $e->{stash}{$_[0]}= $_[1];
44             }
45             sub path_to {
46 0     0 1   my $e= shift;
47 0   0       my $class= ref($e) || $e;
48 0   0       my $path= shift || return $class->config->{dir}{root};
49 0 0         if (my $name= shift) {
50 0   0       my $root= $class->config->{dir}{$path} || croak qq{'$path' is empty.};
51 0           return "${root}/$name";
52             } else {
53 0 0         if (my $dir= $class->config->{dir}{$path}) { return $dir }
  0            
54 0           return $class->config->{root}. "/$path";
55             }
56             }
57             sub uri_to {
58 0     0 1   my $e = shift;
59 0   0       my $uri= shift || croak q{ I want base URI };
60 0           my $result= URI->new($uri);
61 0 0         return $result unless @_;
62 0 0         my %arg= ref($_[0]) eq 'HASH' ? %{$_[0]}: @_;
  0            
63 0           $result->query_form(%arg);
64 0           $result;
65             }
66             sub snip2template {
67 0     0 1   my $e = shift;
68 0   0       my $num = shift || croak q{ I want snip num. };
69 0   0       my $snip= $e->snip || return 0;
70 0 0         @$snip < $num and croak q{ snip num error. };
71 0           my $c= $e->config;
72 0 0         my $tmpl= $e->template(
73 0           join('/', map{$_ || ""}@{$snip}[0..$num]). ".$c->{template_extention}"
  0            
74             );
75 0 0         -e "$c->{template_path}[0]/$tmpl" ? $tmpl: 0;
76             }
77             sub setup_error_header {
78 0     0 1   my($e)= @_;
79 0           $e->response->clear_cookies;
80 0           $e->response->clear_body;
81 0           $e->response->no_cache(1);
82 0           $e->response->headers->{"X-Egg-$e->{namespace}-ERROR"}= 'true';
83 0           1;
84             }
85             sub get_config {
86 0     0 1   my $e = shift;
87 0   0       my $name= shift || (caller())[0] || return {};
88 0           $name=~s{\=.*?$} [];
89 0           $name=~s{^(?:main$|Egg:+)} [];
90 0 0 0       return $e->config if (! $name or $name eq $e->namespace);
91 0           my $conf= $e->config;
92 0           my $key = lc($name); $key=~s{\:+} [_]g;
  0            
93 0 0         return $conf->{$key} if $conf->{$key};
94 0           $key = lc($name);
95 0           $key=~s{\:+[^\:]+$} []; $key=~s{\:+} [_]g;
  0            
96 0 0         $conf->{$key} || {};
97             }
98             sub egg_var {
99 0     0 1   my $e = shift;
100 0   0       my $param= shift || croak q{ I want base parameter. };
101 0 0         my $str = defined($_[0]) ? shift: return "";
102 0           my $text;
103 0 0         if (my $type= ref($str)) {
104 0 0         return $str unless $type eq 'SCALAR';
105 0           $text= $str;
106             } else {
107 0           $text= \$str;
108             }
109 0 0         return "" unless defined($$text);
110 0           $$text=~s{([\\]?)< *\$?e\.([\w\.]+) *>}
111 0 0         [ $1 ? "<e.$2>": _replace($2, $e, $param, @_) ]sge;
112 0           $$text;
113             }
114             sub egg_var_deep {
115 0     0 1   my $e = shift;
116 0   0       my $param= shift || croak q{ I want base parameter. };
117 0 0         my $value= defined($_[0]) ? $_[0]: return "";
118 0 0         if (my $type= ref($value)) {
119 0 0         if ($type eq 'HASH') {
    0          
120 0           while (my($k, $v)= each %$value) {
121 0 0         ref($v) ? $e->egg_var_deep($param, $v)
122             : $e->egg_var($param, \$v);
123 0           $value->{$k}= $v;
124             }
125             } elsif ($type eq 'ARRAY') {
126 0           for (@$value) {
127 0 0         ref($_) ? $e->egg_var_deep($param, $_)
128             : $e->egg_var($param, \$_);
129             }
130             } else {
131 0           return $value;
132             }
133             } else {
134 0           return $e->egg_var($param, \$value);
135             }
136 0           $e;
137             }
138             sub _replace {
139 0     0     my @part= split /\.+/, shift;
140 0           my $v;
141 0           eval "\$v= \$_[1]->{". join('}{', @part)."}"; ## no critic
142 0 0         defined($v) ? do { ref($v) eq 'CODE' ? $v->(@_): $v }: "";
  0 0          
143             }
144             sub error {
145 0     0 1   my $self= shift;
146 0           $self->next::method(@_);
147 0 0         if (my $error= $self->errstr) { $self->stash->{error}= $error }
  0            
148 0           0;
149             }
150             sub _debug_screen {
151 0     0     my $e= shift;
152 0           $e->debugging->error(@_);
153 0           $e->setup_error_header;
154 0           $e->finished('500 Internal Server Error');
155 0           $e->_output;
156             }
157             sub _check_config {
158 0     0     my $e = shift;
159 0   0       my $cf= shift || croak q{ I want configuration. };
160 0 0         $cf->{root} || die q{ I want 'root' configuration. };
161 0           $cf->{root}=~s{[/\\]+$} [];
162 0           $cf->{project}= $e->namespace;
163 0           $cf->{project}=~s{\:+} []g;
164 0   0       $cf->{title} ||= $e->namespace;
165 0   0       $cf->{content_type} ||= 'text/html';
166 0   0       $cf->{template_extention} ||= 'tt';
167 0           $cf->{template_extention}=~s{^\.+} [];
168 0   0       $cf->{template_default_name} ||= 'index';
169 0   0       $cf->{template_path} ||= ["$cf->{root}/root"];
170 0 0         $cf->{template_path}= [$cf->{template_path}]
171             unless ref($cf->{template_path}) eq 'ARRAY';
172 0           s{[/\\]+$} [] for @{$cf->{template_path}};
  0            
173 0   0       $cf->{static_uri} ||= '/';
174 0 0         $cf->{static_uri}.= '/' unless $cf->{static_uri}=~m{/$};
175 0   0       my $dir= $cf->{dir} ||= {};
176 0           for (qw/ cache etc htdocs lib tmp /) {
177 0   0       $dir->{$_} ||= "$cf->{root}/$_";
178 0           $dir->{$_}=~s{[/\\]+$} [];
179             }
180 0           $dir->{root} = $cf->{root};
181 0           $dir->{static} = $dir->{htdocs};
182 0   0       $dir->{temp} ||= $dir->{tmp};
183 0   0       $dir->{comp} ||= $cf->{template_path}->[1] || "$dir->{root}/comp";
      0        
184 0   0       $dir->{template} ||= $cf->{template_path}->[0];
185 0           $dir->{lib_project}= "$dir->{lib}/$cf->{project}";
186 0           $cf;
187             }
188             sub _load_config {
189 0     0     my $class= shift;
190 0   0       my $conf = shift || croak q{ I want config };
191 0 0         $conf = {$conf, @_} if $_[0];
192 0           $class->_check_config($conf);
193 0           $class->egg_var_deep($conf, $conf->{dir});
194 0           $class->egg_var_deep($conf, $conf);
195 0           $conf;
196             }
197              
198             1;
199              
200             __END__
201              
202             =head1 NAME
203              
204             Egg::Util - Standard method of utility for Egg.
205              
206             =head1 DESCRIPTION
207              
208             This module offers the method of utility for Egg.
209              
210             =head1 METHODS
211              
212             =head2 page_title ([TITLE_STRING])
213              
214             The title of the output contents is set.
215              
216             The value is substituted for $e-E<gt>stash-E<gt>{page_title}.
217              
218             When TITLE_STRING was omitted, the value of the defined value or $e-E<gt>config-E<gt>{title}
219             had already been used.
220              
221             $e->page_title('Hoge Page');
222              
223             =head2 debug
224              
225             True is restored if it is operating by debug mode.
226              
227             =head2 flag
228              
229             Refer to the value of the flag set by the start option.
230             The value cannot be set.
231              
232             use Egg qw/ -MyFlag /;
233              
234             if ($e->flag('MyFlag')) {
235             ...........
236              
237             =head2 snip
238              
239             Refer to the value though L<Egg::Response> divides the URI by '/' at each request
240             and it preserves it as ARRAY reference.
241              
242             my($path1, $path2)= @{$e->snip};
243              
244             =head2 action
245              
246             The ARRAY reference to divide request URI to the place matched with dispatch_map
247             by '/' is returned.
248              
249             my($path1, $path2)= @{$e->action};
250              
251             =head2 stash ([KEY], [VALUE]);
252              
253             It is a place where the common data is treated.
254              
255             When KEY is given, data corresponding to KEY is returned.
256              
257             When KEY and VALUE are given, data corresponding to KEY is set.
258              
259             When the argument is not given, the HASH reference of the common data is
260             returned.
261              
262             my $tmpl= $e->stash('template');
263            
264             $e->stash( template => 'hoge.tt' );
265            
266             my $tmpl= $e->stash->{template};
267              
268             =head2 path_to ([ARG1], [ARG2])
269              
270             When the argument is omitted, the value of $e-E<gt>config-E<gt>{root} is returned.
271             Project route in a word.
272              
273             When ARG1 is given, "$e-E<gt>config-E<gt>{root}/ARG1" is returned.
274              
275             When ARG2 is given, "$e-E<gt>config-E<gt>{dir}{ARG1}/ARG2" is returned.
276              
277             my $project_root= $e->path_to;
278            
279             my $cache_dir = $e->path_to('cache');
280            
281             my $yaml= $e->path_to('etc', 'mydata.yaml');
282              
283             =head2 uri_to ([URI], [ARGS])
284              
285             The result of the URI module is returned.
286              
287             my $uri= $e->uri_to($e->req->host_name);
288              
289             =head2 snip2template ([NUM])
290              
291             The template name is generated with the value to the element given with NUM
292             for $e-E<gt>snip.
293              
294             my $template= $e->snip2template(3);
295              
296             =head2 setup_error_header
297              
298             The content set in call L<Egg::Response> when the error etc. occur is initialized
299             and the header for the error etc. are set.
300              
301             =head2 get_config
302              
303             This is a convenient method to the reference to the configuration in which Egg
304             system module is defined in the parents package.
305              
306             =head2 egg_var ([PARAM], [STRING])
307              
308             It is a method for the use of a peculiar replace function to Egg.
309              
310             The HASH reference for the substituted data is given to PARAM.
311              
312             <e.[name]> of STRING is replaced by the value of PARAM corresponding to [name].
313              
314             Even if it is <e.[name].[name2]>, the key can be handled well.
315              
316             my $param= { data => { hoge=> '123' } };
317             my $text = "abc <e.data.hoge>";
318             $e->egg_var($param, $text);
319             print $text; # -> abc 123
320              
321             =head2 egg_var_deep ([PARAM], [VALUE])
322              
323             It is a method for the use of a peculiar replace function to Egg.
324              
325             Only the character string is treated as for egg_var, and if VALUE is HASH and ARRAY,
326             the contents also recurrently try substituting here.
327              
328             my $param= { hoge=> '123' };
329             my $hash = { data=> 'abc <e.hoge>' };
330             $e->egg_var_deep($param, $hash);
331             print $hash->{data}; # -> abc 123
332              
333             =head2 error ([MESSAGE])
334              
335             To do some error processing, the error message is set.
336              
337             The message is set in $e->errstr and $e-E<gt>stash-E<gt>{error}.
338              
339             Because this method always returns 0, it is not possible to use it to judge the
340             error situation of the occurrence.
341             Please look at $e-E<gt>errstr and $e-E<gt>stash-E<gt>{error}.
342              
343             $e->error('Intarnal Error.');
344            
345             if ($e->errstr) {
346             .......
347              
348             =head2 SEE ALSO
349              
350             L<Egg::Release>,
351             L<Tie::Hash::Indexed>,
352             L<URI>,
353              
354             =head2 AUTHOR
355              
356             Masatoshi Mizuno E<lt>lusheE<64>cpan.orgE<gt>
357              
358             =head2 COPYRIGHT AND LICENSE
359              
360             Copyright (C) 2008 Bee Flag, Corp. E<lt>L<http://egg.bomcity.com/>E<gt>.
361              
362             This library is free software; you can redistribute it and/or modify
363             it under the same terms as Perl itself, either Perl version 5.8.6 or,
364             at your option, any later version of Perl 5 you may have available.
365              
366             =cut
367