annotate lib/IMPL/Code/Loader.pm @ 410:9335cf010b23 ref20150831

refactoring
author cin
date Mon, 14 Sep 2015 01:11:53 +0300
parents c6e90e02dd17
children ee36115f6a34
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::Code::Loader;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
2 use strict;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
3 use warnings;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
4
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
5 use IMPL::Const qw(:prop);
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
6 use File::Spec;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
7 use IMPL::declare {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
8 require => {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
9 Exception => 'IMPL::Exception',
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
10 ArgumentException => '-IMPL::InvalidArgumentException'
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
11 },
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
12 base => {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
13 'IMPL::Object' => undef,
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
14 'IMPL::Object::Autofill' => '@_'
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
15 },
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
16 props => [
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
17 verifyNames => PROP_RO,
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
18 prefix => PROP_RO,
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
19 _pending => PROP_RW
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
20 ]
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
21 };
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
22
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
23 my $default;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
24 sub default {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
25 $default ||= new IMPL::Code::Loader;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
26 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
27
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
28 my $safe;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
29 sub safe {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
30 $safe ||= new IMPL::Code::Loader(verifyNames => 1);
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
31 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
32
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
33 sub CTOR {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
34 my ($this) = @_;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
35
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
36 $this->_pending({});
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
37 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
38
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
39 sub Require {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
40 my ($this,$package) = @_;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
41
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
42 if ($this->verifyNames) {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
43 $package =~ m/^([a-zA-Z_0-9]+(?:::[a-zA-Z_0-9]+)*)$/
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
44 or die ArgumentException->new(package => 'Invalid package name') ;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
45 $package = $1;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
46 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
47
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
48 $package = $this->prefix . '::' . $package if $this->prefix;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
49
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
50 my $file = join('/', split(/::/,$package)) . ".pm";
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
51
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
52 require $file;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
53
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
54 return $package;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
55 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
56
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
57 sub ModuleExists {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
58 my ($this,$package) = @_;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
59
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
60 my $file = join('/', split(/::/,$this->GetFullName($package))) . ".pm";
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
61
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
62 -f File::Spec->catfile($_,$file) and return 1 foreach @INC;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
63
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
64 return 0;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
65 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
66
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
67 sub GetFullName {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
68 my ($this,$package) = @_;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
69
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
70 if ($this->verifyNames) {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
71 $package =~ m/^([a-zA-Z_0-9]+(?:::[a-zA-Z_0-9]+)*)$/
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
72 or die ArgumentException->new(package => 'Invalid package name') ;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
73 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
74
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
75 return $this->prefix . '::' . $package if $this->prefix;
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 1;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
79