| 1 | # | ||
| 2 | # This file is part of CatalystX-ExtJS-Direct | ||
| 3 | # | ||
| 4 | # This software is Copyright (c) 2014 by Moritz Onken. | ||
| 5 | # | ||
| 6 | # This is free software, licensed under: | ||
| 7 | # | ||
| 8 | # The (three-clause) BSD License | ||
| 9 | # | ||
| 10 | package CatalystX::Controller::ExtJS::Direct::Route; | ||
| 11 | $CatalystX::Controller::ExtJS::Direct::Route::VERSION = '2.1.5'; | ||
| 12 | #ABSTRACT: Ext.Direct route object | ||
| 13 | use Moose; | ||
| 14 | |||
| 15 | has 'arguments' => ( is => 'rw', isa => 'Int', lazy_build => 1 ); | ||
| 16 | has 'action' => ( is => 'ro', required => 1 ); | ||
| 17 | has 'name' => ( is => 'rw', lazy_build => 1 ); | ||
| 18 | has 'dispatcher' => ( is => 'rw', weak_ref => 1 ); | ||
| 19 | has 'form_handler' => ( is => 'rw', default => 0 ); | ||
| 20 | |||
| 21 | sub _build_name { | ||
| 22 | my ($self) = @_; | ||
| 23 | return $self->action->attributes->{Direct}->[0] || $self->action->name; | ||
| 24 | } | ||
| 25 | |||
| 26 | sub _build_arguments { | ||
| 27 | my ($self) = @_; | ||
| 28 | return $self->action->attributes->{DirectArgs}->[0] || 0; | ||
| 29 | } | ||
| 30 | |||
| 31 | sub build_api { | ||
| 32 | my ($self) = @_; | ||
| 33 | my $fh = $self->form_handler || exists $self->action->attributes->{FormHandler}; | ||
| 34 | return { name => $self->name, len => $self->arguments + 0, $fh ? ( formHandler => \1 ) : () }; | ||
| 35 | } | ||
| 36 | |||
| 37 | sub build_url { | ||
| 38 | my ( $self, $data ) = @_; | ||
| 39 | return $self->action; | ||
| 40 | } | ||
| 41 | |||
| 42 | sub build { | ||
| 43 | return shift->new(@_); | ||
| 44 | } | ||
| 45 | |||
| 46 | sub request { | ||
| 47 | my ($self, $req) = @_; | ||
| 48 | return ( data => $req->{data}); | ||
| 49 | } | ||
| 50 | |||
| 51 | sub prepare_request { | ||
| 52 | shift; | ||
| 53 | return @_; | ||
| 54 | } | ||
| 55 | |||
| 56 | package CatalystX::Controller::ExtJS::Direct::Route::Chained; | ||
| 57 | $CatalystX::Controller::ExtJS::Direct::Route::Chained::VERSION = '2.1.5'; | ||
| 58 | use Moose::Role; | ||
| 59 | |||
| 60 | sub _build_arguments { | ||
| 61 | my ($self) = @_; | ||
| 62 | my $action = $self->action; | ||
| 63 | my $len = $action->attributes->{Args}[0] || 0; | ||
| 64 | my $parent = $action; | ||
| 65 | while ( | ||
| 66 | $parent->attributes->{Chained} | ||
| 67 | && ( | ||
| 68 | $parent = $self->dispatcher->get_action_by_path( | ||
| 69 | $parent->attributes->{Chained}->[0] | ||
| 70 | ) | ||
| 71 | ) | ||
| 72 | ) | ||
| 73 | { | ||
| 74 | |||
| 75 | $len += $parent->attributes->{CaptureArgs}[0]; | ||
| 76 | } | ||
| 77 | $len += $self->action->attributes->{DirectArgs}->[0] | ||
| 78 | if($self->action->attributes->{DirectArgs}); | ||
| 79 | return $len || 0; | ||
| 80 | } | ||
| 81 | |||
| 82 | sub build_url { | ||
| 83 | my ( $route, $data ) = @_; | ||
| 84 | my @data = @{ $data || [] }; | ||
| 85 | @data = grep { !ref $_ } @data; | ||
| 86 | my $captures_length = | ||
| 87 | defined $route->action->attributes->{Args}->[0] | ||
| 88 | ? $route->arguments - $route->action->attributes->{Args}->[0] | ||
| 89 | : 0; | ||
| 90 | my @captures = splice( @data, 0, $captures_length ); | ||
| 91 | return $route->action, \@captures, \@data; | ||
| 92 | } | ||
| 93 | |||
| 94 | package CatalystX::Controller::ExtJS::Direct::Route::REST; | ||
| 95 | $CatalystX::Controller::ExtJS::Direct::Route::REST::VERSION = '2.1.5'; | ||
| 96 | use Moose::Role; | ||
| 97 | |||
| 98 | has 'crud_action' => ( is => 'rw', isa => 'Str' ); | ||
| 99 | |||
| 100 | has 'crud_methods' => ( | ||
| 101 | is => 'rw', | ||
| 102 | isa => 'HashRef', | ||
| 103 | default => sub { | ||
| 104 | { | ||
| 105 | create => 'POST', | ||
| 106 | update => 'PUT', | ||
| 107 | read => 'GET', | ||
| 108 | destroy => 'DELETE', | ||
| 109 | }; | ||
| 110 | } | ||
| 111 | ); | ||
| 112 | |||
| 113 | around '_build_arguments' => sub { | ||
| 114 | my ( $orig, $self, $args ) = @_; | ||
| 115 | my $arguments = $self->$orig(); | ||
| 116 | $arguments++; | ||
| 117 | return $arguments; | ||
| 118 | }; | ||
| 119 | |||
| 120 | sub _build_name { | ||
| 121 | my ($self) = @_; | ||
| 122 | return $self->crud_action; | ||
| 123 | } | ||
| 124 | |||
| 125 | sub build { | ||
| 126 | my ( $class, $args ) = @_; | ||
| 127 | my @routes; | ||
| 128 | foreach my $action (qw(create read update destroy)) { | ||
| 129 | push( @routes, $class->new( { %$args, crud_action => $action } ) ); | ||
| 130 | } | ||
| 131 | push( @routes, | ||
| 132 | CatalystX::Controller::ExtJS::Direct::Route->new( { %$args, name => 'submit', form_handler => 1 } ) ); | ||
| 133 | return @routes; | ||
| 134 | } | ||
| 135 | |||
| 136 | around 'request' => sub { | ||
| 137 | my ($orig, $self, $req) = @_; | ||
| 138 | my %params = $self->$orig($req); | ||
| 139 | return ( | ||
| 140 | %params, | ||
| 141 | method => $self->crud_methods->{ $self->crud_action }, | ||
| 142 | accepted_content_types => ['application/json'], | ||
| 143 | content_types => ['application/json'], | ||
| 144 | content_type => 'application/json' | ||
| 145 | ); | ||
| 146 | |||
| 147 | }; | ||
| 148 | |||
| 149 | # split a request in multiple requests if they affect more than one record | ||
| 150 | |||
| 151 | sub prepare_request { | ||
| 152 | my ($self, $req) = @_; | ||
| 153 | $req->{data} = [$req->{data}] unless(ref $req->{data} eq 'ARRAY'); | ||
| 154 | unless (@{$req->{data} || []}) { | ||
| 155 | return $req; | ||
| 156 | } | ||
| 157 | my $read_or_destroy = $self->crud_action eq 'read' || $self->crud_action eq 'destroy'; | ||
| 158 | my $create = $self->crud_action eq 'create'; | ||
| 159 | my $data = $req->{data}->[-1]; | ||
| 160 | if(ref $data eq 'HASH' && keys %$data == 1) { | ||
| 161 | my ($key) = keys %$data; | ||
| 162 | if(ref $data->{$key} eq 'HASH' && !$read_or_destroy) { | ||
| 163 | $req->{data} = $data->{$key}; | ||
| 164 | } elsif ( ref $data->{$key} eq 'ARRAY' ) { | ||
| 165 | return map { {%$req, data => $_} } @{$data->{$key}}; | ||
| 166 | } elsif ((!ref $data->{$key} || !$read_or_destroy) && !$create) { | ||
| 167 | $req->{data} = $data->{$key}; | ||
| 168 | } | ||
| 169 | } | ||
| 170 | return $req; | ||
| 171 | } | ||
| 172 | |||
| 173 | package CatalystX::Controller::ExtJS::Direct::Route::REST::ExtJS; | ||
| 174 | $CatalystX::Controller::ExtJS::Direct::Route::REST::ExtJS::VERSION = '2.1.5'; | ||
| 175 | use Moose::Role; | ||
| 176 | |||
| 177 | package CatalystX::Controller::ExtJS::Direct::Route::Factory; | ||
| 178 | $CatalystX::Controller::ExtJS::Direct::Route::Factory::VERSION = '2.1.5'; | ||
| 179 | sub build { | ||
| 180 | my ( $class, $dispatcher, $action ) = @_; | ||
| 181 | my $params = { action => $action, dispatcher => $dispatcher }; | ||
| 182 | my @roles; | ||
| 183 | if ( $action->attributes->{Chained} ) { | ||
| 184 | push( @roles, 'Chained' ); | ||
| 185 | } | ||
| 186 | if ( $action->attributes->{ActionClass} | ||
| 187 | && ($action->attributes->{ActionClass}->[0] eq 'Catalyst::Action::REST' | ||
| 188 | || $action->attributes->{ActionClass}->[0] eq 'CatalystX::Action::ExtJS::REST') ) | ||
| 189 | { | ||
| 190 | push( @roles, 'REST' ); | ||
| 191 | } | ||
| 192 | if ( $action->name eq 'object' | ||
| 193 | && $action->class->isa('CatalystX::Controller::ExtJS::REST') ) | ||
| 194 | { | ||
| 195 | push( @roles, 'REST::ExtJS' ); | ||
| 196 | } | ||
| 197 | @roles = | ||
| 198 | map { $_ = 'CatalystX::Controller::ExtJS::Direct::Route::' . $_ } @roles; | ||
| 199 | my $anon_class = Moose::Meta::Class->create_anon_class( | ||
| 200 | superclasses => [qw(CatalystX::Controller::ExtJS::Direct::Route)], | ||
| 201 | ( @roles ? ( roles => [@roles] ) : () ), | ||
| 202 | cache => 1, | ||
| 203 | ); | ||
| 204 | return $anon_class->find_method_by_name('build') | ||
| 205 | ->execute( $anon_class->name, $params ); | ||
| 206 | |||
| 207 | } | ||
| 208 | |||
| 209 | 1; | ||
| 210 | |||
| 211 | __END__ |