Skip to content

Commit

Permalink
Item14237: Various adjustments.
Browse files Browse the repository at this point in the history
- Made all prepareAttr() methods of Foswiki::App public if their
respective attributes are public.

- Added readFile method on Foswiki::App. Considered as a replacement for
Foswiki::readFile.

- Introducing spec files collection on Foswiki::Config.

- Corrected docs and added errno attr for Foswiki::Exception::FileOp.
  • Loading branch information
vrurg committed Dec 10, 2016
1 parent 23d7c8b commit 30cac9d
Show file tree
Hide file tree
Showing 5 changed files with 101 additions and 26 deletions.
7 changes: 3 additions & 4 deletions UnitTestContrib/lib/Unit/TestApp.pm
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
# See bottom of file for license and copyright

package Unit::TestApp;
use v5.14;

=begin TML
Expand Down Expand Up @@ -187,21 +186,21 @@ around callbacksInit => sub {
return $orig->( $this, @_ );
};

around _prepareRequest => sub {
around prepareRequest => sub {
my $orig = shift;
my $this = shift;

return $orig->( $this, %{ $this->requestParams } );
};

around _prepareEngine => sub {
around prepareEngine => sub {
my $orig = shift;
my $this = shift;

return $orig->( $this, %{ $this->engineParams } );
};

around _prepareConfig => sub {
around prepareCfg => sub {
my $orig = shift;
my $this = shift;

Expand Down
9 changes: 8 additions & 1 deletion UnitTestContrib/test/unit/ConfigTests.pm
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
package ConfigTests;
use v5.14;

use Foswiki;
use Try::Tiny;
Expand Down Expand Up @@ -366,4 +365,12 @@ sub test_arg2keys {
}
}

sub test_specFilesAttribute {
my $this = shift;

my $sf = $this->app->cfg->specFiles;

my $list = $sf->list;
}

1;
58 changes: 41 additions & 17 deletions core/lib/Foswiki/App.pm
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
# See bottom of file for license and copyright information

package Foswiki::App;
use v5.14;

=begin TML
Expand Down Expand Up @@ -103,7 +102,7 @@ has cfg => (
is => 'rw',
lazy => 1,
predicate => 1,
builder => '_prepareConfig',
builder => 'prepareCfg',
isa => Foswiki::Object::isaCLASS( 'cfg', 'Foswiki::Config', noUndef => 1, ),
);
has env => (
Expand All @@ -113,7 +112,7 @@ has env => (
has extensions => (
is => 'ro',
lazy => 1,
builder => '_prepareExtensions',
builder => 'prepareExtensions',
);
has forms => (
is => 'ro',
Expand All @@ -140,7 +139,7 @@ has engine => (
is => 'rw',
lazy => 1,
predicate => 1,
builder => '_prepareEngine',
builder => 'prepareEngine',
isa =>
Foswiki::Object::isaCLASS( 'engine', 'Foswiki::Engine', noUndef => 1, ),
);
Expand Down Expand Up @@ -184,7 +183,7 @@ has prefs => (
lazy => 1,
predicate => 1,
clearer => 1,
builder => '_preparePrefs',
builder => 'preparePrefs',
);
has renderer => (
is => 'ro',
Expand All @@ -198,7 +197,7 @@ has renderer => (
has request => (
is => 'rw',
lazy => 1,
builder => '_prepareRequest',
builder => 'prepareRequest',
isa =>
Foswiki::Object::isaCLASS( 'request', 'Foswiki::Request', noUndef => 1, ),
);
Expand Down Expand Up @@ -253,7 +252,7 @@ has context => (
is => 'rw',
lazy => 1,
clearer => 1,
builder => '_prepareContext',
builder => 'prepareContext',
);
has ui => (
is => 'rw',
Expand Down Expand Up @@ -283,7 +282,7 @@ has remoteUser => (
has user => (
is => 'rw',
lazy => 1,
builder => '_prepareUser',
builder => 'prepareUser',
);
has users => (
is => 'rw',
Expand Down Expand Up @@ -1416,7 +1415,7 @@ BOGUS
}
}

sub _prepareContext {
sub prepareContext {
my $this = shift;

my %context =
Expand All @@ -1431,7 +1430,7 @@ sub _prepareContext {
return \%context;
}

sub _prepareEngine {
sub prepareEngine {
my $this = shift;
my @args = @_;
my $env = $this->env;
Expand All @@ -1446,7 +1445,7 @@ sub _prepareEngine {
return $engine;
}

sub _preparePrefs {
sub preparePrefs {
my $this = shift;

my $prefs = $this->create('Foswiki::Prefs');
Expand Down Expand Up @@ -1492,15 +1491,15 @@ sub _readPrefs {
}

# The request attribute default method.
sub _prepareRequest {
sub prepareRequest {
my $this = shift;
my @args = @_;

state $preparing = 0;

if ($preparing) {
Foswiki::Exception::Fatal->throw(
text => 'Circular call to _prepareRequest' );
text => 'Circular call to prepareRequest' );
}
$preparing = 1;

Expand All @@ -1523,7 +1522,7 @@ sub _prepareRequest {
return $request;
}

sub _prepareConfig {
sub prepareCfg {
my $this = shift;
my $cfg = $this->create('Foswiki::Config');
return $cfg;
Expand Down Expand Up @@ -1565,14 +1564,14 @@ sub _prepareDispatcher {
$this->_dispatcherAttrs($dispatcher);
}

sub _prepareUser {
sub prepareUser {
my $this = shift;

#return $this->users->initialiseUser( $this->remoteUser );
return undef;
}

sub _prepareExtensions {
sub prepareExtensions {
my $this = shift;

# Don't use create() here because the latter depends on extensions.
Expand Down Expand Up @@ -1656,7 +1655,7 @@ sub _checkActionAccess {
if ( UNIVERSAL::isa( $this->engine, 'Foswiki::Engine::CLI' )
|| UNIVERSAL::isa( $this->engine, 'Foswiki::Engine::Test' ) )
{
# Done in _prepareContext
# Done in prepareContext
#$dispatcherAttrs->{context}{command_line} = 1;
}
elsif (
Expand Down Expand Up @@ -3871,6 +3870,31 @@ sub writeDebug {
);
}

=begin TML
---+++ ObjectMethod readFile($filename, %params) -> ($content, %response)
Params keys:
| *Name* | *Description* | *Default* |
| =unicode= | Read file as unicode | 1 (true) |
| =raiseError= | Raise =Foswiki::Exception::FileOp= on error | 0 |
=cut

sub readFile {
my $this = shift;
my $filename = shift;
my %params = @_;

$params{unicode} //= 1;
$params{raiseError} //= 0;

my $file = $this->create( 'Foswiki::File', %params );
return $file->content;
}

1;
__END__
Foswiki - The Free and Open Source Wiki, http://foswiki.org/
Expand Down
34 changes: 34 additions & 0 deletions core/lib/Foswiki/Config.pm
Original file line number Diff line number Diff line change
Expand Up @@ -307,6 +307,26 @@ has rootSection => (
),
);

=begin TML
---++ ObjectAttribute specFiles
And object of =Foswiki::Config::Spec::Files= class. List of specs found.
=cut

#has specFiles => (
# is => 'rw',
# lazy => 1,
# clearer => 1,
# isa => Foswiki::Object::isaCLASS(
# 'specFiles',
# 'Foswiki::Config::Spec::Files',
# noUndef => 1,
# ),
# builder => 'prepareSpecFiles',
#);

# Configuration shortcut attributes.

=begin TML
Expand Down Expand Up @@ -2209,6 +2229,20 @@ sub prepareRootSection {
return $this->create( 'Foswiki::Config::Section', name => 'Root' );
}

=begin TML
---++ ObjectMethod prepareSpecFiles
Initializer of =specFiles= attribute.
=cut

sub prepareSpecFiles {
my $this = shift;

return $this->create('Foswiki::Config::Spec::Files');
}

my @validSecOptions = qw(text);
my $secOptRx = '(' . join( '|', @validSecOptions ) . ')';

Expand Down
19 changes: 15 additions & 4 deletions core/lib/Foswiki/Exception.pm
Original file line number Diff line number Diff line change
Expand Up @@ -431,18 +431,25 @@ extends qw(Foswiki::Exception::Fatal);

=begin TML
---++ Exception Foswiki::Exception::CB
---++ Exception Foswiki::Exception::FileOp
Root of callback support exception tree.
Base exception for failed file operations.
Attributes:
| *Name* | *Description* | *Required* |
| =file= | File name | _yes_ |
| =op= | Operation caused the failure, single verb like 'read' | _yes' |
=cut

package Foswiki::Exception::FileOp;
use Foswiki::Class;
extends qw(Foswiki::Exception::Fatal);

has file => ( is => 'rw', required => 1, );
has op => ( is => 'rw', required => 1, );
has file => ( is => 'rw', required => 1, );
has op => ( is => 'rw', required => 1, );
has errno => ( is => 'rw', builder => 'prepareErrno', );

around stringify => sub {
my $orig = shift;
Expand All @@ -463,6 +470,10 @@ around prepareText => sub {
return $!; # text attribute to be set from last file operation error.
};

sub prepareErrno {
return int($!);
}

=begin TML
---+ Exception Foswiki::Exception::HTTPResponse
Expand Down

0 comments on commit 30cac9d

Please sign in to comment.