| 1 | package Catalyst::Seal::Accessors; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 19 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 19 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||
| 6 | 19 | ▶ | use B (); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||
| 8 | 19 | ▶ | use Catalyst::Seal (); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | 19 | ▶ | use Catalyst::Seal::Guard (); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | 19 | our $VERSION = '0.04'; | |||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | 12 | ▶ | sub _seal_config { | ||||||||||||||||||||||||||
|
Subroutine
_seal_config: called CC=5 SCAR=18.0
| |||||||||||||||||||||||||||||
| 14 | 12 | my ($app) = @_; | |||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||
| 16 | 12 | ▶ | my $orig = $app->can('config') or return 0; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | 12 | my $value = eval { $app->config }; | |||||||||||||||||||||||||||
| 19 | 12 | ▶ | if ($@ || !defined $value) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$@ or not defined $value
MC/DC0%0/2
$@ or not defined $value
$@
not defined $value
Truth table33%1/3
A or not(B)
A= $@B= not defined $value
| |||||||||||||||||||||||||||||
| 20 | 0 | Catalyst::Seal::note("could not resolve $app->config, not sealed: $@"); | |||||||||||||||||||||||||||
| 21 | 0 | return 0; | |||||||||||||||||||||||||||
| 22 | } | ||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||
| 24 | 12 | my $ok = eval { | |||||||||||||||||||||||||||
| 25 | 19 | ▶ | no warnings 'redefine'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 26 | 12 | Catalyst::Seal::_install_const($app, 'config', $value, $orig, undef); | |||||||||||||||||||||||||||
| 27 | 12 | 1; | |||||||||||||||||||||||||||
| 28 | }; | ||||||||||||||||||||||||||||
| 29 | 12 | ▶ | unless ($ok) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 30 | 0 | Catalyst::Seal::note("could not seal $app->config: $@"); | |||||||||||||||||||||||||||
| 31 | 0 | return 0; | |||||||||||||||||||||||||||
| 32 | } | ||||||||||||||||||||||||||||
| 33 | 12 | return 1; | |||||||||||||||||||||||||||
| 34 | } | ||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||
| 36 | 19 | my %DELEGATOR = ( | |||||||||||||||||||||||||||
| 37 | req => 'request', | ||||||||||||||||||||||||||||
| 38 | res => 'response', | ||||||||||||||||||||||||||||
| 39 | comp => 'component', | ||||||||||||||||||||||||||||
| 40 | ); | ||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||
| 42 | 12 | ▶ | sub _seal_delegators { | ||||||||||||||||||||||||||
|
Subroutine
_seal_delegators: called CC=6 SCAR=18.4
| |||||||||||||||||||||||||||||
| 43 | 12 | my ($app) = @_; | |||||||||||||||||||||||||||
| 44 | 12 | my $sealed = 0; | |||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||
| 46 | 12 | for my $name (sort keys %DELEGATOR) { | |||||||||||||||||||||||||||
| 47 | 36 | my $target = $DELEGATOR{$name}; | |||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||
| 49 | 36 | ▶ | my $from = $app->can($name) or next; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 50 | 36 | ▶ | my $to = $app->can($target) or next; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||
| 52 | 36 | ▶ | next if $from == $to; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||
| 54 | 36 | my $ok = eval { | |||||||||||||||||||||||||||
| 55 | 19 | ▶ | no strict 'refs'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 56 | 19 | ▶ | no warnings 'redefine'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 57 | 36 | *{"${app}::${name}"} = $to; | |||||||||||||||||||||||||||
| 58 | 36 | 1; | |||||||||||||||||||||||||||
| 59 | }; | ||||||||||||||||||||||||||||
| 60 | 36 | ▶ | unless ($ok) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 61 | 0 | Catalyst::Seal::note("could not alias $app\::$name to $target: $@"); | |||||||||||||||||||||||||||
| 62 | 0 | next; | |||||||||||||||||||||||||||
| 63 | } | ||||||||||||||||||||||||||||
| 64 | 36 | $sealed++; | |||||||||||||||||||||||||||
| 65 | } | ||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||
| 67 | 12 | return $sealed; | |||||||||||||||||||||||||||
| 68 | } | ||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||
| 70 | 12 | ▶ | sub _reader_classes { | ||||||||||||||||||||||||||
|
Subroutine
_reader_classes: called CC=5 SCAR=18.2
| |||||||||||||||||||||||||||||
| 71 | 12 | my ($app) = @_; | |||||||||||||||||||||||||||
| 72 | 12 | ▶ | my @classes = ($app->context_class || $app); | ||||||||||||||||||||||||||
|
Condition33%1/3
$app->context_class || $app
MC/DC0%0/2
$app->context_class || $app
$app->context_class
$app
Truth table33%1/3
A || B
A= $app->context_classB= $app
| |||||||||||||||||||||||||||||
| 73 | 12 | ▶ | push @classes, eval { $app->composed_request_class } || (); | ||||||||||||||||||||||||||
|
Condition33%1/3
eval {
do {
$app->composed_request_class
}
} || ()
MC/DC0%0/2
eval {
do {
$app->composed_request_class
}
} || ()
eval {
do {
$app->composed_request_class
}
}
()
Truth table33%1/3
A || B
A= eval {
do {
$app->composed_request_class
}
}B= ()
| |||||||||||||||||||||||||||||
| 74 | 12 | ▶ | push @classes, eval { $app->composed_response_class } || (); | ||||||||||||||||||||||||||
|
Condition33%1/3
eval {
do {
$app->composed_response_class
}
} || ()
MC/DC0%0/2
eval {
do {
$app->composed_response_class
}
} || ()
eval {
do {
$app->composed_response_class
}
}
()
Truth table33%1/3
A || B
A= eval {
do {
$app->composed_response_class
}
}B= ()
| |||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||
| 76 | 12 | my %seen; | |||||||||||||||||||||||||||
| 77 | 12 | ▶ | return grep { !$seen{$_}++ } grep { defined && length } @classes; | ||||||||||||||||||||||||||
|
Condition33%1/3
defined $_ && length $_
MC/DC0%0/2
defined $_ && length $_
defined $_
length $_
Truth table33%1/3
A && B
A= defined $_B= length $_
| |||||||||||||||||||||||||||||
| 78 | } | ||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||
| 80 | 617 | ▶ | sub _plain_write { | ||||||||||||||||||||||||||
|
Subroutine
_plain_write: called CC=8 SCAR=20.9
| |||||||||||||||||||||||||||||
| 81 | 617 | my ($attr, $name) = @_; | |||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||
| 83 | 617 | my $write = $attr->get_write_method; | |||||||||||||||||||||||||||
| 84 | 617 | ▶ | return 0 unless defined $write && $write eq $name; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
defined $write and $write eq $name
MC/DC100%2/2
defined $write and $write eq $name
defined $write
$write eq $name
Truth table100%3/3
A and B
A= defined $writeB= $write eq $name
| |||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||
| 86 | 412 | ▶ | return 0 if $attr->has_type_constraint; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 87 | 388 | ▶ | return 0 if $attr->has_trigger; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 88 | 388 | ▶ | return 0 if $attr->should_coerce; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 89 | 388 | ▶ | return 0 if $attr->is_weak_ref; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 90 | 376 | ▶ | return 0 if $attr->has_initializer; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||
| 92 | 376 | return 1; | |||||||||||||||||||||||||||
| 93 | } | ||||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||
| 95 | 37 | ▶ | sub _seal_readers { | ||||||||||||||||||||||||||
|
Subroutine
_seal_readers: called CC=15 SCAR=29.2
| |||||||||||||||||||||||||||||
| 96 | 37 | my ($class) = @_; | |||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||
| 98 | 37 | ▶ | my $meta = Class::MOP::class_of($class) or return 0; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 99 | 37 | ▶ | return 0 unless $meta->isa('Class::MOP::Class'); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 100 | 37 | ▶ | unless ($meta->is_immutable) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 101 | 0 | Catalyst::Seal::note("$class is mutable, its readers were not sealed"); | |||||||||||||||||||||||||||
| 102 | 0 | return 0; | |||||||||||||||||||||||||||
| 103 | } | ||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||
| 105 | 37 | my $sealed = 0; | |||||||||||||||||||||||||||
| 106 | 37 | for my $attr ($meta->get_all_attributes) { | |||||||||||||||||||||||||||
| 107 | 725 | ▶ | my $name = eval { $attr->get_read_method } or next; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 108 | 701 | ▶ | my $ref = eval { $attr->get_read_method_ref } or next; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 109 | 701 | ▶ | next unless ref $ref && $ref->isa('Class::MOP::Method'); | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
ref $ref and $ref->isa("Class::MOP::Method")
MC/DC0%0/2
ref $ref and $ref->isa("Class::MOP::Method")
ref $ref
$ref->isa("Class::MOP::Method")
Truth table33%1/3
A and B
A= ref $refB= $ref->isa("Class::MOP::Method")
| |||||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||
| 111 | 701 | ▶ | next if $ref->isa('Class::MOP::Method::Wrapped'); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||
| 113 | 629 | ▶ | my $body = eval { $ref->body } or next; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 114 | 629 | ▶ | my $installed = $class->can($name) or next; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||
| 116 | 629 | ▶ | next unless $installed == $body; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||
| 118 | 617 | my $ok = eval { | |||||||||||||||||||||||||||
| 119 | 19 | ▶ | no warnings 'redefine'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 120 | 617 | ▶ | Catalyst::Seal::_install_reader( | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 121 | $class, $name, $attr->name, $body, ($attr->is_lazy ? 1 : 0), | ||||||||||||||||||||||||||||
| 122 | _plain_write($attr, $name)); | ||||||||||||||||||||||||||||
| 123 | 617 | 1; | |||||||||||||||||||||||||||
| 124 | }; | ||||||||||||||||||||||||||||
| 125 | 617 | ▶ | unless ($ok) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 126 | 0 | Catalyst::Seal::note("could not seal reader $class\::$name: $@"); | |||||||||||||||||||||||||||
| 127 | 0 | next; | |||||||||||||||||||||||||||
| 128 | } | ||||||||||||||||||||||||||||
| 129 | 617 | $sealed++; | |||||||||||||||||||||||||||
| 130 | } | ||||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||
| 132 | 37 | return $sealed; | |||||||||||||||||||||||||||
| 133 | } | ||||||||||||||||||||||||||||
| 134 | |||||||||||||||||||||||||||||
| 135 | 19 | my @GUARDED = ( | |||||||||||||||||||||||||||
| 136 | ['Catalyst::Response', 'headers'], | ||||||||||||||||||||||||||||
| 137 | ['Catalyst::Response', 'status'], | ||||||||||||||||||||||||||||
| 138 | ); | ||||||||||||||||||||||||||||
| 139 | |||||||||||||||||||||||||||||
| 140 | 12 | ▶ | sub _seal_guarded_readers { | ||||||||||||||||||||||||||
|
Subroutine
_seal_guarded_readers: called CC=9 SCAR=23.1
| |||||||||||||||||||||||||||||
| 141 | 12 | my $sealed = 0; | |||||||||||||||||||||||||||
| 142 | |||||||||||||||||||||||||||||
| 143 | 12 | for my $pair (@GUARDED) { | |||||||||||||||||||||||||||
| 144 | 24 | my ($class, $name) = @$pair; | |||||||||||||||||||||||||||
| 145 | |||||||||||||||||||||||||||||
| 146 | 24 | ▶ | my $meta = Class::MOP::class_of($class) or next; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 147 | 24 | ▶ | next unless $meta->is_immutable; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 148 | |||||||||||||||||||||||||||||
| 149 | 24 | ▶ | my $attr = $meta->find_attribute_by_name($name) or next; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 150 | 24 | ▶ | my $installed = $class->can($name) or next; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 151 | |||||||||||||||||||||||||||||
| 152 | 24 | my $gv = B::svref_2object($installed)->GV; | |||||||||||||||||||||||||||
| 153 | 24 | ▶ | next unless $gv->NAME =~ /\A_wrapped_/; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 154 | |||||||||||||||||||||||||||||
| 155 | 24 | my $ok = eval { | |||||||||||||||||||||||||||
| 156 | 19 | ▶ | no warnings 'redefine'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 157 | 24 | ▶ | Catalyst::Seal::_install_reader( | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 158 | $class, $name, $attr->name, $installed, | ||||||||||||||||||||||||||||
| 159 | ($attr->is_lazy ? 1 : 0)); | ||||||||||||||||||||||||||||
| 160 | 24 | 1; | |||||||||||||||||||||||||||
| 161 | }; | ||||||||||||||||||||||||||||
| 162 | 24 | ▶ | unless ($ok) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 163 | 0 | Catalyst::Seal::note("could not seal guarded reader $class\::$name: $@"); | |||||||||||||||||||||||||||
| 164 | 0 | next; | |||||||||||||||||||||||||||
| 165 | } | ||||||||||||||||||||||||||||
| 166 | 24 | $sealed++; | |||||||||||||||||||||||||||
| 167 | } | ||||||||||||||||||||||||||||
| 168 | |||||||||||||||||||||||||||||
| 169 | 12 | return $sealed; | |||||||||||||||||||||||||||
| 170 | } | ||||||||||||||||||||||||||||
| 171 | |||||||||||||||||||||||||||||
| 172 | 12 | ▶ | Catalyst::Seal::register_step('accessors' => sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||
| 173 | 12 | my ($app) = @_; | |||||||||||||||||||||||||||
| 174 | |||||||||||||||||||||||||||||
| 175 | 12 | require Class::MOP; | |||||||||||||||||||||||||||
| 176 | |||||||||||||||||||||||||||||
| 177 | 12 | my $config = _seal_config($app); | |||||||||||||||||||||||||||
| 178 | 12 | my $readers = 0; | |||||||||||||||||||||||||||
| 179 | 12 | $readers += _seal_readers($_) for _reader_classes($app); | |||||||||||||||||||||||||||
| 180 | |||||||||||||||||||||||||||||
| 181 | 12 | $readers += _seal_guarded_readers(); | |||||||||||||||||||||||||||
| 182 | |||||||||||||||||||||||||||||
| 183 | 12 | my $delegators = _seal_delegators($app); | |||||||||||||||||||||||||||
| 184 | |||||||||||||||||||||||||||||
| 185 | 12 | ▶ | Catalyst::Seal::note( | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 186 | "sealed config ($config), $readers reader(s), $delegators delegator(s)") | ||||||||||||||||||||||||||||
| 187 | if $Catalyst::Seal::DEBUG; | ||||||||||||||||||||||||||||
| 188 | 12 | return; | |||||||||||||||||||||||||||
| 189 | 19 | }); | |||||||||||||||||||||||||||
| 190 | |||||||||||||||||||||||||||||
| 191 | 19 | 1; | |||||||||||||||||||||||||||
| 192 | |||||||||||||||||||||||||||||
| 193 | __END__ | ||||||||||||||||||||||||||||