| 1 | package Catalyst::Component; | ||||||||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||||||||
| 3 | 170 | ▶ | use Moose; | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 4 | 170 | ▶ | use Class::MOP; | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 5 | 170 | ▶ | use Class::MOP::Object; | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 6 | 170 | ▶ | use Catalyst::Utils; | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 7 | 170 | ▶ | use Class::C3::Adopt::NEXT; | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 8 | 170 | ▶ | use Devel::InnerPackage (); | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 9 | 170 | ▶ | use MRO::Compat; | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 10 | 170 | ▶ | use mro 'c3'; | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 11 | 170 | ▶ | use Scalar::Util 'blessed'; | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 12 | 170 | ▶ | use Class::Load 'is_class_loaded'; | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 13 | 170 | ▶ | use Moose::Util 'find_meta'; | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 14 | 170 | ▶ | use namespace::clean -except => 'meta'; | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||||||||
| 16 | 170 | with 'MooseX::Emulate::Class::Accessor::Fast'; | |||||||||||||||||||||||||||||||||
| 17 | 170 | with 'Catalyst::ClassData'; | |||||||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||||||||
| 20 | =head1 NAME | ||||||||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||||||||
| 22 | Catalyst::Component - Catalyst Component Base Class | ||||||||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||||||||
| 24 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||||||||
| 26 | # lib/MyApp/Model/Something.pm | ||||||||||||||||||||||||||||||||||
| 27 | package MyApp::Model::Something; | ||||||||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||||||||
| 29 | use base 'Catalyst::Component'; | ||||||||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||||||||
| 31 | __PACKAGE__->config( foo => 'bar' ); | ||||||||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||||||||
| 33 | has foo => ( | ||||||||||||||||||||||||||||||||||
| 34 | is => 'ro', | ||||||||||||||||||||||||||||||||||
| 35 | ); | ||||||||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||||||||
| 37 | sub test { | ||||||||||||||||||||||||||||||||||
| 38 | my $self = shift; | ||||||||||||||||||||||||||||||||||
| 39 | return $self->foo; | ||||||||||||||||||||||||||||||||||
| 40 | } | ||||||||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||||||||
| 42 | sub forward_to_me { | ||||||||||||||||||||||||||||||||||
| 43 | my ( $self, $c ) = @_; | ||||||||||||||||||||||||||||||||||
| 44 | $c->response->output( $self->foo ); | ||||||||||||||||||||||||||||||||||
| 45 | } | ||||||||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||||||||
| 47 | 1; | ||||||||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||||||||
| 49 | # Methods can be a request step | ||||||||||||||||||||||||||||||||||
| 50 | $c->forward(qw/MyApp::Model::Something forward_to_me/); | ||||||||||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||||||||
| 52 | # Or just methods | ||||||||||||||||||||||||||||||||||
| 53 | print $c->comp('MyApp::Model::Something')->test; | ||||||||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||||||||
| 55 | print $c->comp('MyApp::Model::Something')->foo; | ||||||||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||||||||
| 57 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||||||||
| 59 | This is the universal base class for Catalyst components | ||||||||||||||||||||||||||||||||||
| 60 | (Model/View/Controller). | ||||||||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||||||||
| 62 | It provides you with a generic new() for component construction through Catalyst's | ||||||||||||||||||||||||||||||||||
| 63 | component loader with config() support and a process() method placeholder. | ||||||||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||||||||
| 65 | B<Note> that calling C<< $self->config >> inside a component is strongly | ||||||||||||||||||||||||||||||||||
| 66 | not recommended - the correctly merged config should have already been | ||||||||||||||||||||||||||||||||||
| 67 | passed to the constructor and stored in attributes - accessing | ||||||||||||||||||||||||||||||||||
| 68 | the config accessor directly from an instance is likely to get the | ||||||||||||||||||||||||||||||||||
| 69 | wrong values (as it only holds the class wide config, not things loaded | ||||||||||||||||||||||||||||||||||
| 70 | from the config file!) | ||||||||||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||||||||
| 72 | =cut | ||||||||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||||||||
| 74 | 170 | __PACKAGE__->mk_classdata('_plugins'); | |||||||||||||||||||||||||||||||||
| 75 | 170 | __PACKAGE__->mk_classdata('_config'); | |||||||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||||||||
| 77 | 170 | has catalyst_component_name => ( is => 'ro' ); # Cannot be required => 1 as context | |||||||||||||||||||||||||||||||||
| 78 | # class @ISA component - HATE | ||||||||||||||||||||||||||||||||||
| 79 | # Make accessor callable as a class method, as we need to call setup_actions | ||||||||||||||||||||||||||||||||||
| 80 | # on the application class, which we don't have an instance of, ewwwww | ||||||||||||||||||||||||||||||||||
| 81 | # Also, naughty modules like Catalyst::View::JSON try to write to _everything_, | ||||||||||||||||||||||||||||||||||
| 82 | # so spit a warning, ignore that (and try to do the right thing anyway) here.. | ||||||||||||||||||||||||||||||||||
| 83 | 12243 | ▶ | around catalyst_component_name => sub { | ||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=4 SCAR=14.2
| |||||||||||||||||||||||||||||||||||
| 84 | 12243 | my ($orig, $self) = (shift, shift); | |||||||||||||||||||||||||||||||||
| 85 | 12243 | ▶ | Carp::cluck("Tried to write to the catalyst_component_name accessor - is your component broken or just mad? (Write ignored - using default value.)") if scalar @_; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 86 | 12243 | ▶ | blessed($self) ? $self->$orig() || blessed($self) : $self; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$self->$orig or builtin::blessed($self)
MC/DC0%0/2
$self->$orig or builtin::blessed($self)
$self->$orig
builtin::blessed($self)
Truth table66%2/3
A or B
A= $self->$origB= builtin::blessed($self)
| |||||||||||||||||||||||||||||||||||
| 87 | 170 | }; | |||||||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||||||||
| 89 | 7192 | ▶ | sub BUILDARGS { | ||||||||||||||||||||||||||||||||
|
Subroutine
BUILDARGS: called CC=10 SCAR=23.5
Pod
documented
| |||||||||||||||||||||||||||||||||||
| 90 | 7192 | my $class = shift; | |||||||||||||||||||||||||||||||||
| 91 | 7192 | my $args = {}; | |||||||||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||||||||
| 93 | 7192 | ▶ | if (@_ == 1) { | ||||||||||||||||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||||||||||||||||
| 94 | 314 | ▶ | $args = $_[0] if ref($_[0]) eq 'HASH'; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 95 | } elsif (@_ == 2) { # is it ($app, $args) or foo => 'bar' ? | ||||||||||||||||||||||||||||||||||
| 96 | 6871 | ▶ | if (blessed($_[0])) { | ||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||
| 97 | 1 | ▶ | $args = $_[1] if ref($_[1]) eq 'HASH'; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 98 | ▶ | } elsif (is_class_loaded($_[0]) && | |||||||||||||||||||||||||||||||||
|
Condition66%4/6
is_class_loaded($_[0]) and $_[0]->isa("Catalyst") and ref $_[1] eq "HASH"
is_class_loaded($_[0]) and $_[0]->isa("Catalyst")
MC/DC33%1/3
is_class_loaded($_[0]) and $_[0]->isa("Catalyst") and ref $_[1] eq "HASH"
is_class_loaded($_[0])
$_[0]->isa("Catalyst")
ref $_[1] eq "HASH"
Truth table50%2/4
A and B and C
A= is_class_loaded($_[0])B= $_[0]->isa("Catalyst")C= ref $_[1] eq "HASH"
| |||||||||||||||||||||||||||||||||||
| 99 | $_[0]->isa('Catalyst') && ref($_[1]) eq 'HASH') { | ||||||||||||||||||||||||||||||||||
| 100 | 6869 | $args = $_[1]; | |||||||||||||||||||||||||||||||||
| 101 | } else { | ||||||||||||||||||||||||||||||||||
| 102 | 1 | $args = +{ @_ }; | |||||||||||||||||||||||||||||||||
| 103 | } | ||||||||||||||||||||||||||||||||||
| 104 | } elsif (@_ % 2 == 0) { | ||||||||||||||||||||||||||||||||||
| 105 | 7 | $args = +{ @_ }; | |||||||||||||||||||||||||||||||||
| 106 | } | ||||||||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||||||||
| 108 | 7192 | return $class->merge_config_hashes( $class->config, $args ); | |||||||||||||||||||||||||||||||||
| 109 | } | ||||||||||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||||||||
| 111 | 6792 | ▶ | sub COMPONENT { | ||||||||||||||||||||||||||||||||
|
Subroutine
COMPONENT: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||||||||
| 112 | 6792 | my ( $class, $c ) = @_; | |||||||||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||||||||
| 114 | # Temporary fix, some components does not pass context to constructor | ||||||||||||||||||||||||||||||||||
| 115 | 6792 | ▶ | my $arguments = ( ref( $_[-1] ) eq 'HASH' ) ? $_[-1] : {}; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 116 | 6792 | ▶ | if ( my $next = $class->next::can ) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 117 | 1 | my ($next_package) = Class::MOP::get_code_info($next); | |||||||||||||||||||||||||||||||||
| 118 | 1 | warn "There is a COMPONENT method resolving after Catalyst::Component in ${next_package}.\n"; | |||||||||||||||||||||||||||||||||
| 119 | 1 | warn "This behavior can no longer be supported, and so your application is probably broken.\n"; | |||||||||||||||||||||||||||||||||
| 120 | 1 | warn "Your linearized isa hierarchy is: " . join(', ', @{ mro::get_linear_isa($class) }) . "\n"; | |||||||||||||||||||||||||||||||||
| 121 | 1 | warn "Please see perldoc Catalyst::Upgrading for more information about this issue.\n"; | |||||||||||||||||||||||||||||||||
| 122 | } | ||||||||||||||||||||||||||||||||||
| 123 | 6792 | return $class->new($c, $arguments); | |||||||||||||||||||||||||||||||||
| 124 | } | ||||||||||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||||||||||
| 126 | 35232 | ▶ | sub config { | ||||||||||||||||||||||||||||||||
|
Subroutine
config: called CC=6 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||||||||
| 127 | 35232 | my $self = shift; | |||||||||||||||||||||||||||||||||
| 128 | # Uncomment once sane to do so | ||||||||||||||||||||||||||||||||||
| 129 | #Carp::cluck("config method called on instance") if ref $self; | ||||||||||||||||||||||||||||||||||
| 130 | 35232 | ▶ | my $config = $self->_config || {}; | ||||||||||||||||||||||||||||||||
|
Condition100%2/2
$self->_config || {}
MC/DC100%1/1
$self->_config || {}
$self->_config
Truth table100%2/2
A
A= $self->_config
| |||||||||||||||||||||||||||||||||||
| 131 | 35232 | ▶ | if (@_) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 132 | 933 | ▶ | my $newconfig = { %{@_ > 1 ? {@_} : $_[0]} }; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 133 | 933 | $self->_config( | |||||||||||||||||||||||||||||||||
| 134 | $self->merge_config_hashes( $config, $newconfig ) | ||||||||||||||||||||||||||||||||||
| 135 | ); | ||||||||||||||||||||||||||||||||||
| 136 | } else { | ||||||||||||||||||||||||||||||||||
| 137 | # this is a bit of a kludge, required to make | ||||||||||||||||||||||||||||||||||
| 138 | # __PACKAGE__->config->{foo} = 'bar'; | ||||||||||||||||||||||||||||||||||
| 139 | # work in a subclass. | ||||||||||||||||||||||||||||||||||
| 140 | # TODO maybe this should be a ClassData option? | ||||||||||||||||||||||||||||||||||
| 141 | 34299 | ▶ | my $class = blessed($self) || $self; | ||||||||||||||||||||||||||||||||
|
Condition66%2/3
builtin::blessed($self) || $self
MC/DC0%0/2
builtin::blessed($self) || $self
builtin::blessed($self)
$self
Truth table66%2/3
A || B
A= builtin::blessed($self)B= $self
| |||||||||||||||||||||||||||||||||||
| 142 | 34299 | my $meta = find_meta($class); | |||||||||||||||||||||||||||||||||
| 143 | 34299 | ▶ | unless (${ $meta->get_or_add_package_symbol('$_config') }) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 144 | # Call merge_hashes to ensure we deep copy the parent | ||||||||||||||||||||||||||||||||||
| 145 | # config onto the subclass | ||||||||||||||||||||||||||||||||||
| 146 | 6290 | $self->_config( Catalyst::Utils::merge_hashes($config, {}) ); | |||||||||||||||||||||||||||||||||
| 147 | } | ||||||||||||||||||||||||||||||||||
| 148 | } | ||||||||||||||||||||||||||||||||||
| 149 | 35232 | return $self->_config; | |||||||||||||||||||||||||||||||||
| 150 | } | ||||||||||||||||||||||||||||||||||
| 151 | |||||||||||||||||||||||||||||||||||
| 152 | 15862 | ▶ | sub merge_config_hashes { | ||||||||||||||||||||||||||||||||
|
Subroutine
merge_config_hashes: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||
| 153 | 15862 | my ( $self, $lefthash, $righthash ) = @_; | |||||||||||||||||||||||||||||||||
| 154 | |||||||||||||||||||||||||||||||||||
| 155 | 15862 | return Catalyst::Utils::merge_hashes( $lefthash, $righthash ); | |||||||||||||||||||||||||||||||||
| 156 | } | ||||||||||||||||||||||||||||||||||
| 157 | |||||||||||||||||||||||||||||||||||
| 158 | 0 | ▶ | sub process { | ||||||||||||||||||||||||||||||||
|
Subroutine
process: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||||||||
| 159 | |||||||||||||||||||||||||||||||||||
| 160 | 0 | ▶ | Catalyst::Exception->throw( message => ( ref $_[0] || $_[0] ) | ||||||||||||||||||||||||||||||||
|
Condition0%0/3
ref $_[0] || $_[0]
MC/DC0%0/2
ref $_[0] || $_[0]
ref $_[0]
$_[0]
Truth table0%0/3
A || B
A= ref $_[0]B= $_[0]
| |||||||||||||||||||||||||||||||||||
| 161 | . " did not override Catalyst::Component::process" ); | ||||||||||||||||||||||||||||||||||
| 162 | } | ||||||||||||||||||||||||||||||||||
| 163 | |||||||||||||||||||||||||||||||||||
| 164 | 6792 | ▶ | sub expand_modules { | ||||||||||||||||||||||||||||||||
|
Subroutine
expand_modules: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||
| 165 | 6792 | my ($class, $component) = @_; | |||||||||||||||||||||||||||||||||
| 166 | 6792 | return Devel::InnerPackage::list_packages( $component ); | |||||||||||||||||||||||||||||||||
| 167 | } | ||||||||||||||||||||||||||||||||||
| 168 | |||||||||||||||||||||||||||||||||||
| 169 | 170 | __PACKAGE__->meta->make_immutable; | |||||||||||||||||||||||||||||||||
| 170 | |||||||||||||||||||||||||||||||||||
| 171 | 170 | 1; | |||||||||||||||||||||||||||||||||
| 172 | |||||||||||||||||||||||||||||||||||
| 173 | __END__ | ||||||||||||||||||||||||||||||||||