Mercurial > pub > Impl
annotate Lib/IMPL/Config.pm @ 62:c64bd1bf727d
Web application
Page query handler
author | wizard |
---|---|
date | Fri, 12 Mar 2010 16:23:46 +0300 |
parents | b0c068da93ac |
children | 76b878ad6596 |
rev | line source |
---|---|
49 | 1 package IMPL::Config; |
2 use strict; | |
3 use warnings; | |
4 | |
60
b0c068da93ac
Lazy activation for the configuration objects (final concept)
wizard
parents:
58
diff
changeset
|
5 use base qw(IMPL::Object::Accessor IMPL::Object::Serializable IMPL::Object::Autofill); |
49 | 6 |
7 __PACKAGE__->PassThroughArgs; | |
8 | |
9 use IMPL::Class::Member; | |
10 use IMPL::Class::PropertyInfo; | |
11 use IMPL::Exception; | |
12 | |
13 use IMPL::Serialization; | |
14 use IMPL::Serialization::XmlFormatter; | |
15 | |
60
b0c068da93ac
Lazy activation for the configuration objects (final concept)
wizard
parents:
58
diff
changeset
|
16 |
b0c068da93ac
Lazy activation for the configuration objects (final concept)
wizard
parents:
58
diff
changeset
|
17 |
49 | 18 sub LoadXMLFile { |
19 my ($self,$file) = @_; | |
20 | |
21 my $class = ref $self || $self; | |
22 | |
23 my $serializer = new IMPL::Serializer( | |
24 Formatter => new IMPL::Serialization::XmlFormatter( | |
25 IdentOutput => 1, | |
26 SkipWhitespace => 1 | |
27 ) | |
28 ); | |
29 | |
30 open my $hFile,'<',$file or die new IMPL::Exception("Failed to open file",$file,$!); | |
31 | |
32 my $obj; | |
33 eval { | |
34 $obj = $serializer->Deserialize($hFile); | |
35 }; | |
36 | |
37 if ($@) { | |
38 my $e=$@; | |
39 die new IMPL::Exception("Can't load the configuration file",$file,$e); | |
40 } | |
41 return $obj; | |
42 } | |
43 | |
44 sub SaveXMLFile { | |
45 my ($this,$file) = @_; | |
46 | |
47 my $serializer = new IMPL::Serializer( | |
48 Formatter => new IMPL::Serialization::XmlFormatter( | |
49 IdentOutput => 1, | |
50 SkipWhitespace => 1 | |
51 ) | |
52 ); | |
53 | |
54 open my $hFile,'>',$file or die new IMPL::Exception("Failed to open file",$file,$!); | |
55 | |
56 $serializer->Serialize($hFile, $this); | |
57 } | |
58 | |
59 sub xml { | |
60 my $this = shift; | |
61 my $serializer = new IMPL::Serializer( | |
62 Formatter => new IMPL::Serialization::XmlFormatter( | |
63 IdentOutput => 1, | |
64 SkipWhitespace => 1 | |
65 ) | |
66 ); | |
67 my $str = ''; | |
68 open my $hFile,'>',\$str or die new IMPL::Exception("Failed to open stream",$!); | |
69 | |
70 $serializer->Serialize($hFile, $this); | |
71 | |
72 undef $hFile; | |
73 | |
74 return $str; | |
75 } | |
76 | |
77 sub save { | |
78 my ($this,$ctx) = @_; | |
79 | |
80 foreach my $info ($this->get_meta('IMPL::Class::PropertyInfo')) { | |
81 next if $info->Access != IMPL::Class::Member::MOD_PUBLIC; # save only public properties | |
82 | |
83 my $name = $info->Name; | |
60
b0c068da93ac
Lazy activation for the configuration objects (final concept)
wizard
parents:
58
diff
changeset
|
84 $ctx->AddVar($name => $this->rawGet($name)) if $this->rawGet($name); |
49 | 85 } |
60
b0c068da93ac
Lazy activation for the configuration objects (final concept)
wizard
parents:
58
diff
changeset
|
86 |
49 | 87 } |
88 | |
58 | 89 sub spawn { |
90 goto &LoadXMLFile; | |
91 } | |
92 | |
60
b0c068da93ac
Lazy activation for the configuration objects (final concept)
wizard
parents:
58
diff
changeset
|
93 sub get { |
b0c068da93ac
Lazy activation for the configuration objects (final concept)
wizard
parents:
58
diff
changeset
|
94 my $this = shift; |
b0c068da93ac
Lazy activation for the configuration objects (final concept)
wizard
parents:
58
diff
changeset
|
95 |
b0c068da93ac
Lazy activation for the configuration objects (final concept)
wizard
parents:
58
diff
changeset
|
96 if (@_ == 1) { |
b0c068da93ac
Lazy activation for the configuration objects (final concept)
wizard
parents:
58
diff
changeset
|
97 my $obj = $this->SUPER::get(@_); |
b0c068da93ac
Lazy activation for the configuration objects (final concept)
wizard
parents:
58
diff
changeset
|
98 return UNIVERSAL::isa($obj,'IMPL::Config::Activator') ? $obj->activate : $obj; |
b0c068da93ac
Lazy activation for the configuration objects (final concept)
wizard
parents:
58
diff
changeset
|
99 } else { |
b0c068da93ac
Lazy activation for the configuration objects (final concept)
wizard
parents:
58
diff
changeset
|
100 my @objs = $this->SUPER::get(@_); |
b0c068da93ac
Lazy activation for the configuration objects (final concept)
wizard
parents:
58
diff
changeset
|
101 return map UNIVERSAL::isa($_,'IMPL::Config::Activator') ? $_->activate : $_, @objs ; |
b0c068da93ac
Lazy activation for the configuration objects (final concept)
wizard
parents:
58
diff
changeset
|
102 } |
b0c068da93ac
Lazy activation for the configuration objects (final concept)
wizard
parents:
58
diff
changeset
|
103 } |
b0c068da93ac
Lazy activation for the configuration objects (final concept)
wizard
parents:
58
diff
changeset
|
104 |
b0c068da93ac
Lazy activation for the configuration objects (final concept)
wizard
parents:
58
diff
changeset
|
105 sub rawGet { |
b0c068da93ac
Lazy activation for the configuration objects (final concept)
wizard
parents:
58
diff
changeset
|
106 my $this = shift; |
b0c068da93ac
Lazy activation for the configuration objects (final concept)
wizard
parents:
58
diff
changeset
|
107 return $this->SUPER::get(@_); |
b0c068da93ac
Lazy activation for the configuration objects (final concept)
wizard
parents:
58
diff
changeset
|
108 } |
b0c068da93ac
Lazy activation for the configuration objects (final concept)
wizard
parents:
58
diff
changeset
|
109 |
b0c068da93ac
Lazy activation for the configuration objects (final concept)
wizard
parents:
58
diff
changeset
|
110 sub Exists { |
b0c068da93ac
Lazy activation for the configuration objects (final concept)
wizard
parents:
58
diff
changeset
|
111 $_[0]->SUPER::get($_[1]) ? 1 : 0; |
b0c068da93ac
Lazy activation for the configuration objects (final concept)
wizard
parents:
58
diff
changeset
|
112 } |
b0c068da93ac
Lazy activation for the configuration objects (final concept)
wizard
parents:
58
diff
changeset
|
113 |
49 | 114 1; |
115 __END__ | |
116 | |
117 =pod | |
118 | |
119 =h1 SYNOPSIS | |
120 | |
121 package App::Config | |
122 use base qw(IMPL::Config) | |
123 | |
124 use IMPL::Class::Property; | |
125 use IMPL::Config::Class; | |
126 | |
127 BEGIN { | |
128 public property SimpleString => prop_all; | |
129 public property MyClass => prop_all; | |
60
b0c068da93ac
Lazy activation for the configuration objects (final concept)
wizard
parents:
58
diff
changeset
|
130 public property DataSource => prop_all; |
49 | 131 } |
132 | |
133 sub CTOR { | |
134 my $this = shift; | |
135 | |
60
b0c068da93ac
Lazy activation for the configuration objects (final concept)
wizard
parents:
58
diff
changeset
|
136 $this->MyClass(new IMPL::Config::Class(Type => 'MyClass'')) unless $this->MyClass; |
b0c068da93ac
Lazy activation for the configuration objects (final concept)
wizard
parents:
58
diff
changeset
|
137 $this->DataSource( |
b0c068da93ac
Lazy activation for the configuration objects (final concept)
wizard
parents:
58
diff
changeset
|
138 new IMPL::Config::Activator( |
b0c068da93ac
Lazy activation for the configuration objects (final concept)
wizard
parents:
58
diff
changeset
|
139 type => 'MyDataSource', |
b0c068da93ac
Lazy activation for the configuration objects (final concept)
wizard
parents:
58
diff
changeset
|
140 args=>{ |
b0c068da93ac
Lazy activation for the configuration objects (final concept)
wizard
parents:
58
diff
changeset
|
141 host => 'localhost', |
b0c068da93ac
Lazy activation for the configuration objects (final concept)
wizard
parents:
58
diff
changeset
|
142 user => 'dbuser' |
b0c068da93ac
Lazy activation for the configuration objects (final concept)
wizard
parents:
58
diff
changeset
|
143 } |
b0c068da93ac
Lazy activation for the configuration objects (final concept)
wizard
parents:
58
diff
changeset
|
144 ) |
b0c068da93ac
Lazy activation for the configuration objects (final concept)
wizard
parents:
58
diff
changeset
|
145 ) unless $this->Exists('DataSource'); |
49 | 146 } |
147 | |
58 | 148 # in some script |
149 | |
150 my $app = spawn App::Config('default.xml'); | |
151 | |
152 $app->Run(); | |
153 | |
49 | 154 =head1 DESCRIPTION |
155 | |
156 Позволяет сохранить/загрузить конфигурацию. Также все классы конфигурации | |
157 должны наследоваться от данного класса, и все Public свойства будут | |
158 автоматически сохраняться и восстанавливаться. | |
159 | |
160 =head1 MEMBERS | |
161 | |
162 =over | |
163 | |
60
b0c068da93ac
Lazy activation for the configuration objects (final concept)
wizard
parents:
58
diff
changeset
|
164 =item C<< IMPL::Config->LoadXMLFile($fileName) >> |
b0c068da93ac
Lazy activation for the configuration objects (final concept)
wizard
parents:
58
diff
changeset
|
165 |
49 | 166 Создает из XML файла экземпляр приложения |
167 | |
60
b0c068da93ac
Lazy activation for the configuration objects (final concept)
wizard
parents:
58
diff
changeset
|
168 =item C<< $instance->SaveXMLFile($fileName) >> |
b0c068da93ac
Lazy activation for the configuration objects (final concept)
wizard
parents:
58
diff
changeset
|
169 |
49 | 170 Сохраняет приложение в файл |
171 | |
60
b0c068da93ac
Lazy activation for the configuration objects (final concept)
wizard
parents:
58
diff
changeset
|
172 =item C<< xml >> |
b0c068da93ac
Lazy activation for the configuration objects (final concept)
wizard
parents:
58
diff
changeset
|
173 |
49 | 174 Сохраняет конфигурацию приложения в XML строку |
175 | |
60
b0c068da93ac
Lazy activation for the configuration objects (final concept)
wizard
parents:
58
diff
changeset
|
176 =item C<< IMPL::Config->spawn($file) >> |
b0c068da93ac
Lazy activation for the configuration objects (final concept)
wizard
parents:
58
diff
changeset
|
177 |
b0c068da93ac
Lazy activation for the configuration objects (final concept)
wizard
parents:
58
diff
changeset
|
178 Синоним для C<LoadXMLFile> |
b0c068da93ac
Lazy activation for the configuration objects (final concept)
wizard
parents:
58
diff
changeset
|
179 |
49 | 180 =back |
181 | |
182 =cut |