| 1 |
package Dancer2::Core::Factory; |
| 2 |
# ABSTRACT: Instantiate components by type and name |
| 3 |
$Dancer2::Core::Factory::VERSION = '2.2.2'; |
| 4 |
use Moo; |
| 5 |
use Dancer2::Core; |
| 6 |
use Module::Runtime 'use_module'; |
| 7 |
use Carp 'croak'; |
| 8 |
|
| 9 |
sub create { |
| 10 |
my ( $class, $type, $name, %options ) = @_; |
| 11 |
|
| 12 |
$type = Dancer2::Core::camelize($type); |
| 13 |
$name = Dancer2::Core::camelize($name); |
| 14 |
my $was_fully_qualified = ( $name =~ s/^\+// ); # strip any leading '+' |
| 15 |
my $component_class = ( $was_fully_qualified ) ? $name : "Dancer2::${type}::${name}"; |
| 16 |
|
| 17 |
eval { use_module($component_class); 1; } |
| 18 |
or croak "Unable to load class for $type component $name: $@"; |
| 19 |
|
| 20 |
return $component_class->new(%options); |
| 21 |
} |
| 22 |
|
| 23 |
1; |
| 24 |
|
| 25 |
__END__ |
| 26 |
|
| 27 |
=pod |
| 28 |
|
| 29 |
=encoding UTF-8 |
| 30 |
|
| 31 |
=head1 NAME |
| 32 |
|
| 33 |
Dancer2::Core::Factory - Instantiate components by type and name |
| 34 |
|
| 35 |
=head1 VERSION |
| 36 |
|
| 37 |
version 2.2.2 |
| 38 |
|
| 39 |
=head1 AUTHOR |
| 40 |
|
| 41 |
Dancer Core Developers |
| 42 |
|
| 43 |
=head1 COPYRIGHT AND LICENSE |
| 44 |
|
| 45 |
This software is copyright (c) 2026 by Alexis Sukrieh. |
| 46 |
|
| 47 |
This is free software; you can redistribute it and/or modify it under |
| 48 |
the same terms as the Perl 5 programming language system itself. |
| 49 |
|
| 50 |
=cut |