annotate 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
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
412
30e8c6a74937 working on di container (role based registrations)
cin
parents:
diff changeset
1 package IMPL::Config::Hierarchy;
30e8c6a74937 working on di container (role based registrations)
cin
parents:
diff changeset
2 use strict;
30e8c6a74937 working on di container (role based registrations)
cin
parents:
diff changeset
3
30e8c6a74937 working on di container (role based registrations)
cin
parents:
diff changeset
4 use IMPL::Exception();
30e8c6a74937 working on di container (role based registrations)
cin
parents:
diff changeset
5 use IMPL::lang qw(:base);
30e8c6a74937 working on di container (role based registrations)
cin
parents:
diff changeset
6 use IMPL::clone;
30e8c6a74937 working on di container (role based registrations)
cin
parents:
diff changeset
7 use IMPL::declare {
30e8c6a74937 working on di container (role based registrations)
cin
parents:
diff changeset
8 base => {
30e8c6a74937 working on di container (role based registrations)
cin
parents:
diff changeset
9 'IMPL::Object' => undef
30e8c6a74937 working on di container (role based registrations)
cin
parents:
diff changeset
10 },
30e8c6a74937 working on di container (role based registrations)
cin
parents:
diff changeset
11 props => {
30e8c6a74937 working on di container (role based registrations)
cin
parents:
diff changeset
12 roles => '*rw',
30e8c6a74937 working on di container (role based registrations)
cin
parents:
diff changeset
13 _cache => '*rw'
30e8c6a74937 working on di container (role based registrations)
cin
parents:
diff changeset
14 }
30e8c6a74937 working on di container (role based registrations)
cin
parents:
diff changeset
15 };
30e8c6a74937 working on di container (role based registrations)
cin
parents:
diff changeset
16
30e8c6a74937 working on di container (role based registrations)
cin
parents:
diff changeset
17 sub CTOR {
30e8c6a74937 working on di container (role based registrations)
cin
parents:
diff changeset
18 my ( $this, $roles ) = @_;
30e8c6a74937 working on di container (role based registrations)
cin
parents:
diff changeset
19
30e8c6a74937 working on di container (role based registrations)
cin
parents:
diff changeset
20 if ( is( $roles, SELF ) ) {
30e8c6a74937 working on di container (role based registrations)
cin
parents:
diff changeset
21 $this->roles( clone( $roles->roles ) );
30e8c6a74937 working on di container (role based registrations)
cin
parents:
diff changeset
22 }
30e8c6a74937 working on di container (role based registrations)
cin
parents:
diff changeset
23 elsif ( ishash($roles) ) {
30e8c6a74937 working on di container (role based registrations)
cin
parents:
diff changeset
24 $this->roles($roles);
30e8c6a74937 working on di container (role based registrations)
cin
parents:
diff changeset
25 }
30e8c6a74937 working on di container (role based registrations)
cin
parents:
diff changeset
26 elsif ( isarray($roles) ) {
30e8c6a74937 working on di container (role based registrations)
cin
parents:
diff changeset
27 $this->roles( { map { $_, 1 } @$roles } );
30e8c6a74937 working on di container (role based registrations)
cin
parents:
diff changeset
28 }
30e8c6a74937 working on di container (role based registrations)
cin
parents:
diff changeset
29 else {
30e8c6a74937 working on di container (role based registrations)
cin
parents:
diff changeset
30 $this->roles( {} );
30e8c6a74937 working on di container (role based registrations)
cin
parents:
diff changeset
31 }
30e8c6a74937 working on di container (role based registrations)
cin
parents:
diff changeset
32 }
30e8c6a74937 working on di container (role based registrations)
cin
parents:
diff changeset
33
30e8c6a74937 working on di container (role based registrations)
cin
parents:
diff changeset
34 sub AddRole {
30e8c6a74937 working on di container (role based registrations)
cin
parents:
diff changeset
35 my ( $this, $role, $parent ) = @_;
30e8c6a74937 working on di container (role based registrations)
cin
parents:
diff changeset
36
30e8c6a74937 working on di container (role based registrations)
cin
parents:
diff changeset
37 $parent = isarray($parent) ? $parent : [$parent]
30e8c6a74937 working on di container (role based registrations)
cin
parents:
diff changeset
38 if $parent;
30e8c6a74937 working on di container (role based registrations)
cin
parents:
diff changeset
39
30e8c6a74937 working on di container (role based registrations)
cin
parents:
diff changeset
40 die IMPL::InvalidArgumentException->new('role') unless $role;
30e8c6a74937 working on di container (role based registrations)
cin
parents:
diff changeset
41
30e8c6a74937 working on di container (role based registrations)
cin
parents:
diff changeset
42 $this->roles->{$role} = $parent;
30e8c6a74937 working on di container (role based registrations)
cin
parents:
diff changeset
43 }
30e8c6a74937 working on di container (role based registrations)
cin
parents:
diff changeset
44
30e8c6a74937 working on di container (role based registrations)
cin
parents:
diff changeset
45 sub GetLinearRoleHash {
30e8c6a74937 working on di container (role based registrations)
cin
parents:
diff changeset
46 my ( $this, $role ) = @_;
30e8c6a74937 working on di container (role based registrations)
cin
parents:
diff changeset
47
30e8c6a74937 working on di container (role based registrations)
cin
parents:
diff changeset
48 return [] unless $role;
30e8c6a74937 working on di container (role based registrations)
cin
parents:
diff changeset
49
30e8c6a74937 working on di container (role based registrations)
cin
parents:
diff changeset
50 my $cache = $this->{$_cache}{$role};
30e8c6a74937 working on di container (role based registrations)
cin
parents:
diff changeset
51
30e8c6a74937 working on di container (role based registrations)
cin
parents:
diff changeset
52 unless ($cache) {
30e8c6a74937 working on di container (role based registrations)
cin
parents:
diff changeset
53 $cache = {};
30e8c6a74937 working on di container (role based registrations)
cin
parents:
diff changeset
54
30e8c6a74937 working on di container (role based registrations)
cin
parents:
diff changeset
55 my @roles = ($role);
30e8c6a74937 working on di container (role based registrations)
cin
parents:
diff changeset
56
30e8c6a74937 working on di container (role based registrations)
cin
parents:
diff changeset
57 while (my $r = shift @roles ) {
30e8c6a74937 working on di container (role based registrations)
cin
parents:
diff changeset
58 next if $cache->{$r};
30e8c6a74937 working on di container (role based registrations)
cin
parents:
diff changeset
59
30e8c6a74937 working on di container (role based registrations)
cin
parents:
diff changeset
60 $cache->{$r} = 1;
30e8c6a74937 working on di container (role based registrations)
cin
parents:
diff changeset
61 if(my $parents = $this->{$roles}{$r}) {
30e8c6a74937 working on di container (role based registrations)
cin
parents:
diff changeset
62 push @roles, @$parents;
30e8c6a74937 working on di container (role based registrations)
cin
parents:
diff changeset
63 }
30e8c6a74937 working on di container (role based registrations)
cin
parents:
diff changeset
64 }
30e8c6a74937 working on di container (role based registrations)
cin
parents:
diff changeset
65 $this->{$_cache}{$role} = $cache;
30e8c6a74937 working on di container (role based registrations)
cin
parents:
diff changeset
66 }
30e8c6a74937 working on di container (role based registrations)
cin
parents:
diff changeset
67
30e8c6a74937 working on di container (role based registrations)
cin
parents:
diff changeset
68 return $cache;
30e8c6a74937 working on di container (role based registrations)
cin
parents:
diff changeset
69 }
30e8c6a74937 working on di container (role based registrations)
cin
parents:
diff changeset
70
30e8c6a74937 working on di container (role based registrations)
cin
parents:
diff changeset
71 1;