1 package Catalyst::Utils;
5 use Catalyst::Exception;
12 Catalyst::Utils - The Catalyst Utils
26 Returns attributes for coderef in a arrayref
30 sub attrs { attributes::get( $_[0] ) || [] }
32 =item class2appclass($class);
34 Returns the appclass for class.
36 MyApp::C::Foo::Bar becomes MyApp
37 My::App::C::Foo::Bar becomes My::App
42 my $class = shift || '';
44 if ( $class =~ /^(.*)::([MVC]|Model|View|Controller)?::.*$/ ) {
50 =item class2classprefix($class);
52 Returns the classprefix for class.
54 MyApp::C::Foo::Bar becomes MyApp::C
55 My::App::C::Foo::Bar becomes My::App::C
59 sub class2classprefix {
60 my $class = shift || '';
62 if ( $class =~ /^(.*::[MVC]|Model|View|Controller)?::.*$/ ) {
68 =item class2classsuffix($class);
70 Returns the classsuffix for class.
72 MyApp::C::Foo::Bar becomes C::Foo::Bar
76 sub class2classsuffix {
77 my $class = shift || '';
78 my $prefix = class2appclass($class) || '';
79 $class =~ s/$prefix\:\://;
83 =item class2env($class);
85 Returns the enviroment name for class.
88 My::App becomes MY_APP
93 my $class = shift || '';
94 my $class =~ s/\:\:/_/g;
98 =item class2prefix( $class, $case );
100 Returns the prefix for class.
102 My::App::C::Foo::Bar becomes /foo/bar
107 my $class = shift || '';
108 my $case = shift || 0;
110 if ( $class =~ /^.*::([MVC]|Model|View|Controller)?::(.*)$/ ) {
111 $prefix = $case ? $2 : lc $2;
112 $prefix =~ s/\:\:/\//g;
119 Returns home directory for given class.
125 $name =~ s/\:\:/\//g;
127 if ( my $path = $INC{"$name.pm"} ) {
128 $home = file($path)->absolute->dir;
131 my $subdir = dir($home)->subdir($append);
132 for ( split '/', $name ) { $home = dir($home)->parent }
133 if ( $home =~ /blib$/ ) { $home = dir($home)->parent }
134 elsif (!-f file( $home, 'Makefile.PL' )
135 && !-f file( $home, 'Build.PL' ) )
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;
149 =item prefix($class, $name);
151 Returns a prefixed action.
153 MyApp::C::Foo::Bar, yada becomes /foo/bar/yada
158 my ( $class, $name ) = @_;
159 my $prefix = &class2prefix($class);
160 $name = "$prefix/$name" if $prefix;
164 =item reflect_actions($class);
166 Returns an arrayref containing all actions of a component class.
170 sub reflect_actions {
173 eval '$actions = $class->_action_cache';
176 Catalyst::Exception->throw(
177 message => qq/Couldn't reflect actions of component "$class", "$@"/
184 =item request($string);
186 Returns an C<HTTP::Request> from a string.
193 unless ( ref $request ) {
195 if ( $request =~ m/http/i ) {
196 $request = URI->new($request)->canonical;
199 $request = URI->new( 'http://localhost' . $request )->canonical;
203 unless ( ref $request eq 'HTTP::Request' ) {
204 $request = HTTP::Request->new( 'GET', $request );
214 Sebastian Riedel, C<sri@cpan.org>
218 This program is free software, you can redistribute it and/or modify it under
219 the same terms as Perl itself.