blib/lib/PAGI/Nano/ServiceRegistry.pm

filter: Statementstmt 100% 37 / 37 Branchbran 100% 10 / 10 Conditioncond 81.8% 9 / 11 MC/DCmcdc 71.4% 5 / 7 Subroutinesub 100% 8 / 8 Podpod 50.0% 1 / 2 totaltotal 93.3% 70 / 75 CC 11 SCAR 14.2
CC
11
Cov
97%
_resolve
7.0
_build_all
4.0
service
2.0
CRAP 4.1
« examples/game-of-life/app.pl ↑ Summary examples/job-runner/app.pl »
1 package PAGI::Nano::ServiceRegistry;
2 11 $PAGI::Nano::ServiceRegistry::VERSION = '0.001001';
3 11 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 11 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 11 ▶use Carp ();
Subroutine BEGIN: called CC=1 SCAR=0.0
6 11 ▶use Scalar::Util ();
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 # One instance per assembled Nano app that declares any services. Holds the
9 # eagerly-built, per-worker results keyed by name. Vended to builders as the
10 # single "$app" argument (composition, via service()) and injected onto the
11 # PAGI scope for request-time resolution (see PAGI::Nano::Context::service).
12  
13 17 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
Pod undocumented
14 17 my ($class) = @_;
15 17 return bless { built => {} }, $class;
16 }
17  
18 # Run every declared builder once, in declaration order, storing whatever each
19 # returns verbatim (a plain value, an unblessed per-request maker coderef, or a
20 # factory-marked per-call maker; see PAGI::Nano::Context::service for how that
21 # is later discriminated). Called from a PAGI::Lifespan startup hook, so a
22 # builder that dies fails lifespan startup rather than a customer request.
23 16 ▶sub _build_all {
Subroutine _build_all: called CC=4 SCAR=13.9
24 16 my ($self, $services) = @_;
25 16 for my $entry (@$services) {
26 19 my ($name, $builder) = @$entry;
27 19 my $value = $builder->($self);
28 # A builder written as `async sub {...}` returns an unresolved Future.
29 # Builders are synchronous (they run at startup), so storing that as the
30 # service would only fail cryptically later at resolve time -- fail loud
31 # here instead. Duck-type on ->can('then') to catch Future and
32 # Future::AsyncAwait futures without depending on a specific class.
33 18 ▶ Carp::croak(
Branch: 1 branch
BranchTrueFalseTotal
if builtin::blessed($value) and $value->can("then") 1 17 18
Condition100%3/3
builtin::blessed($value) and $value->can("then")
!ll&&!rl&&r
1611
MC/DC100%2/2
builtin::blessed($value) and $value->can("then")
builtin::blessed($value) $value->can("then")
Truth table100%3/3
A and B
A= builtin::blessed($value)B= $value->can("then")
ABresult
0X0
100
111
34 "service '$name' builder returned a Future -- service builders must be "
35 . "synchronous (they run at lifespan startup). Build the value synchronously, "
36 . "or return a per-request maker (a plain coderef) / a factory(...) maker "
37 . "if you need per-request/per-call construction."
38 ) if Scalar::Util::blessed($value) && $value->can('then');
39 17 $self->{built}{$name} = $value;
40 }
41 }
42  
43 29 ▶sub service {
Subroutine service: called CC=2 SCAR=6.9
Pod documented
44 29 my ($self, $name) = @_;
45 Carp::croak("no service named '$name' (services build in declaration order at startup)")
46 29 ▶ unless exists $self->{built}{$name};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->{"built"}{$name} 3 26 29
47 26 return $self->{built}{$name};
48 }
49  
50 # The request-time accessor behind PAGI::Nano::Context::service: applies the
51 # scope-discrimination rule to the raw builder result. A factory-marked
52 # coderef (see PAGI::Nano::factory) is invoked fresh on every call. An
53 # unblessed coderef is a per-request maker, invoked with $ctx and memoized on
54 # $ctx's scope. Anything else (a plain value, or any other blessed object) is
55 # an app-scoped singleton, returned as-is.
56 #
57 # The memoization cache is a single flat hash on the scope, not sub-keyed by
58 # registry: mount() refuses to mount a Nano app that declares its own services
59 # (the outermost app owns lifecycle -- see PAGI::Nano/mount), so only one
60 # registry is ever in play for a given request.
61 24 ▶sub _resolve {
Subroutine _resolve: called CC=7 SCAR=19.5
62 24 my ($self, $name, $ctx) = @_;
63 24 my $raw = $self->service($name);
64  
65 23 ▶ if (Scalar::Util::blessed($raw) && $raw->isa('PAGI::Nano::Marker::Factory')) {
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::blessed($raw) and $raw->isa("PAGI::Nano::Marker::Factory")) 2 21 23
Condition66%2/3
builtin::blessed($raw) and $raw->isa("PAGI::Nano::Marker::Factory")
!ll&&!rl&&r
2102
MC/DC50%1/2
builtin::blessed($raw) and $raw->isa("PAGI::Nano::Marker::Factory")
builtin::blessed($raw) $raw->isa("PAGI::Nano::Marker::Factory")
Truth table66%2/3
A and B
A= builtin::blessed($raw)B= $raw->isa("PAGI::Nano::Marker::Factory")
ABresult
0X0
100
111
66 2 return $raw->($ctx);
67 }
68  
69 21 ▶ if (!Scalar::Util::blessed($raw) && ref($raw) eq 'CODE') {
Branch: 1 branch
BranchTrueFalseTotal
if (not builtin::blessed($raw) and ref $raw eq "CODE") 12 9 21
Condition66%2/3
not builtin::blessed($raw) and ref $raw eq "CODE"
!ll&&!rl&&r
0912
MC/DC50%1/2
not builtin::blessed($raw) and ref $raw eq "CODE"
not builtin::blessed($raw) ref $raw eq "CODE"
Truth table66%2/3
not(A) and B
A= not builtin::blessed($raw)B= ref $raw eq "CODE"
ABresult
0X0
100
111
70 12 my $scope = $ctx->{scope};
71 12 ▶ my $cache = $scope->{'pagi.nano.service_cache'} //= {};
Condition100%2/2
$scope->{"pagi.nano.service_cache"} //= {}
l!l
66
MC/DC100%1/1
$scope->{"pagi.nano.service_cache"} //= {}
$scope->{"pagi.nano.service_cache"}
Truth table100%2/2
A
A= $scope->{"pagi.nano.service_cache"}
Aresult
11
00
72 12 ▶ return $cache->{$name} if exists $cache->{$name};
Branch: 1 branch
BranchTrueFalseTotal
if exists $cache->{$name} 6 6 12
73  
74 6 my $value = $raw->($ctx);
75 6 $cache->{$name} = $value;
76 6 return $value;
77 }
78  
79 9 return $raw;
80 }
81  
82 11 1;
83  
84 =encoding utf8
85  
86 =head1 NAME
87  
88 PAGI::Nano::ServiceRegistry - The per-app registry backing PAGI::Nano's service keyword
89  
90 =head1 DESCRIPTION
91  
92 One instance is created per assembled L<PAGI::Nano> app that declares any
93 C<service>. It is never constructed directly by application code; it is the
94 object passed to every service builder (as C<$app> in the C<service> examples)
95 and the object C<< $c->service >> resolves against at request time.
96  
97 =head1 METHODS
98  
99 =head2 service
100  
101 my $value = $app->service('name');
102  
103 Returns an already-built service, for use inside another service's builder
104 (composition). Services build eagerly, in declaration order, at lifespan
105 startup; asking for one declared later in the same C<app { }> block (or not
106 declared at all) croaks, naming the service.
107  
108 A test that wants to read an app-scoped service without issuing a request does
109 not reach this object directly (it is private); it uses the
110 C<< PAGI::Nano::resolve_service($app, $name) >> seam, which resolves against
111 this registry after lifespan startup. See L<PAGI::Nano/resolve_service>.
112  
113 =head1 SEE ALSO
114  
115 L<PAGI::Nano>, L<PAGI::Nano::Context>.
116  
117 =head1 AUTHOR
118  
119 John Napiorkowski C<< <jjnapiork@cpan.org> >>
120  
121 =head1 COPYRIGHT & LICENSE
122  
123 Copyright 2026, John Napiorkowski. This library is free software; you can
124 redistribute it and/or modify it under the same terms as Perl itself.
125  
126 =cut
« examples/game-of-life/app.pl ↑ Summary examples/job-runner/app.pl »