annotate lib/IMPL/Config/YAMLConfig.pm @ 421:7798345304bc ref20150831

working on IMPL::Config, removed old stuff
author cin
date Sun, 16 Jul 2017 22:59:39 +0300
parents bbc4739c4d48
children b0481c071bea
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
418
cin
parents:
diff changeset
1 package IMPL::Config::YAMLConfig;
cin
parents:
diff changeset
2 use strict;
cin
parents:
diff changeset
3
419
bbc4739c4d48 working on IMPL::Config::Container
cin
parents: 418
diff changeset
4 use IMPL::lang qw(:base);
bbc4739c4d48 working on IMPL::Config::Container
cin
parents: 418
diff changeset
5 use IMPL::Exception();
bbc4739c4d48 working on IMPL::Config::Container
cin
parents: 418
diff changeset
6 use YAML::XS();
bbc4739c4d48 working on IMPL::Config::Container
cin
parents: 418
diff changeset
7
421
7798345304bc working on IMPL::Config, removed old stuff
cin
parents: 419
diff changeset
8 use IMPL::declare {
7798345304bc working on IMPL::Config, removed old stuff
cin
parents: 419
diff changeset
9 require => {
7798345304bc working on IMPL::Config, removed old stuff
cin
parents: 419
diff changeset
10 ReferenceDescriptor => 'IMPL::Config::ReferenceDescriptor',
7798345304bc working on IMPL::Config, removed old stuff
cin
parents: 419
diff changeset
11 ServiceDescriptor => 'IMPL::Config::ServiceDescriptor',
7798345304bc working on IMPL::Config, removed old stuff
cin
parents: 419
diff changeset
12 ValueDescriptor => 'IMPL::Config::ValueDescriptor'
7798345304bc working on IMPL::Config, removed old stuff
cin
parents: 419
diff changeset
13 },
7798345304bc working on IMPL::Config, removed old stuff
cin
parents: 419
diff changeset
14 base => [
7798345304bc working on IMPL::Config, removed old stuff
cin
parents: 419
diff changeset
15 'IMPL::Object' => undef
7798345304bc working on IMPL::Config, removed old stuff
cin
parents: 419
diff changeset
16 ],
7798345304bc working on IMPL::Config, removed old stuff
cin
parents: 419
diff changeset
17 props => [
7798345304bc working on IMPL::Config, removed old stuff
cin
parents: 419
diff changeset
18 container => 'ro'
7798345304bc working on IMPL::Config, removed old stuff
cin
parents: 419
diff changeset
19 ]
7798345304bc working on IMPL::Config, removed old stuff
cin
parents: 419
diff changeset
20 };
419
bbc4739c4d48 working on IMPL::Config::Container
cin
parents: 418
diff changeset
21
421
7798345304bc working on IMPL::Config, removed old stuff
cin
parents: 419
diff changeset
22 sub CTOR {
7798345304bc working on IMPL::Config, removed old stuff
cin
parents: 419
diff changeset
23 my ( $this, $container ) = @_;
7798345304bc working on IMPL::Config, removed old stuff
cin
parents: 419
diff changeset
24 die IMPL::InvalidArgumentException('container')
7798345304bc working on IMPL::Config, removed old stuff
cin
parents: 419
diff changeset
25 unless $container;
7798345304bc working on IMPL::Config, removed old stuff
cin
parents: 419
diff changeset
26 $this->container($container);
7798345304bc working on IMPL::Config, removed old stuff
cin
parents: 419
diff changeset
27 }
7798345304bc working on IMPL::Config, removed old stuff
cin
parents: 419
diff changeset
28
7798345304bc working on IMPL::Config, removed old stuff
cin
parents: 419
diff changeset
29 sub LoadConfiguration {
7798345304bc working on IMPL::Config, removed old stuff
cin
parents: 419
diff changeset
30 my ( $this, $file ) = @_;
7798345304bc working on IMPL::Config, removed old stuff
cin
parents: 419
diff changeset
31
7798345304bc working on IMPL::Config, removed old stuff
cin
parents: 419
diff changeset
32 $this->Configure(
7798345304bc working on IMPL::Config, removed old stuff
cin
parents: 419
diff changeset
33 isscalar($file)
7798345304bc working on IMPL::Config, removed old stuff
cin
parents: 419
diff changeset
34 ? YAML::XS::Load( ${$file} )
7798345304bc working on IMPL::Config, removed old stuff
cin
parents: 419
diff changeset
35 : YAML::XS::LoadFile($file)
7798345304bc working on IMPL::Config, removed old stuff
cin
parents: 419
diff changeset
36 );
419
bbc4739c4d48 working on IMPL::Config::Container
cin
parents: 418
diff changeset
37 }
bbc4739c4d48 working on IMPL::Config::Container
cin
parents: 418
diff changeset
38
418
cin
parents:
diff changeset
39 sub Configure {
421
7798345304bc working on IMPL::Config, removed old stuff
cin
parents: 419
diff changeset
40 my ( $this, $config ) = @_;
7798345304bc working on IMPL::Config, removed old stuff
cin
parents: 419
diff changeset
41
7798345304bc working on IMPL::Config, removed old stuff
cin
parents: 419
diff changeset
42 die IMPL::InvalidArgumentException('config')
7798345304bc working on IMPL::Config, removed old stuff
cin
parents: 419
diff changeset
43 unless ishash($config);
7798345304bc working on IMPL::Config, removed old stuff
cin
parents: 419
diff changeset
44
7798345304bc working on IMPL::Config, removed old stuff
cin
parents: 419
diff changeset
45 my $container = $this->container;
7798345304bc working on IMPL::Config, removed old stuff
cin
parents: 419
diff changeset
46 foreach my $item ( @{ $this->ParseServices( $config->{services} ) } ) {
7798345304bc working on IMPL::Config, removed old stuff
cin
parents: 419
diff changeset
47 $container->Register( $item->{role}, $item->{descriptor} );
7798345304bc working on IMPL::Config, removed old stuff
cin
parents: 419
diff changeset
48 }
7798345304bc working on IMPL::Config, removed old stuff
cin
parents: 419
diff changeset
49
7798345304bc working on IMPL::Config, removed old stuff
cin
parents: 419
diff changeset
50 return $container;
7798345304bc working on IMPL::Config, removed old stuff
cin
parents: 419
diff changeset
51 }
7798345304bc working on IMPL::Config, removed old stuff
cin
parents: 419
diff changeset
52
7798345304bc working on IMPL::Config, removed old stuff
cin
parents: 419
diff changeset
53 sub ParseServices {
7798345304bc working on IMPL::Config, removed old stuff
cin
parents: 419
diff changeset
54 my ( $this, $services ) = @_;
7798345304bc working on IMPL::Config, removed old stuff
cin
parents: 419
diff changeset
55
7798345304bc working on IMPL::Config, removed old stuff
cin
parents: 419
diff changeset
56 return $services
7798345304bc working on IMPL::Config, removed old stuff
cin
parents: 419
diff changeset
57 ? [
7798345304bc working on IMPL::Config, removed old stuff
cin
parents: 419
diff changeset
58 map {
7798345304bc working on IMPL::Config, removed old stuff
cin
parents: 419
diff changeset
59 {
7798345304bc working on IMPL::Config, removed old stuff
cin
parents: 419
diff changeset
60 role => delete $_->{name},
7798345304bc working on IMPL::Config, removed old stuff
cin
parents: 419
diff changeset
61 descriptor => $this->ParseDescriptor($_)
7798345304bc working on IMPL::Config, removed old stuff
cin
parents: 419
diff changeset
62 };
7798345304bc working on IMPL::Config, removed old stuff
cin
parents: 419
diff changeset
63 } @$services
7798345304bc working on IMPL::Config, removed old stuff
cin
parents: 419
diff changeset
64 ]
7798345304bc working on IMPL::Config, removed old stuff
cin
parents: 419
diff changeset
65 : undef;
7798345304bc working on IMPL::Config, removed old stuff
cin
parents: 419
diff changeset
66 }
7798345304bc working on IMPL::Config, removed old stuff
cin
parents: 419
diff changeset
67
7798345304bc working on IMPL::Config, removed old stuff
cin
parents: 419
diff changeset
68 sub ParseDescriptor {
7798345304bc working on IMPL::Config, removed old stuff
cin
parents: 419
diff changeset
69 my ( $this, $data ) = @_;
7798345304bc working on IMPL::Config, removed old stuff
cin
parents: 419
diff changeset
70
7798345304bc working on IMPL::Config, removed old stuff
cin
parents: 419
diff changeset
71 my %opts = ( onwer => $this->container() );
7798345304bc working on IMPL::Config, removed old stuff
cin
parents: 419
diff changeset
72
7798345304bc working on IMPL::Config, removed old stuff
cin
parents: 419
diff changeset
73 if ( my $type = $data->{'$type'} ) {
7798345304bc working on IMPL::Config, removed old stuff
cin
parents: 419
diff changeset
74 $opts{services} = $this->ParseServices( $data->{services} );
7798345304bc working on IMPL::Config, removed old stuff
cin
parents: 419
diff changeset
75 $opts{type} = $type;
7798345304bc working on IMPL::Config, removed old stuff
cin
parents: 419
diff changeset
76 $opts{args} = $this->ParseDescriptor( $data->{params} )
7798345304bc working on IMPL::Config, removed old stuff
cin
parents: 419
diff changeset
77 if $data->{params};
7798345304bc working on IMPL::Config, removed old stuff
cin
parents: 419
diff changeset
78 $opts{norequire} = $data->{norequire};
7798345304bc working on IMPL::Config, removed old stuff
cin
parents: 419
diff changeset
79 $opts{activation} = $data->{activation};
419
bbc4739c4d48 working on IMPL::Config::Container
cin
parents: 418
diff changeset
80
421
7798345304bc working on IMPL::Config, removed old stuff
cin
parents: 419
diff changeset
81 return ServiceDescriptor->new(%opts);
7798345304bc working on IMPL::Config, removed old stuff
cin
parents: 419
diff changeset
82 }
7798345304bc working on IMPL::Config, removed old stuff
cin
parents: 419
diff changeset
83 elsif ( my $dep = $data->{'$ref'} ) {
7798345304bc working on IMPL::Config, removed old stuff
cin
parents: 419
diff changeset
84 $opts{services} = $this->ParseServices( $data->{services} );
7798345304bc working on IMPL::Config, removed old stuff
cin
parents: 419
diff changeset
85 $opts{lazy} = $data->{lazy};
7798345304bc working on IMPL::Config, removed old stuff
cin
parents: 419
diff changeset
86 $opts{optional} = $data->{optional};
7798345304bc working on IMPL::Config, removed old stuff
cin
parents: 419
diff changeset
87 $opts{default} = $this->ParseDescriptor( $data->{default} )
7798345304bc working on IMPL::Config, removed old stuff
cin
parents: 419
diff changeset
88 if exists $data->{default};
7798345304bc working on IMPL::Config, removed old stuff
cin
parents: 419
diff changeset
89
7798345304bc working on IMPL::Config, removed old stuff
cin
parents: 419
diff changeset
90 return ReferenceDesriptor->new( $dep, %opts );
7798345304bc working on IMPL::Config, removed old stuff
cin
parents: 419
diff changeset
91 }
7798345304bc working on IMPL::Config, removed old stuff
cin
parents: 419
diff changeset
92 elsif ( my $value = $data->{'$value'} ) {
7798345304bc working on IMPL::Config, removed old stuff
cin
parents: 419
diff changeset
93 my ( $parsed, $raw ) = $this->ParseValue($value);
7798345304bc working on IMPL::Config, removed old stuff
cin
parents: 419
diff changeset
94 $opts{services} = $this->ParseServices( $data->{services} );
7798345304bc working on IMPL::Config, removed old stuff
cin
parents: 419
diff changeset
95 $opts{raw} = $raw;
7798345304bc working on IMPL::Config, removed old stuff
cin
parents: 419
diff changeset
96 return ValueDescriptor->new( $parsed, %opts );
7798345304bc working on IMPL::Config, removed old stuff
cin
parents: 419
diff changeset
97 }
7798345304bc working on IMPL::Config, removed old stuff
cin
parents: 419
diff changeset
98 else {
7798345304bc working on IMPL::Config, removed old stuff
cin
parents: 419
diff changeset
99 my ( $parsed, $raw ) = $this->ParseValue($value);
7798345304bc working on IMPL::Config, removed old stuff
cin
parents: 419
diff changeset
100 $opts{raw} = $raw;
7798345304bc working on IMPL::Config, removed old stuff
cin
parents: 419
diff changeset
101 return ValueDescriptor->new( $parsed, %opts );
7798345304bc working on IMPL::Config, removed old stuff
cin
parents: 419
diff changeset
102 }
7798345304bc working on IMPL::Config, removed old stuff
cin
parents: 419
diff changeset
103 }
7798345304bc working on IMPL::Config, removed old stuff
cin
parents: 419
diff changeset
104
7798345304bc working on IMPL::Config, removed old stuff
cin
parents: 419
diff changeset
105 sub ParseValue {
7798345304bc working on IMPL::Config, removed old stuff
cin
parents: 419
diff changeset
106 my ( $this, $value ) = @_;
7798345304bc working on IMPL::Config, removed old stuff
cin
parents: 419
diff changeset
107
7798345304bc working on IMPL::Config, removed old stuff
cin
parents: 419
diff changeset
108 my $raw = 1;
7798345304bc working on IMPL::Config, removed old stuff
cin
parents: 419
diff changeset
109
7798345304bc working on IMPL::Config, removed old stuff
cin
parents: 419
diff changeset
110 if ( ishash($value) ) {
7798345304bc working on IMPL::Config, removed old stuff
cin
parents: 419
diff changeset
111 return ( $this->ParseDescriptor($value), 0 )
7798345304bc working on IMPL::Config, removed old stuff
cin
parents: 419
diff changeset
112 if grep exists $value->{$_}, qw($type $ref $value);
7798345304bc working on IMPL::Config, removed old stuff
cin
parents: 419
diff changeset
113
7798345304bc working on IMPL::Config, removed old stuff
cin
parents: 419
diff changeset
114 my %res;
7798345304bc working on IMPL::Config, removed old stuff
cin
parents: 419
diff changeset
115 while ( my ( $k, $v ) = each %$value ) {
7798345304bc working on IMPL::Config, removed old stuff
cin
parents: 419
diff changeset
116 my ( $parsed, $flag ) = $this->ParseValue($v);
7798345304bc working on IMPL::Config, removed old stuff
cin
parents: 419
diff changeset
117 $res{$k} = $parsed;
7798345304bc working on IMPL::Config, removed old stuff
cin
parents: 419
diff changeset
118 $raw &&= $flag;
7798345304bc working on IMPL::Config, removed old stuff
cin
parents: 419
diff changeset
119 }
7798345304bc working on IMPL::Config, removed old stuff
cin
parents: 419
diff changeset
120 return ( \%res, $raw );
7798345304bc working on IMPL::Config, removed old stuff
cin
parents: 419
diff changeset
121 }
7798345304bc working on IMPL::Config, removed old stuff
cin
parents: 419
diff changeset
122 elsif ( isarray($value) ) {
7798345304bc working on IMPL::Config, removed old stuff
cin
parents: 419
diff changeset
123 return (
7798345304bc working on IMPL::Config, removed old stuff
cin
parents: 419
diff changeset
124 [
7798345304bc working on IMPL::Config, removed old stuff
cin
parents: 419
diff changeset
125 map {
7798345304bc working on IMPL::Config, removed old stuff
cin
parents: 419
diff changeset
126 my ( $parsed, $flag ) = $this->ParseValue($_);
7798345304bc working on IMPL::Config, removed old stuff
cin
parents: 419
diff changeset
127 $raw &&= $flag;
7798345304bc working on IMPL::Config, removed old stuff
cin
parents: 419
diff changeset
128 return $parsed;
7798345304bc working on IMPL::Config, removed old stuff
cin
parents: 419
diff changeset
129 } @$value
7798345304bc working on IMPL::Config, removed old stuff
cin
parents: 419
diff changeset
130 ],
7798345304bc working on IMPL::Config, removed old stuff
cin
parents: 419
diff changeset
131 $raw
7798345304bc working on IMPL::Config, removed old stuff
cin
parents: 419
diff changeset
132 );
7798345304bc working on IMPL::Config, removed old stuff
cin
parents: 419
diff changeset
133 }
7798345304bc working on IMPL::Config, removed old stuff
cin
parents: 419
diff changeset
134 else {
7798345304bc working on IMPL::Config, removed old stuff
cin
parents: 419
diff changeset
135 return ($value, 1);
7798345304bc working on IMPL::Config, removed old stuff
cin
parents: 419
diff changeset
136 }
418
cin
parents:
diff changeset
137 }
cin
parents:
diff changeset
138
cin
parents:
diff changeset
139 1;
cin
parents:
diff changeset
140
cin
parents:
diff changeset
141 __END__
cin
parents:
diff changeset
142
cin
parents:
diff changeset
143 =pod
cin
parents:
diff changeset
144
421
7798345304bc working on IMPL::Config, removed old stuff
cin
parents: 419
diff changeset
145 =head1 NAME
7798345304bc working on IMPL::Config, removed old stuff
cin
parents: 419
diff changeset
146
7798345304bc working on IMPL::Config, removed old stuff
cin
parents: 419
diff changeset
147 =head1 SYNOPSIS
7798345304bc working on IMPL::Config, removed old stuff
cin
parents: 419
diff changeset
148
7798345304bc working on IMPL::Config, removed old stuff
cin
parents: 419
diff changeset
149 =
418
cin
parents:
diff changeset
150
419
bbc4739c4d48 working on IMPL::Config::Container
cin
parents: 418
diff changeset
151 =cut