| title | API Reference |
|---|---|
| nav_order | 3 |
| permalink | /api-reference/ |
{: .no_toc }
Every public class lives in the FluentPhp namespace.
{: .fs-6 .fw-300 }
Table of contents
{: .text-delta } - TOC {:toc}A bundle owns the messages for one locale. Add resources to it, optionally register functions, then format messages by id.
public function __construct(string $langCode)Create a bundle for a locale (a BCP-47 language identifier such as en,
en-GB, or pt-BR).
- Throws
FluentPhp\Exceptionif the language identifier is invalid.
public function addResource(string|FluentResource $resource): voidAdd a parsed resource to the bundle. Accepts either a
FluentResource object or a raw FTL string.
{: .note }
String arguments are parsed inline and are not cached. To reuse a parse, pass a
FluentResourcefromResourceCacheorFluentResource::fromFile().
- Throws
FluentPhp\ParserExceptionif a string argument contains syntax errors. - Throws
FluentPhp\Exceptionif any entry in the resource duplicates an existing one.
public function addFunction(string $name, callable $callable): voidRegister a PHP callable as a Fluent function, callable from FTL as
{ NAME($arg) }.
- Throws
FluentPhp\Exceptionif a function with that name is already registered.
public function formatPattern(string $messageId, array $parameters): stringFormat a message by id, substituting $parameters into its placeables.
See [Values]({{ '/guide/#values' | relative_url }}) for accepted parameter types.
- Throws
FluentPhp\Exceptionif the message is not found, has no value, or an argument type is unsupported. - Throws
FluentPhp\ResolverExceptionif the pattern references undefined variables or functions.
public function hasMessage(string $messageId): boolReturn whether the bundle contains a message with the given id.
A parsed FTL resource that can be added to one or more bundles. Both
constructors bypass the process cache — use
ResourceCache when you want caching.
final class, not instantiable directly; use the static factories.
public static function fromString(string $source): selfParse an FTL source string without using the process cache.
- Throws
FluentPhp\ParserExceptionif the FTL source contains syntax errors.
public static function fromFile(string $path): selfRead and parse an FTL file without using the process cache.
- Throws
FluentPhp\ParserExceptionif the FTL file contains syntax errors. - Throws
FluentPhp\Exceptionif the file cannot be read.
A cache of parsed FluentResource objects, reused across requests. All methods
are static; the class is not meant to be instantiated.
The cache is process-local. In multi-worker runtimes, each worker has its own
cache, and clear() / invalidateFile() affect only the worker process that
handles that call. They do not broadcast to the rest of a PHP-FPM, Swoole,
RoadRunner, or FrankenPHP worker pool.
For how caching works, configuration, validation modes, and statistics, see the [Cache page]({{ '/cache/' | relative_url }}).
public static function fromString(string $source): FluentResourceReturn a parsed resource cached by source-content identity (a 128-bit content hash).
- Throws
FluentPhp\ParserExceptionif the FTL source contains syntax errors. - Throws
FluentPhp\CacheExceptionif the cache is unavailable.
public static function fromFile(string $path): FluentResourceReturn a parsed resource cached by canonical file path. By default, file changes
are detected using path, size, and modification time; set
fluent.cache_file_validation=checksum to hash the file before reusing a cached
parse.
- Throws
FluentPhp\ParserExceptionif the FTL file contains syntax errors. - Throws
FluentPhp\Exceptionif the file cannot be read. - Throws
FluentPhp\CacheExceptionif the cache is unavailable.
public static function invalidateFile(string $path): boolInvalidate a cached file entry in the current worker process. The next
fromFile() call in that worker reloads the file. Existing FluentResource
objects and bundles remain valid. Returns whether an entry was removed.
{: .warning }
In multi-worker runtimes this does not invalidate other workers. Use validating cache modes (
metadata/checksum) or reload/restart the worker pool when every worker must see new translation files.
public static function clear(): voidRemove all entries from the current worker process cache. Existing
FluentResource objects and bundles remain valid.
{: .warning }
In multi-worker runtimes this clears only the worker handling the call.
- Throws
FluentPhp\CacheExceptionif the cache is unavailable.
public static function getStats(): arrayReturn cache statistics for the current process:
| Key | Meaning |
|---|---|
entries |
Number of cached resources (string + file). |
cache_weight |
Approximate total weight currently held. |
hits |
Total cache hits. |
metadata_hits |
File hits validated by path, modification time, and size. |
content_hits |
File hits validated by content hash. |
misses |
Lookups that required a parse. |
loads |
Resources parsed and inserted. |
errors |
Parse or I/O errors recorded. |
evictions |
Entries removed by LRU eviction. |
skipped_oversize |
Parses returned but not cached (over max_entry_size). |
max_weight |
Configured weight cap. |
pid |
Process id that owns this cache. |
- Throws
FluentPhp\CacheExceptionif the cache is unavailable.
All extension-specific exceptions extend FluentPhp\Exception, which extends
PHP's \Exception.
Base class for every error this extension raises. Catch it to handle any FluentPHP failure.
Invalid FTL syntax.
/** @return array<array{line: int, col: int, source: string}> */
public function getErrors(): arraygetErrors() returns one entry per syntax error, each with the line, column,
and a source snippet.
Formatting failed because a message references missing variables, unknown functions, or other resolver errors.
/** @return array<string> */
public function getErrors(): arraygetErrors() returns the resolver error messages.
The process cache is unavailable (for example, an internal lock was poisoned).