1 |
8 |
ahitrov@rambler.ru |
package Contenido::MVC::DispatchType::Regex; |
2 |
|
|
|
3 |
|
|
use strict; |
4 |
|
|
use base 'Contenido::MVC::DispatchType'; |
5 |
|
|
use Data::Dumper; |
6 |
|
|
|
7 |
|
|
sub match { |
8 |
|
|
my $self = shift; |
9 |
|
|
my $path = shift || '/'; |
10 |
|
|
|
11 |
|
|
my $compiled = $self->{compiled} || []; |
12 |
|
|
|
13 |
|
|
foreach my $comp (@$compiled) { |
14 |
|
|
if ( my @snippets = ( $path =~ $comp->{regex} ) ) { |
15 |
|
|
return { |
16 |
|
|
'sub' => $comp->{'sub'}, |
17 |
|
|
'path' => $path, |
18 |
|
|
'controller_class' => $comp->{'controller_class'}, |
19 |
|
|
'snippets' => \@snippets, |
20 |
|
|
(exists $comp->{'begin'} ? ('begin' => $comp->{'begin'}) : ()), |
21 |
|
|
(exists $comp->{'end'} ? ('end' => $comp->{'end'}) : ()), |
22 |
|
|
}; |
23 |
|
|
} |
24 |
|
|
} |
25 |
|
|
return undef; |
26 |
|
|
} |
27 |
|
|
|
28 |
|
|
sub register { |
29 |
|
|
my $self = shift; |
30 |
|
|
my $action = shift; |
31 |
|
|
|
32 |
|
|
return undef unless $action && ref($action) eq 'HASH'; |
33 |
|
|
|
34 |
|
|
push @{ $self->{compiled} }, { |
35 |
|
|
'regex' => qr#$action->{'path'}#, |
36 |
|
|
'sub' => $action->{'sub'}, |
37 |
|
|
'sorder' => defined $action->{'sorder'} ? $action->{'sorder'} : 1000, |
38 |
|
|
'controller_class' => $action->{'controller_class'}, |
39 |
|
|
(exists $action->{'begin'} ? ('begin' => $action->{'begin'}) : ()), |
40 |
|
|
(exists $action->{'end'} ? ('end' => $action->{'end'}) : ()), |
41 |
|
|
}; |
42 |
|
|
|
43 |
|
|
return 1; |
44 |
|
|
} |
45 |
|
|
|
46 |
|
|
sub after_register { |
47 |
|
|
my $self = shift; |
48 |
|
|
|
49 |
|
|
my $compiled = $self->{compiled} || []; |
50 |
|
|
$self->{compiled} = [ sort { $a->{sorder} <=> $b->{sorder} } @$compiled ]; |
51 |
|
|
|
52 |
|
|
return 1; |
53 |
|
|
} |
54 |
|
|
|
55 |
|
|
1; |