Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Non filesystem store #540

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
82 changes: 53 additions & 29 deletions lib/Statocles/Store.pm
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ has _realpath => (
is => 'ro',
isa => Path,
lazy => 1,
default => sub { $_[0]->path->realpath },
default => sub { $_[0]->_resolve_path( $_[0]->path ) },
);

# If true, we've already checked if this store's path exists. We need to
Expand Down Expand Up @@ -121,24 +121,28 @@ objects|Statocles::Document> inside. Returns an arrayref of document objects.

sub read_documents {
my ( $self ) = @_;
$self->_check_exists;

my $root_path = $self->path;

my @docs;
my $iter = $root_path->iterator( { recurse => 1, follow_symlinks => 1 } );
my $iter = $self->find_files( include_documents => 1 );

while ( my $path = $iter->() ) {
next unless $path->is_file;
next unless $self->_is_owned_path( $path );
next unless $self->is_document( $path );
my $rel_path = rootdir->child( $path->relative( $root_path ) );
push @docs, $self->read_document( $rel_path );
push @docs, $self->read_document( $path );
}
return \@docs;
}

sub _resolve_path {
my ( $self, $path ) = @_;
return $path->realpath;
}

sub _is_owned_path {
my ( $self, $path ) = @_;
my $self_path = $self->_realpath;
$path = $path->realpath;
$path = $self->_resolve_path( $path );
my $dir = $path->parent;
for my $store_path ( keys %FILE_STORES ) {
# This is us!
Expand All @@ -164,7 +168,7 @@ sub read_document {
site->log->debug( "Read document: " . $path );
my $full_path = $self->path->child( $path );
my $relative_path = $full_path->relative( cwd );
my %doc = $self->parse_frontmatter( $relative_path, $full_path->slurp_utf8 );
my %doc = $self->parse_frontmatter( $relative_path, $self->read_file( $path ) );
my $class = $doc{class} ? use_module( delete $doc{class} ) : 'Statocles::Document';
my $obj = eval { $class->new( %doc, path => $path, store => $self ) };
if ( $@ ) {
Expand Down Expand Up @@ -257,7 +261,7 @@ sub write_document {
chomp $header;

my $full_path = $self->path->child( $path );
$full_path->touchpath->spew_utf8( join "\n", $header, '---', $content );
$self->write_file( $path, join "\n", $header, '---', $content );

if ( defined wantarray ) {
derp "Statocles::Store->write_document returning a value is deprecated and will be removed in v1.0. Use Statocles::Store->path to find the full path to the document.";
Expand Down Expand Up @@ -298,7 +302,7 @@ sub is_document {

my $content = $store->read_file( $path )

Read the file from the given C<path>.
Read the file from the given C<path> as UTF8 encoded data.

=cut

Expand All @@ -308,6 +312,20 @@ sub read_file {
return $self->path->child( $path )->slurp_utf8;
}

=method read_file_raw

my $content = $store->read_file_raw( $path )

Read the file from the given C<path> as raw data.

=cut

sub read_file_raw {
my ( $self, $path ) = @_;
site->log->debug( "Read file: " . $path );
return $self->path->child( $path )->slurp_raw;
}

=method has_file

my $bool = $store->has_file( $path )
Expand Down Expand Up @@ -336,7 +354,14 @@ object or undef if no files remain. It is used by L<find_files>.

sub files {
my ( $self ) = @_;
return $self->path->iterator({ recurse => 1 });
my $iter = $self->path->iterator({ recurse => 1, follow_symlinks => 1 });

sub {
while( my $path = $iter->() ) {
return $path if $path->is_file;
}
return;
}
}


Expand Down Expand Up @@ -366,7 +391,6 @@ sub find_files {
return sub {
my $path;
while ( $path = $iter->() ) {
next if $path->is_dir;
next if !$self->_is_owned_path( $path );
next if !$opt{include_documents} && $self->is_document( $path );
last;
Expand All @@ -376,31 +400,31 @@ sub find_files {
};
}

=method open_file
=method write_file

my $fh = $store->open_file( $path )
$store->write_file( $path, $content );

Open the file with the given path. Returns a filehandle.
Write the given C<content> to the given C<path>. This is mostly used to write
out L<page objects|Statocles::Page>.

The filehandle opened is using raw bytes, not UTF-8 characters.
C<content> may be a:

=cut
=over

sub open_file {
my ( $self, $path ) = @_;
return $self->path->child( $path )->openr_raw;
}
=item *

=method write_file
a simple string, which will be written using UTF-8 characters.

$store->write_file( $path, $content );
=item *

Write the given C<content> to the given C<path>. This is mostly used to write
out L<page objects|Statocles::Page>.
a L<Path::Tiny> object whose C<copy> method will be used to
write it;

=item *

a filehandle which will be read from with no special encoding.

C<content> may be a simple string or a filehandle. If given a string, will
write the string using UTF-8 characters. If given a filehandle, will write out
the raw bytes read from it with no special encoding.
=back

=cut

Expand Down
Loading