Filename | /opt/perl-5.18.1/lib/site_perl/5.18.1/darwin-thread-multi-2level/Moose/Util/MetaRole.pm |
Statements | Executed 20 statements in 1.01ms |
Calls | P | F | Exclusive Time |
Inclusive Time |
Subroutine |
---|---|---|---|---|---|
1 | 1 | 1 | 16µs | 93µs | BEGIN@15 | Moose::Util::MetaRole::
1 | 1 | 1 | 11µs | 11µs | BEGIN@2 | Moose::Util::MetaRole::
1 | 1 | 1 | 10µs | 17µs | BEGIN@16 | Moose::Util::MetaRole::
1 | 1 | 1 | 9µs | 14µs | BEGIN@10 | Moose::Util::MetaRole::
1 | 1 | 1 | 9µs | 34µs | BEGIN@17 | Moose::Util::MetaRole::
1 | 1 | 1 | 9µs | 34µs | BEGIN@13 | Moose::Util::MetaRole::
1 | 1 | 1 | 9µs | 33µs | BEGIN@14 | Moose::Util::MetaRole::
1 | 1 | 1 | 8µs | 37µs | BEGIN@11 | Moose::Util::MetaRole::
1 | 1 | 1 | 8µs | 27µs | BEGIN@9 | Moose::Util::MetaRole::
0 | 0 | 0 | 0s | 0s | __ANON__[:131] | Moose::Util::MetaRole::
0 | 0 | 0 | 0s | 0s | __ANON__[:87] | Moose::Util::MetaRole::
0 | 0 | 0 | 0s | 0s | _make_new_class | Moose::Util::MetaRole::
0 | 0 | 0 | 0s | 0s | _make_new_metaclass | Moose::Util::MetaRole::
0 | 0 | 0 | 0s | 0s | _metathing_for | Moose::Util::MetaRole::
0 | 0 | 0 | 0s | 0s | apply_base_class_roles | Moose::Util::MetaRole::
0 | 0 | 0 | 0s | 0s | apply_metaroles | Moose::Util::MetaRole::
Line | State ments |
Time on line |
Calls | Time in subs |
Code |
---|---|---|---|---|---|
1 | package Moose::Util::MetaRole; | ||||
2 | # spent 11µs within Moose::Util::MetaRole::BEGIN@2 which was called:
# once (11µs+0s) by Moose::Exporter::BEGIN@15 at line 4 | ||||
3 | 1 | 6µs | $Moose::Util::MetaRole::AUTHORITY = 'cpan:STEVAN'; | ||
4 | 1 | 40µs | 1 | 11µs | } # spent 11µs making 1 call to Moose::Util::MetaRole::BEGIN@2 |
5 | { | ||||
6 | 2 | 1µs | $Moose::Util::MetaRole::VERSION = '2.1005'; | ||
7 | } | ||||
8 | |||||
9 | 2 | 30µs | 2 | 46µs | # spent 27µs (8+19) within Moose::Util::MetaRole::BEGIN@9 which was called:
# once (8µs+19µs) by Moose::Exporter::BEGIN@15 at line 9 # spent 27µs making 1 call to Moose::Util::MetaRole::BEGIN@9
# spent 19µs making 1 call to strict::import |
10 | 2 | 30µs | 2 | 19µs | # spent 14µs (9+5) within Moose::Util::MetaRole::BEGIN@10 which was called:
# once (9µs+5µs) by Moose::Exporter::BEGIN@15 at line 10 # spent 14µs making 1 call to Moose::Util::MetaRole::BEGIN@10
# spent 5µs making 1 call to warnings::import |
11 | 2 | 33µs | 2 | 65µs | # spent 37µs (8+28) within Moose::Util::MetaRole::BEGIN@11 which was called:
# once (8µs+28µs) by Moose::Exporter::BEGIN@15 at line 11 # spent 37µs making 1 call to Moose::Util::MetaRole::BEGIN@11
# spent 28µs making 1 call to Exporter::import |
12 | |||||
13 | 2 | 33µs | 2 | 59µs | # spent 34µs (9+25) within Moose::Util::MetaRole::BEGIN@13 which was called:
# once (9µs+25µs) by Moose::Exporter::BEGIN@15 at line 13 # spent 34µs making 1 call to Moose::Util::MetaRole::BEGIN@13
# spent 25µs making 1 call to Exporter::import |
14 | 2 | 31µs | 2 | 58µs | # spent 33µs (9+24) within Moose::Util::MetaRole::BEGIN@14 which was called:
# once (9µs+24µs) by Moose::Exporter::BEGIN@15 at line 14 # spent 33µs making 1 call to Moose::Util::MetaRole::BEGIN@14
# spent 24µs making 1 call to Exporter::import |
15 | 2 | 34µs | 2 | 105µs | # spent 93µs (16+78) within Moose::Util::MetaRole::BEGIN@15 which was called:
# once (16µs+78µs) by Moose::Exporter::BEGIN@15 at line 15 # spent 93µs making 1 call to Moose::Util::MetaRole::BEGIN@15
# spent 11µs making 1 call to List::Util::import |
16 | 2 | 32µs | 2 | 24µs | # spent 17µs (10+7) within Moose::Util::MetaRole::BEGIN@16 which was called:
# once (10µs+7µs) by Moose::Exporter::BEGIN@15 at line 16 # spent 17µs making 1 call to Moose::Util::MetaRole::BEGIN@16
# spent 7µs making 1 call to Package::DeprecationManager::__ANON__[Package/DeprecationManager.pm:61] |
17 | 2 | 731µs | 2 | 59µs | # spent 34µs (9+25) within Moose::Util::MetaRole::BEGIN@17 which was called:
# once (9µs+25µs) by Moose::Exporter::BEGIN@15 at line 17 # spent 34µs making 1 call to Moose::Util::MetaRole::BEGIN@17
# spent 25µs making 1 call to Exporter::import |
18 | |||||
19 | sub apply_metaroles { | ||||
20 | my %args = @_; | ||||
21 | |||||
22 | my $for = _metathing_for( $args{for} ); | ||||
23 | |||||
24 | if ( $for->isa('Moose::Meta::Role') ) { | ||||
25 | return _make_new_metaclass( $for, $args{role_metaroles}, 'role' ); | ||||
26 | } | ||||
27 | else { | ||||
28 | return _make_new_metaclass( $for, $args{class_metaroles}, 'class' ); | ||||
29 | } | ||||
30 | } | ||||
31 | |||||
32 | sub _metathing_for { | ||||
33 | my $passed = shift; | ||||
34 | |||||
35 | my $found | ||||
36 | = blessed $passed | ||||
37 | ? $passed | ||||
38 | : Class::MOP::class_of($passed); | ||||
39 | |||||
40 | return $found | ||||
41 | if defined $found | ||||
42 | && blessed $found | ||||
43 | && ( $found->isa('Moose::Meta::Role') | ||||
44 | || $found->isa('Moose::Meta::Class') ); | ||||
45 | |||||
46 | local $Carp::CarpLevel = $Carp::CarpLevel + 1; | ||||
47 | |||||
48 | my $error_start | ||||
49 | = 'When using Moose::Util::MetaRole, you must pass a Moose class name,' | ||||
50 | . ' role name, metaclass object, or metarole object.'; | ||||
51 | |||||
52 | if ( defined $found && blessed $found ) { | ||||
53 | croak $error_start | ||||
54 | . " You passed $passed, and we resolved this to a " | ||||
55 | . ( blessed $found ) | ||||
56 | . ' object.'; | ||||
57 | } | ||||
58 | |||||
59 | if ( defined $passed && !defined $found ) { | ||||
60 | croak $error_start | ||||
61 | . " You passed $passed, and this did not resolve to a metaclass or metarole." | ||||
62 | . ' Maybe you need to call Moose->init_meta to initialize the metaclass first?'; | ||||
63 | } | ||||
64 | |||||
65 | if ( !defined $passed ) { | ||||
66 | croak $error_start | ||||
67 | . " You passed an undef." | ||||
68 | . ' Maybe you need to call Moose->init_meta to initialize the metaclass first?'; | ||||
69 | } | ||||
70 | } | ||||
71 | |||||
72 | sub _make_new_metaclass { | ||||
73 | my $for = shift; | ||||
74 | my $roles = shift; | ||||
75 | my $primary = shift; | ||||
76 | |||||
77 | return $for unless keys %{$roles}; | ||||
78 | |||||
79 | my $new_metaclass | ||||
80 | = exists $roles->{$primary} | ||||
81 | ? _make_new_class( ref $for, $roles->{$primary} ) | ||||
82 | : blessed $for; | ||||
83 | |||||
84 | my %classes; | ||||
85 | |||||
86 | for my $key ( grep { $_ ne $primary } keys %{$roles} ) { | ||||
87 | my $attr = first {$_} | ||||
88 | map { $for->meta->find_attribute_by_name($_) } ( | ||||
89 | $key . '_metaclass', | ||||
90 | $key . '_class' | ||||
91 | ); | ||||
92 | |||||
93 | my $reader = $attr->get_read_method; | ||||
94 | |||||
95 | $classes{ $attr->init_arg } | ||||
96 | = _make_new_class( $for->$reader(), $roles->{$key} ); | ||||
97 | } | ||||
98 | |||||
99 | my $new_meta = $new_metaclass->reinitialize( $for, %classes ); | ||||
100 | |||||
101 | return $new_meta; | ||||
102 | } | ||||
103 | |||||
104 | sub apply_base_class_roles { | ||||
105 | my %args = @_; | ||||
106 | |||||
107 | my $meta = _metathing_for( $args{for} || $args{for_class} ); | ||||
108 | croak 'You can only apply base class roles to a Moose class, not a role.' | ||||
109 | if $meta->isa('Moose::Meta::Role'); | ||||
110 | |||||
111 | my $new_base = _make_new_class( | ||||
112 | $meta->name, | ||||
113 | $args{roles}, | ||||
114 | [ $meta->superclasses() ], | ||||
115 | ); | ||||
116 | |||||
117 | $meta->superclasses($new_base) | ||||
118 | if $new_base ne $meta->name(); | ||||
119 | } | ||||
120 | |||||
121 | sub _make_new_class { | ||||
122 | my $existing_class = shift; | ||||
123 | my $roles = shift; | ||||
124 | my $superclasses = shift || [$existing_class]; | ||||
125 | |||||
126 | return $existing_class unless $roles; | ||||
127 | |||||
128 | my $meta = Class::MOP::Class->initialize($existing_class); | ||||
129 | |||||
130 | return $existing_class | ||||
131 | if $meta->can('does_role') && all { $meta->does_role($_) } | ||||
132 | grep { !ref $_ } @{$roles}; | ||||
133 | |||||
134 | return Moose::Meta::Class->create_anon_class( | ||||
135 | superclasses => $superclasses, | ||||
136 | roles => $roles, | ||||
137 | cache => 1, | ||||
138 | )->name(); | ||||
139 | } | ||||
140 | |||||
141 | 1 | 3µs | 1; | ||
142 | |||||
143 | # ABSTRACT: Apply roles to any metaclass, as well as the object base class | ||||
144 | |||||
145 | __END__ |