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