Mercurial > pub > Impl
annotate Lib/IMPL/Code/Loader.pm @ 379:a471e8b77544
sync
author | cin |
---|---|
date | Thu, 16 Jan 2014 18:10:50 +0400 |
parents | 4ddb27ff4a0b |
children |
rev | line source |
---|---|
165 | 1 package IMPL::Code::Loader; |
2 use strict; | |
3 use warnings; | |
4 | |
230 | 5 use IMPL::Const qw(:prop); |
274 | 6 use File::Spec; |
209 | 7 use IMPL::declare { |
8 require => { | |
9 Exception => 'IMPL::Exception', | |
10 ArgumentException => '-IMPL::InvalidArgumentException' | |
11 }, | |
12 base => { | |
13 'IMPL::Object' => undef, | |
14 'IMPL::Object::Autofill' => '@_' | |
230 | 15 }, |
16 props => [ | |
17 verifyNames => PROP_RO, | |
18 prefix => PROP_RO, | |
19 _pending => PROP_RW | |
20 ] | |
209 | 21 }; |
165 | 22 |
209 | 23 my $default; |
24 sub default { | |
25 $default ||= new IMPL::Code::Loader; | |
165 | 26 } |
27 | |
209 | 28 my $safe; |
29 sub safe { | |
30 $safe ||= new IMPL::Code::Loader(verifyNames => 1); | |
31 } | |
32 | |
230 | 33 sub CTOR { |
34 my ($this) = @_; | |
35 | |
36 $this->_pending({}); | |
209 | 37 } |
38 | |
165 | 39 sub Require { |
209 | 40 my ($this,$package) = @_; |
194 | 41 |
209 | 42 if ($this->verifyNames) { |
229
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
209
diff
changeset
|
43 $package =~ m/^([a-zA-Z_0-9]+(?:::[a-zA-Z_0-9]+)*)$/ |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
209
diff
changeset
|
44 or die ArgumentException->new(package => 'Invalid package name') ; |
278 | 45 $package = $1; |
209 | 46 } |
194 | 47 |
229
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
209
diff
changeset
|
48 $package = $this->prefix . '::' . $package if $this->prefix; |
194 | 49 |
209 | 50 my $file = join('/', split(/::/,$package)) . ".pm"; |
194 | 51 |
209 | 52 require $file; |
230 | 53 |
54 return $package; | |
55 } | |
56 | |
274 | 57 sub ModuleExists { |
58 my ($this,$package) = @_; | |
59 | |
60 my $file = join('/', split(/::/,$this->GetFullName($package))) . ".pm"; | |
61 | |
62 -f File::Spec->catfile($_,$file) and return 1 foreach @INC; | |
63 | |
64 return 0; | |
65 } | |
66 | |
230 | 67 sub GetFullName { |
68 my ($this,$package) = @_; | |
69 | |
70 if ($this->verifyNames) { | |
71 $package =~ m/^([a-zA-Z_0-9]+(?:::[a-zA-Z_0-9]+)*)$/ | |
72 or die ArgumentException->new(package => 'Invalid package name') ; | |
73 } | |
74 | |
75 return $this->prefix . '::' . $package if $this->prefix; | |
165 | 76 } |
77 | |
78 1; | |
79 |