| 1 | package CatalystX::Imports; | ||||||||||||||
| 2 | |||||||||||||||
| 3 | =head1 NAME | ||||||||||||||
| 4 | |||||||||||||||
| 5 | CatalystX::Imports - Shortcut functions for L<Catalyst> controllers | ||||||||||||||
| 6 | |||||||||||||||
| 7 | =cut | ||||||||||||||
| 8 | |||||||||||||||
| 9 | 6 | ▶ | use warnings; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 10 | 6 | ▶ | use strict; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 11 | |||||||||||||||
| 12 | 6 | ▶ | use vars qw( | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 13 | $VERSION | ||||||||||||||
| 14 | $STORE_CONTROLLER $STORE_CONTEXT $STORE_ARGUMENTS | ||||||||||||||
| 15 | $ACTION_WRAPPER_VAR | ||||||||||||||
| 16 | 6 | ); | |||||||||||||
| 17 | |||||||||||||||
| 18 | 6 | ▶ | use Class::MOP; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 19 | 6 | ▶ | use Carp::Clan qw{ ^CatalystX::Imports(?:::|$) }; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 20 | 6 | ▶ | use Filter::EOF; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 21 | 6 | ▶ | use Sub::Name 'subname'; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 22 | |||||||||||||||
| 23 | =head1 VERSION | ||||||||||||||
| 24 | |||||||||||||||
| 25 | 0.04 | ||||||||||||||
| 26 | |||||||||||||||
| 27 | =cut | ||||||||||||||
| 28 | |||||||||||||||
| 29 | 6 | $VERSION = '0.05'; | |||||||||||||
| 30 | 6 | $VERSION = eval $VERSION; | |||||||||||||
| 31 | |||||||||||||||
| 32 | =head1 SYNOPSIS | ||||||||||||||
| 33 | |||||||||||||||
| 34 | package MyApp::Controller::User; | ||||||||||||||
| 35 | use base 'Catalyst::Controller'; | ||||||||||||||
| 36 | |||||||||||||||
| 37 | use CatalystX::Imports | ||||||||||||||
| 38 | Context => { Default => [qw( :all )], | ||||||||||||||
| 39 | Config => [{model => 'model_name'}, 'template'] }, | ||||||||||||||
| 40 | Vars => { Stash => [qw( $user $user_rs $template )], | ||||||||||||||
| 41 | Session => [qw( @shown_users )], | ||||||||||||||
| 42 | Flash => [qw( $message )] }; | ||||||||||||||
| 43 | |||||||||||||||
| 44 | sub list: Chained { | ||||||||||||||
| 45 | $user_rs = model(model_name)->search_rs; | ||||||||||||||
| 46 | } | ||||||||||||||
| 47 | |||||||||||||||
| 48 | sub load: Chained PathPart('') CaptureArgs(1) { | ||||||||||||||
| 49 | $user = model(model_name)->find($args[0]); | ||||||||||||||
| 50 | } | ||||||||||||||
| 51 | |||||||||||||||
| 52 | sub view: Chained('load') { | ||||||||||||||
| 53 | push @shown_users, $user->id; | ||||||||||||||
| 54 | $template = template; | ||||||||||||||
| 55 | } | ||||||||||||||
| 56 | |||||||||||||||
| 57 | sub edit: Chained('load') { | ||||||||||||||
| 58 | if (validate_params(request->params)) { | ||||||||||||||
| 59 | $user->update(request->params); | ||||||||||||||
| 60 | $message = "user updated"; | ||||||||||||||
| 61 | } | ||||||||||||||
| 62 | } | ||||||||||||||
| 63 | |||||||||||||||
| 64 | 1; | ||||||||||||||
| 65 | |||||||||||||||
| 66 | =head1 DESCRIPTION | ||||||||||||||
| 67 | |||||||||||||||
| 68 | This module is B<not> stable yet. Features may change. | ||||||||||||||
| 69 | |||||||||||||||
| 70 | This module exports commonly used functionality and shortcuts to | ||||||||||||||
| 71 | L<Catalyst>s own feature set into your controller. Currently, these | ||||||||||||||
| 72 | groups of exports are available: | ||||||||||||||
| 73 | |||||||||||||||
| 74 | =head2 Context Exports | ||||||||||||||
| 75 | |||||||||||||||
| 76 | See also L<CatalystX::Imports::Context>. This will export functions | ||||||||||||||
| 77 | into your namespace that will allow you to access common methods and | ||||||||||||||
| 78 | values easier. As an example see the uses of | ||||||||||||||
| 79 | L<stash|CatalystX::Imports::Context::Default/stash>, | ||||||||||||||
| 80 | L<model|CatalystX::Imports::Context::Default/model> and | ||||||||||||||
| 81 | L<args|CatalystX::Imports::Context::Default/args> in the L</SYNOPSIS>. | ||||||||||||||
| 82 | |||||||||||||||
| 83 | You can ask for these imports by specifying a C<Context> argument on | ||||||||||||||
| 84 | the C<use> line: | ||||||||||||||
| 85 | |||||||||||||||
| 86 | use CatalystX::Imports Context => ... | ||||||||||||||
| 87 | |||||||||||||||
| 88 | The C<Config> library is a special case that has no predefined | ||||||||||||||
| 89 | exports, but allows you to import accessors to your local controller | ||||||||||||||
| 90 | configuration. | ||||||||||||||
| 91 | |||||||||||||||
| 92 | =head2 Variable Exports | ||||||||||||||
| 93 | |||||||||||||||
| 94 | See also L<CatalystX::Imports::Vars>. With this module, you can import | ||||||||||||||
| 95 | the C<$self>, C<$ctx> and C<@args> variables as if you'd have done | ||||||||||||||
| 96 | |||||||||||||||
| 97 | my ($self, $ctx, @args) = @_; | ||||||||||||||
| 98 | |||||||||||||||
| 99 | in one of your actions. It also allows you to import variables bound to | ||||||||||||||
| 100 | values in the stash, flash or session stores, like shown in the | ||||||||||||||
| 101 | L</SYNOPSIS>. | ||||||||||||||
| 102 | |||||||||||||||
| 103 | You can use this functionality via the C<Vars> argument on the C<use> | ||||||||||||||
| 104 | line: | ||||||||||||||
| 105 | |||||||||||||||
| 106 | use CatalystX::Imports Vars => ... | ||||||||||||||
| 107 | |||||||||||||||
| 108 | =cut | ||||||||||||||
| 109 | |||||||||||||||
| 110 | # names of the localized stores in the controllers | ||||||||||||||
| 111 | 6 | $STORE_CONTROLLER = 'CATALYSTX_IMPORTS_STORE_CONTROLLER'; | |||||||||||||
| 112 | 6 | $STORE_CONTEXT = 'CATALYSTX_IMPORTS_STORE_CONTEXT'; | |||||||||||||
| 113 | 6 | $STORE_ARGUMENTS = 'CATALYSTX_IMPORTS_STORE_ARGUMENTS'; | |||||||||||||
| 114 | |||||||||||||||
| 115 | # where the wrappers for action calls will be sitting | ||||||||||||||
| 116 | 6 | $ACTION_WRAPPER_VAR = 'CATALYSTX_IMPORTS_ACTION_WRAPPERS'; | |||||||||||||
| 117 | |||||||||||||||
| 118 | =head1 METHODS | ||||||||||||||
| 119 | |||||||||||||||
| 120 | =cut | ||||||||||||||
| 121 | |||||||||||||||
| 122 | =head2 import | ||||||||||||||
| 123 | |||||||||||||||
| 124 | This is a method used by all subclasses. When called, it fetches the | ||||||||||||||
| 125 | caller as target (the C<use>ing class) and passes it to the | ||||||||||||||
| 126 | C<export_into> method that must be implemented by a C<use>able class. | ||||||||||||||
| 127 | |||||||||||||||
| 128 | It also makes sure that L</install_action_wrap_into> is called after | ||||||||||||||
| 129 | the initial runtime of your controller. | ||||||||||||||
| 130 | |||||||||||||||
| 131 | =cut | ||||||||||||||
| 132 | |||||||||||||||
| 133 | 10 | ▶ | sub import { | ||||||||||||
|
Subroutine
import: called CC=1 SCAR=0.0
| |||||||||||||||
| 134 | 10 | my ($class, @args) = @_; | |||||||||||||
| 135 | |||||||||||||||
| 136 | # the class that 'use'd us | ||||||||||||||
| 137 | 10 | my $caller = scalar caller; | |||||||||||||
| 138 | |||||||||||||||
| 139 | # call install_action_wrap_into after package runtime | ||||||||||||||
| 140 | 9 | ▶ | Filter::EOF->on_eof_call( sub { | ||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||
| 141 | 9 | my $eof = shift; | |||||||||||||
| 142 | 9 | $$eof = "; ${class}->install_action_wrap_into('${caller}'); 1;"; | |||||||||||||
| 143 | 10 | }); | |||||||||||||
| 144 | |||||||||||||||
| 145 | # call current export mechanism | ||||||||||||||
| 146 | 10 | return $class->export_into($caller, @args); | |||||||||||||
| 147 | } | ||||||||||||||
| 148 | |||||||||||||||
| 149 | =head2 register_action_wrap_in | ||||||||||||||
| 150 | |||||||||||||||
| 151 | Takes a code reference and a target and registers the reference to | ||||||||||||||
| 152 | be a wrapper for action code. As an example, without any functionality: | ||||||||||||||
| 153 | |||||||||||||||
| 154 | CatalystX::Imports->register_action_wrap_in($class, sub { | ||||||||||||||
| 155 | my $code = shift; | ||||||||||||||
| 156 | my @wrappers = @{ shift(@_) }; | ||||||||||||||
| 157 | |||||||||||||||
| 158 | # ... put your code here ... | ||||||||||||||
| 159 | |||||||||||||||
| 160 | if (my $wrapper = shift @wrappers) { | ||||||||||||||
| 161 | return $wrapper->($code, [@wrappers], @_); | ||||||||||||||
| 162 | } | ||||||||||||||
| 163 | else { | ||||||||||||||
| 164 | return $code->(@_); | ||||||||||||||
| 165 | } | ||||||||||||||
| 166 | }); | ||||||||||||||
| 167 | |||||||||||||||
| 168 | =cut | ||||||||||||||
| 169 | |||||||||||||||
| 170 | 7 | ▶ | sub register_action_wrap_in { | ||||||||||||
|
Subroutine
register_action_wrap_in: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||
| 171 | 7 | my ($class, $target, $code) = @_; | |||||||||||||
| 172 | 6 | ▶ | no strict 'refs'; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 173 | 6 | ▶ | no warnings 'once'; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 174 | 7 | push @{ "${target}::${ACTION_WRAPPER_VAR}" }, $code; | |||||||||||||
| 175 | 7 | return 1; | |||||||||||||
| 176 | } | ||||||||||||||
| 177 | |||||||||||||||
| 178 | =head2 install_action_wrap_into | ||||||||||||||
| 179 | |||||||||||||||
| 180 | This module needs a few parts of data to provide it's functionality. | ||||||||||||||
| 181 | Namely, the current controller and context object, as well as the | ||||||||||||||
| 182 | arguments to the last called action. To get to these, it will simply | ||||||||||||||
| 183 | wrap all action code in your controller. This is what this function | ||||||||||||||
| 184 | does, essentially. | ||||||||||||||
| 185 | |||||||||||||||
| 186 | =cut | ||||||||||||||
| 187 | |||||||||||||||
| 188 | 9 | ▶ | sub install_action_wrap_into { | ||||||||||||
|
Subroutine
install_action_wrap_into: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||
| 189 | 9 | my ($class, $target) = @_; | |||||||||||||
| 190 | |||||||||||||||
| 191 | # get all action methods of the target class (not inherited actions) | ||||||||||||||
| 192 | 9 | my $meta = Class::MOP::class_of($target); | |||||||||||||
| 193 | 9 | my @actions = $meta->get_method_with_attributes_list; | |||||||||||||
| 194 | |||||||||||||||
| 195 | # replace every action code with a wrapper | ||||||||||||||
| 196 | 9 | for my $action (@actions) { | |||||||||||||
| 197 | # the wrapper fetches controller, context and args and stores | ||||||||||||||
| 198 | # them for other parts of the CX:I module | ||||||||||||||
| 199 | 56 | ▶ | $meta->add_around_method_modifier($action->name => sub { | ||||||||||||
|
Subroutine
__ANON__: called CC=3 SCAR=11.0
| |||||||||||||||
| 200 | 56 | my $next = shift; | |||||||||||||
| 201 | 56 | my ($self, $c, @args) = @_; | |||||||||||||
| 202 | |||||||||||||||
| 203 | # fetch registered action call wrappers | ||||||||||||||
| 204 | 56 | my @wrappers = do { | |||||||||||||
| 205 | 6 | ▶ | no strict 'refs'; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 206 | 56 | @{ "${target}::${ACTION_WRAPPER_VAR}" }; | |||||||||||||
| 207 | }; | ||||||||||||||
| 208 | |||||||||||||||
| 209 | # defines where the needed object will be stored | ||||||||||||||
| 210 | 56 | my %mapping = ( | |||||||||||||
| 211 | CONTROLLER => $self, | ||||||||||||||
| 212 | CONTEXT => $c, | ||||||||||||||
| 213 | ARGUMENTS => \@args, | ||||||||||||||
| 214 | ); | ||||||||||||||
| 215 | |||||||||||||||
| 216 | # store the objects | ||||||||||||||
| 217 | 6 | ▶ | { no strict 'refs'; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 218 | 168 | ${ "${target}::CATALYSTX_IMPORTS_STORE_${_}" } | |||||||||||||
| 219 | = $mapping{ $_ } | ||||||||||||||
| 220 | 56 | for keys %mapping; | |||||||||||||
| 221 | } | ||||||||||||||
| 222 | |||||||||||||||
| 223 | # call original code with original arguments | ||||||||||||||
| 224 | 56 | ▶ | unless (@wrappers) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 225 | 1 | return $next->(@_); | |||||||||||||
| 226 | } | ||||||||||||||
| 227 | |||||||||||||||
| 228 | # delegate to wrapper | ||||||||||||||
| 229 | else { | ||||||||||||||
| 230 | 55 | my $wrapper = shift @wrappers; | |||||||||||||
| 231 | 55 | return $wrapper->($next, [@wrappers], @_); | |||||||||||||
| 232 | } | ||||||||||||||
| 233 | 120 | }); | |||||||||||||
| 234 | } | ||||||||||||||
| 235 | |||||||||||||||
| 236 | 9 | return 1; | |||||||||||||
| 237 | } | ||||||||||||||
| 238 | |||||||||||||||
| 239 | =head2 export_into | ||||||||||||||
| 240 | |||||||||||||||
| 241 | Tells every specified exporter class (C<Context>, etc.) to export | ||||||||||||||
| 242 | themselves and passes their respective arguments. | ||||||||||||||
| 243 | |||||||||||||||
| 244 | =cut | ||||||||||||||
| 245 | |||||||||||||||
| 246 | 14 | ▶ | sub export_into { | ||||||||||||
|
Subroutine
export_into: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||
| 247 | 14 | my ($class, $target, @args) = @_; | |||||||||||||
| 248 | |||||||||||||||
| 249 | # we need exporter => options pairs | ||||||||||||||
| 250 | 14 | ▶ | croak 'CatalystX::Imports expects a key/value list as argument' | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 251 | if @args % 2; | ||||||||||||||
| 252 | 13 | my %exporters = @args; | |||||||||||||
| 253 | |||||||||||||||
| 254 | # walk the exporters list and let every one export itself | ||||||||||||||
| 255 | # to the target class | ||||||||||||||
| 256 | 13 | for my $exporter (keys %exporters) { | |||||||||||||
| 257 | 18 | my $exporter_class = __PACKAGE__ . "::$exporter"; | |||||||||||||
| 258 | 18 | Class::MOP::load_class($exporter_class); | |||||||||||||
| 259 | 18 | $exporter_class->export_into($target, $exporters{ $exporter }); | |||||||||||||
| 260 | } | ||||||||||||||
| 261 | |||||||||||||||
| 262 | 11 | return 1; | |||||||||||||
| 263 | } | ||||||||||||||
| 264 | |||||||||||||||
| 265 | =head2 resolve_component | ||||||||||||||
| 266 | |||||||||||||||
| 267 | Some functionality will allow you to prefix used components with a | ||||||||||||||
| 268 | configurable string. They will use this method to find a component | ||||||||||||||
| 269 | according to the current configuration. | ||||||||||||||
| 270 | |||||||||||||||
| 271 | =cut | ||||||||||||||
| 272 | |||||||||||||||
| 273 | 3 | ▶ | sub resolve_component { | ||||||||||||
|
Subroutine
resolve_component: called CC=8 SCAR=21.4
Pod
documented
| |||||||||||||||
| 274 | 3 | my ($class, $controller, $c, $type, $name, $args) = @_; | |||||||||||||
| 275 | |||||||||||||||
| 276 | # just use the name if nothing is configured at all | ||||||||||||||
| 277 | 3 | my $config = $controller->config->{component_prefix}; | |||||||||||||
| 278 | |||||||||||||||
| 279 | # a hashref means per-type configuration | ||||||||||||||
| 280 | 3 | ▶ | if (ref($config) eq 'HASH') { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 281 | $config = exists($config->{ $type }) ? $config->{ $type } | ||||||||||||||
| 282 | : exists($config->{-default}) ? $config->{-default} | ||||||||||||||
| 283 | 1 | ▶ | : return $name; | ||||||||||||
|
Branch: 2 branches
| |||||||||||||||
| 284 | } | ||||||||||||||
| 285 | |||||||||||||||
| 286 | # if the result of the above is not an arrayref, make it one | ||||||||||||||
| 287 | # for convenience reasons | ||||||||||||||
| 288 | 3 | ▶ | unless (ref($config) eq 'ARRAY') { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 289 | 2 | $config = [$config]; | |||||||||||||
| 290 | } | ||||||||||||||
| 291 | |||||||||||||||
| 292 | # try to find a component under that prefix and return it if found | ||||||||||||||
| 293 | 3 | for my $prefix (@$config) { | |||||||||||||
| 294 | 4 | my $comp_name = join('::', grep { $_ } $prefix, $name); | |||||||||||||
| 295 | 4 | ▶ | my $comp = $c->$type($comp_name, @{ $args || [] }); | ||||||||||||
|
Condition50%1/2
$args || []
MC/DC0%0/1
$args || []
$args
Truth table50%1/2
A
A= $args
| |||||||||||||||
| 296 | 4 | ▶ | return $comp if defined($comp); | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 297 | } | ||||||||||||||
| 298 | |||||||||||||||
| 299 | # nothing found | ||||||||||||||
| 300 | 0 | return; | |||||||||||||
| 301 | } | ||||||||||||||
| 302 | |||||||||||||||
| 303 | =head1 DIAGNOSTICS | ||||||||||||||
| 304 | |||||||||||||||
| 305 | See also L<CatalystX::Imports::Context/DIAGNOSTICS> and | ||||||||||||||
| 306 | L<CatalystX::Imports::Vars/DIAGNOSTICS> for further messages. | ||||||||||||||
| 307 | |||||||||||||||
| 308 | =head2 CatalystX::Imports expects a key/value list as argument | ||||||||||||||
| 309 | |||||||||||||||
| 310 | The use line expects a set of key/value pairs as arguments, but you gave | ||||||||||||||
| 311 | it a list with an odd number of elements. | ||||||||||||||
| 312 | |||||||||||||||
| 313 | =head1 SEE ALSO | ||||||||||||||
| 314 | |||||||||||||||
| 315 | L<Catalyst>, | ||||||||||||||
| 316 | L<CatalystX::Imports::Context>, | ||||||||||||||
| 317 | L<CatalystX::Imports::Vars> | ||||||||||||||
| 318 | |||||||||||||||
| 319 | =head1 AUTHOR AND COPYRIGHT | ||||||||||||||
| 320 | |||||||||||||||
| 321 | Robert 'phaylon' Sedlacek C<E<lt>rs@474.atE<gt>> | ||||||||||||||
| 322 | |||||||||||||||
| 323 | =head1 LICENSE | ||||||||||||||
| 324 | |||||||||||||||
| 325 | This program is free software; you can redistribute it and/or modify | ||||||||||||||
| 326 | it under the same terms as perl itself. | ||||||||||||||
| 327 | |||||||||||||||
| 328 | =cut | ||||||||||||||
| 329 | |||||||||||||||
| 330 | 6 | 1; | |||||||||||||