412
|
1 package IMPL::Config::Hierarchy;
|
|
2 use strict;
|
|
3
|
|
4 use IMPL::Exception();
|
|
5 use IMPL::lang qw(:base);
|
|
6 use IMPL::clone;
|
|
7 use IMPL::declare {
|
|
8 base => {
|
|
9 'IMPL::Object' => undef
|
|
10 },
|
|
11 props => {
|
|
12 roles => '*rw',
|
|
13 _cache => '*rw'
|
|
14 }
|
|
15 };
|
|
16
|
|
17 sub CTOR {
|
|
18 my ( $this, $roles ) = @_;
|
|
19
|
|
20 if ( is( $roles, SELF ) ) {
|
|
21 $this->roles( clone( $roles->roles ) );
|
|
22 }
|
|
23 elsif ( ishash($roles) ) {
|
|
24 $this->roles($roles);
|
|
25 }
|
|
26 elsif ( isarray($roles) ) {
|
|
27 $this->roles( { map { $_, 1 } @$roles } );
|
|
28 }
|
|
29 else {
|
|
30 $this->roles( {} );
|
|
31 }
|
|
32 }
|
|
33
|
|
34 sub AddRole {
|
|
35 my ( $this, $role, $parent ) = @_;
|
|
36
|
|
37 $parent = isarray($parent) ? $parent : [$parent]
|
|
38 if $parent;
|
|
39
|
|
40 die IMPL::InvalidArgumentException->new('role') unless $role;
|
|
41
|
|
42 $this->roles->{$role} = $parent;
|
|
43 }
|
|
44
|
|
45 sub GetLinearRoleHash {
|
|
46 my ( $this, $role ) = @_;
|
|
47
|
|
48 return [] unless $role;
|
|
49
|
|
50 my $cache = $this->{$_cache}{$role};
|
|
51
|
|
52 unless ($cache) {
|
414
|
53 $cache = { $role, 1 };
|
412
|
54
|
414
|
55 my @roles = [$role, 1];
|
413
|
56
|
412
|
57 while (my $r = shift @roles ) {
|
413
|
58 my ($name, $level) = @$r;
|
|
59
|
|
60 $cache->{$name} = $level;
|
|
61 if(my $parents = $this->{$roles}{$name}) {
|
|
62 foreach my $p (@$parents) {
|
|
63 next if $cache{$p};
|
|
64 push @roles, [$p, $cache{$p} = $level + 1];
|
|
65 }
|
412
|
66 }
|
|
67 }
|
|
68 $this->{$_cache}{$role} = $cache;
|
|
69 }
|
|
70
|
|
71 return $cache;
|
|
72 }
|
|
73
|
|
74 1;
|