comparison Lib/IMPL/Web/Application/Resource.pm @ 335:e8be9062ecf2

improved resource classes, contracts are deprecated
author cin
date Thu, 13 Jun 2013 20:13:24 +0400
parents 71221d79e6b4
children ec58c47edb52
comparison
equal deleted inserted replaced
334:71221d79e6b4 335:e8be9062ecf2
11 Exception => 'IMPL::Exception', 11 Exception => 'IMPL::Exception',
12 ArgumentException => '-IMPL::InvalidArgumentException', 12 ArgumentException => '-IMPL::InvalidArgumentException',
13 OperationException => '-IMPL::InvalidOperationException', 13 OperationException => '-IMPL::InvalidOperationException',
14 NotAllowedException => 'IMPL::Web::NotAllowedException', 14 NotAllowedException => 'IMPL::Web::NotAllowedException',
15 NotFoundException => 'IMPL::Web::NotFoundException', 15 NotFoundException => 'IMPL::Web::NotFoundException',
16 Loader => 'IMPL::Code::Loader' 16 Loader => 'IMPL::Code::Loader',
17 CustomResource => '-IMPL::Web::Application::CustomResource'
17 }, 18 },
18 base => [ 19 base => [
19 'IMPL::Object' => undef, 20 'IMPL::Object' => undef,
20 'IMPL::Web::Application::ResourceInterface' => undef 21 'IMPL::Web::Application::ResourceInterface' => undef
21 ], 22 ],
24 application => PROP_RO, 25 application => PROP_RO,
25 parent => PROP_RO, 26 parent => PROP_RO,
26 model => PROP_RO, 27 model => PROP_RO,
27 id => PROP_RO, 28 id => PROP_RO,
28 location => PROP_RO, 29 location => PROP_RO,
29 resources => PROP_RO,
30 verbs => PROP_RO,
31 namedResources => PROP_RO,
32 regexResources => PROP_RO
33 ] 30 ]
34 }; 31 };
35 32
36 sub CTOR { 33 sub CTOR {
37 my ( $this, %args ) = @_; 34 my ( $this, %args ) = @_;
46 $this->request( $args{request} ); 43 $this->request( $args{request} );
47 $this->parent( $args{parent} ); 44 $this->parent( $args{parent} );
48 $this->model( $args{model} ); 45 $this->model( $args{model} );
49 $this->id( $args{id} ); 46 $this->id( $args{id} );
50 $this->application( $args{request}->application ); 47 $this->application( $args{request}->application );
51 $this->verbs( $args{verbs} || {} ); 48
52 $this->resources($args{resources} || []);
53
54 $this->PrepareResourcesCache();
55
56 # если расположение явно не указано, то оно вычисляется автоматически, 49 # если расположение явно не указано, то оно вычисляется автоматически,
57 # либо остается не заданным 50 # либо остается не заданным
58 $this->location( $args{location} 51 $this->location( $args{location}
59 || eval { $this->parent->location->Child( $this->id ) } ); 52 || eval { $this->parent->location->Child( $this->id ) } );
60 } 53 }
61 54
62 sub PrepareResourcesCache {
63 my ($this,$resources) = @_;
64 my %nameMap;
65 my @rxMap;
66
67 foreach my $res (@{$this->resources}) {
68 #skip resources without contract
69 next unless $res->{contract};
70
71 if ( my $name = $res->{name} ) {
72 $nameMap{$name} = $res;
73 }
74 if ( $res->{match} ) {
75 push @rxMap,$res;
76 }
77 }
78
79 $this->regexResources(\@rxMap);
80 $this->namedResources(\%nameMap);
81 }
82
83 sub InvokeHttpVerb { 55 sub InvokeHttpVerb {
84 my ( $this, $verb ) = @_; 56 my ( $this, $verb ) = @_;
85 57
86 my $operation = $this->verbs->{ lc($verb) }; 58 my $operation = $this->verbs->{ lc($verb) };
87 59
88 die NotAllowedException->new( 60 die NotAllowedException->new(
89 allow => join( ',', map( uc, keys %{ $this->verbs } ) ) ) 61 allow => join( ',', $this->GetAllowedMethods ) )
90 unless $operation; 62 unless $operation;
91 63
92 $this->AccessCheck($verb); 64 $this->AccessCheck($verb);
93 my $request = $this->request; 65 my $request = $this->request;
94 66
108 } 80 }
109 81
110 return _InvokeDelegate( $operation, $this, $request ); 82 return _InvokeDelegate( $operation, $this, $request );
111 } 83 }
112 84
85 sub GetAllowedMethods {
86
87 }
88
89 sub FindChildResourceInfo {
90
91 }
92
113 sub AccessCheck { 93 sub AccessCheck {
114 94
115 } 95 }
116 96
117 sub PrepareEnvironment { 97 sub PrepareEnvironment {
125 } 105 }
126 106
127 map $_->SetupEnvironment($env), reverse @stack; 107 map $_->SetupEnvironment($env), reverse @stack;
128 108
129 return $env; 109 return $env;
130 }
131
132 sub FindChildResourceInfo {
133 my ( $this, $name ) = @_;
134
135 if ( my $info = $this->namedResources->{$name} ) {
136 return $info, [$name];
137 }
138 else {
139 foreach my $info ( @{$this->regexResources} ) {
140 my $rx = $info->{match};
141 if(my @childId = $name =~ m/$rx/) {
142 return $info, \@childId;
143 }
144 }
145 }
146
147 return;
148 } 110 }
149 111
150 # это реализация по умолчанию, базируется информации о ресурсах, содержащийся 112 # это реализация по умолчанию, базируется информации о ресурсах, содержащийся
151 # в контракте. 113 # в контракте.
152 sub FetchChildResource { 114 sub FetchChildResource {
184 $args{request} = $this->request; 146 $args{request} = $this->request;
185 147
186 my $factory; 148 my $factory;
187 149
188 if (ref($contract) eq 'HASH') { 150 if (ref($contract) eq 'HASH') {
189 $factory = delete $contract->{class} || __PACKAGE__; 151 $factory = delete $contract->{class} || CustomResource;
190 hashApply(\%args,$contract); 152 hashApply(\%args,$contract);
191 153
192 Loader->default->Require($factory) 154 Loader->default->Require($factory)
193 unless ref($factory); 155 unless ref($factory);
194 } else { 156 } else {