| 1 | package Data::Localize::Namespace; | ||||||||||||||||||||||||||||
| 2 | 2 | ▶ | use Moo; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 3 | 2 | ▶ | use Module::Pluggable::Object; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 2 | ▶ | use Encode (); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 2 | ▶ | use Data::Localize; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | 2 | ▶ | BEGIN { | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | 2 | if (Data::Localize::DEBUG) { | |||||||||||||||||||||||||||
| 9 | require Data::Localize::Log; | ||||||||||||||||||||||||||||
| 10 | Data::Localize::Log->import; | ||||||||||||||||||||||||||||
| 11 | } | ||||||||||||||||||||||||||||
| 12 | } | ||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||
| 14 | 2 | extends 'Data::Localize::Localizer'; | |||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||
| 16 | has _namespaces => ( | ||||||||||||||||||||||||||||
| 17 | is => 'rw', | ||||||||||||||||||||||||||||
| 18 | 0 | ▶ | default => sub { [] }, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 19 | 2 | init_arg => 'namespaces', | |||||||||||||||||||||||||||
| 20 | ); | ||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||
| 22 | has _loaded_classes => ( | ||||||||||||||||||||||||||||
| 23 | is => 'ro', | ||||||||||||||||||||||||||||
| 24 | 5 | ▶ | default => sub { +{} } | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 25 | 2 | ); | |||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||
| 27 | has _failed_classes => ( | ||||||||||||||||||||||||||||
| 28 | is => 'ro', | ||||||||||||||||||||||||||||
| 29 | 5 | ▶ | default => sub { +{} } | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 30 | 2 | ); | |||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||
| 32 | 3 | ▶ | around register => sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||
| 33 | 3 | my ($next, $self, $loc) = @_; | |||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||
| 35 | 3 | $self->$next($loc); | |||||||||||||||||||||||||||
| 36 | 3 | my $finder = Module::Pluggable::Object->new( | |||||||||||||||||||||||||||
| 37 | 'require' => 1, | ||||||||||||||||||||||||||||
| 38 | search_path => [ $self->namespaces ] | ||||||||||||||||||||||||||||
| 39 | ); | ||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||
| 41 | # find the languages that we currently support | ||||||||||||||||||||||||||||
| 42 | 3 | my $re = join('|', $self->namespaces); | |||||||||||||||||||||||||||
| 43 | 3 | foreach my $plugin ($finder->plugins) { | |||||||||||||||||||||||||||
| 44 | 6 | $plugin =~ s/^(?:$re):://; | |||||||||||||||||||||||||||
| 45 | 6 | $plugin =~ s/::/_/g; | |||||||||||||||||||||||||||
| 46 | 6 | if (Data::Localize::DEBUG) { | |||||||||||||||||||||||||||
| 47 | debugf("register - Registering for language %s -> $self", | ||||||||||||||||||||||||||||
| 48 | $plugin, | ||||||||||||||||||||||||||||
| 49 | $self | ||||||||||||||||||||||||||||
| 50 | ); | ||||||||||||||||||||||||||||
| 51 | } | ||||||||||||||||||||||||||||
| 52 | 6 | $loc->add_localizer_map($plugin, $self); | |||||||||||||||||||||||||||
| 53 | } | ||||||||||||||||||||||||||||
| 54 | 3 | $loc->add_localizer_map('*', $self); | |||||||||||||||||||||||||||
| 55 | 2 | }; | |||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||
| 57 | 1 | ▶ | sub add_namespaces { | ||||||||||||||||||||||||||
|
Subroutine
add_namespaces: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 58 | 1 | my $self = shift; | |||||||||||||||||||||||||||
| 59 | 1 | unshift @{ $self->_namespaces }, @_; | |||||||||||||||||||||||||||
| 60 | } | ||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||
| 62 | 12 | ▶ | sub namespaces { | ||||||||||||||||||||||||||
|
Subroutine
namespaces: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 63 | 12 | my $self = shift; | |||||||||||||||||||||||||||
| 64 | 12 | return @{ $self->_namespaces }; | |||||||||||||||||||||||||||
| 65 | } | ||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||
| 67 | 6 | ▶ | sub get_lexicon { | ||||||||||||||||||||||||||
|
Subroutine
get_lexicon: called CC=11 SCAR=24.4
Pod
documented
| |||||||||||||||||||||||||||||
| 68 | 6 | my ($self, $lang, $id) = @_; | |||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||
| 70 | 6 | $lang =~ s/-/_/g; | |||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||
| 72 | 6 | my $LOADED = $self->_loaded_classes; | |||||||||||||||||||||||||||
| 73 | 6 | my $FAILED = $self->_failed_classes; | |||||||||||||||||||||||||||
| 74 | 6 | foreach my $namespace ($self->namespaces) { | |||||||||||||||||||||||||||
| 75 | 6 | my $klass = "$namespace\::$lang"; | |||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||
| 77 | 6 | ▶ | if ($FAILED->{ $klass }) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 78 | 0 | if (Data::Localize::DEBUG) { | |||||||||||||||||||||||||||
| 79 | debugf("get_lexicon - Already attempted loading %s and failed. Skipping...", $klass); | ||||||||||||||||||||||||||||
| 80 | } | ||||||||||||||||||||||||||||
| 81 | 0 | next; | |||||||||||||||||||||||||||
| 82 | } | ||||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||
| 84 | 6 | if (Data::Localize::DEBUG) { | |||||||||||||||||||||||||||
| 85 | debugf("get_lexicon - Trying %s", $klass); | ||||||||||||||||||||||||||||
| 86 | } | ||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||
| 88 | # Catch the very weird case where is_class_loaded() returns true | ||||||||||||||||||||||||||||
| 89 | # but the class really hasn't been loaded yet. | ||||||||||||||||||||||||||||
| 90 | 2 | ▶ | no strict 'refs'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 91 | 6 | my $first_load = 0; | |||||||||||||||||||||||||||
| 92 | 6 | ▶ | if (! $LOADED->{$klass}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 93 | 6 | ▶ | if (%{"$klass\::Lexicon"} && %{"$klass\::"}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
%{"${klass}::Lexicon";} and %{"${klass}::";}
MC/DC50%1/2
%{"${klass}::Lexicon";} and %{"${klass}::";}
%{"${klass}::Lexicon";}
%{"${klass}::";}
Truth table66%2/3
A and B
A= %{"${klass}::Lexicon";}B= %{"${klass}::";}
| |||||||||||||||||||||||||||||
| 94 | 4 | if (Data::Localize::DEBUG) { | |||||||||||||||||||||||||||
| 95 | debugf("get_lexicon - class %s already loaded", $klass); | ||||||||||||||||||||||||||||
| 96 | } | ||||||||||||||||||||||||||||
| 97 | } else { | ||||||||||||||||||||||||||||
| 98 | 2 | if (Data::Localize::DEBUG) { | |||||||||||||||||||||||||||
| 99 | debugf("get_lexicon - loading %s", $klass); | ||||||||||||||||||||||||||||
| 100 | } | ||||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||
| 102 | 2 | my $code = | |||||||||||||||||||||||||||
| 103 | "\n" . | ||||||||||||||||||||||||||||
| 104 | "#line " . __LINE__ . ' "' . __FILE__ . '"' . "\n" . | ||||||||||||||||||||||||||||
| 105 | "require $klass;" | ||||||||||||||||||||||||||||
| 106 | ; | ||||||||||||||||||||||||||||
| 107 | 2 | eval($code); | |||||||||||||||||||||||||||
| 108 | 2 | ▶ | if ($@) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 109 | 0 | if (Data::Localize::DEBUG) { | |||||||||||||||||||||||||||
| 110 | debugf("get_lexicon - Failed to load %s: %s", $klass, $@); | ||||||||||||||||||||||||||||
| 111 | $FAILED->{$klass}++; | ||||||||||||||||||||||||||||
| 112 | } | ||||||||||||||||||||||||||||
| 113 | 0 | next; | |||||||||||||||||||||||||||
| 114 | } | ||||||||||||||||||||||||||||
| 115 | } | ||||||||||||||||||||||||||||
| 116 | 6 | if (Data::Localize::DEBUG) { | |||||||||||||||||||||||||||
| 117 | debugf("get_lexicon - setting %s to already loaded", $klass); | ||||||||||||||||||||||||||||
| 118 | } | ||||||||||||||||||||||||||||
| 119 | 6 | $LOADED->{$klass}++; | |||||||||||||||||||||||||||
| 120 | 6 | $first_load = 1; | |||||||||||||||||||||||||||
| 121 | } | ||||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||
| 123 | 6 | if (Data::Localize::DEBUG) { | |||||||||||||||||||||||||||
| 124 | debugf("get_lexicon - returning lexicon from %s (%d lexicons)", | ||||||||||||||||||||||||||||
| 125 | $klass, | ||||||||||||||||||||||||||||
| 126 | scalar keys %{"$klass\::Lexicon"}, | ||||||||||||||||||||||||||||
| 127 | ); | ||||||||||||||||||||||||||||
| 128 | } | ||||||||||||||||||||||||||||
| 129 | 6 | my $h = \%{ "$klass\::Lexicon" }; | |||||||||||||||||||||||||||
| 130 | 6 | ▶ | if ($first_load) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 131 | 6 | my %t; | |||||||||||||||||||||||||||
| 132 | 6 | ▶ | while (my($k, $v) = each %$h) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 133 | 6 | ▶ | if ( ! Encode::is_utf8($k) ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 134 | 3 | $k = Encode::decode_utf8($k); | |||||||||||||||||||||||||||
| 135 | } | ||||||||||||||||||||||||||||
| 136 | 6 | ▶ | if ( ! Encode::is_utf8($v) ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 137 | 2 | $v = Encode::decode_utf8($v); | |||||||||||||||||||||||||||
| 138 | } | ||||||||||||||||||||||||||||
| 139 | 6 | $t{$k} = $v; | |||||||||||||||||||||||||||
| 140 | } | ||||||||||||||||||||||||||||
| 141 | 6 | %$h = (); | |||||||||||||||||||||||||||
| 142 | 6 | %$h = %t; | |||||||||||||||||||||||||||
| 143 | } | ||||||||||||||||||||||||||||
| 144 | 6 | return $h->{$id}; | |||||||||||||||||||||||||||
| 145 | |||||||||||||||||||||||||||||
| 146 | } | ||||||||||||||||||||||||||||
| 147 | 0 | return (); | |||||||||||||||||||||||||||
| 148 | } | ||||||||||||||||||||||||||||
| 149 | |||||||||||||||||||||||||||||
| 150 | 2 | 1; | |||||||||||||||||||||||||||
| 151 | |||||||||||||||||||||||||||||
| 152 | __END__ | ||||||||||||||||||||||||||||