annotate Lib/IMPL/Config/Activator.pm @ 393:69a1f1508696

minor security refactoring
author cin
date Fri, 14 Feb 2014 16:41:12 +0400
parents 8088779e539d
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
59
0f3e369553bd Rewritten property implementation (probably become slower but more flexible)
wizard
parents:
diff changeset
1 package IMPL::Config::Activator;
0f3e369553bd Rewritten property implementation (probably become slower but more flexible)
wizard
parents:
diff changeset
2 use strict;
0f3e369553bd Rewritten property implementation (probably become slower but more flexible)
wizard
parents:
diff changeset
3
293
15d87ef41764 *IMPL::Config::Activator: refactoring
sergey
parents: 206
diff changeset
4 use Scalar::Util qw(reftype);
15d87ef41764 *IMPL::Config::Activator: refactoring
sergey
parents: 206
diff changeset
5 use IMPL::lang;
15d87ef41764 *IMPL::Config::Activator: refactoring
sergey
parents: 206
diff changeset
6 use IMPL::Const qw(:prop);
15d87ef41764 *IMPL::Config::Activator: refactoring
sergey
parents: 206
diff changeset
7 use IMPL::declare {
294
8088779e539d corrected IMPL::Config::Activator
cin
parents: 293
diff changeset
8 require => {
293
15d87ef41764 *IMPL::Config::Activator: refactoring
sergey
parents: 206
diff changeset
9 Loader => 'IMPL::Code::Loader',
15d87ef41764 *IMPL::Config::Activator: refactoring
sergey
parents: 206
diff changeset
10 Exception => 'IMPL::Exception'
294
8088779e539d corrected IMPL::Config::Activator
cin
parents: 293
diff changeset
11 },
293
15d87ef41764 *IMPL::Config::Activator: refactoring
sergey
parents: 206
diff changeset
12 base => [
15d87ef41764 *IMPL::Config::Activator: refactoring
sergey
parents: 206
diff changeset
13 'IMPL::Object' => undef,
15d87ef41764 *IMPL::Config::Activator: refactoring
sergey
parents: 206
diff changeset
14 'IMPL::Object::Autofill' => '@_',
15d87ef41764 *IMPL::Config::Activator: refactoring
sergey
parents: 206
diff changeset
15 'IMPL::Object::PublicSerializable' => undef
15d87ef41764 *IMPL::Config::Activator: refactoring
sergey
parents: 206
diff changeset
16 ],
15d87ef41764 *IMPL::Config::Activator: refactoring
sergey
parents: 206
diff changeset
17 props => [
15d87ef41764 *IMPL::Config::Activator: refactoring
sergey
parents: 206
diff changeset
18 factory => PROP_RW,
15d87ef41764 *IMPL::Config::Activator: refactoring
sergey
parents: 206
diff changeset
19 parameters => PROP_RW,
294
8088779e539d corrected IMPL::Config::Activator
cin
parents: 293
diff changeset
20 singleCall => PROP_RW,
8088779e539d corrected IMPL::Config::Activator
cin
parents: 293
diff changeset
21 _object => PROP_RW
293
15d87ef41764 *IMPL::Config::Activator: refactoring
sergey
parents: 206
diff changeset
22 ]
15d87ef41764 *IMPL::Config::Activator: refactoring
sergey
parents: 206
diff changeset
23 };
59
0f3e369553bd Rewritten property implementation (probably become slower but more flexible)
wizard
parents:
diff changeset
24
293
15d87ef41764 *IMPL::Config::Activator: refactoring
sergey
parents: 206
diff changeset
25 use constant {
294
8088779e539d corrected IMPL::Config::Activator
cin
parents: 293
diff changeset
26 SELF_CLASS => __PACKAGE__,
293
15d87ef41764 *IMPL::Config::Activator: refactoring
sergey
parents: 206
diff changeset
27 };
59
0f3e369553bd Rewritten property implementation (probably become slower but more flexible)
wizard
parents:
diff changeset
28
0f3e369553bd Rewritten property implementation (probably become slower but more flexible)
wizard
parents:
diff changeset
29 sub CTOR {
0f3e369553bd Rewritten property implementation (probably become slower but more flexible)
wizard
parents:
diff changeset
30 my $this = shift;
60
b0c068da93ac Lazy activation for the configuration objects (final concept)
wizard
parents: 59
diff changeset
31
293
15d87ef41764 *IMPL::Config::Activator: refactoring
sergey
parents: 206
diff changeset
32 die Exception->new("A factory parameter is required") unless $this->factory;
59
0f3e369553bd Rewritten property implementation (probably become slower but more flexible)
wizard
parents:
diff changeset
33
0f3e369553bd Rewritten property implementation (probably become slower but more flexible)
wizard
parents:
diff changeset
34 }
0f3e369553bd Rewritten property implementation (probably become slower but more flexible)
wizard
parents:
diff changeset
35
0f3e369553bd Rewritten property implementation (probably become slower but more flexible)
wizard
parents:
diff changeset
36
60
b0c068da93ac Lazy activation for the configuration objects (final concept)
wizard
parents: 59
diff changeset
37 sub activate {
59
0f3e369553bd Rewritten property implementation (probably become slower but more flexible)
wizard
parents:
diff changeset
38 my $this = shift;
0f3e369553bd Rewritten property implementation (probably become slower but more flexible)
wizard
parents:
diff changeset
39
294
8088779e539d corrected IMPL::Config::Activator
cin
parents: 293
diff changeset
40 unless ($this->_object) {
60
b0c068da93ac Lazy activation for the configuration objects (final concept)
wizard
parents: 59
diff changeset
41 my @args;
b0c068da93ac Lazy activation for the configuration objects (final concept)
wizard
parents: 59
diff changeset
42
b0c068da93ac Lazy activation for the configuration objects (final concept)
wizard
parents: 59
diff changeset
43 my $params = $this->parameters;
294
8088779e539d corrected IMPL::Config::Activator
cin
parents: 293
diff changeset
44 if (ref $params eq 'HASH') {
194
4d0e1962161c Replaced tabs with spaces
cin
parents: 180
diff changeset
45 while ( my ($key,$value) = each %$params ) {
293
15d87ef41764 *IMPL::Config::Activator: refactoring
sergey
parents: 206
diff changeset
46 push @args,$key, is($value,SELF_CLASS) ? $value->activate : $value;
194
4d0e1962161c Replaced tabs with spaces
cin
parents: 180
diff changeset
47 }
294
8088779e539d corrected IMPL::Config::Activator
cin
parents: 293
diff changeset
48 } elsif (ref $params eq 'ARRAY') {
293
15d87ef41764 *IMPL::Config::Activator: refactoring
sergey
parents: 206
diff changeset
49 push @args, map is($_,SELF_CLASS) ? $_->activate : $_, @$params;
60
b0c068da93ac Lazy activation for the configuration objects (final concept)
wizard
parents: 59
diff changeset
50 } else {
293
15d87ef41764 *IMPL::Config::Activator: refactoring
sergey
parents: 206
diff changeset
51 push @args, is($params,SELF_CLASS) ? $params->activate : $params;
60
b0c068da93ac Lazy activation for the configuration objects (final concept)
wizard
parents: 59
diff changeset
52 }
b0c068da93ac Lazy activation for the configuration objects (final concept)
wizard
parents: 59
diff changeset
53
293
15d87ef41764 *IMPL::Config::Activator: refactoring
sergey
parents: 206
diff changeset
54 push @args, map is($_,SELF_CLASS) ? $_->activate : $_, @_ if @_;
60
b0c068da93ac Lazy activation for the configuration objects (final concept)
wizard
parents: 59
diff changeset
55
b0c068da93ac Lazy activation for the configuration objects (final concept)
wizard
parents: 59
diff changeset
56 my $factory = $this->factory;
294
8088779e539d corrected IMPL::Config::Activator
cin
parents: 293
diff changeset
57 Loader->default->Require($factory)
8088779e539d corrected IMPL::Config::Activator
cin
parents: 293
diff changeset
58 unless ref($factory);
8088779e539d corrected IMPL::Config::Activator
cin
parents: 293
diff changeset
59
8088779e539d corrected IMPL::Config::Activator
cin
parents: 293
diff changeset
60 my $instance = $factory->new(@args);
60
b0c068da93ac Lazy activation for the configuration objects (final concept)
wizard
parents: 59
diff changeset
61
294
8088779e539d corrected IMPL::Config::Activator
cin
parents: 293
diff changeset
62 $this->_object($instance)
8088779e539d corrected IMPL::Config::Activator
cin
parents: 293
diff changeset
63 unless $this->singleCall;
8088779e539d corrected IMPL::Config::Activator
cin
parents: 293
diff changeset
64
8088779e539d corrected IMPL::Config::Activator
cin
parents: 293
diff changeset
65 return $instance;
59
0f3e369553bd Rewritten property implementation (probably become slower but more flexible)
wizard
parents:
diff changeset
66 } else {
294
8088779e539d corrected IMPL::Config::Activator
cin
parents: 293
diff changeset
67 return $this->_object;
59
0f3e369553bd Rewritten property implementation (probably become slower but more flexible)
wizard
parents:
diff changeset
68 }
0f3e369553bd Rewritten property implementation (probably become slower but more flexible)
wizard
parents:
diff changeset
69 }
0f3e369553bd Rewritten property implementation (probably become slower but more flexible)
wizard
parents:
diff changeset
70
180
d1676be8afcc Перекодировка в utf-8
sourcer
parents: 165
diff changeset
71 1;
293
15d87ef41764 *IMPL::Config::Activator: refactoring
sergey
parents: 206
diff changeset
72
15d87ef41764 *IMPL::Config::Activator: refactoring
sergey
parents: 206
diff changeset
73 __END__
15d87ef41764 *IMPL::Config::Activator: refactoring
sergey
parents: 206
diff changeset
74
15d87ef41764 *IMPL::Config::Activator: refactoring
sergey
parents: 206
diff changeset
75 =pod
15d87ef41764 *IMPL::Config::Activator: refactoring
sergey
parents: 206
diff changeset
76
15d87ef41764 *IMPL::Config::Activator: refactoring
sergey
parents: 206
diff changeset
77 =head1 NAME
15d87ef41764 *IMPL::Config::Activator: refactoring
sergey
parents: 206
diff changeset
78
294
8088779e539d corrected IMPL::Config::Activator
cin
parents: 293
diff changeset
79 C<IMPL::Config::Activator> - объект, используемый для получения других объектов.
8088779e539d corrected IMPL::Config::Activator
cin
parents: 293
diff changeset
80
8088779e539d corrected IMPL::Config::Activator
cin
parents: 293
diff changeset
81 =head1 DESCRIPTION
8088779e539d corrected IMPL::Config::Activator
cin
parents: 293
diff changeset
82
8088779e539d corrected IMPL::Config::Activator
cin
parents: 293
diff changeset
83 Служит дополнительным уровнем абстракции в тех случаях, когда нужный объект
8088779e539d corrected IMPL::Config::Activator
cin
parents: 293
diff changeset
84 заранее не известен или его создание должно происходить по требованию.
8088779e539d corrected IMPL::Config::Activator
cin
parents: 293
diff changeset
85 От обычной фабрики отличается также тем, что рассматривает формальные параметры
8088779e539d corrected IMPL::Config::Activator
cin
parents: 293
diff changeset
86 на наличие активаторов и выполняет их при активации.
8088779e539d corrected IMPL::Config::Activator
cin
parents: 293
diff changeset
87
8088779e539d corrected IMPL::Config::Activator
cin
parents: 293
diff changeset
88 Кроме того можно указать, что процесс активации должен происходить при каждом
8088779e539d corrected IMPL::Config::Activator
cin
parents: 293
diff changeset
89 обращении.
293
15d87ef41764 *IMPL::Config::Activator: refactoring
sergey
parents: 206
diff changeset
90
15d87ef41764 *IMPL::Config::Activator: refactoring
sergey
parents: 206
diff changeset
91 =cut