annotate lib/IMPL/Config/Hierarchy.pm @ 413:af8d359ee4cc ref20150831

working on di container
author cin
date Thu, 24 Sep 2015 12:19:30 +0300
parents 30e8c6a74937
children ec6f2d389d1e
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) {
413
af8d359ee4cc working on di container
cin
parents: 412
diff changeset
53 $cache = { $role, 0 };
412
30e8c6a74937 working on di container (role based registrations)
cin
parents:
diff changeset
54
413
af8d359ee4cc working on di container
cin
parents: 412
diff changeset
55 my @roles = [$role, 0];
af8d359ee4cc working on di container
cin
parents: 412
diff changeset
56
412
30e8c6a74937 working on di container (role based registrations)
cin
parents:
diff changeset
57 while (my $r = shift @roles ) {
413
af8d359ee4cc working on di container
cin
parents: 412
diff changeset
58 my ($name, $level) = @$r;
af8d359ee4cc working on di container
cin
parents: 412
diff changeset
59
af8d359ee4cc working on di container
cin
parents: 412
diff changeset
60 $cache->{$name} = $level;
af8d359ee4cc working on di container
cin
parents: 412
diff changeset
61 if(my $parents = $this->{$roles}{$name}) {
af8d359ee4cc working on di container
cin
parents: 412
diff changeset
62 foreach my $p (@$parents) {
af8d359ee4cc working on di container
cin
parents: 412
diff changeset
63 next if $cache{$p};
af8d359ee4cc working on di container
cin
parents: 412
diff changeset
64 push @roles, [$p, $cache{$p} = $level + 1];
af8d359ee4cc working on di container
cin
parents: 412
diff changeset
65 }
412
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 $this->{$_cache}{$role} = $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 return $cache;
30e8c6a74937 working on di container (role based registrations)
cin
parents:
diff changeset
72 }
30e8c6a74937 working on di container (role based registrations)
cin
parents:
diff changeset
73
30e8c6a74937 working on di container (role based registrations)
cin
parents:
diff changeset
74 1;