StagedFileSet
in package
Publishes a set of files (sitemaps, buckets) atomically into one directory.
IMPORTANT: writing straight to the final names, or deleting old files first, leaves the site without a sitemap when a later file fails, and a crawler reading during a write gets truncated XML. This class avoids both:
- stage() writes the content to a random
.tmpnext to the target (same filesystem, sorenameis atomic). Content is not kept in memory; each bucket is written at once. - commit() renames the temporary files to the final names only after all files were
written, then deletes old files matching
$stalePatternthat the new generation did not overwrite. - abort() (and a failure in stage()) deletes only its own temporary files; the old set stays.
A reader thus sees, for every name, either the whole old or the whole new version.
Table of Contents
Properties
- $dir : string
- $finished : bool
- $staged : array<string, string>
- $stalePattern : string|null
Methods
- __construct() : mixed
- abort() : void
- commit() : array<int, string>
- reserve() : string
- Reserves a temporary file for `$fileName` and returns its path, for content streamed into the file gradually.
- stage() : void
Properties
$dir read-only
private
string
$dir
$finished
private
bool
$finished
= false
$staged
private
array<string, string>
$staged
= []
final path mapped to temporary path
$stalePattern read-only
private
string|null
$stalePattern
= null
Methods
__construct()
public
__construct(string $dir[, string|null $stalePattern = null ]) : mixed
Parameters
- $dir : string
-
target directory (must exist)
- $stalePattern : string|null = null
-
globpattern of old-generation names in$dir(e.g.sitemap-blog-*.xml) that are deleted after commit unless the new generation overwrote them;nulldeletes nothing
abort()
public
abort() : void
commit()
public
commit() : array<int, string>
Tags
Return values
array<int, string> —final paths of the new generation
reserve()
Reserves a temporary file for `$fileName` and returns its path, for content streamed into the file gradually.
public
reserve(string $fileName) : string
The file belongs to the set as after stage(): commit renames it, abort deletes it. The caller controls the write and must call abort() when it fails.
Parameters
- $fileName : string
Tags
Return values
stringstage()
public
stage(string $fileName, string $contents) : void
Parameters
- $fileName : string
- $contents : string