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