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