49
|
1 package IMPL::Config;
|
|
2 use strict;
|
|
3 use warnings;
|
|
4
|
|
5 use base qw(IMPL::Object IMPL::Object::Serializable IMPL::Object::Autofill);
|
|
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
|
|
16 sub LoadXMLFile {
|
|
17 my ($self,$file) = @_;
|
|
18
|
|
19 my $class = ref $self || $self;
|
|
20
|
|
21 my $serializer = new IMPL::Serializer(
|
|
22 Formatter => new IMPL::Serialization::XmlFormatter(
|
|
23 IdentOutput => 1,
|
|
24 SkipWhitespace => 1
|
|
25 )
|
|
26 );
|
|
27
|
|
28 open my $hFile,'<',$file or die new IMPL::Exception("Failed to open file",$file,$!);
|
|
29
|
|
30 my $obj;
|
|
31 eval {
|
|
32 $obj = $serializer->Deserialize($hFile);
|
|
33 };
|
|
34
|
|
35 if ($@) {
|
|
36 my $e=$@;
|
|
37 die new IMPL::Exception("Can't load the configuration file",$file,$e);
|
|
38 }
|
|
39 return $obj;
|
|
40 }
|
|
41
|
|
42 sub SaveXMLFile {
|
|
43 my ($this,$file) = @_;
|
|
44
|
|
45 my $serializer = new IMPL::Serializer(
|
|
46 Formatter => new IMPL::Serialization::XmlFormatter(
|
|
47 IdentOutput => 1,
|
|
48 SkipWhitespace => 1
|
|
49 )
|
|
50 );
|
|
51
|
|
52 open my $hFile,'>',$file or die new IMPL::Exception("Failed to open file",$file,$!);
|
|
53
|
|
54 $serializer->Serialize($hFile, $this);
|
|
55 }
|
|
56
|
|
57 sub xml {
|
|
58 my $this = shift;
|
|
59 my $serializer = new IMPL::Serializer(
|
|
60 Formatter => new IMPL::Serialization::XmlFormatter(
|
|
61 IdentOutput => 1,
|
|
62 SkipWhitespace => 1
|
|
63 )
|
|
64 );
|
|
65 my $str = '';
|
|
66 open my $hFile,'>',\$str or die new IMPL::Exception("Failed to open stream",$!);
|
|
67
|
|
68 $serializer->Serialize($hFile, $this);
|
|
69
|
|
70 undef $hFile;
|
|
71
|
|
72 return $str;
|
|
73 }
|
|
74
|
|
75 sub save {
|
|
76 my ($this,$ctx) = @_;
|
|
77
|
|
78 foreach my $info ($this->get_meta('IMPL::Class::PropertyInfo')) {
|
|
79 next if $info->Access != IMPL::Class::Member::MOD_PUBLIC; # save only public properties
|
|
80
|
|
81 my $name = $info->Name;
|
|
82 $ctx->AddVar($name => $this->$name()) if $this->$name();
|
|
83 }
|
|
84 }
|
|
85
|
58
|
86 sub spawn {
|
|
87 goto &LoadXMLFile;
|
|
88 }
|
|
89
|
49
|
90 1;
|
|
91 __END__
|
|
92
|
|
93 =pod
|
|
94
|
|
95 =h1 SYNOPSIS
|
|
96
|
|
97 package App::Config
|
|
98 use base qw(IMPL::Config)
|
|
99
|
|
100 use IMPL::Class::Property;
|
|
101 use IMPL::Config::Class;
|
|
102
|
|
103 BEGIN {
|
|
104 public property SimpleString => prop_all;
|
|
105 public property MyClass => prop_all;
|
58
|
106 public lazy property DataSource => prop_all, {type => 'App::DataSource', factory => sub {}};
|
49
|
107 }
|
|
108
|
|
109 sub CTOR {
|
|
110 my $this = shift;
|
|
111 $this->superCTOR(@_);
|
|
112
|
|
113 $this->MyClass(new IMPL::Config::Class(Type => MyClass)) unless $this->MyClass;
|
|
114 }
|
|
115
|
58
|
116 # in some script
|
|
117
|
|
118 my $app = spawn App::Config('default.xml');
|
|
119
|
|
120 $app->Run();
|
|
121
|
49
|
122 =head1 DESCRIPTION
|
|
123
|
|
124 Позволяет сохранить/загрузить конфигурацию. Также все классы конфигурации
|
|
125 должны наследоваться от данного класса, и все Public свойства будут
|
|
126 автоматически сохраняться и восстанавливаться.
|
|
127
|
|
128 =head1 MEMBERS
|
|
129
|
|
130 =over
|
|
131
|
|
132 =item static LoadXMLFile($fileName)
|
|
133 Создает из XML файла экземпляр приложения
|
|
134
|
|
135 =item SaveXMLFile($fileName)
|
|
136 Сохраняет приложение в файл
|
|
137
|
|
138 =item xml
|
|
139 Сохраняет конфигурацию приложения в XML строку
|
|
140
|
|
141 =back
|
|
142
|
|
143 =cut
|