annotate Lib/IMPL/Object/Factory.pm @ 94:79bf75223afe

Fixed security related bugs
author wizard
date Thu, 29 Apr 2010 01:31:27 +0400
parents 9cb8e730fa86
children 7084af955c57
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
64
259cd3df6e53 Doc generation
wizard
parents:
diff changeset
1 package IMPL::Object::Factory;
259cd3df6e53 Doc generation
wizard
parents:
diff changeset
2 use strict;
259cd3df6e53 Doc generation
wizard
parents:
diff changeset
3
259cd3df6e53 Doc generation
wizard
parents:
diff changeset
4 use base qw(IMPL::Object IMPL::Object::Serializable);
259cd3df6e53 Doc generation
wizard
parents:
diff changeset
5
259cd3df6e53 Doc generation
wizard
parents:
diff changeset
6 use IMPL::Class::Property;
259cd3df6e53 Doc generation
wizard
parents:
diff changeset
7
259cd3df6e53 Doc generation
wizard
parents:
diff changeset
8 BEGIN {
259cd3df6e53 Doc generation
wizard
parents:
diff changeset
9 public property factory => prop_get | owner_set;
73
wizard
parents: 67
diff changeset
10 public property parameters => prop_get | owner_set;
wizard
parents: 67
diff changeset
11 public property method => prop_get | owner_set;
64
259cd3df6e53 Doc generation
wizard
parents:
diff changeset
12 }
259cd3df6e53 Doc generation
wizard
parents:
diff changeset
13
259cd3df6e53 Doc generation
wizard
parents:
diff changeset
14 # custom factory, overrides default
259cd3df6e53 Doc generation
wizard
parents:
diff changeset
15 sub new {
259cd3df6e53 Doc generation
wizard
parents:
diff changeset
16 my $self = shift;
259cd3df6e53 Doc generation
wizard
parents:
diff changeset
17
259cd3df6e53 Doc generation
wizard
parents:
diff changeset
18 return ref $self ? $self->CreateObject(@_) : $self->SUPER::new(@_);
259cd3df6e53 Doc generation
wizard
parents:
diff changeset
19 }
259cd3df6e53 Doc generation
wizard
parents:
diff changeset
20
259cd3df6e53 Doc generation
wizard
parents:
diff changeset
21 sub CTOR {
73
wizard
parents: 67
diff changeset
22 my ($this,$factory,$parameters,$method) = @_;
64
259cd3df6e53 Doc generation
wizard
parents:
diff changeset
23
259cd3df6e53 Doc generation
wizard
parents:
diff changeset
24 $this->factory($factory) or die new IMPL::InvalidArgumentException("The argument 'factory' is mandatory");
259cd3df6e53 Doc generation
wizard
parents:
diff changeset
25 $this->parameters($parameters) if $parameters;
73
wizard
parents: 67
diff changeset
26 $this->method($method) if $method;
64
259cd3df6e53 Doc generation
wizard
parents:
diff changeset
27 }
259cd3df6e53 Doc generation
wizard
parents:
diff changeset
28
259cd3df6e53 Doc generation
wizard
parents:
diff changeset
29 # override default restore method
259cd3df6e53 Doc generation
wizard
parents:
diff changeset
30 sub restore {
259cd3df6e53 Doc generation
wizard
parents:
diff changeset
31 my ($class,$data,$surrogate) = @_;
259cd3df6e53 Doc generation
wizard
parents:
diff changeset
32
259cd3df6e53 Doc generation
wizard
parents:
diff changeset
33 my %args = @$data;
259cd3df6e53 Doc generation
wizard
parents:
diff changeset
34
259cd3df6e53 Doc generation
wizard
parents:
diff changeset
35 if ($surrogate) {
91
9cb8e730fa86 fixed factory
wizard
parents: 73
diff changeset
36 $surrogate->callCTOR($args{factory},$args{parameters},$args{method});
64
259cd3df6e53 Doc generation
wizard
parents:
diff changeset
37 return $surrogate;
259cd3df6e53 Doc generation
wizard
parents:
diff changeset
38 } else {
91
9cb8e730fa86 fixed factory
wizard
parents: 73
diff changeset
39 return $class->new($args{factory},$args{parameters},$args{method});
64
259cd3df6e53 Doc generation
wizard
parents:
diff changeset
40 }
259cd3df6e53 Doc generation
wizard
parents:
diff changeset
41 }
259cd3df6e53 Doc generation
wizard
parents:
diff changeset
42
259cd3df6e53 Doc generation
wizard
parents:
diff changeset
43 sub CreateObject {
259cd3df6e53 Doc generation
wizard
parents:
diff changeset
44 my $this = shift;
259cd3df6e53 Doc generation
wizard
parents:
diff changeset
45
73
wizard
parents: 67
diff changeset
46 if (my $method = $this->method) {
wizard
parents: 67
diff changeset
47 $this->factory->$method($this->parameters ? (_as_list($this->parameters),@_) : @_);
wizard
parents: 67
diff changeset
48 } else {
wizard
parents: 67
diff changeset
49 $this->factory->new($this->parameters ? (_as_list($this->parameters),@_) : @_);
wizard
parents: 67
diff changeset
50 }
64
259cd3df6e53 Doc generation
wizard
parents:
diff changeset
51 }
259cd3df6e53 Doc generation
wizard
parents:
diff changeset
52
259cd3df6e53 Doc generation
wizard
parents:
diff changeset
53 sub _as_list {
259cd3df6e53 Doc generation
wizard
parents:
diff changeset
54 ref $_[0] ?
259cd3df6e53 Doc generation
wizard
parents:
diff changeset
55 (ref $_[0] eq 'HASH' ?
259cd3df6e53 Doc generation
wizard
parents:
diff changeset
56 %{$_[0]}
259cd3df6e53 Doc generation
wizard
parents:
diff changeset
57 :
259cd3df6e53 Doc generation
wizard
parents:
diff changeset
58 (ref $_[0] eq 'ARRAY'?
259cd3df6e53 Doc generation
wizard
parents:
diff changeset
59 @{$_[0]}
259cd3df6e53 Doc generation
wizard
parents:
diff changeset
60 :
259cd3df6e53 Doc generation
wizard
parents:
diff changeset
61 $_[0]
259cd3df6e53 Doc generation
wizard
parents:
diff changeset
62 )
259cd3df6e53 Doc generation
wizard
parents:
diff changeset
63 )
259cd3df6e53 Doc generation
wizard
parents:
diff changeset
64 :
259cd3df6e53 Doc generation
wizard
parents:
diff changeset
65 ($_[0]);
259cd3df6e53 Doc generation
wizard
parents:
diff changeset
66 }
259cd3df6e53 Doc generation
wizard
parents:
diff changeset
67
259cd3df6e53 Doc generation
wizard
parents:
diff changeset
68
259cd3df6e53 Doc generation
wizard
parents:
diff changeset
69 1;
259cd3df6e53 Doc generation
wizard
parents:
diff changeset
70
259cd3df6e53 Doc generation
wizard
parents:
diff changeset
71 __END__
259cd3df6e53 Doc generation
wizard
parents:
diff changeset
72
259cd3df6e53 Doc generation
wizard
parents:
diff changeset
73 =pod
259cd3df6e53 Doc generation
wizard
parents:
diff changeset
74
259cd3df6e53 Doc generation
wizard
parents:
diff changeset
75 =head1 SYNOPSIS
259cd3df6e53 Doc generation
wizard
parents:
diff changeset
76
259cd3df6e53 Doc generation
wizard
parents:
diff changeset
77 =begin code
259cd3df6e53 Doc generation
wizard
parents:
diff changeset
78
259cd3df6e53 Doc generation
wizard
parents:
diff changeset
79 my $factory = new IMPL::Object::Factory(
259cd3df6e53 Doc generation
wizard
parents:
diff changeset
80 'MyApp::User',
259cd3df6e53 Doc generation
wizard
parents:
diff changeset
81 {
259cd3df6e53 Doc generation
wizard
parents:
diff changeset
82 isAdmin => 1
259cd3df6e53 Doc generation
wizard
parents:
diff changeset
83 }
259cd3df6e53 Doc generation
wizard
parents:
diff changeset
84 );
259cd3df6e53 Doc generation
wizard
parents:
diff changeset
85
67
9f5795a10939 Documentation, minor fixes
wizard
parents: 64
diff changeset
86 my $class = 'MyApp::User';
9f5795a10939 Documentation, minor fixes
wizard
parents: 64
diff changeset
87
9f5795a10939 Documentation, minor fixes
wizard
parents: 64
diff changeset
88 my $user;
9f5795a10939 Documentation, minor fixes
wizard
parents: 64
diff changeset
89
9f5795a10939 Documentation, minor fixes
wizard
parents: 64
diff changeset
90 $user = $class->new(name => 'nobody'); # will create object MyApp::User
9f5795a10939 Documentation, minor fixes
wizard
parents: 64
diff changeset
91 # and pass parameters (name=>'nobody')
9f5795a10939 Documentation, minor fixes
wizard
parents: 64
diff changeset
92
9f5795a10939 Documentation, minor fixes
wizard
parents: 64
diff changeset
93 $user = $factory->new(name => 'root'); # will create object MyApp::User
9f5795a10939 Documentation, minor fixes
wizard
parents: 64
diff changeset
94 # and pass paremeters (isAdmin => 1, name => 'root')
9f5795a10939 Documentation, minor fixes
wizard
parents: 64
diff changeset
95
64
259cd3df6e53 Doc generation
wizard
parents:
diff changeset
96 =end code
259cd3df6e53 Doc generation
wizard
parents:
diff changeset
97
67
9f5795a10939 Documentation, minor fixes
wizard
parents: 64
diff changeset
98 Или сериализованная форма в XML.
9f5795a10939 Documentation, minor fixes
wizard
parents: 64
diff changeset
99
9f5795a10939 Documentation, minor fixes
wizard
parents: 64
diff changeset
100 =begin code xml
64
259cd3df6e53 Doc generation
wizard
parents:
diff changeset
101
67
9f5795a10939 Documentation, minor fixes
wizard
parents: 64
diff changeset
102 <factory type="IMPL::Object::Factory">
9f5795a10939 Documentation, minor fixes
wizard
parents: 64
diff changeset
103 <factory>MyApp::User</factory>,
9f5795a10939 Documentation, minor fixes
wizard
parents: 64
diff changeset
104 <parameters type="HASH">
9f5795a10939 Documentation, minor fixes
wizard
parents: 64
diff changeset
105 <isAdmin>1</isAdmin>
9f5795a10939 Documentation, minor fixes
wizard
parents: 64
diff changeset
106 </parameters>
9f5795a10939 Documentation, minor fixes
wizard
parents: 64
diff changeset
107 </factory>
9f5795a10939 Documentation, minor fixes
wizard
parents: 64
diff changeset
108
9f5795a10939 Documentation, minor fixes
wizard
parents: 64
diff changeset
109 =end code xml
64
259cd3df6e53 Doc generation
wizard
parents:
diff changeset
110
259cd3df6e53 Doc generation
wizard
parents:
diff changeset
111 =head1 DESCRIPTION
259cd3df6e53 Doc generation
wizard
parents:
diff changeset
112
67
9f5795a10939 Documentation, minor fixes
wizard
parents: 64
diff changeset
113 C<[Serializable]>
9f5795a10939 Documentation, minor fixes
wizard
parents: 64
diff changeset
114
64
259cd3df6e53 Doc generation
wizard
parents:
diff changeset
115 Класс, реализующий фабрику классов.
259cd3df6e53 Doc generation
wizard
parents:
diff changeset
116
259cd3df6e53 Doc generation
wizard
parents:
diff changeset
117 Фабрика классов это любой объект, который имеет метод C< new > вызов которого приводит к созданию нового
259cd3df6e53 Doc generation
wizard
parents:
diff changeset
118 объекта. Например каждый класс сам явялется фабрикой, поскольку, если у него вызвать метод
259cd3df6e53 Doc generation
wizard
parents:
diff changeset
119 C< new >, то будет создан объект. Полученные объекты, в силу механизмов языка Perl, также
259cd3df6e53 Doc generation
wizard
parents:
diff changeset
120 являются фабриками, притом такимиже, что и класс.
259cd3df6e53 Doc generation
wizard
parents:
diff changeset
121
259cd3df6e53 Doc generation
wizard
parents:
diff changeset
122 Данный класс меняет поведение метода C< new > в зависимости от контекста вызова: статического
259cd3df6e53 Doc generation
wizard
parents:
diff changeset
123 метода или метода объекта. При вызове метода C< new > у класса происходит создание объекта
259cd3df6e53 Doc generation
wizard
parents:
diff changeset
124 фабрики с определенными параметрами. Далее объект-фабрика может быть использована для создания
259cd3df6e53 Doc generation
wizard
parents:
diff changeset
125 объектов уже на основе параметров фабрики.
259cd3df6e53 Doc generation
wizard
parents:
diff changeset
126
259cd3df6e53 Doc generation
wizard
parents:
diff changeset
127 =head1 MEMBERS
259cd3df6e53 Doc generation
wizard
parents:
diff changeset
128
259cd3df6e53 Doc generation
wizard
parents:
diff changeset
129 =over
259cd3df6e53 Doc generation
wizard
parents:
diff changeset
130
73
wizard
parents: 67
diff changeset
131 =item C< CTOR($factory,$parameters,$method) >
67
9f5795a10939 Documentation, minor fixes
wizard
parents: 64
diff changeset
132
73
wizard
parents: 67
diff changeset
133 Создает новый экземпляр фабрики.
67
9f5795a10939 Documentation, minor fixes
wizard
parents: 64
diff changeset
134
9f5795a10939 Documentation, minor fixes
wizard
parents: 64
diff changeset
135 =over
9f5795a10939 Documentation, minor fixes
wizard
parents: 64
diff changeset
136
9f5795a10939 Documentation, minor fixes
wizard
parents: 64
diff changeset
137 =item C<$factory>
9f5795a10939 Documentation, minor fixes
wizard
parents: 64
diff changeset
138
9f5795a10939 Documentation, minor fixes
wizard
parents: 64
diff changeset
139 Либо имя класса, либо другая фабрика.
9f5795a10939 Documentation, minor fixes
wizard
parents: 64
diff changeset
140
9f5795a10939 Documentation, minor fixes
wizard
parents: 64
diff changeset
141 =item C<$parameters>
9f5795a10939 Documentation, minor fixes
wizard
parents: 64
diff changeset
142
9f5795a10939 Documentation, minor fixes
wizard
parents: 64
diff changeset
143 Ссылка на параметры для создания объектов, может быть ссылкой на хеш, массив и т.д.
9f5795a10939 Documentation, minor fixes
wizard
parents: 64
diff changeset
144
9f5795a10939 Documentation, minor fixes
wizard
parents: 64
diff changeset
145 Если является ссылкой на хеш, то при создании объектов данной фабрикой этот хеш
9f5795a10939 Documentation, minor fixes
wizard
parents: 64
diff changeset
146 будет развернут в список и передан параметрами методу C<new>.
9f5795a10939 Documentation, minor fixes
wizard
parents: 64
diff changeset
147
9f5795a10939 Documentation, minor fixes
wizard
parents: 64
diff changeset
148 Если является ссылкой на массив, то при создании объектов данной фабрикой этот массив
9f5795a10939 Documentation, minor fixes
wizard
parents: 64
diff changeset
149 будет передан в списк и передан параметрами методу C<new>.
9f5795a10939 Documentation, minor fixes
wizard
parents: 64
diff changeset
150
9f5795a10939 Documentation, minor fixes
wizard
parents: 64
diff changeset
151 Если является любым другим объектом или скаляром, то будет передан параметром методу
9f5795a10939 Documentation, minor fixes
wizard
parents: 64
diff changeset
152 C<new> как есть.
9f5795a10939 Documentation, minor fixes
wizard
parents: 64
diff changeset
153
73
wizard
parents: 67
diff changeset
154 =item C<$method>
wizard
parents: 67
diff changeset
155
wizard
parents: 67
diff changeset
156 Имя метода (или ссылка на процедуру), который будет вызван у C<$factory> при создании
wizard
parents: 67
diff changeset
157 текущей фабрикой нового объекта.
wizard
parents: 67
diff changeset
158
67
9f5795a10939 Documentation, minor fixes
wizard
parents: 64
diff changeset
159 =back
9f5795a10939 Documentation, minor fixes
wizard
parents: 64
diff changeset
160
9f5795a10939 Documentation, minor fixes
wizard
parents: 64
diff changeset
161 =item C< [get] factory >
64
259cd3df6e53 Doc generation
wizard
parents:
diff changeset
162
259cd3df6e53 Doc generation
wizard
parents:
diff changeset
163 Свойство, содержащее фабрику для создание новых объектов текущей фабрикой. Чаще всего оно содержит
259cd3df6e53 Doc generation
wizard
parents:
diff changeset
164 имя класса.
259cd3df6e53 Doc generation
wizard
parents:
diff changeset
165
67
9f5795a10939 Documentation, minor fixes
wizard
parents: 64
diff changeset
166 =item C< [get] parameters >
64
259cd3df6e53 Doc generation
wizard
parents:
diff changeset
167
259cd3df6e53 Doc generation
wizard
parents:
diff changeset
168 Свойство, содержит ссылку на параметры для создания объектов, при создании объекта эти параметры будут
259cd3df6e53 Doc generation
wizard
parents:
diff changeset
169 развернуты в список и переданы оператору C< new > фабрике из свойства C< factory >, за ними будут
67
9f5795a10939 Documentation, minor fixes
wizard
parents: 64
diff changeset
170 следовать параметры непосредственно текущей фабрики.
9f5795a10939 Documentation, minor fixes
wizard
parents: 64
diff changeset
171
9f5795a10939 Documentation, minor fixes
wizard
parents: 64
diff changeset
172 =item C<new(@params)>
9f5795a10939 Documentation, minor fixes
wizard
parents: 64
diff changeset
173
9f5795a10939 Documentation, minor fixes
wizard
parents: 64
diff changeset
174 Создает новый объект, используя свйство C<factory> как фабрику и передавая туда параметры
9f5795a10939 Documentation, minor fixes
wizard
parents: 64
diff changeset
175 из свойства C<parameters> и списка C<@params>. Ниже приведен упрощенный пример, как это происходит.
9f5795a10939 Documentation, minor fixes
wizard
parents: 64
diff changeset
176
9f5795a10939 Documentation, minor fixes
wizard
parents: 64
diff changeset
177 =begin code
9f5795a10939 Documentation, minor fixes
wizard
parents: 64
diff changeset
178
9f5795a10939 Documentation, minor fixes
wizard
parents: 64
diff changeset
179 sub new {
9f5795a10939 Documentation, minor fixes
wizard
parents: 64
diff changeset
180 my ($this,@params) = @_;
9f5795a10939 Documentation, minor fixes
wizard
parents: 64
diff changeset
181
73
wizard
parents: 67
diff changeset
182 my $method = $this->method || 'new';
wizard
parents: 67
diff changeset
183
wizard
parents: 67
diff changeset
184 return $this->factory->$method(_as_list($this->parameters), @params);
67
9f5795a10939 Documentation, minor fixes
wizard
parents: 64
diff changeset
185 }
9f5795a10939 Documentation, minor fixes
wizard
parents: 64
diff changeset
186
9f5795a10939 Documentation, minor fixes
wizard
parents: 64
diff changeset
187 =end code
64
259cd3df6e53 Doc generation
wizard
parents:
diff changeset
188
259cd3df6e53 Doc generation
wizard
parents:
diff changeset
189 =back
259cd3df6e53 Doc generation
wizard
parents:
diff changeset
190
259cd3df6e53 Doc generation
wizard
parents:
diff changeset
191 =cut