/[Frey]/trunk/lib/Frey/ClassLoader.pm
This is repository of my old source code which isn't updated any more. Go to git.rot13.org for current projects!
ViewVC logotype

Contents of /trunk/lib/Frey/ClassLoader.pm

Parent Directory Parent Directory | Revision Log Revision Log


Revision 125 - (show annotations)
Mon Jul 14 23:12:07 2008 UTC (15 years, 9 months ago) by dpavlin
File size: 2352 byte(s)
try to load all available classes if needed
1 package Frey::ClassLoader;
2 use Moose;
3
4 extends 'Frey';
5
6 use Data::Dump qw/dump/;
7 use File::Find;
8
9 our $package_path;
10 our @classes;
11
12 sub classes {
13 my $self = shift;
14 return @classes if @classes;
15
16 # FIXME there must be better way to do this in Moose style
17 finddepth({ no_chdir => 1, wanted => sub {
18 return unless s/\.pm$//;
19 my @a = split(m!/!,$_);
20 my $package = join('::', @a[ 1 .. $#a ]);
21 warn ">> $_ ",dump( @a ), " >> $package\n" if $self->debug;
22 $package_path->{ $package } = "$_.pm";
23 } }, 'lib');
24 warn "## package_path = ",dump( $package_path ) if $self->debug;
25
26 @classes = sort keys %$package_path;
27 }
28
29 sub package_path {
30 my ( $self, $package ) = @_;
31 $self->classes unless $package_path;
32 confess "can't find path for package $package" unless defined $package_path->{$package};
33 return $package_path->{$package};
34 }
35
36 =head2 load_package
37
38 my ( $class, $meta, $is_role ) = $o->load_package( 'Some::Package' );
39
40 =cut
41
42 sub load_package {
43 my ( $self, $package ) = @_;
44
45 #intercept role application so we can accurately generate
46 #method and attribute information for the parent class.
47 #this is fragile, but there is not better way that i am aware of
48 my $rmeta = Moose::Meta::Role->meta;
49 $rmeta->make_mutable if $rmeta->is_immutable;
50 my $original_apply = $rmeta->get_method("apply")->body;
51 $rmeta->remove_method("apply");
52 my @roles_to_apply;
53 $rmeta->add_method("apply", sub{push(@roles_to_apply, [@_])});
54 #load the package with the hacked Moose::Meta::Role
55
56 #eval { Class::MOP::load_class($package); };
57 #confess "Failed to load package ${package} $@" if $@;
58 Class::MOP::load_class($package);
59
60 if ( ! $package->can('meta') ) {
61 my $class = Moose::Meta::Class->create_anon_class;
62 warn "package $package doesn't have meta faking anon class";
63 return ( $class, $class->meta, 0 );
64 }
65
66 my $meta = $package->meta;
67
68 my ($class, $is_role);
69 if($package->meta->isa('Moose::Meta::Role')){
70 $is_role = 1;
71 # we need to apply the role to a class to be able to properly introspect it
72 $class = Moose::Meta::Class->create_anon_class;
73 $original_apply->($meta, $class);
74 } else {
75 #roles don't have superclasses ...
76 $class = $meta;
77 }
78 return ( $class, $meta, $is_role );
79 }
80
81 sub load_all_classes {
82 my $self = shift;
83 my $loaded = 0;
84 foreach ( $self->classes ) {
85 Class::MOP::load_class($_);
86 $loaded++;
87 }
88 $loaded;
89 }
90
91 1;

  ViewVC Help
Powered by ViewVC 1.1.26