BarefootJS/Backend/Mojo.pm

filter: Statementstmt 85.7% 24 / 28 Branchbran 50.0% 3 / 6 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 60.0% 6 / 10 Podpod 20.0% 1 / 5 totaltotal 69.3% 34 / 49 CC 4 SCAR 8.1
CC
4
Cov
79%
materialize
6.0
new
2.0
encode_json
2.0
CRAP 2.2
« Mojolicious/Plugin/BarefootJS/DevReload.pm ↑ Summary
1 package BarefootJS::Backend::Mojo;
2 3 our $VERSION = "0.41.4";
3 3 ▶use Mojo::Base -base, -signatures;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 3 ▶use Mojo::ByteStream qw(b);
Subroutine BEGIN: called CC=1 SCAR=0.0
6 3 ▶use Mojo::JSON qw(to_json);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 3 ▶use Scalar::Util qw(weaken);
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 # ---------------------------------------------------------------------------
10 # Reference rendering backend (Mojolicious / Mojo::Template).
11 # ---------------------------------------------------------------------------
12 #
13 # BarefootJS.pm holds all the template-engine-agnostic logic (the JS-compat
14 # value helpers, array/string methods, hydration markers). Everything that is
15 # specific to *how a template is rendered* — JSON marshalling, raw-string
16 # marking, JSX-children materialisation, and named-template rendering — lives
17 # behind this backend object so the same runtime can drive a different Perl
18 # template engine (Text::Xslate, Template Toolkit, …) without rewriting the
19 # helper surface.
20 #
21 # A backend MUST implement:
22 # - encode_json($data) -> string
23 # - mark_raw($str) -> value the engine emits without escaping
24 # - materialize($value) -> string (resolve a captured-children ref)
25 # - render_named($name, $bf, \%vars) -> string
26 #
27 # This Mojo implementation is the reference. To target another engine, write a
28 # sibling backend (BarefootJS::Backend::Xslate, …) implementing the same four
29 # methods and pass it via `BarefootJS->new($c, { backend => $b })`.
30  
31 # The Mojolicious controller. Optional: the value-marshalling helpers
32 # (`encode_json` / `mark_raw` / `materialize`) work without it; only
33 # `render_named` reaches into the controller's renderer + stash.
34 3 has 'c';
35  
36 # Pluggable JSON encoder (#engine-abstraction). Defaults to
37 # `Mojo::JSON::to_json`, which returns a *character* string (not bytes)
38 # suitable for embedding in HTML output via `<%==` / Mojo::ByteStream.
39 #
40 # Override with any `sub ($data) { ... }` to swap in a faster XS encoder —
41 # e.g. `json_encoder => sub { Cpanel::JSON::XS->new->canonical->encode($_[0]) }`.
42 # The pure-Perl JSON::PP fallback Mojo::JSON uses can be a hot spot for large
43 # props payloads; the seam lets a host pick its own implementation without
44 # touching the runtime.
45 0 ▶has 'json_encoder' => sub { \&to_json };
Subroutine __ANON__: not called CC=1 SCAR=1.2
46  
47 # Hold the controller weakly for the same reason BarefootJS does: the
48 # controller owns the bf instance (which owns this backend) via its stash,
49 # so a strong back-reference would close a per-request cycle the refcount GC
50 # can't reclaim. `render_named` only touches `$self->c` mid-render, while the
51 # controller is still alive on the request stack.
52 13 ▶sub new ($class, %args) {
Subroutine new: called CC=2 SCAR=7.1
Pod documented
53 13 my $self = $class->SUPER::new(%args);
54 13 ▶ weaken($self->{c}) if $self->{c};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"c"} 13 0 13
55 13 return $self;
56 }
57  
58 0 ▶sub encode_json ($self, $data) {
Subroutine encode_json: not called CC=1 SCAR=6.9
Pod undocumented
59 0 return $self->json_encoder->($data);
60 }
61  
62 # Mark a string as already-safe so the template engine emits it verbatim
63 # (no re-escaping). In Mojo this is a Mojo::ByteStream, which the calling
64 # template's `<%==` raw-emit passes through unescaped.
65 0 ▶sub mark_raw ($self, $str) {
Subroutine mark_raw: not called CC=1 SCAR=6.9
Pod undocumented
66 0 return b($str);
67 }
68  
69 # JSX children / async fallbacks arrive via Mojo's `begin %>...<% end`
70 # capture, which produces a CODE ref returning a Mojo::ByteStream. Resolve
71 # it to a string before embedding. Plain (already-rendered) strings pass
72 # through unchanged.
73 0 ▶sub materialize ($self, $value) {
Subroutine materialize: not called CC=2 SCAR=17.9
Pod undocumented
74 0 ▶ return ref($value) eq 'CODE' ? $value->() : $value;
Branch: 1 branch
BranchTrueFalseTotal
ref $value eq 'CODE' ? : 0 0 0
75 }
76  
77 # Render a named template with `$child_bf` bound as the active runtime
78 # instance for that render. The Mojo `bf` helper resolves the current
79 # instance off `$c->stash->{'bf.instance'}`; swap it for the duration of
80 # the nested render and restore it afterwards so sibling renders are
81 # unaffected. `local` (rather than a manual swap) keeps the restore
82 # exception-safe: a die inside the nested render — e.g. a grandchild with
83 # no registered renderer — propagates without leaving the child instance
84 # bound as the request's active one.
85 4 ▶sub render_named ($self, $template_name, $child_bf, $vars) {
Subroutine render_named: called CC=2 SCAR=6.9
Pod undocumented
86 4 my $c = $self->c;
87 4 local $c->stash->{'bf.instance'} = $child_bf;
88 4 my $html = $c->render_to_string(template => $template_name, %$vars);
89 # `render_to_string` returns undef — it does NOT die — when the named
90 # template can't be rendered (typically: the template file is missing
91 # from the renderer paths). The calling template's `<%==` would emit
92 # that as an empty string, silently dropping the whole child subtree
93 # from the page (#2132). Fail the render loudly instead.
94 3 ▶ die qq{BarefootJS: child template "$template_name" rendered no output }
Branch: 1 branch
BranchTrueFalseTotal
unless defined $html 1 2 3
95 . qq{(is the template missing from the renderer paths?)\n}
96 unless defined $html;
97 2 return $html;
98 }
99  
100 3 1;
« Mojolicious/Plugin/BarefootJS/DevReload.pm ↑ Summary