| 1 |
package Dancer2::Core::Role::DSL; |
| 2 |
# ABSTRACT: Role for DSL |
| 3 |
$Dancer2::Core::Role::DSL::VERSION = '2.2.2'; |
| 4 |
use Moo::Role; |
| 5 |
use Dancer2::Core::Types; |
| 6 |
use Carp 'croak'; |
| 7 |
use Scalar::Util qw(); |
| 8 |
|
| 9 |
with 'Dancer2::Core::Role::Hookable'; |
| 10 |
|
| 11 |
has app => ( is => 'ro', required => 1 ); |
| 12 |
|
| 13 |
has keywords => ( |
| 14 |
is => 'rw', |
| 15 |
isa => HashRef, |
| 16 |
lazy => 1, |
| 17 |
builder => '_build_dsl_keywords', |
| 18 |
); |
| 19 |
|
| 20 |
sub _build_dsl_keywords { |
| 21 |
my ($self) = @_; |
| 22 |
$self->can('dsl_keywords') |
| 23 |
? $self->dsl_keywords |
| 24 |
: {}; |
| 25 |
} |
| 26 |
|
| 27 |
sub register { |
| 28 |
my ( $self, $keyword, $is_global ) = @_; |
| 29 |
my $keywords = $self->keywords; |
| 30 |
my $pkg = ref($self); |
| 31 |
$pkg =~ s/__WITH__.+$//; |
| 32 |
|
| 33 |
if ( exists $keywords->{$keyword} ) { |
| 34 |
my $reg_pkg = $keywords->{$keyword}{'pkg'}; |
| 35 |
$reg_pkg =~ s/__WITH__.+$//; |
| 36 |
$reg_pkg eq $pkg and return; |
| 37 |
|
| 38 |
croak "[$pkg] Keyword $keyword already registered by $reg_pkg"; |
| 39 |
} |
| 40 |
|
| 41 |
$keywords->{$keyword} = { is_global => $is_global, pkg => $pkg }; |
| 42 |
} |
| 43 |
|
| 44 |
sub dsl { $_[0] } |
| 45 |
|
| 46 |
# exports new symbol to caller |
| 47 |
sub export_symbols_to { |
| 48 |
my ( $self, $caller, $args ) = @_; |
| 49 |
my $exports = $self->_construct_export_map($args); |
| 50 |
|
| 51 |
## no critic |
| 52 |
foreach my $export ( keys %{$exports} ) { |
| 53 |
no strict 'refs'; ## no critic (TestingAndDebugging::ProhibitNoStrict) |
| 54 |
my $existing = *{"${caller}::${export}"}{CODE}; |
| 55 |
|
| 56 |
next if defined $existing; |
| 57 |
|
| 58 |
*{"${caller}::${export}"} = $exports->{$export}; |
| 59 |
} |
| 60 |
## use critic |
| 61 |
|
| 62 |
return keys %{$exports}; |
| 63 |
} |
| 64 |
|
| 65 |
# private |
| 66 |
|
| 67 |
sub _compile_keyword { |
| 68 |
my ( $self, $keyword, $opts ) = @_; |
| 69 |
|
| 70 |
my $code = $opts->{is_global} |
| 71 |
? sub { $self->$keyword(@_) } |
| 72 |
: sub { |
| 73 |
croak "Function '$keyword' must be called from a route handler" |
| 74 |
unless defined $Dancer2::Core::Route::REQUEST; |
| 75 |
|
| 76 |
$self->$keyword(@_) |
| 77 |
}; |
| 78 |
|
| 79 |
return $self->_apply_prototype($code, $opts); |
| 80 |
} |
| 81 |
|
| 82 |
sub _apply_prototype { |
| 83 |
my ($self, $code, $opts) = @_; |
| 84 |
|
| 85 |
# set prototype if one is defined for the keyword. undef => no prototype |
| 86 |
my $prototype; |
| 87 |
exists $opts->{'prototype'} and $prototype = $opts->{'prototype'}; |
| 88 |
return Scalar::Util::set_prototype( \&$code, $prototype ); |
| 89 |
} |
| 90 |
|
| 91 |
sub _construct_export_map { |
| 92 |
my ( $self, $args ) = @_; |
| 93 |
my $keywords = $self->keywords; |
| 94 |
my %map; |
| 95 |
foreach my $keyword ( keys %$keywords ) { |
| 96 |
# check if the keyword were excluded from importation |
| 97 |
$args->{ '!' . $keyword } and next; |
| 98 |
$map{$keyword} = $self->_compile_keyword( $keyword, $keywords->{$keyword} ); |
| 99 |
} |
| 100 |
return \%map; |
| 101 |
} |
| 102 |
|
| 103 |
1; |
| 104 |
|
| 105 |
__END__ |
| 106 |
|
| 107 |
=pod |
| 108 |
|
| 109 |
=encoding UTF-8 |
| 110 |
|
| 111 |
=head1 NAME |
| 112 |
|
| 113 |
Dancer2::Core::Role::DSL - Role for DSL |
| 114 |
|
| 115 |
=head1 VERSION |
| 116 |
|
| 117 |
version 2.2.2 |
| 118 |
|
| 119 |
=head1 AUTHOR |
| 120 |
|
| 121 |
Dancer Core Developers |
| 122 |
|
| 123 |
=head1 COPYRIGHT AND LICENSE |
| 124 |
|
| 125 |
This software is copyright (c) 2026 by Alexis Sukrieh. |
| 126 |
|
| 127 |
This is free software; you can redistribute it and/or modify it under |
| 128 |
the same terms as the Perl 5 programming language system itself. |
| 129 |
|
| 130 |
=cut |