Fixed pod and added Catalyst::Utils::appprefix
[catagits/Catalyst-Runtime.git] / lib / Catalyst / Utils.pm
1 package Catalyst::Utils;
2
3 use strict;
4 use attributes ();
5 use Catalyst::Exception;
6 use File::Spec;
7 use HTTP::Request;
8 use Path::Class;
9 use URI;
10
11 =head1 NAME
12
13 Catalyst::Utils - The Catalyst Utils
14
15 =head1 SYNOPSIS
16
17 See L<Catalyst>.
18
19 =head1 DESCRIPTION
20
21 =head1 METHODS
22
23 =over 4
24
25 =item appprefix($class)
26
27 Returns the application prefix for the class
28
29 =cut
30
31 sub appprefix {
32     my $class = shift;
33     $class =~ s/\:\:/_/g;
34     $class = lc($class);
35     return $class;
36 }
37
38 =item attrs($coderef)
39
40 Returns attributes for coderef in a arrayref
41
42 =cut
43
44 sub attrs { attributes::get( $_[0] ) || [] }
45
46 =item class2appclass($class);
47
48 Returns the appclass for class.
49
50     MyApp::C::Foo::Bar becomes MyApp
51     My::App::C::Foo::Bar becomes My::App
52
53 =cut
54
55 sub class2appclass {
56     my $class = shift || '';
57     my $appname = '';
58     if ( $class =~ /^(.*)::([MVC]|Model|View|Controller)?::.*$/ ) {
59         $appname = $1;
60     }
61     return $appname;
62 }
63
64 =item class2classprefix($class);
65
66 Returns the classprefix for class.
67
68     MyApp::C::Foo::Bar becomes MyApp::C
69     My::App::C::Foo::Bar becomes My::App::C
70
71 =cut
72
73 sub class2classprefix {
74     my $class = shift || '';
75     my $prefix;
76     if ( $class =~ /^(.*::[MVC]|Model|View|Controller)?::.*$/ ) {
77         $prefix = $1;
78     }
79     return $prefix;
80 }
81
82 =item class2classsuffix($class);
83
84 Returns the classsuffix for class.
85
86     MyApp::C::Foo::Bar becomes C::Foo::Bar
87
88 =cut
89
90 sub class2classsuffix {
91     my $class = shift || '';
92     my $prefix = class2appclass($class) || '';
93     $class =~ s/$prefix\:\://;
94     return $class;
95 }
96
97 =item class2env($class);
98
99 Returns the environment name for class.
100
101     MyApp becomes MYAPP
102     My::App becomes MY_APP
103
104 =cut
105
106 sub class2env {
107     my $class = shift || '';
108     $class =~ s/\:\:/_/g;
109     return uc($class);
110 }
111
112 =item class2prefix( $class, $case );
113
114 Returns the prefix for class.
115
116     My::App::C::Foo::Bar becomes /foo/bar
117
118 =cut
119
120 sub class2prefix {
121     my $class = shift || '';
122     my $case  = shift || 0;
123     my $prefix;
124     if ( $class =~ /^.*::([MVC]|Model|View|Controller)?::(.*)$/ ) {
125         $prefix = $case ? $2 : lc $2;
126         $prefix =~ s/\:\:/\//g;
127     }
128     return $prefix;
129 }
130
131 =item class2tempdir( $class [, $create ] );
132
133 Returns a tempdir for class. If create is true it will try to create the path.
134
135     My::App becomes /tmp/my/app
136     My::App::C::Foo::Bar becomes /tmp/my/app/c/foo/bar
137
138 =cut
139
140 sub class2tempdir {
141     my $class  = shift || '';
142     my $create = shift || 0;
143     my @parts = split '::', lc $class;
144
145     my $tmpdir = dir( File::Spec->tmpdir, @parts )->cleanup;
146
147     if ( $create && !-e $tmpdir ) {
148
149         eval { $tmpdir->mkpath };
150
151         if ($@) {
152             Catalyst::Exception->throw(
153                 message => qq/Couldn't create tmpdir '$tmpdir', "$@"/ );
154         }
155     }
156
157     return $tmpdir->stringify;
158 }
159
160 =item home($class)
161
162 Returns home directory for given class.
163
164 =cut
165
166 sub home {
167     my $name = shift;
168     $name =~ s/\:\:/\//g;
169     my $home = 0;
170     if ( my $path = $INC{"$name.pm"} ) {
171         $home = file($path)->absolute->dir;
172         $name =~ /(\w+)$/;
173         my $append = $1;
174         my $subdir = dir($home)->subdir($append);
175         for ( split '/', $name ) { $home = dir($home)->parent }
176         if ( $home =~ /blib$/ ) { $home = dir($home)->parent }
177         elsif (!-f file( $home, 'Makefile.PL' )
178             && !-f file( $home, 'Build.PL' ) )
179         {
180             $home = $subdir;
181         }
182
183         # clean up relative path:
184         # MyApp/script/.. -> MyApp
185         my ($lastdir) = $home->dir_list( -1, 1 );
186         if ( $lastdir eq '..' ) {
187             $home = dir($home)->parent->parent;
188         }
189     }
190     return $home;
191 }
192
193 =item prefix($class, $name);
194
195 Returns a prefixed action.
196
197     MyApp::C::Foo::Bar, yada becomes /foo/bar/yada
198
199 =cut
200
201 sub prefix {
202     my ( $class, $name ) = @_;
203     my $prefix = &class2prefix($class);
204     $name = "$prefix/$name" if $prefix;
205     return $name;
206 }
207
208 =item reflect_actions($class);
209
210 Returns an arrayref containing all actions of a component class.
211
212 =cut
213
214 sub reflect_actions {
215     my $class   = shift;
216     my $actions = [];
217     eval '$actions = $class->_action_cache';
218
219     if ($@) {
220         Catalyst::Exception->throw( message =>
221               qq/Couldn't reflect actions of component "$class", "$@"/ );
222     }
223
224     return $actions;
225 }
226
227 =back
228
229 =head1 AUTHOR
230
231 Sebastian Riedel, C<sri@cpan.org>
232
233 =head1 COPYRIGHT
234
235 This program is free software, you can redistribute it and/or modify it under
236 the same terms as Perl itself.
237
238 =cut
239
240 1;