Mercurial > pub > Impl
comparison lib/IMPL/Config/Hierarchy.pm @ 412:30e8c6a74937 ref20150831
working on di container (role based registrations)
| author | cin |
|---|---|
| date | Mon, 21 Sep 2015 19:54:10 +0300 |
| parents | |
| children | af8d359ee4cc |
comparison
equal
deleted
inserted
replaced
| 411:ee36115f6a34 | 412:30e8c6a74937 |
|---|---|
| 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) { | |
| 53 $cache = {}; | |
| 54 | |
| 55 my @roles = ($role); | |
| 56 | |
| 57 while (my $r = shift @roles ) { | |
| 58 next if $cache->{$r}; | |
| 59 | |
| 60 $cache->{$r} = 1; | |
| 61 if(my $parents = $this->{$roles}{$r}) { | |
| 62 push @roles, @$parents; | |
| 63 } | |
| 64 } | |
| 65 $this->{$_cache}{$role} = $cache; | |
| 66 } | |
| 67 | |
| 68 return $cache; | |
| 69 } | |
| 70 | |
| 71 1; |
