phug/util
phug/util is a lightweight PHP utility library for the Phug template engine, providing shared helpers and low-level tools for parsing, rendering, and template handling. It simplifies common tasks and supports building Pug-based PHP applications.
phug/util is the low-level support library that sits underneath the Phug/Pug PHP templating stack. If you installed phug/phug or pug-php/pug, it is already in your vendor/ directory. Install it directly only when you are writing a Phug module, an extension, or a small library that wants the same building blocks.
composer require phug/util
Where to look first:
src/Partial/ — the trait library. This is the highest-value part of the package: small, single-responsibility traits (NameTrait, ValueTrait, PathTrait, LineTrait, OffsetTrait, SourceTrait, OptionTrait, ParentTrait, ChildrenTrait, AttributesTrait, …) that you can compose into your own classes instead of copy-pasting accessors.src/OptionInterface.php / src/Partial/OptionTrait.php — the canonical "options bag" pattern used everywhere in Phug. Implement this and your class behaves like every other configurable Phug object.src/ModuleInterface.php + src/AbstractModule.php — the plugin contract for extending Phug.src/Collection.php and src/OrderedList.php — array wrappers used throughout the ecosystem.src/SourceLocation.php — line/column/offset bookkeeping for anything that reports template errors.First use case: give a class a clean option bag so callers can pass ['foo' => 'bar'] and read it via $obj->getOption('foo', $default) without writing boilerplate. That single trait is the most reused piece of the package.
Option bag on your own classes. This is the pattern you will reach for daily. Implement OptionInterface and pull in OptionTrait, then merge caller options over defaults:
use Phug\Util\OptionInterface;
use Phug\Util\Partial\OptionTrait;
class Renderer implements OptionInterface
{
use OptionTrait;
public function __construct(array $options = [])
{
$this->setOptions(array_replace([
'pretty' => false,
'encoding' => 'UTF-8',
], $options));
}
}
$r = new Renderer(['pretty' => true]);
$r->getOption('pretty'); // true
Ordered collections instead of raw arrays. OrderedList gives you add, insert, remove, indexOf, and predictable iteration order, which is exactly what you need for ordered things like middleware, filters, or event listeners. Reach for Collection when you want keyed access with get/set/has/remove semantics without sprinkling array_key_exists through your code.
Source location for diagnostics. Any component that can fail on user input (a parser, a compiler, a formatter) should attach a SourceLocation rather than just a string message. Consumers can then render path:line:column consistently with Phug's own error output, and IDE/editor integrations can jump to the right spot.
Module pattern for extensibility. If you are extending Phug, subclass AbstractModule and declare what events or options you care about. The module is then registered on the Phug engine and receives the shared container/option bag, so your code sees the same configuration as the host. Do not reach into the engine's internals; use the accessors the module interface exposes.
Profiler for hot paths. Profiler wraps start/end style timing around named sections and pairs well with a debug flag in your options. Wire it behind getOption('debug') and you get cheap, opt-in instrumentation for template compilation without pulling in a heavier profiler.
Compose traits, do not inherit deeply. The Partial traits are designed to be mixed into classes of different shapes. A small value object with NameTrait + ValueTrait + SourceTrait is idiomatic here; a three-level class hierarchy is not.
Sandbox for user-supplied callables. SandBox is the escape hatch for invoking closures in a controlled scope with access to a defined set of variables — useful when you are evaluating user-provided expressions and want to avoid leaking $this or local state.
phug/phug and pug-php/pug consume it. Read src/ first; do not expect a README walkthrough.phug/util directly, pin a range in composer.json and run your test suite on upgrade rather than floating on ^.OptionTrait, SourceTrait, and friends can define overlapping properties/methods if you stack too many. Resolve with insteadof / as, or implement the specific accessor yourself. Do not blindly use a dozen traits.setOptions / mergeOptions will overwrite top-level keys, not deep-merge nested arrays. If you need deep merging you must do it explicitly before passing options in — a common source of "my default was ignored" bugs.array_map, array_filter, and count() behave differently than on a plain array, and strict comparisons (===) will fail between a Collection and its array equivalent. Call toArray() (or iterate) before handing data to third-party code.path, line, offset, and the original source string at the point where you know them, or downstream error messages will be unknown:0.Phug\Util\Exception\* carries location context; catching the base runtime exception and dumping getMessage() loses that context. Inspect the location object, and enable Xdebug's develop mode if you want readable stack traces through the trait-heavy call chains.OptionInterface (safe), composing Partial traits (mostly safe), subclassing AbstractModule (couples you to Phug's event naming), extending Collection/OrderedList (watch for method additions that shadow yours). Prefer the lowest-risk option that solves the problem.How can I help you explore Laravel packages today?