Web-Components

 view release on metacpan or  search on metacpan

lib/Web/Components/Loader.pm  view on Meta::CPAN

      $attr->{domain_prefix} = $prefix if $prefix;

      return $attr;
   };
}

sub _build__factory {
   my $self = shift;

   return Web::ComposableRequest->new(
      buildargs => $self->factory_args,
      config    => $self->config,
   );
}

sub _build__routes {
   my $self        = shift;
   my $controllers = $self->controllers;
   my @keys        = keys %{$controllers};

   return [ map { $controllers->{$_}->dispatch_request } sort @keys ];
}

# Private functions
sub _header () {
   return [ 'Content-Type' => 'text/plain', @{ $_[0] // [] } ];
};

# Private methods
sub _get_context {
   my ($self, $req, $moniker, $method) = @_;

   my $model  = $self->models->{$moniker};
   my $action = "${moniker}/${method}";
   my $args   = {
      action      => $action,
      controllers => $self->controllers,
      models      => $self->models,
      request     => $req,
      views       => $self->views,
   };

   return $model->get_context($args) if $model->can('get_context');

   return Web::Components::Loader::Context->new($args);
}

sub _internal_server_error {
   my ($self, $e) = @_;

   $self->log->error($e);

   return [ HTTP_INTERNAL_SERVER_ERROR, _header, [$e] ];
}

sub _parse_sig {
   my ($self, $args) = @_;

   return @{$args} if exists $self->models->{$args->[0]};

   my ($moniker, $method) = split m{ / }mx, $args->[0], 2;

   if (exists $self->models->{$moniker}) {
      shift @{$args};
      return $moniker, $method, @{$args};
   }

   return;
}

sub _recognise_signature {
   my ($self, $args) = @_;

   return 1 if is_arrayref $args and $args->[0]
      and exists $self->models->{$args->[0]};

   my ($moniker, $method) = split m{ / }mx, $args->[0], 2;

   return 1 if $moniker and exists $self->models->{$moniker};

   return 0;
}

sub _redirect {
   my ($self, $context) = @_;

   my $req      = $context->request;
   my $stash    = $context->stash;
   my $code     = $stash->{code} // HTTP_FOUND;
   my $redirect = $stash->{redirect};
   my $location = $redirect->{location};

   if (my $message = $redirect->{message}) {
      my $default = {should_log_messages => 1};
      my $attr    = deref $self->config, $self->_config_attr, $default;

      if ($attr->{should_log_messages} && $req->can('loc_default')) {
         my $level = $redirect->{level} ? $redirect->{level} : 'info';

         $self->log->$level($req->loc_default(@{$message}), $context);
      }

      if ($req->can('session')) {
         my $session = $req->session;

         if ($session->can('add_status_message')) {
            if (my $mid = $session->add_status_message($message)) {
               $location->query_form($location->query_form, 'mid' => $mid);
            }
         }
      }
   }

   my $headers = [ %{$redirect->{http_headers} // {}}, 'Location', $location ];

   return [ $code, $headers, [] ];
}

sub _render_view {
   my ($self, $moniker, $context, $method) = @_;

   my $stash = $context->stash;

   return $self->_redirect($context) if exists $stash->{redirect};

   throw 'Model [_1] method [_2] stashed no view', [ $moniker, $method ]
      unless $stash->{view};

   my $view = $self->views->{$stash->{view}}
      or throw 'Model [_1] method [_2] unknown view [_3]',
               [ $moniker, $method, $stash->{view} ];
   my $res  = $view->serialize($context)
      or throw 'View [_1] returned false', [ $stash->{view} ];

   return $res;
}



( run in 0.764 second using v1.01-cache-2.11-cpan-71847e10f99 )