-
-
Notifications
You must be signed in to change notification settings - Fork 0
Logic stream wrapper
LogicStreamWrapper exists for one specific problem: path-mapped logic files often declare the same function names, such as go().
If we load several of those files in one PHP process, the function names would normally collide. The stream wrapper prevents that by injecting a namespace when the file does not already declare one.
A typical WebEngine request may load several logic files:
page/_common.phppage/shop/_common.phppage/shop/@category/index.php
All of them may define a function called go(). Without namespacing, PHP would reject the second declaration.
When a file is required through the stream wrapper:
- the file must begin with
<?php - if it already declares a namespace, that namespace is left in place
- otherwise a namespace is injected based on the file path
- unqualified internal PHP class names are prefixed so built-in types such as
DateTimestill resolve correctly inside the injected namespace
The generated namespace prefix is:
GT\AppLogic
with the file path appended and normalised.
use GT\Routing\LogicStream\LogicStreamWrapper;
stream_wrapper_register("gt-logic-stream", LogicStreamWrapper::class);
require "gt-logic-stream://page/shop/_common.php";The namespace path is handled by LogicStreamNamespace, which converts path separators and unsupported characters into namespace-safe names.
For example, a path such as:
/tmp/page/shop/@category/index.php
becomes a generated namespace rooted under GT\AppLogic.
For the wrapper to work correctly:
- the file must be a PHP file
- it must begin with an opening PHP tag
- it should contain only PHP code, and have no side effects (including it should only declare symbols, not echo content)
If the opening tag is missing, the wrapper throws an exception explaining which file failed.
If we are only using class-based router callbacks, we may never need the stream wrapper.
If we are building a filesystem-mapped application runtime, or trying to understand how WebEngine can load many go() functions in one request, this is the missing piece.
To see how the callback router, path matcher, dynamic paths and stream wrapper are used together, read example project.
PHP.GT/Routing is a separately maintained component used by PHP.GT/WebEngine.