<commit_before>package Mojolicious::Plugin::DefaultHelpers;
use Mojo::Base 'Mojolicious::Plugin';

use Mojo::ByteStream;
use Mojo::Collection;
use Mojo::Exception;
use Mojo::IOLoop;
use Mojo::Util qw(deprecated dumper sha1_sum steady_time);

sub register {
  my ($self, $app) = @_;

  # DEPRECATED in Tiger Face!
  $app->helper(
    render_exception => sub {
      deprecated 'Mojolicious::Controller::render_exception is DEPRECATED in'
        . ' favor of the reply->exception helper';
      shift->helpers->reply->exception(@_);
    }
  );
  $app->helper(
    render_not_found => sub {
      deprecated 'Mojolicious::Controller::render_not_found is DEPRECATED in'
        . ' favor of the reply->not_found helper';
      shift->helpers->reply->not_found;
    }
  );

  # Controller alias helpers
  for my $name (qw(app flash param stash session url_for validation)) {
    $app->helper($name => sub { shift->$name(@_) });
  }

  # Stash key shortcuts (should not generate log messages)
  for my $name (qw(extends layout title)) {
    $app->helper($name => sub { shift->stash($name, @_) });
  }

  $app->helper(accepts => sub { $_[0]->app->renderer->accepts(@_) });
  $app->helper(b       => sub { shift; Mojo::ByteStream->new(@_) });
  $app->helper(c       => sub { shift; Mojo::Collection->new(@_) });
  $app->helper(config  => sub { shift->app->config(@_) });

  $app->helper($_ => $self->can("_$_"))
    for qw(content content_for csrf_token current_route delay),
    qw(inactivity_timeout is_fresh url_with);

  $app->helper(dumper => sub { shift; dumper(@_) });
  $app->helper(include => sub { shift->render_to_string(@_) });

  $app->helper("reply.$_" => $self->can("_$_")) for qw(asset static);

  $app->helper('reply.exception' => sub { _development('exception', @_) });
  $app->helper('reply.not_found' => sub { _development('not_found', @_) });
  $app->helper(ua                => sub { shift->app->ua });
}

sub _asset {
  my $c = shift;
  $c->app->static->serve_asset($c, @_);
  $c->rendered;
}

sub _content {
  my ($c, $name, $content) = @_;
  $name ||= 'content';

  # Set (first come)
  my $hash = $c->stash->{'mojo.content'} ||= {};
  $hash->{$name} //= ref $content eq 'CODE' ? $content->() : $content
    if defined $content;

  # Get
  return Mojo::ByteStream->new($hash->{$name} // '');
}

sub _content_for {
  my ($c, $name, $content) = @_;
  return _content($c, $name) unless defined $content;
  my $hash = $c->stash->{'mojo.content'} ||= {};
  return $hash->{$name} .= ref $content eq 'CODE' ? $content->() : $content;
}

sub _csrf_token {
  my $c = shift;
  $c->session->{csrf_token}
    ||= sha1_sum($c->app->secrets->[0] . steady_time . rand 999);
}

sub _current_route {
  return '' unless my $route = shift->match->endpoint;
  return @_ ? $route->name eq shift : $route->name;
}

sub _delay {
  my $c     = shift;
  my $tx    = $c->render_later->tx;
  my $delay = Mojo::IOLoop->delay(@_);
  $delay->catch(sub { $c->helpers->reply->exception(pop) and undef $tx })
    ->wait;
}

sub _development {
  my ($page, $c, $e) = @_;

  # DEPRECATED in Tiger Face!
  if (my $sub = $c->can("render_$page")) {
    deprecated "Mojolicious::Controller::render_$page is DEPRECATED in favor"
      . " of the reply->$page helper";
    return $c->$sub($page eq 'exception' ? $e : ());
  }

  my $app = $c->app;
  $app->log->error($e = Mojo::Exception->new($e)) if $page eq 'exception';

  # Filtered stash snapshot
  my $stash = $c->stash;
  my %snapshot = map { $_ => $stash->{$_} }
    grep { !/^mojo\./ and defined $stash->{$_} } keys %$stash;

  # Render with fallbacks
  my $mode     = $app->mode;
  my $renderer = $app->renderer;
  my $options  = {
    exception => $page eq 'exception' ? $e : undef,
    format => $stash->{format} || $renderer->default_format,
    handler  => undef,
    snapshot => \%snapshot,
    status   => $page eq 'exception' ? 500 : 404,
    template => "$page.$mode"
  };
  my $inline = $renderer->_bundled($mode eq 'development' ? $mode : $page);
  return $c if _fallbacks($c, $options, $page, $inline);
  _fallbacks($c, {%$options, format => 'html'}, $page, $inline);
  return $c;
}

sub _fallbacks {
  my ($c, $options, $template, $inline) = @_;

  # Mode specific template
  return 1 if $c->render_maybe(%$options);

  # Normal template
  return 1 if $c->render_maybe(%$options, template => $template);

  # Inline template
  my $stash = $c->stash;
  return undef unless $stash->{format} eq 'html';
  delete @$stash{qw(extends layout)};
  return $c->render_maybe(%$options, inline => $inline, handler => 'ep');
}

sub _inactivity_timeout {
  return unless my $stream = Mojo::IOLoop->stream(shift->tx->connection // '');
  $stream->timeout(shift);
}

sub _is_fresh {
  my ($c, %options) = @_;
  return $c->app->static->is_fresh($c, \%options);
}

sub _static {
  my ($c, $file) = @_;
  return !!$c->rendered if $c->app->static->serve($c, $file);
  $c->app->log->debug(qq{File "$file" not found, public directory missing?});
  return !$c->helpers->reply->not_found;
}

sub _url_with {
  my $c = shift;
  return $c->url_for(@_)->query($c->req->url->query->clone);
}

1;

=encoding utf8

=head1 NAME

Mojolicious::Plugin::DefaultHelpers - Default helpers plugin

=head1 SYNOPSIS
<commit_msg>no need to use closures<commit_after>package Mojolicious::Plugin::DefaultHelpers;
use Mojo::Base 'Mojolicious::Plugin';

use Mojo::ByteStream;
use Mojo::Collection;
use Mojo::Exception;
use Mojo::IOLoop;
use Mojo::Util qw(deprecated dumper sha1_sum steady_time);

sub register {
  my ($self, $app) = @_;

  # DEPRECATED in Tiger Face!
  $app->helper(render_exception => \&_render_esception);
  $app->helper(render_not_found => \&_render_not_found);

  # Controller alias helpers
  for my $name (qw(app flash param stash session url_for validation)) {
    $app->helper($name => sub { shift->$name(@_) });
  }

  # Stash key shortcuts (should not generate log messages)
  for my $name (qw(extends layout title)) {
    $app->helper($name => sub { shift->stash($name, @_) });
  }

  $app->helper(accepts => sub { $_[0]->app->renderer->accepts(@_) });
  $app->helper(b       => sub { shift; Mojo::ByteStream->new(@_) });
  $app->helper(c       => sub { shift; Mojo::Collection->new(@_) });
  $app->helper(config  => sub { shift->app->config(@_) });

  $app->helper($_ => $self->can("_$_"))
    for qw(content content_for csrf_token current_route delay),
    qw(inactivity_timeout is_fresh url_with);

  $app->helper(dumper => sub { shift; dumper(@_) });
  $app->helper(include => sub { shift->render_to_string(@_) });

  $app->helper("reply.$_" => $self->can("_$_")) for qw(asset static);

  $app->helper('reply.exception' => sub { _development('exception', @_) });
  $app->helper('reply.not_found' => sub { _development('not_found', @_) });
  $app->helper(ua                => sub { shift->app->ua });
}

sub _asset {
  my $c = shift;
  $c->app->static->serve_asset($c, @_);
  $c->rendered;
}

sub _content {
  my ($c, $name, $content) = @_;
  $name ||= 'content';

  # Set (first come)
  my $hash = $c->stash->{'mojo.content'} ||= {};
  $hash->{$name} //= ref $content eq 'CODE' ? $content->() : $content
    if defined $content;

  # Get
  return Mojo::ByteStream->new($hash->{$name} // '');
}

sub _content_for {
  my ($c, $name, $content) = @_;
  return _content($c, $name) unless defined $content;
  my $hash = $c->stash->{'mojo.content'} ||= {};
  return $hash->{$name} .= ref $content eq 'CODE' ? $content->() : $content;
}

sub _csrf_token {
  my $c = shift;
  $c->session->{csrf_token}
    ||= sha1_sum($c->app->secrets->[0] . steady_time . rand 999);
}

sub _current_route {
  return '' unless my $route = shift->match->endpoint;
  return @_ ? $route->name eq shift : $route->name;
}

sub _delay {
  my $c     = shift;
  my $tx    = $c->render_later->tx;
  my $delay = Mojo::IOLoop->delay(@_);
  $delay->catch(sub { $c->helpers->reply->exception(pop) and undef $tx })
    ->wait;
}

sub _development {
  my ($page, $c, $e) = @_;

  # DEPRECATED in Tiger Face!
  if (my $sub = $c->can("render_$page")) {
    deprecated "Mojolicious::Controller::render_$page is DEPRECATED in favor"
      . " of the reply->$page helper";
    return $c->$sub($page eq 'exception' ? $e : ());
  }

  my $app = $c->app;
  $app->log->error($e = Mojo::Exception->new($e)) if $page eq 'exception';

  # Filtered stash snapshot
  my $stash = $c->stash;
  my %snapshot = map { $_ => $stash->{$_} }
    grep { !/^mojo\./ and defined $stash->{$_} } keys %$stash;

  # Render with fallbacks
  my $mode     = $app->mode;
  my $renderer = $app->renderer;
  my $options  = {
    exception => $page eq 'exception' ? $e : undef,
    format => $stash->{format} || $renderer->default_format,
    handler  => undef,
    snapshot => \%snapshot,
    status   => $page eq 'exception' ? 500 : 404,
    template => "$page.$mode"
  };
  my $inline = $renderer->_bundled($mode eq 'development' ? $mode : $page);
  return $c if _fallbacks($c, $options, $page, $inline);
  _fallbacks($c, {%$options, format => 'html'}, $page, $inline);
  return $c;
}

sub _fallbacks {
  my ($c, $options, $template, $inline) = @_;

  # Mode specific template
  return 1 if $c->render_maybe(%$options);

  # Normal template
  return 1 if $c->render_maybe(%$options, template => $template);

  # Inline template
  my $stash = $c->stash;
  return undef unless $stash->{format} eq 'html';
  delete @$stash{qw(extends layout)};
  return $c->render_maybe(%$options, inline => $inline, handler => 'ep');
}

sub _inactivity_timeout {
  return unless my $stream = Mojo::IOLoop->stream(shift->tx->connection // '');
  $stream->timeout(shift);
}

sub _is_fresh {
  my ($c, %options) = @_;
  return $c->app->static->is_fresh($c, \%options);
}

# DEPRECATED in Tiger Face!
sub _render_exception {
  deprecated 'Mojolicious::Controller::render_exception is DEPRECATED in'
    . ' favor of the reply->exception helper';
  shift->helpers->reply->exception(@_);
}

# DEPRECATED in Tiger Face!
sub _render_not_found {
  deprecated 'Mojolicious::Controller::render_not_found is DEPRECATED in'
    . ' favor of the reply->not_found helper';
  shift->helpers->reply->not_found;
}

sub _static {
  my ($c, $file) = @_;
  return !!$c->rendered if $c->app->static->serve($c, $file);
  $c->app->log->debug(qq{File "$file" not found, public directory missing?});
  return !$c->helpers->reply->not_found;
}

sub _url_with {
  my $c = shift;
  return $c->url_for(@_)->query($c->req->url->query->clone);
}

1;

=encoding utf8

=head1 NAME

Mojolicious::Plugin::DefaultHelpers - Default helpers plugin

=head1 SYNOPSIS

{'fraction_non_alphanumeric': 0.19962157048249762, 'fraction_numerical': 0.0035950804162724692, 'mean_word_length': 3.751011235955056, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 277, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '919136', 'n_tokens_mistral': 3881, 'n_tokens_neox': 3809, 'n_words': 884}