laika-core

Relays & the Container

How a Relay Resolves

A relay is a class in the Laika\Service namespace that extends Laika\Relay\Relay. It has no logic of its own. getRelayAccessor() names a container key, and every static call is forwarded to the instance the container holds for that key:

use Laika\Service\Url;

Url::base();          // same as: $registry->make('url')->base()

The Relay base class adds a few static methods to every relay:

Method Does
X::relayRoot() Returns the underlying instance, when you need the real typed object
X::swap(object $instance) Replaces the bound instance, typically in tests
X::clearResolvedInstance() Forgets the cached instance, so the next call builds a fresh one

Singleton or Per-Use

Most bindings are singletons, so one instance serves the whole request. Two are bound per resolution instead:

Sharing either of those would hand every caller the same half-used object.

Note: singletons keep request state. Request parses the input once, Client caches the IP and user agent, and Url reads $_SERVER once. In a long-running worker, reset them between jobs, for example with Visitor::refresh() or X::clearResolvedInstance().

Relay List

Relay (Laika\Service\…) Key Class Lifetime Page
Activity activity Laika\Core\Log\Activity singleton Data
AppKey app.key Laika\Core\App\Key singleton Config
Asset template.asset Laika\Core\Template\Asset singleton Templates
Config config Laika\Core\Helper\Config singleton Config
Context template.context Laika\Core\Template\Context singleton Templates
Cookie cookie Laika\Core\Helper\Cookie singleton URL & Client
CORS cors Laika\Core\Http\CORS singleton HTTP
CSRF csrf Laika\Core\Http\CSRF singleton HTTP
Date date Laika\Core\Helper\Date singleton Utilities
Directory directory Laika\Core\Helper\Directory singleton Files
File file Laika\Core\Helper\File singleton Files
Hook hook Laika\Core\Helper\Hook singleton Config
Icon icon Laika\Core\Generator\Icon singleton Templates
Image image Laika\Core\Helper\Image per use Files
Infra infra Laika\Core\App\Infra singleton Config
Init init Laika\Core\Helper\Init singleton Config
IP ip Laika\Core\IP\IP singleton URL & Client
Local local Laika\Core\Helper\Local singleton Config
Math math Laika\Core\Helper\Math singleton Utilities
Meta template.meta Laika\Core\Template\Meta singleton Templates
MimeType mime Laika\Core\Helper\MimeType singleton Files
Nav nav Laika\Core\Nav\Builder singleton Templates
Option option Laika\Core\Model\OptionModel singleton Data
Page page Laika\Core\Helper\Page singleton URL & Client
PhpMetadataParser php.metadata.parser Laika\Core\Helper\PhpMetadataParser singleton Utilities
Redirect redirect Laika\Core\Http\Redirect singleton HTTP
Regex regex Laika\Core\Regex\Regex singleton Security
Request request Laika\Core\Http\Request singleton HTTP
Resource resource Laika\Core\App\Resource singleton Config
Response response Laika\Core\Http\Response singleton HTTP
Token token Laika\Core\Generator\Token singleton Security
Uid uid Laika\Core\Generator\Uid singleton Security
Unique unique Laika\Core\Generator\Unique singleton Security
Upload upload Laika\Core\Helper\Upload per use Files
Url url Laika\Core\Helper\Url singleton URL & Client
Vault vault Laika\Core\Helper\Vault singleton Security
Visitor visitor Laika\Core\Helper\Client singleton URL & Client

Two relays don’t share their class’s name: Visitor fronts Client, and AppKey fronts App\Key.

Using Classes Directly

A relay only forwards to a shared instance, so you can construct any class yourself when you want your own copy:

use Laika\Core\Http\Request;
use Laika\Core\Sanitizer\NullSanitizer;

$raw = new Request(new NullSanitizer()); // a request that doesn't HTML-encode input

Some classes are static-only: CORS, Hook, Config, MimeType, Resource, Asset, Meta, Context, Icon and Uid. Calling them through the relay or on the class directly does the same thing, because the state lives in static properties.

Registering Your Own

Applications add bindings through relay providers in lf-app/Relay/. Every RelayProvider found there is registered after the package providers, so an application can override a core binding. See the laika-relay README for singleton(), bind(), instance() and auto-wiring.