Class-DBI-Factory
view release on metacpan or search on metacpan
lib/Class/DBI/Factory/Config.pm view on Meta::CPAN
$self->file($self->config_files);
$self->extra_prep;
$self->file( $_ ) for @{ $self->get('include_file') };
return $self;
}
=head2 config_files()
Accessor for the list of config files that will be read. This list can't be set after construction, but you can always call file() to read more files in.
This method will always return the list of files that was supposed to be read on construction. Call files() if you would like the list of files (successfully) read during the lifetime of the config object.
=head2 extra_prep()
Placeholder for any configuration-loading steps you want to include.
This method is called after config files have been read, so settings here will override defaults.
=cut
sub config_files {
my $self = shift;
return unless $self->{_config_files};
return @{ $self->{_config_files} };
}
sub extra_prep { }
=head2 file()
$config->file('/path/to/file', '/path/to/otherfile');
Reads configuration files from the supplied addresses, and stores their addresses and modification dates in case of a later refresh() or rebuild().
=cut
sub file {
my ($self, @files) = @_;
my $time = scalar time;
for (@files) {
next unless _readable($_);
my $mdate = _mdate($_);
push @{ $self->{_files} } , $_;
$self->{_file_read}->{$_} = $mdate;
$self->ac->file($_) || next;
}
}
sub _readable {
my $self = shift;
my $f = ref ($self) ? shift : $self;
$f =~ s/\/+/\//g;
return $f if -e $f && -f _ && -r _;
return;
}
sub _mdate {
my $self = shift;
my $f = ref ($self) ? shift : $self;
$f =~ s/\/+/\//g;
my @stat = stat($f);
return $stat[9];
}
=head2 refresh()
$config->refresh();
$config->refresh('/path/to/file');
Checks the modification date for each of the configuration files that have been read: if any have changed since we read it, the whole configuration object is dropped and rebuilt.
By default this will revisit the whole set of read configuration files, but if you supply a list of files, refresh() will confine itself to looking at the intersection of your list and the list of files already read. Either way, configuration files a...
Note that if a configuration file is missing at startup it will not be looked for later: this only refreshes the files that were successfully read.
=head2 rebuild()
This will drop all configuration information and start again by re-reading all the configuration files. Any other changes your application has made, eg by setting values directly, will be lost.
=head2 files()
Returns a list in date order of all the configuration files successfully read during the lifetime of this object.
=cut
sub refresh {
my $self = shift;
my @files = @_ || $self->files;
return unless @files;
for (@files) {
next unless exists $self->{_file_read}->{$_};
next unless _readable($_);
next unless _mdate($_) > $self->{_file_read}->{$_};
return $self->rebuild;
}
return;
}
sub rebuild {
my $self = shift;
my @files = $self->files;
$self->{_files} = [];
$self->{_file_read} = {};
$self->_new_ac;
$self->file( @files );
$self->{_timestamp} = scalar time,
}
sub files {
return @{ shift->{_files} };
}
=head2 timestamp()
A possibly-useful read-only method that returns the epoch time at which this object read in its configuration files (ie when it was built, or last rebuilt).
=cut
sub timestamp {
return shift->{_timestamp};
}
( run in 1.002 second using v1.01-cache-2.11-cpan-39bf76dae61 )