承接 ap-lib/context 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

ap-lib/context

Composer 安装命令:

composer require ap-lib/context

包简介

The Context library provides a lightweight context storage system for managing and retrieving shared data across different parts of an application. It supports dynamic storage, object retrieval, and type enforcement.

README 文档

README

MIT License

The Context library provides a lightweight context storage system for managing and retrieving shared data across different parts of an application. It supports dynamic storage, object retrieval, and type enforcement.

Installation

composer require ap-lib/normalizer

Features

  • Store and retrieve arbitrary values using string keys.
  • Store objects and retrieve them with type enforcement.
  • Prevent accidental overwrites with an optional replace flag.
  • Retrieve stored values by name or class.
  • Exception handling for invalid operations.

Requirements

  • PHP 8.3 or higher

Getting started

Storing and Retrieving a Single Object

If you store an object without a name, its class name is automatically used as the key:

use AP\Context\Context;

class User {
    public function __construct(public int $id, public string $email) {}
}

$context = new Context();

$user = new User(12, "name@gmail.com");

// Store the object using its class name
$context->set($user);

// Retrieve the object by class name
$retrievedUser = $context->get(User::class);
var_dump($retrievedUser === $user); // true

// Retrieve with type enforcement
$retrievedUser = $context->getObject(User::class);
var_dump($retrievedUser === $user); // true

Storing and Retrieving Multiple Similar Objects

You can store multiple objects of the same class using custom names:

$context = new Context();

$user = new User(12, "name@gmail.com");
$realUser = new User(1, "admin@gmail.com");

$context->set($user);                     // Stored with class name
$context->set($realUser, "realUser");      // Stored with custom name

// Retrieve the default user
$retrievedUser = $context->get(User::class);
var_dump($retrievedUser === $user); // true

// Retrieve with type enforcement
$retrievedUser = $context->getObject(User::class);
var_dump($retrievedUser === $user); // true

// Retrieve the real user by custom name
$retrievedRealUser = $context->get("realUser");
var_dump($retrievedRealUser === $realUser); // true

// Retrieve the real user with type enforcement
$retrievedRealUser = $context->getObject(User::class, "realUser");
var_dump($retrievedRealUser === $realUser); // true

Storing and Retrieving Custom Data

You can store and retrieve non-object values, such as arrays, strings, or numbers, using a custom name:

$context = new Context();

$userData = ["id" => 12, "email" => "name@gmail.com"];

// Store the array with a custom name
$context->set($userData, "user");

// Retrieve the stored array
$retrievedData = $context->get("user");
var_dump($retrievedData === $userData); // true

Working with References

Working with Array References

When retrieving an array by reference, modifications to the retrieved array will affect the stored array.

$context = new Context();

$name = "original";
$original = ["foo" => "boo"];

$context->set($original, $name);

// Get a reference to the stored array
$ref = &$context->get($name);
$ref["foo"] = "changed";

// The original stored array is now modified
var_dump($context->get($name)); 
// Output: ["foo" => "changed"]

Fully Replacing an Array via Reference:

$name = "original";
$original = ["foo" => "boo"];

$context->set($original, $name);

// Get a reference to the stored array
$ref = &$context->get($name);
$ref = ["hello" => "world"];

// The original stored array is now modified
var_dump($context->get($name)); 
// Output: ["hello" => "world"]

Working with Object References

hen retrieving an object, modifications to its properties affect the stored object.

$context = new Context();

$user = new User(1, "a@b.com");

$context->set($user);

// Get a reference to the stored object
$ref = $context->getObject(User::class);
$ref->id = 2;

// The original stored object is now modified
var_dump($context->getObject(User::class)); 
// Output: User { id: 2, email: "a@b.com" }

Error Handling

Type Enforcement Errors

If you try to retrieve an object with getObject(), but the stored data does not match the expected class, an exception is thrown:

$context = new Context();

// Store an array using the class name
$context->set(["id" => 12, "email" => "name@gmail.com"], User::class);

// This will throw an UnexpectedValueException because the stored data is not a User object
$context->getObject(User::class);

Handling Missing

If you try to retrieve an object that has not been stored, an exception is thrown:

$context = new Context();

// This will throw an UnexpectedValueException because no User object exists
$context->getObject(User::class);

// This will throw an UnexpectedValueException because "randomName" does not exist too
$context->get("randomName");

ap-lib/context 适用场景与选型建议

ap-lib/context 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 53 次下载、GitHub Stars 达 0, 最近一次更新时间为 2025 年 02 月 03 日, 在 PHP 生态内属于活跃度较高的组件。

我们在过去多个企业项目中使用过 ap-lib/context 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 ap-lib/context 我们能提供哪些服务?
定制开发 / 二次开发

基于 ap-lib/context 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 53
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 19
  • 依赖项目数: 5
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2025-02-03