annotate lib/IMPL/declare.pm @ 425:c27434cdd611 ref20150831

sync
author cin
date Tue, 03 Apr 2018 19:30:01 +0300
parents b0481c071bea
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
407
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
1 package IMPL::declare;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
2 use strict;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
3
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
4 use Carp qw(carp);
417
3ed0c58e9da3 working on di container, tests
cin
parents: 411
diff changeset
5 use IMPL::lang qw( :base );
407
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
6 use IMPL::Class::PropertyInfo();
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
7 use IMPL::Const qw(:access);
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
8 use IMPL::require();
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
9
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
10 BEGIN {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
11 *_require = *IMPL::require::_require;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
12 *_trace = *IMPL::require::_trace;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
13 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
14
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
15 sub import {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
16 my ( $self, $args ) = @_;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
17
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
18 return unless $args;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
19
417
3ed0c58e9da3 working on di container, tests
cin
parents: 411
diff changeset
20 die "A hash reference is required" unless ishash($args);
407
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
21
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
22 no strict 'refs';
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
23 no warnings 'once';
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
24
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
25 my $caller = caller;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
26
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
27 my $aliases = $args->{require} || {};
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
28
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
29 $IMPL::require::PENDING{$caller} = 1;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
30 _trace("declare $caller");
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
31 $IMPL::require::level++;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
32
422
b0481c071bea IMPL::Config::Container tests, YAMLConfiguration now works and tested
cin
parents: 417
diff changeset
33 my $tcaller = $caller;
407
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
34 *{"${caller}::SELF"} = sub () {
422
b0481c071bea IMPL::Config::Container tests, YAMLConfiguration now works and tested
cin
parents: 417
diff changeset
35 $tcaller;
407
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
36 };
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
37
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
38 while ( my ( $alias, $class ) = each %$aliases ) {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
39 _trace("$alias => $class");
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
40 $IMPL::require::level++;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
41 my $c = _require($class);
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
42
411
cin
parents: 407
diff changeset
43 *{"${caller}::$alias"} = sub () {
407
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
44 $c;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
45 };
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
46 $IMPL::require::level--;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
47 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
48
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
49 my $base = $args->{base} || {};
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
50
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
51 my %ctor;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
52 my @isa;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
53
417
3ed0c58e9da3 working on di container, tests
cin
parents: 411
diff changeset
54 if ( isarray($base) ) {
407
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
55 carp "Odd elements number in require"
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
56 unless scalar(@$base) % 2 == 0;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
57 while ( my ( $class, $mapper ) = splice @$base, 0, 2 ) {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
58 _trace("parent $class");
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
59 $IMPL::require::level++;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
60 $class = $aliases->{$class} || _require($class);
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
61 $IMPL::require::level--;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
62
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
63 push @isa, $class;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
64 $ctor{$class} = $mapper;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
65 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
66 }
417
3ed0c58e9da3 working on di container, tests
cin
parents: 411
diff changeset
67 elsif ( ishash($base) ) {
407
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
68 while ( my ( $class, $mapper ) = each %$base ) {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
69 _trace("parent $class");
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
70 $IMPL::require::level++;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
71 $class = $aliases->{$class} || _require($class);
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
72 $IMPL::require::level--;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
73
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
74 push @isa, $class;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
75 $ctor{$class} = $mapper;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
76 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
77 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
78
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
79 %{"${caller}::CTOR"} = %ctor;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
80 push @{"${caller}::ISA"}, @isa;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
81
417
3ed0c58e9da3 working on di container, tests
cin
parents: 411
diff changeset
82 if ( isarray( $args->{meta} ) ) {
407
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
83 $caller->SetMeta($_) foreach @{ $args->{meta} };
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
84 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
85
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
86 my $props = $args->{props} || [];
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
87
417
3ed0c58e9da3 working on di container, tests
cin
parents: 411
diff changeset
88 if ( ishash($props) ) {
407
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
89 $props = [%$props];
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
90 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
91
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
92 die "A hash or an array reference is required in the properties list"
417
3ed0c58e9da3 working on di container, tests
cin
parents: 411
diff changeset
93 unless isarray($props);
407
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
94
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
95 carp "Odd elements number in properties declaration of $caller"
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
96 unless scalar(@$props) % 2 == 0;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
97
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
98 if (@$props) {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
99 $self->_implementProps( $props, $caller );
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
100 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
101
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
102 if ( $args->{_implement} ) {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
103 $self->_implementProps( $caller->abstractProps, $caller );
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
104 $caller->abstractProps( [] );
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
105 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
106
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
107 $IMPL::require::level--;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
108 delete $IMPL::require::PENDING{$caller};
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
109 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
110
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
111 sub _implementProps {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
112 my ( $self, $props, $caller ) = @_;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
113
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
114 for ( my $i = 0 ; $i < @$props - 1 ; $i = $i + 2 ) {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
115 my ( $prop, $spec ) = @{$props}[ $i, $i + 1 ];
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
116
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
117 $caller->ClassPropertyImplementor->Implement(
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
118 $spec,
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
119 {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
120 name => $prop,
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
121 class => $caller,
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
122 access => $prop =~ /^_/
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
123 ? ACCESS_PRIVATE
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
124 : ACCESS_PUBLIC
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
125 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
126 );
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
127 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
128 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
129
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
130 1;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
131
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
132 __END__
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
133
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
134 =pod
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
135
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
136 =head1 NAME
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
137
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
138 C<IMPL::declare> - описывает класс
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
139
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
140 =head1 SYNOPSIS
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
141
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
142 =begin code
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
143
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
144 package My::Bar;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
145
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
146 use IMPL::declare {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
147 require => {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
148 TFoo => 'My::Foo',
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
149 TBox => 'My::Box'
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
150 },
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
151 base => {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
152 TFoo => '@_',
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
153 'IMPL::Object' => undef,
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
154 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
155 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
156
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
157 sub CreateBox {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
158 my ($this) = @_;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
159 return TBox->new($this);
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
160 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
161
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
162 =end code
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
163
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
164 Специальная ситрока C<@_> означает передачу параметров конструктора текущего класса конструктору
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
165 базового класса без изменений.
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
166
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
167 =head1 DESCRIPTION
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
168
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
169 Описывает текущий пакет(модуль) как класс. В качестве параметра получает ссылку на хеш,
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
170 в которой храняться метаданные для объявления класса.
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
171
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
172 =head1 METADATA
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
173
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
174 =head2 C<require>
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
175
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
176 Содержит ссылку на хеш с синонимами модулей, которые будут доступны в текушем модуле,
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
177 аналогично использованию C<IMPL::require>. Однако, если модуль не требует загрузки при
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
178 помощи C<require> нужно использовать префикс C<'-'> в его имени
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
179
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
180 =begin code
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
181
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
182 {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
183 require => {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
184 TObject => 'IMPL::Object', # will be loaded with require
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
185 TFoo => '-My:App::Data::Foo' # will not use 'require' to load module
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
186 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
187 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
188
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
189 =end code
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
190
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
191 =head2 C<base>
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
192
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
193 Обисывает базове классы для текущего класса. Если данный параметр - ссылка массив, то
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
194 этот массив будет превращен в массив C<@ISA>. Если данный параметр - ссылка на хеш, то
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
195 его ключи опичавют список базовых классов, а значения - преобразование параметров для
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
196 вызова базовых конструкторов.
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
197
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
198 В качестве имен базовых классов могут быть как полные имена модулей, так и назначенные
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
199 ранее псевдонимы. Использование префикса C<'-'> перед B<полным именем модуля> означает,
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
200 что модуль не требуется загружать, в случае с псевдонимами, префикс C<'-'> уже был указан
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
201 при их объявлении.
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
202
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
203 =begin code
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
204
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
205 {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
206 require => {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
207 TFoo => '-My:App::Data::Foo' # will not use 'require' to load module
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
208 },
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
209 base => {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
210 TFoo => '@_', # pass parameters unchanged
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
211 'My::Base::Class' => sub { name => $_[0], data => $_[1] }, # remap parameters
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
212 '-My::Extentions' => undef, # do not pass any parameters
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
213 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
214 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
215
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
216 =end code
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
217
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
218 =cut