The Game Panel Design record
The Game Panel
RFC-0015 Proposed

Module system

Comparing
v1 to v2
Current
Read the document

Abstract

We I will add give the engine's its extension mechanism, in which a module is either bundled inside the binary or installed as a Composer package, package and is described by the same manifest and registrar metadata either way. Metadata will be , compiled ahead of of time in production and reflected at boot in debug, behind one interface the registry consumes. Enabled Enabled modules will pass through two phases, register and then boot, and every every binding they register will will be assembled into the container's s catalogues. Everything else a module contributes will be pulled: a be pulled by the component will ask every module for contributions that wants it, at the moment it needs themwants it, rather than pushed by modules pushing during boot.

Motivation

Each Every core feature of the panel is a module, as ADR-0013 decides, so the panel cannot run without a module system. It is not is not an optional extension point to be added once the engine works.

Several designs are already written against it and cannot be finished without stop at it. The event dispatcher in in RFC-0009 leaves reading listener listener attributes off a class to the module system. The HTTP transport in in RFC-0010 leaves modules modules contributing middleware or routes to it. The views in in RFC-0011 leave collecting template sources and and slot definitions to it. The dependency injection injection container in RFC-0001 leaves leaves assembling its catalogues catalogues to it.

Modules Discovering modules also have has to be discovered cheaplycheap. The panel runs as a worker, as ADR-0009 decides, so discovery happens once happens once at boot and is paid back over every request the worker serves. A developer editing a module still needs their change to be visible see the change without a rebuild.

Proposal

Module sourcesWhere a module comes from

A module will be either a bundled module or an external module, and nothing downstream of after discovery can will be able to tell which:

Bundled External
Ships Inside the binary, in the panel's own codebase and autoloader In the modules directory, with its own vendor/
Metadata declared In code In Under extra.tgp in the package's composer.json
Discovered from The panel itself The Composer lock file in the modules directory
Core flag Set Unset

An external module will be a Composer package of type tgp-module, installed into the modules directory. Its module identifier will be derived taken from its package name, and where a bundled module will declare its identifierown.

External modules will be installed with Composer driven as a library, as ADR-0012 decides. That decision also settles the settles the separate Composer project, the provide block generated from the panel's own lock file, and extra.tgp as the place the place for metadata that Composer's schema does not carry.

Both types of module will produce the same ModuleManifest and ModuleRegistrar. The registry will hold one set of of them and will never ask where a module came from. First, so a first-party features will therefore ship feature shipped as a bundled modules and module will be ordinary modules in an ordinary module in every other respect, as ADR-0013 decides.

MetadataWhat describes a module

A module's ModuleManifest will be an immutable and hold one module's record of its resolved metadata: its module identifier, vendor, version, name, name and description, declared capabilitiesthe capability declarations it makes, core its core flag, registrar the class namename of its registrar, its icon and its definition. For an external module, ModuleManifestBuilder will build it for an external module from the Composer package, , reading the standard Composer fields from from the package itself and the rest everything else from extra.tgp. A A bundled module will construct its its manifest directly.

A The registrar will be a module's registrar is its entry class. ModuleRegistrar will be an immutable and hold what record of what reflection found on on that class, and built by ModuleRegistrarBuilder will build it. It will record : which method carries carries #[Register], which carries #[Boot], and, for for each method carrying #[Collect] method, the collector type it accepts, whether it carries also carries #[Unscoped], and whether it takes a PanelContext parameter. It will hold metadata about describe the class, class and never hold an instance of of it.

Both records will be hydrated from an array, so both that they survive being written out and read back.

Each will be injectable by identifier through a resolvable attribute, Manifest or Registrar, as ADR-0002 decides:

public function __construct(
    #[Manifest('backups')] private ModuleManifest $manifest,
) {}

Capabilities

A module module will declare each capability in its its manifest, and from a Capability will be the enum of those recognised. The record names CrossRoutes, , ExtendSchema, ModifyUi and and DaemonAccess; , and the set grows will grow with the components that enforce them.

Whatever enforces a capability governs will check it at runtime, never and the module systemsystem never will. Declaring a capability is will not being be the same as being granted it: routing decides will decide what CrossRoutes permits, and the module system only carries will only carry the declaration.

Sources of metadataWhere the metadata comes from

Reflection is the cost this design manages. Every module source will implement one interface:

interface ModuleSource
{
    public function manifests(): array;

    public function registrars(): array;
}
Implementation Used in Behaviour
CachedModuleSource Production Reads bundled metadata from a file built into the binary, and external metadata from a file written during discovery. Reflects nothing.
LiveModuleSource Debug Reflects every module at boot, bundled from the panel's own codebase and external from the modules directory.

Whatever boots the panel LiveModuleSource will choose the implementation and hand it exist for developing modules: a change to a module's code will show on the registrynext request, which will not know which it was givenwith no rebuild and no discovery run.

The metadata written for LiveModuleSourceCachedModuleSource will exist go under the compiled root from RFC-0008, as PHP the engine writes for module development: a change PHP to module code include, so that it is opcached rather than decoded on every cold boot. It will be visible on the next requestcompiled output, with no rebuild not cached content, and no discovery runwill not go through a filesystem.

Module metadata written for CachedModuleSourceBundled registrar metadata will go to be reflected during the compiled root from RFC-0008, as PHP build and written into the engine writes for PHP to includebinary. No build tooling exists yet, so it will be opcached rather than decoded on every cold boot. It will not be cached content nothing produces that file, and LiveModuleSource will not go through be the only source a filesystemdeveloper can run.

Bundled registrar metadata A module's classes will be reflected during the build and written into able to carry attributes from libraries the binary. No build tooling exists yet, so panel knows nothing produces that file today, about. Reflection will therefore match attributes by name and LiveModuleSourceinstantiate none of them, as ADR-0014 decides, so a third-party attribute's constructor will be the only implementation never run because a developer can runmodule was discovered.

A module class may carry attributes from libraries the panel knows nothing about. Reflection will therefore match attributes by name and instantiate none of them, as ADR-0014 decides, so a third-party attribute's constructor will never run because a module was discovered.

There will be two. In production, CachedModuleSource will read bundled metadata from a file built into the binary and external metadata from a file written during discovery, and will reflect nothing. In debug, LiveModuleSource will reflect every module at boot, bundled modules from the panel's own codebase and external modules from the modules directory. Whatever boots the panel will choose one and hand it to the registry, which will not know which it was given.

The registry

ModuleRegistry will be immutable, built from a module source when the panel boots. It will look up one module's manifest or registrar metadata by module identifier, list every manifest or every identifier, and say whether a module is enabled, disabled, or known at all.

Lookup Returns
manifest(string $ident) One module's manifest.
registrar(string $ident) One module's registrar metadata.
manifests() Every manifest.
idents() Every identifier.
isEnabled(), isDisabled(), has() Whether a module is enabled, disabled, or known at all.

Which modules are enabled will come from ModulesEnabled, A module present in the core configuration object, read through source but missing from the configuration registry between its two seals, as RFC-0004 describesenabled list will be known and disabled. The A module system in the enabled list with no manifest will never scan a directory to work out what is enabled, be logged and will never repeat the rule that a module's skipped, because an installation whose configuration file being present is what enables it. Configuration is the single source of thatstill names a module that has since been removed should still boot.

A module present in the source but not in the enabled list The registry's own lifetime will be known and disabled. A module in follow the enabled list with no manifestmode that produced it, as RFC-0007 describes. In production it will be logged Process: built once at worker boot from compiled metadata and skippedshared by every request. In debug it will be Cycle: built again for each cycle, because an installation whose configuration names a module so that is no longer installed should still bootedited module code takes effect at once. The class will be the same in both, and only what constructs it will differ.

The registry's own lifetime will follow the mode that produced it, as RFC-0007 describes:

Mode Lifetime Effect
Production Process Built once at worker boot from compiled metadata, and shared by every request.
Debug Cycle Rebuilt for each cycle, so edited module code takes effect immediately.

The class will be the same in both. Only what constructs it will differ.

Lifecycle

Every enabled module Registrars will pass through two phasesbe instantiated directly during register, in order:because the container will not exist yet: building it will be what the phase produces. Boot will call the same registrar objects that register created, by which point the container will exist.

  1. Register. Each module's registrar will be instantiated, and its #[Register] method called with an EngineBuilder, through which the module registers its bindings and resolvers.
  2. Boot. Once every module has registered and the container exists, each module's #[Boot] method will be called with no arguments.

Every module will register before any module boots, so a module's boot may depend on another module having registered. A module with no method for a phase will be skipped for that phase, without error.

Registrars will be instantiated directly during register, because the container does not exist yet: building it is what the phase produces. By boot the container will exist, and the registrars instantiated during register will be the same objects.

Which modules are enabled will come from ModulesEnabled, the core configuration object, read through the configuration registry between its two seals, as RFC-0004 describes. The module system will never scan a directory to work out what is enabled, and will never repeat the rule that a module's configuration file being present is what enables it. Configuration will be the one source of that answer.

Register and boot

Every enabled module will pass through two phases, in order:

  1. Register. Each module's registrar will be instantiated, and its #[Register] method called with an EngineBuilder, through which the module will register its bindings and resolvers.
  2. Boot. Once every module has registered and the container has been built, each module's #[Boot] method will be called with no arguments.

Every module will register before any module boots, so a module's boot may rely on another module having registered. A module with no method for a phase will be skipped for that phase, without error.

The builder

A module will register its bindings and resolvers through one surface, EngineBuilder , which will be the mutable half of hide the container's two registries, per the mutable halves that ADR-0003 seals into catalogues. Registering a binding will hand back the builder from RFC-0001, presented to so a module will describe a binding exactly as one surfacethe engine does:

Method Effect
bind(string $abstract): BindingBuilder Registers a binding, returning the builder from RFC-0001.
resolver(string $resolvable, string $resolver, bool $default = false) Registers a resolver against a resolvable attribute.

A builder will be scoped to Registering a module for the duration of that module's register call, so every binding registered through it resolver will record name the module identifierresolvable attribute it answers and the resolver class, and may make it that registered it. Recording the owner is this designattribute's part. What an owner means at resolution is left to RFC-0016default resolver.

After For the length of one module's register phase call, the builder will be consumedscoped to that module, producing each catalogueso every binding registered through it will record the module identifier that registered it. Recording the container owner is constructed with, BindingCataloguethis design's part; what an owner means at resolution and ResolverCatalogue, and will be unavailable afterwards. This is the assembly left to RFC-00010016 leaves to the module lifecycle.

#[Register]
public function register(EngineBuilder $engine): void
{
    $engine->bind(BackupStore::class)->to(DatabaseBackupStore::class);
}

When the register phase ends, the builder will be consumed into the catalogues the container is constructed with, BindingCatalogue and ResolverCatalogue, and will be unavailable afterwards. This will be the assembly that RFC-0001 leaves to the module lifecycle.

Alias chains

An alias may name another alias. Assembly will flatten every chain, so that each alias names the abstract that owns the binding directly and resolution stays a single hop.

Flattening The chain will happen be flattened at assembly because a catalogue is will be assembled once and never changes afterwardsnever change, while an alias is will be normalised before the instance cache cache is consulted on on every resolution, as as RFC-0006 describes. Flattening Walking the chain at assembly will pay for the walk it once, at boot, rather rather than on every resolution for of a structure that cannot change.

A chain that returns comes back to an alias already seen cannot will not be flattened. It will fail the assembly, naming the aliases in the loop. A , so that a loop will therefore be a boot failure that says saying what is wrong, wrong rather than a resolution that exhausts the the stack.

CollectionPulling contributions

Everything a module contributes beyond bindings its bindings will be pulled pulled rather than pushed. A component component will ask for contributions when it wants them, which may be at boot, on first use, or or never.

A Each component will define its own collector type type, and a handler that drives will drive collection for it:

interface Collector {}

interface CollectorHandler
{
    public function collects(): string;

    public function create(ModuleManifest $manifest, ?PanelContext $context, bool $scoped): Collector;

    public function process(Collector $collector, ModuleManifest $manifest, ?PanelContext $context, bool $scoped): void;

    public function finalise(): void;
}

The registry will drive the flow, and the component will decide when it runs. The component will pass its handler to ModuleRegistry::collect(CollectorHandler $handler, ?PanelContext $context), and the registry will walk every enabled module. For each, it will look in the registrar metadata for a #[Collect] method accepting the handler's collector type, and will skip the module when there is none. Otherwise create() will produce a fresh collector for that module, the module's method will fill it, and process() will receive it filled. Once every module has been walked, finalise() will run once.

  1. The component will pass its handler to ModuleRegistry::collect(CollectorHandler $handler, ?PanelContext $context).
  2. The registry will walk every enabled module.
  3. For each, it will check the registrar metadata for a #[Collect] method accepting that collector type, and skip the module when there is none.
  4. create() will produce a fresh collector for that module.
  5. The module's method will populate it.
  6. process() will receive the populated collector.
  7. After every module, finalise() will run once.

A module will receive a fresh collector each time, so nothing a module contributes can reach or overwrite what another contributed. The handler will be the only thing that sees them all.

Each module will receive a fresh collector, so nothing one module contributes can reach or overwrite what another contributed. The handler will be the only thing that sees them all. A module contributing routes to a backups page will look like this:

#[Collect]
public function routes(RouteCollector $routes): void
{
    $routes->get('/backups', ListBackups::class);
}

The collector type will be taken from the method's type hint. A #[Collect] method may also declare a PanelContext parameter, in which case it will participate take part only when collection runs for that panel context; a method without one will take part whatever the context.

Scope of Where a contributioncontribution reaches

By default default, a module's #[Collect] method will contribute within its own module's boundary, and , and the $scoped flag passed to the handler will say so. #[Unscoped] will mark a method as contributing outside itthat boundary.

Whether that is permitted allowed will be the handler's decision, never the module system's. The handler will receive both the flag and the module's manifest, and so it has will have the declaration and every capability the module declares to decide with. For Permissions, for example, permissions will refuse unscoped contributions an unscoped contribution outright, while routing may allow will be able to accept one from a module declaring CrossRoutes.

#[Unscoped] here means will mean outside the module's own contribution boundary. It is will be unrelated to the container's module scopes module scopes in RFC-0016, which are will be a separate axis.

Caching what was collected

There will be no cacheable collector abstraction. Different , because components will cache differently: a . A route collector can will be able to cache a form that skips collection entirely on later later requests, while while another gains collector will gain nothing from caching at all. A Each component will cache around collection collection however suits it.

Panel context

PanelContext will be an enum with one case for each panel context: Account, Server and Platform. It will be used to scope collection, and routing will use it to decide a a route's URL URL prefix. For routing, Server will be a subcontext of Account; for collection, it will be a value in its own right.

Server It will be a subcontext of Accountdefined among the engine's shared values, alongside the client address value object, rather than inside collection. Collection is where it was first written down, but routing will need it too, which is the promotion RFC-0010 for routingrecords as unsettled. With two consumers it will be shared, and a value in its neither will own right for collectionit.

It will be defined in the engine's shared values rather than inside collection, alongside the client address value object. Collection is where it was first written down, but routing needs it too, which is the promotion RFC-0010 records as unsettled. Two consumers make it shared, so it will not be owned by either.

Errors

Every exception the module system throws will implement ModuleException, a marker contract. ModuleSourceException will be thrown when metadata is missing or unreadable, with a message saying what to do: run discovery, or check the installation. UnknownModuleException will be thrown when a manifest or registrar is asked for by an identifier that is not known.

Exception Thrown when
ModuleSourceException Metadata is missing or unreadable. Its message says what to do: run discovery, or check the installation.
UnknownModuleException A manifest or registrar is asked for by an identifier that is not known.

An enabled module with no manifest will be a warning rather than an exception, and the module will be skipped.

An enabled module with no manifest will be a warning rather than an exception, and the module will be skipped.

Out of scope

  • Module scopes. What the owner owner recorded on a binding means at resolution, and what a module module is handed because of it, is its its own design, , RFC-0016.
  • Module resources. Provisioning a module's schema, directories or filesystems.
  • The build step. Reflecting bundled registrars registrars during the build and writing them into into the binary belongs to will belong to bootstrapping, along with everything else that composes will put the engine together.
  • Enabling and disabling modules. That belongs will belong to the command line, as RFC-0004 describes.
  • Which collectors exist. Each component defines will define its own collector and handler. This design defines will define the mechanism and nothing that uses it.
  • Enforcing capabilities. That belongs will belong to whatever a capability governs.
  • A module API package and an SDK for third-party development.
  • Inter-module dependenciesDependencies between modules. Nothing here will resolve a dependency between two modules, modules or order them modules by one.

Alternatives considered

The decisions this This design rests on are five decisions recorded separately: ADR-0002, with dependencies selecting their instance through parameter attributes; ADR-0003, mutable registries sealed into immutable catalogues; ADR-0009, the alternatives each one rejected:panel running as a FrankenPHP worker in a single binary; ADR-0012, modules installed with Composer driven as a library; and ADR-0013, core features being modules.

  • dependencies selecting their instance through parameter attributes, in ADR-0002
  • sealing mutable registries into immutable catalogues, in ADR-0003
  • the panel running as a FrankenPHP worker in a single binary, in ADR-0009
  • installing modules with Composer driven as a library, in ADR-0012
  • core features being modules, in ADR-0013

Collection as a third lifecycle phaseA cacheable collector abstraction., with modules pushing every contribution during boot. We I rejected this this because a component would then receive contributions whether or not it is ever usedcomponents cache collected data in different shapes, and and some gain nothing from caching, so one abstraction would have nowhere to put the decision of when to collect. Pulling lets a component collect on first use, or nevercover cases that do not resemble each other.

A cacheable collector abstraction.Following alias chains at runtime We , walking from one alias to the next on each lookup. I rejected this because components cache collected data in different shapes, and some gain nothing from cachingthe walk would repeat on every resolution, so an abstraction on the path taken before the instance cache is consulted, for a structure fixed at boot. A loop would have to cover cases that do not resemble each otheralso be found by exhausting the stack rather than when the catalogue was assembled.

Following alias chains at runtime, walking from one alias to the next on each lookup. We rejected this because the walk would repeat on every resolution, in the path taken before the instance cache is consulted, for a structure fixed at boot. A loop would also be found by exhausting the stack rather than when the catalogue was assembled.

No other alternatives were weighed.

Collection as a third lifecycle phase, with modules pushing every contribution during boot. I rejected this because a component would then receive contributions whether or not it is ever used, and would have nowhere to decide when to collect. Pulling lets a component collect on first use, or never.

Backwards compatibility

Nothing will break. No module exists, and the modules directory in the repository is empty.

The container is constructed with its catalogues catalogues today, and will gain the step that builds builds them. That step is what what RFC-0001 left to this design, not a change to it.

Open questions

Changelog

  • : Alias chains are flattened when the catalogue is assembled, which settles the only open question.
  • : Reworded in the readable house style, and the project's own vocabulary linked to the glossary. The design is unchanged.
  • : Rewrote the document in full, with no change to the design.

Sources

  • Issue #35, Engine - Modules, original text of in its edit history: the module system as the extension mechanism, the two sources and their differenceshow they differ, both producing the same structures with the registry not distinguishing themnot telling them apart, capabilities declared in manifests and enforced elsewhere, the two lifecycle phases driven by by a bootstrapper, , collection being pull-based and not part of the lifecycle, and the module API package left out.
  • Issue #35, revised on in its edit history: the ModuleSource seam with its compiled and reflecting implementations, bootstrap selecting one, and the registry not knowing which it was given. A later edit on on replaced the subissue names with issue links and changed nothing else.
  • Issue #36, Engine - Modules - Discovery and Manifest, , with no edits: both source implementations and what each reads, external discovery from the Composer lock file, the metadata declared under extra.tgp, the manifest the manifest and registrar and their builders, hydration from an array, the capability enum with the four it names, , the four four registrar attributes, the prebuilt bundled file, and the #[Manifest] and #[Registrar] attributes with their with their resolvers. It places external metadata in a JSON file in the cache directory; issue #73 supersedes that.
  • Issue #37, Engine - Modules - Registry, , with no edits: the immutable registry built from a source, every , every lookup, enabled modules coming from configuration, a disabled module still being known, an enabled module module with no no manifest being logged and skipped, an unknown ident identifier throwing, a missing source being an actionable error, the , the collection entry point, and the registry being built once per worker in production and per request in debug.
  • Issue #38, Engine - Modules - Lifecycle, , with no edits: the two phases and their order, every module registering before any boots, EngineBuilder with its two methods and its scoping to a module during each registration call, its consumption into the immutable container, registrars instantiated directly during register because the container does not exist, and a missing method for a phase being skipped silently.
  • Issue #39, Engine - Modules - Collectors, , with no edits: collection being pull-based and triggered by triggered by the component, the collector marker and the handler contract with its four methods, the seven steps of the the flow, a a fresh collector per module, the collector type inferred from the type hint, the optional PanelContext parameter parameter filtering participationparticipation and a method without it participating whatever the context, #[Unscoped] with the decision decision left to the handler, permissions and routes as as the two worked examples, PanelContext and its three cases with with Server as a subcontext of Account in routing, , and caching left to each component with no cacheable collector collector abstraction.
  • Issue #44, Engine - Container - Alias cache key mismatch, text of , edited the same day in its edit history: chained aliases not aliases not resolving, and the choice between flattening them when catalogues are built and following them at runtime being left to this design.
  • Issue #70, Engine - Container - Module scopes, , edited the same day: a binding registered through a module-scoped builder knowing its owner, which is what module scopes are built on, and Unscoped already meaning something different for a #[Collect] method.
  • Issue #73, Engine - Filesystems - Paths: compiled, , edited on : external module metadata moving metadata moving out of the cache directory to the compiled root, as an includable PHP file opcached rather than decoded on on every cold cold boot.
  • Issue #43, Engine - Container - Memoise class-level attribute lookups, original text of , with no edits in its edit history: third-party party classes carrying attributes from unrelated libraries once the module system lands, which is why why matching is by by name and nothing is instantiated.
  • PanelContext being defined in the engine's shared values rather than inside collection; the registry's two construction modes expressed as process and cycle lifetimes; EngineBuilder being the module-facing surface over the over the container's two registries; alias chains being flattened at assembly, with a cycle failing the assembly and and naming the the aliases in it; and the ModuleException marker with ModuleSourceException and and UnknownModuleException: first first written down on .

Source rfc/0015-module-system.md · commit 9444a99 · published