php-cooper

LiteralTag
in package

FinalYes

A consumer tag that takes **only a string literal written in the document**, registered under the `tags` option like any other:

Cooper::loadFile($path, ['tags' => [
    'php' => new LiteralTag(fn (string $code): mixed => run($code)),
]]);

!php("..."), !php('...') and !php("""...""") reach the function. Anything else written as the argument -- ${...}, @{...}, %{...}, !{...}, another tag, a string that interpolates, a number, a list -- makes the load fail before the function is called, naming the tag, the key and the file. So does the tag interpolated into a string ("a !php(\"x\") b"). A loop variable or a variable holding a literal is refused too: the rule is about what is written in the argument's place, not about what it turns out to hold.

It exists for a tag whose argument must never come from anywhere but the file -- a framework integration's tag that runs its argument as code, where a value from the environment, a resolver or another file's variable must not be able to become code. That is host-specific integration behaviour, not CASC's: a plain callable tag takes any argument, as it always has.

A literal tag that throws also fails the load naming the key and the file, since what it ran was written there.

Table of Contents

Properties

$fn  : callable(string): mixed

Methods

__construct()  : mixed
__invoke()  : mixed
Applies the tag. Only Cooper calls this, once it has checked the argument was a literal string.

Properties

$fn read-only

private callable(string): mixed $fn

Methods

__construct()

public __construct(callable(string): mixed $fn) : mixed
Parameters
$fn : callable(string): mixed

the tag's function, given the literal string; return the value, or throw

__invoke()

Applies the tag. Only Cooper calls this, once it has checked the argument was a literal string.

public __invoke(mixed $argument) : mixed
Parameters
$argument : mixed

the literal string

Tags
throws
InvalidArgumentException

when $argument is not a string

Return values
mixed —

what the function returns

On this page

Search results