comparison Lib/IMPL/Web/View/TTFactory.pm @ 312:75a78cbf7dcf

View refactoring: INIT blocks are deprecated
author cin
date Mon, 29 Apr 2013 01:10:42 +0400
parents 5e4e7c8fbca1
children 86336d451b82
comparison
equal deleted inserted replaced
311:d3b5a67ad2e8 312:75a78cbf7dcf
2 use strict; 2 use strict;
3 3
4 use Template::Context(); 4 use Template::Context();
5 5
6 use Carp qw(carp); 6 use Carp qw(carp);
7 use IMPL::lang qw(:hash); 7 use IMPL::lang qw(:hash is);
8 use IMPL::Exception(); 8 use IMPL::Exception();
9 use Scalar::Util qw(weaken); 9 use Scalar::Util qw(weaken);
10 10
11 11
12 use IMPL::Const qw(:prop); 12 use IMPL::Const qw(:prop);
21 shift; 21 shift;
22 } 22 }
23 ], 23 ],
24 props => [ 24 props => [
25 template => PROP_RW, 25 template => PROP_RW,
26 activation => PROP_RW,
26 context => PROP_RW, 27 context => PROP_RW,
27 instances => PROP_RW,
28 baseLocation => PROP_RW, 28 baseLocation => PROP_RW,
29 base => PROP_RW, 29 base => PROP_RW,
30 registry => PROP_RO, 30 registry => PROP_RO,
31 blocks => PROP_RO, 31 blocks => PROP_RO,
32 initialized => PROP_RO 32 _instance => PROP_RW
33 ] 33 ]
34 }; 34 };
35 35
36 sub CTOR { 36 sub CTOR {
37 my ($this,$class,$template,$context,$path,$registry) = @_; 37 my ($this,$class,$template,$context,$path,$registry) = @_;
44 unless ref $class ; 44 unless ref $class ;
45 45
46 $context ||= new Template::Context(); 46 $context ||= new Template::Context();
47 my $baseLocation = join( '/', splice( @{[split(/\//,$path)]}, 0, -1 ) ); 47 my $baseLocation = join( '/', splice( @{[split(/\//,$path)]}, 0, -1 ) );
48 48
49 $this->activation($template->activation || 'new');
49 $this->template($template); 50 $this->template($template);
50 $this->context($context); 51 $this->context($context);
51 $this->baseLocation($baseLocation); 52 $this->baseLocation($baseLocation);
52 $this->instances(0);
53 $this->registry($registry); 53 $this->registry($registry);
54 54
55 if (my $baseTplName = $template->extends) { 55 if (my $baseTplName = $template->extends) {
56 $baseTplName =~ s{^\./}{$baseLocation/}; 56 $baseTplName =~ s{^\./}{$baseLocation/};
57 57
124 } 124 }
125 125
126 sub CreateObject { 126 sub CreateObject {
127 my $this = shift; 127 my $this = shift;
128 128
129 $this->InitOnDemand(); 129 $this->activation eq 'singleton' ?
130 130 $this->_instance || $this->_instance($this->next::method(@_)) :
131 my $instance = $this->SUPER::CreateObject(@_); 131 $this->next::method(@_);
132
133 my $count = $this->instances;
134 $count++;
135 $this->instances($count);
136
137 return $instance;
138 }
139
140 sub InitOnDemand {
141 my ($this) = @_;
142
143 unless ($this->initialized) {
144 $this->initialized(1);
145
146 $this->base->InitOnDemand()
147 if $this->base;
148
149 if (my $init = $this->template->blocks->{INIT}) {
150 $this->context->process($init);
151 }
152 }
153 } 132 }
154 133
155 sub CloneContext { 134 sub CloneContext {
156 my ($this) = @_; 135 my ($this) = @_;
157 136