-
Notifications
You must be signed in to change notification settings - Fork 38
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Item14152: First class replacement implementation.
Implemented with Moo::Role create_class_with_roles. Foswiki::Config has been choosen as the first victim. An example extension class Foswiki::Extension::Sample::Class overrides its assignGLOB() method and instead of aliasing %Foswiki::cfg into $Foswiki::app->cfg->data it ties the global. This implementation is only an example. The module will later be deleted from the repository. The real implementation would make sense if we decide to move the config into a database, for example. In this case direct references to Foswiki::Config data attribute must be replaced with calls to get/set/delete/etc. method calls.
- Loading branch information
Showing
7 changed files
with
224 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
# See bottom of file for license and copyright information | ||
|
||
package Foswiki::Extension::Sample::TiedConfig; | ||
use Assert; | ||
|
||
sub TIEHASH { | ||
my $class = shift; | ||
my ($cfgObject) = @_; | ||
|
||
my $thisHash = { cfg => $cfgObject, }; | ||
|
||
return bless $thisHash, $class; | ||
} | ||
|
||
sub FETCH { | ||
my ( $this, $key ) = @_; | ||
ASSERT( defined $this->{cfg} ) if DEBUG; | ||
$this->trace("FETCH{$key}"); | ||
return $this->{cfg}->data->{$key}; | ||
} | ||
|
||
sub STORE { | ||
my ( $this, $key, $value ) = @_; | ||
ASSERT( defined $this->{cfg} ) if DEBUG; | ||
$this->trace("STORE{$key}"); | ||
$this->{cfg}->data->{$key} = $value; | ||
} | ||
|
||
sub DELETE { | ||
my ( $this, $key ) = @_; | ||
ASSERT( defined $this->{cfg} ) if DEBUG; | ||
$this->trace("DELETE{$key}"); | ||
delete $this->{cfg}->data->{$key}; | ||
} | ||
|
||
sub CLEAR { | ||
my ($this) = @_; | ||
ASSERT( defined $this->{cfg} ) if DEBUG; | ||
$this->{cfg}->clear_data; | ||
} | ||
|
||
sub EXISTS { | ||
my ( $this, $key ) = @_; | ||
ASSERT( defined $this->{cfg} ) if DEBUG; | ||
return exists $this->{cfg}->data->{$key}; | ||
} | ||
|
||
sub FIRSTKEY { | ||
my ($this) = @_; | ||
ASSERT( defined $this->{cfg} ) if DEBUG; | ||
my $_ignore = keys %{ $this->{cfg}->data }; | ||
return each %{ $this->{cfg}->data }; | ||
} | ||
|
||
sub NEXTKEY { | ||
my ($this) = @_; | ||
ASSERT( defined $this->{cfg} ) if DEBUG; | ||
return each %{ $this->{cfg}->data }; | ||
} | ||
|
||
sub SCALAR { | ||
my ($this) = @_; | ||
ASSERT( defined $this->{cfg} ) if DEBUG; | ||
return scalar %{ $this->{cfg}->data }; | ||
} | ||
|
||
sub UNTIE { | ||
my ($this) = @_; | ||
|
||
undef $this->{cfg}; | ||
} | ||
|
||
sub setTrace { | ||
my $this = shift; | ||
$this->{_trace} = shift; | ||
} | ||
|
||
sub trace { | ||
my $this = shift; | ||
if ( $this->{_trace} ) { | ||
say STDERR @_; | ||
} | ||
} | ||
|
||
package Foswiki::Extension::Sample::Config; | ||
|
||
use Moo::Role; | ||
|
||
around assignGLOB => sub { | ||
my $orig = shift; | ||
my $this = shift; | ||
|
||
say STDERR "This is a replacement assignGLOB method."; | ||
|
||
tie %Foswiki::cfg, 'Foswiki::Extension::Sample::TiedConfig', $this; | ||
}; | ||
|
||
1; | ||
__END__ | ||
Foswiki - The Free and Open Source Wiki, http://foswiki.org/ | ||
Copyright (C) 2016 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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters