1 package Catalyst::Utils;
4 use Catalyst::Exception;
13 Catalyst::Utils - The Catalyst Utils
23 =head2 appprefix($class)
25 MyApp::Foo becomes myapp_foo
36 =head2 class2appclass($class);
38 MyApp::C::Foo::Bar becomes MyApp
39 My::App::C::Foo::Bar becomes My::App
44 my $class = shift || '';
46 if ( $class =~ /^(.*)::([MVC]|Model|View|Controller)?::.*$/ ) {
52 =head2 class2classprefix($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 =head2 class2classsuffix($class);
70 MyApp::C::Foo::Bar becomes C::Foo::Bar
74 sub class2classsuffix {
75 my $class = shift || '';
76 my $prefix = class2appclass($class) || '';
77 $class =~ s/$prefix\:\://;
81 =head2 class2env($class);
83 Returns the environment name for class.
86 My::App becomes MY_APP
91 my $class = shift || '';
96 =head2 class2prefix( $class, $case );
98 Returns the uri prefix for a class. If case is false the prefix is converted to lowercase.
100 My::App::C::Foo::Bar becomes foo/bar
105 my $class = shift || '';
106 my $case = shift || 0;
108 if ( $class =~ /^.*::([MVC]|Model|View|Controller)?::(.*)$/ ) {
109 $prefix = $case ? $2 : lc $2;
110 $prefix =~ s/\:\:/\//g;
115 =head2 class2tempdir( $class [, $create ] );
117 Returns a tempdir for a class. If create is true it will try to create the path.
119 My::App becomes /tmp/my/app
120 My::App::C::Foo::Bar becomes /tmp/my/app/c/foo/bar
125 my $class = shift || '';
126 my $create = shift || 0;
127 my @parts = split '::', lc $class;
129 my $tmpdir = dir( File::Spec->tmpdir, @parts )->cleanup;
131 if ( $create && !-e $tmpdir ) {
133 eval { $tmpdir->mkpath };
136 Catalyst::Exception->throw(
137 message => qq/Couldn't create tmpdir '$tmpdir', "$@"/ );
141 return $tmpdir->stringify;
146 Returns home directory for given class.
153 # make an $INC{ $key } style string from the class name
154 (my $file = "$class.pm") =~ s{::}{/}g;
156 if ( my $inc_entry = $INC{$file} ) {
158 # look for an uninstalled Catalyst app
160 # find the @INC entry in which $file was found
161 (my $path = $inc_entry) =~ s/$file$//;
162 my $home = dir($path)->absolute->cleanup;
164 # pop off /lib and /blib if they're there
165 $home = $home->parent while $home =~ /b?lib$/;
167 # only return the dir if it has a Makefile.PL or Build.PL
168 if (-f $home->file("Makefile.PL") or -f $home->file("Build.PL")) {
170 # clean up relative path:
171 # MyApp/script/.. -> MyApp
173 my ($lastdir) = $home->dir_list( -1, 1 );
174 if ( $lastdir eq '..' ) {
175 $home = dir($home)->parent->parent;
178 return $home->stringify;
183 # look for an installed Catalyst app
185 # trim the .pm off the thing ( Foo/Bar.pm -> Foo/Bar/ )
186 ( my $path = $inc_entry) =~ s/\.pm$//;
187 my $home = dir($path)->absolute->cleanup;
189 # return if if it's a valid directory
190 return $home->stringify if -d $home;
198 =head2 prefix($class, $name);
200 Returns a prefixed action.
202 MyApp::C::Foo::Bar, yada becomes foo/bar/yada
207 my ( $class, $name ) = @_;
208 my $prefix = &class2prefix($class);
209 $name = "$prefix/$name" if $prefix;
215 Returns an L<HTTP::Request> object for a uri.
221 unless ( ref $request ) {
222 if ( $request =~ m/^http/i ) {
223 $request = URI->new($request)->canonical;
226 $request = URI->new( 'http://localhost' . $request )->canonical;
229 unless ( ref $request eq 'HTTP::Request' ) {
230 $request = HTTP::Request->new( 'GET', $request );
235 =head2 ensure_class_loaded($class_name)
237 Loads the class unless it already has been loaded.
241 sub ensure_class_loaded {
245 return if !$opts->{ ignore_loaded }
246 && Class::Inspector->loaded( $class ); # if a symbol entry exists we don't load again
248 # this hack is so we don't overwrite $@ if the load did not generate an error
252 eval "require $class";
256 die $error if $error;
257 die "require $class was successful but the package is not defined"
258 unless Class::Inspector->loaded($class);
263 =head2 merge_hashes($hashref, $hashref)
265 Base code to recursively merge two hashes together with right-hand precedence.
270 my ( $lefthash, $righthash ) = @_;
272 return $lefthash unless defined $righthash;
274 my %merged = %$lefthash;
275 for my $key ( keys %$righthash ) {
\r
276 my $right_ref = ( ref $righthash->{ $key } || '' ) eq 'HASH';
\r
277 my $left_ref = ( ( exists $lefthash->{ $key } && ref $lefthash->{ $key } ) || '' ) eq 'HASH';
\r
278 if( $right_ref and $left_ref ) {
\r
279 $merged{ $key } = merge_hashes(
280 $lefthash->{ $key }, $righthash->{ $key }
284 $merged{ $key } = $righthash->{ $key };
294 Sebastian Riedel, C<sri@cpan.org>
295 Yuval Kogman, C<nothingmuch@woobling.org>
299 This program is free software, you can redistribute it and/or modify it under
300 the same terms as Perl itself.