Roles

$envless->roles->iterate()

Stream roles one row at a time

php
/** @return \Generator<int, RoleResource, mixed, void> */
public function iterate(
    ?int $limit = null,
    ?int $offset = null,
    ?string $search = null,
    ?string $sortBy = null,
    int|float|null $timeout = null,
): \Generator

Yields roles individually, fetching the next page only once the current one is drained. Role sets are small, so this exists mainly for symmetry with the other collections and for the ability to stop as soon as you find the row you wanted.

It returns a Generator, so put the call straight into a foreach loop. No request is issued until the first iteration, and breaking out of the loop leaves any remaining pages unfetched.

Example

php
use Envless\Envless;

$envless = new Envless();

foreach ($envless->roles->iterate() as $role) {
    if (!$role['isSystem']) {
        echo $role['name'], ' ', count($role['permissions']), PHP_EOL;
    }
}

Parameters

ParameterTypeRequiredDescription
$limitintNoPage size fetched behind the scenes, clamped to 100.
$offsetintNoRow to start streaming from.
$searchstringNoCase insensitive substring match against role name and description.
$sortBystringNoOrder within each group: name (the default) is A to Z, updated and created are newest first. System roles always come first.
$timeoutint|floatNoSeconds each attempt of this call may take, the response body included, before it throws Envless\Exception\NetworkException with isTimeout() true. Overrides the client's timeout for this call, and 0 turns the limit off.

Returns

A Generator that yields RoleResource values until the pages run out.

Reads the roles collection. list returns one page as an Envless\Result\Page with items and pagination, listAll collects every page into an array, and iterate returns a Generator that yields one item at a time, fetching the next page only when the loop gets there.

Worth knowing

  • The offset advances by the number of rows actually received, so a role created mid walk can shift the window.
  • System roles are yielded before custom roles, so a loop that breaks on the first row always sees a system role.

Required scopes

roles:read

Underlying endpoint

GET /roles

Request and response schema, every error code, and a live playground.

In TypeScript

envless.roles.iterate()

The same method in @goenvless/sdk, with the same request, retries and errors.