Skip to content

Commit

Permalink
Item13897: Pulled bootstrapping code into Foswiki::Config.
Browse files Browse the repository at this point in the history
- Foswiki::Config takes bootstrapping over from Configure::Load.

- Added new base base class Foswiki::AppObject which must be the base for
all classess depending on initialized $Foswiki::App object. Defines app
attribute and a convinience method create() which calls new() with an
additional named parameter 'app'. So, an AppObject can create another
AppObject using notation:

$this->create('Foswiki::AppObject::Descendant',
        param1 => $param1,
        param2 => $param2,
);

- Added $FOSWIKI_HOME/bin/run_app script for testing the Foswiki::App
class. Temporary, will be removed later.
  • Loading branch information
vrurg committed Mar 24, 2016
1 parent be425e9 commit a5c6cc8
Show file tree
Hide file tree
Showing 7 changed files with 467 additions and 24 deletions.
5 changes: 5 additions & 0 deletions core/bin/run_app
@@ -0,0 +1,5 @@
#!env perl
use Cwd;
use lib Cwd::abs_path("../lib");
use Foswiki::App;
exit Foswiki::App->run;
48 changes: 40 additions & 8 deletions core/lib/Foswiki/App.pm
Expand Up @@ -4,7 +4,7 @@ package Foswiki::App;
use v5.14;

use Cwd;

use Try::Tiny;
use Foswiki::Config;

use Moo;
Expand Down Expand Up @@ -68,11 +68,15 @@ sub BUILD {
my $this = shift;
my $params = shift;

if ( $this->cfg->{isBOOTSTRAPPING} ) {
$Foswiki::app = $this;

#code
unless ( defined $this->engine ) {
Foswiki::Exception::Fatal->throw( text => "Cannot initialize engine" );
}

unless ( $this->cfg->data->{isVALID} ) {
$this->cfg->bootstrap;
}
}

=begin TML
Expand Down Expand Up @@ -106,17 +110,21 @@ sub run {
$params{env}{PWD} //= getcwd;

try {
$app = $Foswiki::app = Foswiki::App->new(%params);
$app = Foswiki::App->new(%params);
$app->handleRequest;
}
catch {
my $e = $_;

unless ( ref($e) && $e->isa('Foswiki::Exception') ) {
$e = Foswiki::Exception->transmute($e);
}

# Low-level report of errors to user.
if ( $app && $app->engine ) {
if ( defined $app && defined $app->engine ) {

# TODO Send error output to user using the initialized engine.
...;
$app->engine->write( $e->stringify );
}
else {
# Propagade the error using the most primitive way.
Expand All @@ -126,7 +134,31 @@ sub run {
}

sub handleRequest {
my $this = shift;

my $res = Foswiki::UI::handleRequest( $this->request );
$this->engine->finalize( $res, $this->request );
}

=begin TML
--++ ObjectMethod create($className, %initArgs)
Similar to =Foswiki::AppObject::create()= method but for the =Foswiki::App=
itself.
=cut

sub create {
my $this = shift;
my $class = shift;

unless ( $class->isa('Foswiki::AppObject') ) {
Foswiki::Exception::Fatal->throw(
text => "Class $class is not a Foswiki::AppObject descendant." );
}

return $class->new( app => $this, @_ );
}

sub _prepareEngine {
Expand All @@ -136,7 +168,7 @@ sub _prepareEngine {

# Foswiki::Engine has to determine what environment are we run within and
# return an object of corresponding class.
$engine = Foswiki::Engine->start( env => $env );
$engine = Foswiki::Engine::start( env => $env );

return $engine;
}
Expand All @@ -149,7 +181,7 @@ sub _prepareRequest {

sub _readConfig {
my $this = shift;
my $cfg = Foswiki::Config->new( env => $this->env );
my $cfg = $this->create( 'Foswiki::Config', env => $this->env );
return $cfg;
}

Expand Down
68 changes: 68 additions & 0 deletions core/lib/Foswiki/AppObject.pm
@@ -0,0 +1,68 @@
# See bottom of file for license and copyright information

package Foswiki::AppObject;
use v5.14;

=begin TML
---+ Class Foswiki::AppObject;
This is the base class for all classes which cannot be instantiated without
active =Foswiki::App= object.
=cut

use Assert;
use Foswiki::Exception;

use Moo;
use namespace::clean;
extends qw(Foswiki::Object);

has app => (
is => 'ro',
weak_ref => 1,
isa => Foswiki::Object::isaCLASS( 'app', 'Foswiki::App', noUndef => 1, ),
required => 1,
);

=begin TML
---++ ObjectMethod create($className, %initArgs)
Creates a new object of =Foswiki::AppObject= based class. It's a wrapper to
the =new()= constructor which automatically passes =app= parameter to the newly
created object.
=cut

sub create {
my $this = shift;
my $class = shift;

unless ( $class->isa(__PACKAGE__) ) {
Foswiki::Exception::Fatal->throw(
text => "Class $class is not a " . __PACKAGE__ . " descendant." );
}

return $class->new( app => $this->app, @_ );
}

1;
__END__
Foswiki - The Free and Open Source Wiki, http://foswiki.org/
Copyright (C) 2013 Foswiki Contributors. Foswiki Contributors
are listed in the AUTHORS file in the root of this distribution.
NOTE: Please extend that file, not this notice.
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version. For
more details read LICENSE in the root of this distribution.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
As per the GPL, removal of this notice is prohibited.

0 comments on commit a5c6cc8

Please sign in to comment.