Make ensure_class_loaded behave better with malformed classnames
[dbsrgits/DBIx-Class.git] / lib / DBIx / Class / Componentised.pm
1 package # hide from PAUSE
2     DBIx::Class::Componentised;
3
4 use strict;
5 use warnings;
6
7 use Class::C3;
8 use Class::Inspector;
9 use Carp::Clan qw/^DBIx::Class/;
10
11 sub inject_base {
12   my ($class, $target, @to_inject) = @_;
13   {
14     no strict 'refs';
15     foreach my $to (reverse @to_inject) {
16       my @comps = qw(DigestColumns ResultSetManager Ordered UTF8Columns);
17            # Add components here that need to be loaded before Core
18       foreach my $first_comp (@comps) {
19         if ($to eq 'DBIx::Class::Core' &&
20             $target->isa("DBIx::Class::${first_comp}")) {
21           warn "Possible incorrect order of components in ".
22                "${target}::load_components($first_comp) call: Core loaded ".
23                "before $first_comp. See the documentation for ".
24                "DBIx::Class::$first_comp for more information";
25         }
26       }
27       unshift( @{"${target}::ISA"}, $to )
28         unless ($target eq $to || $target->isa($to));
29     }
30   }
31
32   # Yes, this is hack. But it *does* work. Please don't submit tickets about
33   # it on the basis of the comments in Class::C3, the author was on #dbix-class
34   # while I was implementing this.
35
36   eval "package $target; import Class::C3;" unless exists $Class::C3::MRO{$target};
37 }
38
39 sub load_components {
40   my $class = shift;
41   my $base = $class->component_base_class;
42   my @comp = map { /^\+(.*)$/ ? $1 : "${base}::$_" } grep { $_ !~ /^#/ } @_;
43   $class->_load_components(@comp);
44   Class::C3::reinitialize();
45 }
46
47 sub load_own_components {
48   my $class = shift;
49   my @comp = map { "${class}::$_" } grep { $_ !~ /^#/ } @_;
50   $class->_load_components(@comp);
51 }
52
53 sub _load_components {
54   my ($class, @comp) = @_;
55   foreach my $comp (@comp) {
56     $class->ensure_class_loaded($comp);
57   }
58   $class->inject_base($class => @comp);
59 }
60
61 # Given a class name, tests to see if it is already loaded or otherwise
62 # defined. If it is not yet loaded, the package is require'd, and an exception
63 # is thrown if the class is still not loaded.
64 #
65 # TODO: handle ->has_many('rel', 'Class'...) instead of
66 #              ->has_many('rel', 'Some::Schema::Class'...)
67 #
68 # BUG: For some reason, packages with syntax errors are added to %INC on
69 #      require
70 sub ensure_class_loaded {
71   my ($class, $f_class) = @_;
72
73   croak "Invalid class name $f_class"
74       if ($f_class=~m/(?:\b:\b|\:{3,})/);
75   return if Class::Inspector->loaded($f_class);
76   eval "require $f_class"; # require needs a bareword or filename
77   if ($@) {
78     if ($class->can('throw_exception')) {
79       $class->throw_exception($@);
80     } else {
81       croak $@;
82     }
83   }
84 }
85
86 # Returns true if the specified class is installed or already loaded, false
87 # otherwise
88 sub ensure_class_found {
89   my ($class, $f_class) = @_;
90   return Class::Inspector->loaded($f_class) ||
91          Class::Inspector->installed($f_class);
92 }
93
94 # Returns a true value if the specified class is installed and loaded
95 # successfully, throws an exception if the class is found but not loaded
96 # successfully, and false if the class is not installed
97 sub load_optional_class {
98   my ($class, $f_class) = @_;
99   if ($class->ensure_class_found($f_class)) {
100     $class->ensure_class_loaded($f_class);
101     return 1;
102   } else {
103     return 0;
104   }
105 }
106
107 1;