super-kernel/runtime-context
Composer 安装命令:
composer require super-kernel/runtime-context
包简介
Runtime context and scoped path component for super-kernel.
README 文档
README
A runtime context and scoped path component for the super-kernel framework.
This package provides a small and stable public contract for detecting the current runtime environment, locating the runtime root path, and navigating paths safely within that root boundary.
Table of Contents
- Features
- Why This Package Exists
- Installation
- Requirements
- Quick Start
- Public API
- Runtime Modes
- Usage Examples
- Path Safety Rules
- Feature Matrix
- Architecture Overview
- Testing
- License
Features
- Automatically detects the current runtime environment
- Supports Composer script runtime, Phar runtime, and CLI entry runtime
- Resolves the runtime root path automatically
- Provides a scoped path object for constrained path navigation
- Prevents escaping above the runtime root boundary
- Exposes a minimal and stable public contract
- Keeps runtime detection strategies internal
- Uses one public exception contract for all package-level failures
Why This Package Exists
Runtime-dependent path handling is easy to get wrong when the same framework may run in multiple forms:
- through Composer
- through a Phar package
- through a direct CLI entry script
This package centralizes that responsibility.
Instead of scattering environment detection and path normalization logic across different components, consumers can rely on a single runtime context object and a single scoped path model.
Installation
composer require super-kernel/runtime-context
Requirements
- PHP
~8.4.0
Quick Start
<?php declare(strict_types=1); use SuperKernel\RuntimeContext\RuntimeContext; use SuperKernel\RuntimeContext\Contract\RuntimeContextInterface; $context = RuntimeContext::getContext(); if ($context->mode() === RuntimeContextInterface::MODE_PHAR) { echo 'Running in phar mode.' . PHP_EOL; } echo $context->rootPath() . PHP_EOL; echo $context->entryPath() . PHP_EOL;
Public API
RuntimeContext::getContext()
Returns the current runtime context instance.
RuntimeContextInterface
Provides:
- current runtime mode
- runtime root path
- runtime entry path
- root scoped path
- path resolution from runtime root
ScopedPathInterface
Provides:
- absolute path
- relative path from runtime root
- root state detection
- downward navigation
- upward navigation
- relative path resolution within runtime boundary
RuntimeContextExceptionInterface
A single public exception contract for all package-level failures.
Runtime Modes
The runtime mode is returned by RuntimeContextInterface::mode().
Available constants:
RuntimeContextInterface::MODE_COMPOSERRuntimeContextInterface::MODE_PHARRuntimeContextInterface::MODE_CLI
Usage Examples
Get the Current Runtime Context
<?php declare(strict_types=1); use SuperKernel\RuntimeContext\RuntimeContext; $context = RuntimeContext::getContext(); echo $context->mode() . PHP_EOL; echo $context->rootPath() . PHP_EOL; echo $context->entryPath() . PHP_EOL;
Determine the Runtime Mode
<?php declare(strict_types=1); use SuperKernel\RuntimeContext\RuntimeContext; use SuperKernel\RuntimeContext\Contract\RuntimeContextInterface; $context = RuntimeContext::getContext(); if ($context->mode() === RuntimeContextInterface::MODE_COMPOSER) { echo 'Composer runtime' . PHP_EOL; } if ($context->mode() === RuntimeContextInterface::MODE_PHAR) { echo 'Phar runtime' . PHP_EOL; } if ($context->mode() === RuntimeContextInterface::MODE_CLI) { echo 'CLI runtime' . PHP_EOL; }
Access the Runtime Root Path
<?php declare(strict_types=1); use SuperKernel\RuntimeContext\RuntimeContext; $root = RuntimeContext::getContext()->root(); echo $root->absolute() . PHP_EOL; echo $root->relative() . PHP_EOL; // "." var_dump($root->isRoot()); // true
Resolve a Path from the Runtime Root
<?php declare(strict_types=1); use SuperKernel\RuntimeContext\RuntimeContext; $context = RuntimeContext::getContext(); $config = $context->path('config/packages'); echo $config->absolute() . PHP_EOL; echo $config->relative() . PHP_EOL;
Navigate Downward
<?php declare(strict_types=1); use SuperKernel\RuntimeContext\RuntimeContext; $root = RuntimeContext::getContext()->root(); $src = $root->enter('vendor', 'package-name', 'src'); echo $src->absolute() . PHP_EOL;
Navigate Upward
<?php declare(strict_types=1); use SuperKernel\RuntimeContext\RuntimeContext; $root = RuntimeContext::getContext()->root(); $path = $root->enter('var', 'cache', 'runtime'); $parent = $path->parent(); $top = $path->parent(3); echo $parent->absolute() . PHP_EOL; echo $top->absolute() . PHP_EOL;
Resolve Relative Paths Safely
<?php declare(strict_types=1); use SuperKernel\RuntimeContext\RuntimeContext; $root = RuntimeContext::getContext()->root(); $path = $root->enter('vendor', 'package-name', 'src'); $bin = $path->resolve('../../../bin'); echo $bin->absolute() . PHP_EOL;
Handle Runtime Context Exceptions
<?php declare(strict_types=1); use SuperKernel\RuntimeContext\RuntimeContext; use SuperKernel\RuntimeContext\Contract\RuntimeContextExceptionInterface; try { $path = RuntimeContext::getContext()->root()->resolve('../../../../etc'); } catch (RuntimeContextExceptionInterface $exception) { echo $exception->getMessage() . PHP_EOL; }
Path Safety Rules
This package applies strict path constraints.
Allowed
- resolving relative paths inside the runtime root
- navigating downward with
enter() - navigating upward with
parent()as long as the root boundary is not crossed - resolving
.and..throughresolve()within the runtime root
Rejected
- absolute paths passed to
resolve() - empty path segments
.passed toenter()..passed toenter()- multi-segment input passed to
enter() - any operation that escapes above the runtime root
Feature Matrix
| Capability | Public Contract | Notes |
|---|---|---|
| Detect current runtime | RuntimeContext::getContext() |
Single public entry point |
| Read runtime mode | RuntimeContextInterface::mode() |
Uses public mode constants |
| Read root path | RuntimeContextInterface::rootPath() |
Absolute path |
| Read entry path | RuntimeContextInterface::entryPath() |
Absolute path |
| Get root scoped path | RuntimeContextInterface::root() |
Relative path is . |
| Resolve root-relative path | RuntimeContextInterface::path() |
Throws on invalid or out-of-bound path |
| Navigate downward | ScopedPathInterface::enter() |
Accepts only single valid segments |
| Navigate upward | ScopedPathInterface::parent() |
Throws if root boundary is crossed |
| Resolve relative path | ScopedPathInterface::resolve() |
Supports . and .. within boundary |
| Catch all package errors | RuntimeContextExceptionInterface |
Single public exception contract |
Architecture Overview
The following diagram shows the public surface and the internal detection structure.
graph TD
A[RuntimeContext::getContext()] --> B[RuntimeContextInterface]
B --> C[ScopedPathInterface]
A --> D[Internal Runtime Detection]
D --> E[Composer Script Detector]
D --> F[Phar Detector]
D --> G[CLI Entry Detector]
C --> H[Path Normalization]
C --> I[Boundary Protection]
Loading
Design Intent
- Keep the public contract small
- Keep runtime detection internal
- Keep path navigation explicit and constrained
- Keep boundary violations exceptional, not optional
Testing
Run the PHPUnit test suite directly:
php vendor/bin/phpunit --configuration=phpunit.xml.dist
If you explicitly want to execute PHPUnit through Composer:
composer run-script phpunit
License
This project is licensed under the MIT License.
See the LICENSE file for full license text.
super-kernel/runtime-context 适用场景与选型建议
super-kernel/runtime-context 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 3 次下载、GitHub Stars 达 0, 最近一次更新时间为 2026 年 04 月 14 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「path」 「cli」 「composer」 「phar」 「runtime」 「super-kernel」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 super-kernel/runtime-context 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 super-kernel/runtime-context 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 super-kernel/runtime-context 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
JSONPath implementation for querying and updating JSON objects with support for json flattening into a table
The InstantConfigurationCopy module provides easy way to copy configuration field information for admin in back office Magento 2.
This composer plugin enables installation of GravityForms WordPress plugin and its addons.
simple agnostic package for IO releted (files, dir, etc..)
Simple ASCII output of array data
统计信息
- 总下载量: 3
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 36
- 依赖项目数: 1
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2026-04-14