lib/Mojolicious/Plugin/YamlRoutes.pm

filter: Statementstmt 86.0% 37 / 43 Branchbran 70.0% 14 / 20 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 6 / 6 Podpod 100% 1 / 1 totaltotal 82.8% 58 / 70 CC 14 SCAR 17.5
CC
14
Cov
81%
_read_yaml
8.6
register
5.5
_dir_yaml
3.0
CRAP 5.7
↑ Summary
1 package Mojolicious::Plugin::YamlRoutes;
2  
3 1 ▶use Mojo::Base 'Mojolicious::Plugin';
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use Mojo::File 'path';
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use YAML::XS 'LoadFile';
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 1 our $VERSION = '0.01';
8  
9 2 ▶sub register {
Subroutine register: called CC=5 SCAR=17.1
Pod documented
10 2 my ($self, $app, $conf) = @_;
11  
12 2 ▶ return $self->_read_yaml($app,$app->home->rel_file($conf->{file})) if $conf->{file};
Branch: 1 branch
BranchTrueFalseTotal
if $conf->{"file"} 1 1 2
13  
14 1 ▶ if ( $conf->{directory} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($conf->{"directory"}) 1 0 1
15 1 my $dir = $app->home->rel_file($conf->{directory});
16  
17 1 ▶ $dir .= '/' unless $dir =~ /\/$/;
Branch: 1 branch
BranchTrueFalseTotal
unless $dir =~ m[/$]u 1 0 1
18  
19 ## Check is directory
20 1 ▶ unless ( -d $dir ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-d $dir) 0 1 1
21 0 $app->log->debug((caller(0))[3].' => Directory not found : '.$dir);
22 0 return;
23 }
24
25 1 $self->_dir_yaml($app,$dir);
26 }
27  
28 ## No args register helpers
29 1 $app->helper(RouteYaml => \&_read_yaml);
30
31 }
32  
33 1 ▶sub _dir_yaml {
Subroutine _dir_yaml: called CC=3 SCAR=11.1
34 1 my ($self, $app, $dir) = @_;
35
36 1 for my $path ( path($dir)->list->each ) {
37 1 ▶ next unless $path->basename =~ /\.yaml$/;
Branch: 1 branch
BranchTrueFalseTotal
unless $path->basename =~ /\.yaml$/u 0 1 1
38 1 $app = $self->_read_yaml($app,$dir.$path->basename);
39 }
40 }
41  
42 2 ▶sub _read_yaml {
Subroutine _read_yaml: called CC=8 SCAR=21.6
43 2 my ($self, $app, $yaml) = @_;
44  
45 2 ▶ unless ( -e $yaml ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-e $yaml) 0 2 2
46 0 $app->log->debug((caller(0))[3].'Error file not found : '.$yaml);
47 0 return;
48 }
49  
50 2 my $RoutesYaml = LoadFile $yaml;
51  
52 2 foreach my $routes_init ( sort keys %{ $RoutesYaml->{routes} } ){
53  
54 4 foreach my $menu_item ( sort keys %{ $RoutesYaml->{routes}->{$routes_init} } ){
55  
56 # GET by default
57 8 ▶ $RoutesYaml->{routes}->{$routes_init}->{$menu_item}->{via} = 'GET' unless $RoutesYaml->{routes}->{$routes_init}->{$menu_item}->{via};
Branch: 1 branch
BranchTrueFalseTotal
unless $RoutesYaml->{"routes"}{$routes_init}{$menu_item}{"via"} 4 4 8
58  
59 8 ▶ if ( $RoutesYaml->{routes}->{$routes_init}->{$menu_item}->{via} eq 'GET' ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($RoutesYaml->{'routes'}{$routes_init}{$menu_item}{'via'} eq 'GET') { } 4 4 8
elsif ($RoutesYaml->{'routes'}{$routes_init}{$menu_item}{'via'} eq 'POST') { } 2 2 4
60 4 $app->routes->get( $RoutesYaml->{routes}->{$routes_init}->{$menu_item}->{route})->to(controller => $routes_init, action => $menu_item );
61 } elsif ( $RoutesYaml->{routes}->{$routes_init}->{$menu_item}->{via} eq 'POST' ) {
62 2 $app->routes->post( $RoutesYaml->{routes}->{$routes_init}->{$menu_item}->{route})->to( controller => $routes_init, action => $menu_item );
63 } else {
64 2 $app->routes->any( ['GET', 'POST'] => $RoutesYaml->{routes}->{$routes_init}->{$menu_item}->{route})->to( controller => $routes_init, action => $menu_item );
65 }
66  
67 8 ▶ if ( $RoutesYaml->{routes}->{$routes_init}->{$menu_item}->{r0} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($RoutesYaml->{"routes"}{$routes_init}{$menu_item}{"r0"}) 0 8 8
68 0 say qq{$RoutesYaml->{routes}->{$routes_init}->{$menu_item}->{route} - $routes_init - $menu_item};
69 0 $app->routes->get( $RoutesYaml->{routes}->{$routes_init}->{$menu_item}->{r0})->any( ['GET', 'POST'] => $RoutesYaml->{routes}->{$routes_init}->{$menu_item}->{route})->to( controller => $routes_init , action => $menu_item );
70 }
71
72 }
73 }
74  
75 2 return $app;
76 }
77  
78 1 1;
79  
80 =encoding utf8
81  
82 =head1 NAME
83  
84 Mojolicious::Plugin::YamlRoutes - Generates routes from a yaml file
85  
86 =head1 SYNOPSIS
87  
88
89 $self->plugin(YamlRoutes => { directory => 'config/routes/' } );
90 $self->plugin(YamlRoutes => { file => 'routes.yaml'} );
91  
92 $self->plugin(YamlRoutes);
93  
94 =head1 DESCRIPTION
95  
96 L<Mojolicious::Plugin::YamlRoutes> Generate routes in Mojoliciouse based a yaml file
97  
98 By default all http methods are GET can be force in the var 'via'.
99  
100 routes:
101 Example:
102 Tester:
103 route: /tester
104 Example::Tester:
105 Tester:
106 route: /example/teste
107 Post:
108 route: /tester/post
109 via: POST
110 Any:
111 route: /tester/any
112 via: ANY
113  
114 =head1 METHODS
115  
116 L<Mojolicious::Plugin::YamlRoutes> inherits all methods from
117 L<Mojolicious::Plugin> and implements the following new ones.
118  
119 =head2 register
120  
121 $plugin->register(Mojolicious->new);
122  
123 Register plugin in L<Mojolicious> application.
124  
125 =head1 SEE ALSO
126  
127 L<Mojolicious>, L<Mojolicious::Guides>, L<https://mojolicious.org>.
128  
129 =cut
↑ Summary