Class: AppContext
Full name: Azera\AppContext
Application context and dependency injection container.
This class is a singleton that manages the application's services, including request-scoped services, critical services, and the service container.
Public methods
__construct() ·
public function __construct(): mixed
Return value
- Type:
mixed
instance() ·
public static function instance(): static
Get/create shared singleton instance
Return value
- Type:
static
setInstance() ·
public static function setInstance(Azera\AppContext $instance): void
Set the shared singleton instance (e.g. for testing or multi-context scenarios).
Parameters
| Name | Type | Default | Description |
|---|---|---|---|
$instance |
AppContext | - |
Return value
- Type:
void
reset() ·
public static function reset(): void
Drop the shared singleton instance.
Call in test setUp()/tearDown() (or between multi-context scenarios) to
guarantee each test starts from a pristine context, instead of relying on
a previous test having called setInstance(). After this, the next
instance() call lazily builds a fresh context.
Return value
- Type:
void
request() ·
public function request(): Azera\Http\Request
Get the HttpRequest instance, resolved through the DI container.
Return value
- Type: Request
- Description: The HttpRequest instance.
view() ·
public function view(): Azera\Core\ViewEngine
Get the active view engine instance.
Resolution is fully delegated to the DI container: the default definition
builds a ClarityEngine, an app may register its own factory via
set(ViewEngine::class, $factory) (deferred build, resolved on first
use) or an instance via setView() (e.g. test doubles). view() and
get(ViewEngine::class) always return the same instance — one resolution
path, memoized by the container.
Boot code must NOT call this method (or get(ViewEngine::class)) — that is what triggers the engine build and its class autoload cost.
Return value
- Type: ViewEngine
- Description: The active view engine instance.
setView() ·
public function setView(Azera\Core\ViewEngine $engine): static
Replace the active view engine instance (e.g. swap in a specific engine or a test double at bootstrap). Sugar over set(): the instance is bound as both the container definition and the memoized instance.
To register a deferred factory instead, use
set(ViewEngine::class, $factory) — registering a callable unsets the
cached instance, so the factory applies on the next resolution.
Parameters
| Name | Type | Default | Description |
|---|---|---|---|
$engine |
ViewEngine | - | The engine to use from this point on. |
Return value
- Type:
static
translator() ·
public function translator(): Azera\Localization\Translator
Get the application-wide translation service.
Engine-independent: the same instance serves controllers, CLI tasks, validators and templates. Configure it once in bootstrap and, for template use, bind it to the active view engine:
$ctx->translator()->configure([
'locale' => 'de_DE',
'loader' => new TranslationLoaderBridge(
new \Clarity\Localization\FileTranslationLoader(__DIR__ . '/locales')
),
]);
$ctx->translator()->bind($ctx->view());
The service is request-scoped: clearRequestScope() resets its locale
and domain scopes after every request in a resident worker.
Return value
- Type: Translator
intl() ·
public function intl(): Azera\Localization\Intl
Get the application-wide locale-aware formatting service.
Engine-independent, like translator(), and it shares the
translator's locale stack: {% with_locale %}, $translator->withLocale()
and $intl->withLocale() all move the same stack, so formatting and
translations cannot disagree.
$ctx->intl()->configure(['timezone' => 'Europe/Berlin']);
$ctx->intl()->formatCurrency(1234.5, 'EUR'); // "1.234,50 €" in de_DE
$ctx->intl()->bind($ctx->view()); // format_* filters in templates
Every method has a native fallback when ext-intl is absent, so output
degrades (fewer locale conventions) but never fails.
Return value
- Type: Intl
cookies() ·
public function cookies(): Azera\Http\Cookies
Get the Cookies instance, resolved through the DI container.
Return value
- Type: Cookies
- Description: The Cookies instance.
heap() ·
public function heap(): Azera\Orm\Heap
Get the request-scoped ORM Heap (identity map).
One heap per request: the same DB row read twice yields the same
entity object, and the EntityManager diffs against the node
snapshot instead of scanning every constructed instance. The
heap is created lazily, registered in the container, and wiped by
clearRequestScope() via its RequestScoped hook (non-negotiable
in persistent workers — a leaking heap would serve stale entities
across requests/tenants).
Return value
- Type: Heap
entityManager() ·
public function entityManager(): Azera\Orm\EntityManager
Get the request-scoped EntityManager (identity map + write pipeline).
Shares the request's Heap with everything else — FastHydrator reads,
Model::save() and direct EM calls all see the same identity space.
persist() schedules, flush() executes in one transaction. Wiped by
clearRequestScope() like the heap.
Return value
- Type: EntityManager
dbManager() ·
public function dbManager(): Azera\Db\DatabaseManager
Get the DatabaseManager instance, resolved through the DI container.
Return value
- Type: DatabaseManager
router() ·
public function router(): Azera\Core\Router
Get the Router instance, resolved through the DI container.
Return value
- Type: Router
- Description: The Router instance.
dispatcher() ·
public function dispatcher(): Azera\Core\Dispatcher
Get the Dispatcher instance, resolved through the DI container.
Return value
- Type: Dispatcher
- Description: The Dispatcher instance.
kernel() ·
public function kernel(): Azera\Core\Kernel
Get the HTTP front controller — request in, response out.
The counterpart of HttpKernel::handle(), Application::handle() and
Server::run() in the frameworks that have one, and the object a transport
should call: it owns the 404 and 405 decisions, the error boundary and the
queued-cookie flush, none of which belong in each entry point separately.
Built on demand and cached, because it holds no request state of its own — it resolves the router, the dispatcher and the handlers from this context on every call, so a request-scoped reset is visible to it without a rebuild.
Return value
- Type: Kernel
- Description: The front controller.
logger() ·
public function logger(): Psr\Log\LoggerInterface
Get the logger instance. Returns a NullLogger if no logger
has been registered, so calling code can safely log without
null-checks. Register a real logger via set(LoggerInterface::class, ...).
Return value
- Type:
Psr\Log\LoggerInterface
events() ·
public function events(): Psr\EventDispatcher\EventDispatcherInterface
Get the event dispatcher. Returns a NullEventDispatcher if
none has been registered, so dispatch() is always safe. Register
a real dispatcher via set(EventDispatcherInterface::class, ...).
Return value
- Type:
Psr\EventDispatcher\EventDispatcherInterface
cache() ·
public function cache(): Psr\SimpleCache\CacheInterface
Get the cache instance. Returns a NullCache if none has been
registered (always reports a miss). Register a real cache via
set(CacheInterface::class, ...).
Return value
- Type:
Psr\SimpleCache\CacheInterface
queue() ·
public function queue(): Azera\Queue\QueueInterface
Get the queue instance.
Unlike the other subsystems, the queue has no null implementation
because silently dropping jobs would be dangerous. If no queue is
registered, this throws a LogicException with an install hint.
Register a queue via set(QueueInterface::class, ...).
Return value
- Type: QueueInterface
Throws
- LogicException If no queue is registered.
config() ·
public function config(): Azera\Config\Config
Get the configuration service. Lazily creates a Config
if none has been registered.
Return value
- Type: Config
pipeline() ·
public function pipeline(array $interceptors = []): Azera\Aop\Pipeline
Create a pipeline for explicit interceptor composition.
This is the no-proxy alternative to AOP attributes. It lets you wrap any callable with interceptors without generating proxy classes. The same interceptors that work with the proxy AOP also work here.
Example:
$result = $ctx->pipeline([new RetryInterceptor(3), new LogInterceptor($logger)])
->call(fn() => $service->chargeCard(100));
Parameters
| Name | Type | Default | Description |
|---|---|---|---|
$interceptors |
array | [] |
Return value
- Type: Pipeline
registerInterceptor() ·
public function registerInterceptor(string $adviceClass, Azera\Aop\InterceptorInterface|callable $interceptor): void
Register an interceptor for a specific advice type.
Once at least one interceptor is registered, the DI container
will proxy classes marked with Advised that have methods
carrying the corresponding advice attribute. Registering an interceptor is
the only thing that turns AOP on; there is no separate switch.
The interceptor may also be passed as a zero-argument factory (closure/invokable) returning an InterceptorInterface. Factories are resolved exactly once — when the ProxyFactory is first built — so boot can register lazy wiring (e.g. an interceptor depending on dbManager()) without constructing anything during bootstrap.
Parameters
| Name | Type | Default | Description |
|---|---|---|---|
$adviceClass |
string | - | The advice attribute class. |
$interceptor |
InterceptorInterface|callable | - | The interceptor to handle it (or a factory returning one). |
Return value
- Type:
void
aopCacheDir() ·
public function aopCacheDir(): string
Get the effective proxy cache directory.
Return value
- Type:
string - Description: The effective proxy cache directory.
setAopCacheDir() ·
public function setAopCacheDir(string|null $dir = null): void
Set the AOP proxy cache directory.
Pass null to use the default temporary directory with OPcache-cached proxies.
Parameters
| Name | Type | Default | Description |
|---|---|---|---|
$dir |
string|null | null |
Return value
- Type:
void
session() ·
public function session(): Azera\Http\Session|null
Get the Session instance, or null until setSession() binds one.
Return value
- Type: Session|
null
setSession() ·
public function setSession(Azera\Http\Session $session): void
Set the Session instance.
Parameters
| Name | Type | Default | Description |
|---|---|---|---|
$session |
Session | - | The Session instance to set in the context. |
Return value
- Type:
void
flash() ·
public function flash(): Azera\Http\Flash
Get the session-backed one-request message bag.
The bag ages itself the first time a session is available, so its position
relative to SessionMiddleware does not matter:
$this->flash()->success('Saved.'); // POST, then redirect
$messages = $this->flash()->consume(); // the page after the redirect
In a controller the accessor is Controller::flash().
Without a session the bag falls back to memory, so the same calls work in a
CLI task or a unit test.
Return value
- Type: Flash
route() ·
public function route(): Azera\Core\ResolvedRoute|null
Get the current resolved route information.
Return value
- Type: ResolvedRoute|
null
setRoute() ·
public function setRoute(Azera\Core\ResolvedRoute $route): void
Set the current resolved route information.
Parameters
| Name | Type | Default | Description |
|---|---|---|---|
$route |
ResolvedRoute | - | The resolved route to set in the context. |
Return value
- Type:
void
clearRequestScope() ·
public function clearRequestScope(): void
Clear request-scoped state for persistent application servers.
Removes request, session, cookie, flash, and route instances, then calls
RequestScoped::resetState() on registered services. Instances and
infrastructure handles remain available; their request-specific state is reset.
Safe to call repeatedly.
Return value
- Type:
void
set() ·
public function set(string $id, callable|object|null $service = null): void
Register a service instance or lazy factory in the context.
Registered callables are treated as zero-argument factories. They are invoked on first resolution and their returned object is cached for subsequent lookups.
Parameters
| Name | Type | Default | Description |
|---|---|---|---|
$id |
string | - | The identifier for the service (usually the class name). |
$service |
callable|object|null | null |
Optional service instance or zero-argument factory to register. |
Return value
- Type:
void
has() ·
public function has(string $id): bool
Check if a service is registered in the context.
Parameters
| Name | Type | Default | Description |
|---|---|---|---|
$id |
string | - | The identifier of the service to check. |
Return value
- Type:
bool - Description: True if the service is registered, false otherwise.
get() ·
public function get(string $id): object
Get a service instance from the context.
If the service is registered as a callable, it will be invoked lazily once and the returned object will be cached. If the service is not registered but the identifier is a class name, it will attempt to auto-wire and instantiate it.
Parameters
| Name | Type | Default | Description |
|---|---|---|---|
$id |
string | - | The identifier of the service to retrieve. |
Return value
- Type:
object - Description: The service instance associated with the given identifier.
Throws
- RuntimeException If the service is not found and cannot be auto-wired.
tryGet() ·
public function tryGet(string $id): object|null
Try to get a service instance from the context.
If the service is registered as a callable, it will be invoked lazily once and the returned object will be cached. If the service is not registered but the identifier is a class name, it will attempt to auto-wire and instantiate it. Returns null if the service is not found, or if a registered factory currently resolves to null.
Parameters
| Name | Type | Default | Description |
|---|---|---|---|
$id |
string | - | The identifier of the service to retrieve. |
Return value
- Type:
object|null - Description: The service instance associated with the given identifier, or null if not found.
getOrNull() ·
public function getOrNull(string $id): object|null
Get a registered service instance if it exists, or null if it does not.
Registered factories are resolved lazily. This method does not attempt to auto-wire or instantiate classes that have not been registered explicitly.
Parameters
| Name | Type | Default | Description |
|---|---|---|---|
$id |
string | - | The identifier of the service to retrieve. |
Return value
- Type:
object|null - Description: The service instance associated with the given identifier, or null if not found.
resolved() ·
public function resolved(string $id): object|null
Get the service instance if it has already been created. Does not attempt to resolve or build the service.
Parameters
| Name | Type | Default | Description |
|---|---|---|---|
$id |
string | - | The identifier of the service to retrieve. |
Return value
- Type:
object|null - Description: The service instance if it exists, or null otherwise.