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 class2prefix( $class, $case );
85 Returns the prefix for class.
87 My::App::C::Foo::Bar becomes /foo/bar
92 my $class = shift || '';
93 my $case = shift || 0;
95 if ( $class =~ /^.*::([MVC]|Model|View|Controller)?::(.*)$/ ) {
96 $prefix = $case ? $2 : lc $2;
97 $prefix =~ s/\:\:/\//g;
104 Returns home directory for given class.
110 $name =~ s/\:\:/\//g;
112 if ( my $path = $INC{"$name.pm"} ) {
113 $home = file($path)->absolute->dir;
116 my $subdir = dir($home)->subdir($append);
117 for ( split '/', $name ) { $home = dir($home)->parent }
118 if ( $home =~ /blib$/ ) { $home = dir($home)->parent }
119 elsif (!-f file( $home, 'Makefile.PL' )
120 && !-f file( $home, 'Build.PL' ) )
124 # clean up relative path:
125 # MyApp/script/.. -> MyApp
126 my ($lastdir) = $home->dir_list( -1, 1 );
127 if ( $lastdir eq '..' ) {
128 $home = dir($home)->parent->parent;
134 =item prefix($class, $name);
136 Returns a prefixed action.
138 MyApp::C::Foo::Bar, yada becomes /foo/bar/yada
143 my ( $class, $name ) = @_;
144 my $prefix = &class2prefix($class);
145 $name = "$prefix/$name" if $prefix;
149 =item reflect_actions($class);
151 Returns an arrayref containing all actions of a component class.
155 sub reflect_actions {
158 eval '$actions = $class->_action_cache';
161 Catalyst::Exception->throw(
162 message => qq/Couldn't reflect actions of component "$class", "$@"/
169 =item request($string);
171 Returns an C<HTTP::Request> from a string.
178 unless ( ref $request ) {
180 if ( $request =~ m/http/i ) {
181 $request = URI->new($request)->canonical;
184 $request = URI->new( 'http://localhost' . $request )->canonical;
188 unless ( ref $request eq 'HTTP::Request' ) {
189 $request = HTTP::Request->new( 'GET', $request );
199 Sebastian Riedel, C<sri@cpan.org>
203 This program is free software, you can redistribute it and/or modify it under
204 the same terms as Perl itself.