-
Notifications
You must be signed in to change notification settings - Fork 0
Segment
InitPHP\Auth\Segment is the facade you reach for in application code.
It does three things:
-
Resolves an adapter from a class name or one of the
ADAPTER_*constants, so callers do not have to wire up the concrete adapter themselves. -
Implements
AdapterInterfaceitself, so the facade is a drop-in replacement wherever the interface is expected. -
Forwards the six contract methods to the underlying adapter and
exposes an
adapter()escape hatch for implementation-specific calls.
The typed factories are the v2-recommended entry points:
use InitPHP\Auth\Segment;
Segment::session('auth'); // SessionAdapter
Segment::cookie('auth', ['salt' => $secret]); // CookieAdapter
Segment::custom('auth', App\Auth\DatabaseAdapter::class); // custom adapterThe legacy v1 factory accepts an int|string adapter and is kept for
backwards compatibility:
Segment::create('auth', Segment::ADAPTER_SESSION);
Segment::create('auth', Segment::ADAPTER_COOKIE, ['salt' => $secret]);
Segment::create('auth', App\Auth\DatabaseAdapter::class);
new Segment('auth', Segment::ADAPTER_SESSION);
new Segment('auth', App\Auth\DatabaseAdapter::class, ['pdo' => $pdo]);| Constant | Value | Equivalent factory |
|---|---|---|
Segment::ADAPTER_SESSION |
0 |
Segment::session($name, $options) |
Segment::ADAPTER_COOKIE |
1 |
Segment::cookie($name, $options) |
Segment implements AdapterInterface, so each contract method has a
dedicated proxy:
$segment->get(string $key, mixed $default = null): mixed
$segment->set(string $key, mixed $value): AdapterInterface
$segment->collective(array $data): AdapterInterface
$segment->has(string $key): bool
$segment->remove(string ...$keys): AdapterInterface
$segment->destroy(): boolset(), collective(), and remove() return $this (the Segment),
not the underlying adapter, so chained calls keep flowing through the
facade:
$segment->set('user_id', 42)
->set('role', 'editor')
->remove('legacy_flag');For implementation-specific methods that are not part of
AdapterInterface (e.g. a custom adapter that exposes
refreshToken(string $reason): string), use either of these:
// 1. The explicit escape hatch — preferred for static analysis.
$segment->adapter(); // returns AdapterInterface
$segment->adapter()->refreshToken('exp');
// 2. The __call magic delegation — handy for ad-hoc calls.
$segment->refreshToken('exp');__call() forwards verbatim. A method that does not exist on the
adapter raises the standard PHP "Call to undefined method" Error:
$segment->totallyMadeUp();
// Error: Call to undefined method ...::totallyMadeUp()Segment::custom() and the generic constructor accept a FQCN string.
The resolver applies these checks in order:
- The class must exist (
class_exists()). - The class must extend
AbstractAdapter. - It is instantiated with
new $class($name, $options).
Failure modes all raise InvalidArgumentException with a message that
names the failing case:
new Segment('auth', 999);
// InvalidArgumentException:
// Unknown adapter constant: 999. Expected ADAPTER_SESSION (0) or ADAPTER_COOKIE (1).
new Segment('auth', 'App\\No\\Such\\Class');
// InvalidArgumentException: Adapter class "App\No\Such\Class" does not exist.
new Segment('auth', stdClass::class);
// InvalidArgumentException: Adapter class "stdClass" must extend InitPHP\Auth\AbstractAdapter.
new Segment('auth', 1.5);
// InvalidArgumentException:
// $adapter must be one of the ADAPTER_* constants or a class name that extends InitPHP\Auth\AbstractAdapter.See Exceptions for the full list.
When Segment instantiates your custom adapter it calls:
new YourAdapter($name, $options);If your adapter needs more constructor arguments (a PDO handle, a
Redis client), pass them through $options and unpack them inside
your constructor. See Custom Adapters for a worked
example.
$segment = Segment::custom('auth', App\Auth\DatabaseAdapter::class, [
'pdo' => $pdo,
]);If your adapter does not follow the (string $name, array $options)
convention, do not use Segment::custom(). Instantiate it directly and
hand-build the segment:
$adapter = new App\Auth\WeirdAdapter($somethingElse);
// Wrap it manually — Segment does not currently accept a pre-built adapter
// in its public surface, so most callers will just use $adapter directly.The $options array is passed straight to the adapter constructor.
The package does not split, filter, or merge it. Each adapter
documents its own option matrix:
- Session Adapter → Constructor options
- Cookie Adapter → Constructor options
- Custom Adapters → Constructor signature
-
Calling
$segment->adapter()->set(...). That works but breaks the chain —$segment->set(...)is the same thing and returns the facade, not the inner adapter, so subsequent calls go through the same proxy. -
Calling
new Segmentwith a class string that does not extendAbstractAdapter. The error message tells you exactly what to fix — extendAbstractAdapter, not just implement the interface, when you wantSegment::custom()compatibility. -
Passing a relative class name. Always pass a FQCN (e.g.
App\Auth\DatabaseAdapter::class). The resolver does not search namespaces.
- Adapter Interface — the contract behind the facade.
- Custom Adapters — implementing your own.
- API Reference — full method signatures.
initphp/auth · MIT License · part of the InitPHP family
Source · Issues · Discussions · Packagist · Contributing · Security Policy
Getting Started
Core Types
Adapters
Reference
Recipes
Migration & Help