CoolCMS API

StagedFileSet
in package

FinalYes

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:

  1. stage() writes the content to a random .tmp next to the target (same filesystem, so rename is atomic). Content is not kept in memory; each bucket is written at once.
  2. commit() renames the temporary files to the final names only after all files were written, then deletes old files matching $stalePattern that the new generation did not overwrite.
  3. 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

$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

glob pattern of old-generation names in $dir (e.g. sitemap-blog-*.xml) that are deleted after commit unless the new generation overwrote them; null deletes nothing

commit()

public commit() : array<int, string>
Tags
throws
RuntimeException

when a rename fails; what was already published stays (every name is whole)

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
throws
RuntimeException

when the name is invalid or the file cannot be created

Return values
string

stage()

public stage(string $fileName, string $contents) : void
Parameters
$fileName : string
$contents : string
Tags
throws
RuntimeException

when the write fails; the temporary files of the whole generation are removed

On this page

Search results