/usr/share/perl5/Dancer/App.pm is in libdancer-perl 1.3091+dfsg-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 | package Dancer::App;
use strict;
use warnings;
use Carp;
use base 'Dancer::Object';
use Dancer::Config;
use Dancer::ModuleLoader;
use Dancer::Route::Registry;
use Dancer::Logger;
use Dancer::Exception qw(:all);
Dancer::App->attributes(qw(name app_prefix prefix registry settings on_lexical_prefix));
# singleton that saves any app created, we want unicity for app names
my $_apps = {};
sub applications { values %$_apps }
sub app_exists {
my ( $self, $name ) = @_;
grep { $_ eq $name } keys %$_apps;
}
sub set_running_app {
my ($self, $name) = @_;
my $app = Dancer::App->get($name);
$app = Dancer::App->new(name => $name) unless defined $app;
Dancer::App->current($app);
}
sub set_app_prefix {
my ($self, $prefix) = @_;
$self->app_prefix($prefix);
$self->prefix($prefix);
}
sub set_prefix {
my ($self, $prefix, $cb) = @_;
undef $prefix if defined($prefix) and $prefix eq "/";
raise core_app => "not a valid prefix: `$prefix', must start with a /"
if defined($prefix) && $prefix !~ /^\//;
my $app_prefix = defined $self->app_prefix ? $self->app_prefix : "";
my $previous = Dancer::App->current->prefix;
$prefix ||= "";
if (Dancer::App->current->on_lexical_prefix) {
Dancer::App->current->prefix($previous.$prefix);
} else {
Dancer::App->current->prefix($app_prefix.$prefix);
}
if (ref($cb) eq 'CODE') {
Dancer::App->current->on_lexical_prefix(1);
eval { $cb->() };
my $e = $@;
Dancer::App->current->on_lexical_prefix(0);
Dancer::App->current->prefix($previous);
die $e if $e;
}
return 1; # prefix may have been set to undef
}
sub routes {
my ($self, $method) = @_;
map { $_->pattern } @{$self->registry->{'routes'}{$method}};
}
sub reload_apps {
my ($class) = @_;
my @missing_modules = grep { not Dancer::ModuleLoader->load($_) }
qw(Module::Refresh Clone);
if (not @missing_modules) {
# saving apps & purging app registries
my $orig_apps = {};
while (my ($name, $app) = each %$_apps) {
$orig_apps->{$name} = $app->clone;
$app->registry->init();
}
# reloading changed modules, getting apps reloaded
Module::Refresh->refresh;
# make sure old apps that didn't get reloaded are kept
while (my ($name, $app) = each %$orig_apps) {
$_apps->{$name} = $app unless defined $_apps->{$name};
$_apps->{$name} = $app if $_apps->{$name}->registry->is_empty;
}
}
else {
carp "Modules required for auto_reload are missing. Install modules"
. " [@missing_modules] or unset 'auto_reload' in your config file.";
}
}
sub find_route_through_apps {
my ($class, $request) = @_;
for my $app (Dancer::App->current, Dancer::App->applications) {
my $route = $app->find_route($request);
if ($route) {
Dancer::App->current($route->app);
return $route;
}
return $route if $route;
}
return;
}
# instance
sub find_route {
my ($self, $request) = @_;
my $method = lc($request->method);
# if route cache is enabled, we check if we handled this path before
if (Dancer::Config::setting('route_cache')) {
my $route = Dancer::Route::Cache->get->route_from_path($method,
$request->path_info);
# NOTE maybe we should cache the match data as well
if ($route) {
$route->match($request);
return $route;
}
}
my @routes = @{$self->registry->routes($method)};
for my $r (@routes) {
my $match = $r->match($request);
if ($match) {
next if $r->has_options && (not $r->validate_options($request));
# if we have a route cache, store the result
if (Dancer::Config::setting('route_cache')) {
Dancer::Route::Cache->get->store_path($method,
$request->path_info => $r);
}
return $r;
}
}
return;
}
sub init {
my ($self) = @_;
$self->name('main') unless defined $self->name;
raise core_app => "an app named '" . $self->name . "' already exists"
if exists $_apps->{$self->name};
# default values for properties
$self->settings({});
$self->init_registry();
$_apps->{$self->name} = $self;
}
sub init_registry {
my ($self, $reg) = @_;
$self->registry($reg || Dancer::Route::Registry->new);
}
# singleton that saves the current active Dancer::App object
my $_current;
sub current {
my ($class, $app) = @_;
return $_current = $app if defined $app;
if (not defined $_current) {
$_current = Dancer::App->get('main') || Dancer::App->new();
}
return $_current;
}
sub get {
my ($class, $name) = @_;
$_apps->{$name};
}
sub setting {
my $self = shift;
if ($self->name eq 'main') {
return (@_ > 1)
? Dancer::Config::setting( @_ )
: Dancer::Config::setting( $_[0] );
}
if (@_ > 1) {
$self->_set_settings(@_)
} else {
my $name = shift;
exists($self->settings->{$name}) ? $self->settings->{$name}
: Dancer::Config::setting($name);
}
}
sub _set_settings {
my $self = shift;
die "Odd number of elements in set" unless @_ % 2 == 0;
while (@_) {
my $name = shift;
my $value = shift;
$self->settings->{$name} =
Dancer::Config->normalize_setting($name => $value);
}
}
1;
|