holistic-agency/decouple
Composer 安装命令:
composer require holistic-agency/decouple
包简介
decoupling interfaces
README 文档
README
Decoupling interfaces.
This package is a very light and agnostic set of interfaces and implementations designed to fake or render unpredictable return values of some PHP native functions dealing with external concerns.
Installation
composer require holistic-agency/decouple:^1.0
Usage
ClockInterface
This package implememts psr\clock. See https://www.php-fig.org/psr/psr-20/
The Clock now() method returns a new DateTimeImmutable object
with the current time acccording to the php date.timezone setting.
The Frozen\Clock now() method returns the DateTimeImmutable object
passed to the constructor.
// src/MyClass.php use HolisticAgency\Decouple\ClockInterface; use Psr\Clock\ClockInterface; class MyClass { public function myMethod(ClockInterface $clock) { // some code ... $now = $clock->now()->format('Y-m-d H:i:s T'); // some code ... } } // src/Elsewhere.php use HolisticAgency\Decouple\Clock; $myClass = new Myclass(); $myClass->myMethod(new Clock()); // tests/MyClassTest.php use HolisticAgency\Decouple\Frozen\Clock; class MyClassTest { public function testMyMethod() { // Given $myTestClass = new MyClass(); $frozen = new Clock(new DateTimeImmutable('2022-02-05 16:32:29 CET')); // When $actual = $myTestClass->myMethod($frozen); // Then // assert what you need // knowing $now is equal to '2022-02-05 16:32:29 CET' // inside myMethod() } }
NumberGeneratorInterface
The MersenneTwister draw() method returns an integer from a mt_rand() call.
The Frozen\NumberGenerator draw() method returns one of ordered values passed to the constructor cyclically.
$generator = new MersenneTwister(); $unknon = $generator->draw(); // an integer between 0 and PHP_INT_MAX included $generator = new MersenneTwister(1, 6); $diceType = 'D' . strval($generator->max); // D6 $diceRoll = $generator->draw() ; // an integer between 1 and 6 included if ($diceRoll == $this->min) { echo $diceType . ' roll, you loose.'; // if 1 is rolled } $guesser = new Frozen\NumberGenerator(10, 17, 1); $cheater = $guesser->draw(); // 10 $cheater = $guesser->draw(); // 17 $cheater = $guesser->draw(); // 1 $cheater = $guesser->draw(); // 10
SystemInterface
| method | native PHP call |
|---|---|
| freeSpace($directory) | disk_free_space($directory) |
| documentRoot() | $_SERVER['DOCUMENT_ROOT'] or empty string ('') |
| pid() | getmypid() |
| umask() | umask() |
PlatformInterface
| method | native PHP call |
|---|---|
| sapi() | PHP_SAPI |
| version() | PHP_VERSION |
| extensions() | get_loaded_extensions() |
| memory() | ini_get('memory_limit') or empty string ('') |
NetworkInterface
| method | native PHP call |
|---|---|
| hostname() | gethostname() or empty string ('') |
| ipV4() | gethostbyname() |
| httpHost() | $_SERVER['HTTP_HOST'] or empty string |
| resolve($remote) | gethostbyname($remote) or empty string |
| dnsGetRecord(...). | dns_get_record(...) |
Example
// src/MyNetwork.php use HolisticAgency\Decouple\NetworkInterface; class MyNetwork { public checkIfRemoteIsAvailable(NetworkInterface $network, string $remote): bool { return $network->resolve($remote) != ''; } } // tests/MyNetworkTest.php use HolisticAgency\Decouple\Frozen\Network as FrozenNetwork; class MyNetworkTest { public function testCheckIfRemoteIsAvailable() { // Given $myTestClass = new MyNetwork(); $frozen = new FrozenNetwork( 'production.local', '192.168.1.10', 'app.my.org', ['proxy.inside.local' => '10.10.0.10'], ); // When $actual1 = $myTestClass->checkIfRemoteIsAvailable($frozen, 'api.outside.net'); $actual2 = $myTestClass->checkIfRemoteIsAvailable($frozen, 'proxy.inside.local'); // Then // $actual1 is false // $actual2 is tue } }
holistic-agency/decouple 适用场景与选型建议
holistic-agency/decouple 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 312 次下载、GitHub Stars 达 1, 最近一次更新时间为 2024 年 10 月 28 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「network」 「system」 「interfaces」 「platform」 「clock」 「pseudo-random」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 holistic-agency/decouple 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 holistic-agency/decouple 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 holistic-agency/decouple 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
O2System PSR it's build based on PHP Framework Interop Group (PHP-FIG) standards recommendations. This repository contains a collection of PHP interfaces based on the PSR-0 until the PSR-7.
LinkedIn API PHP SDK with OAuth 2.0 & CSRF support. Can be used for social sign in or sharing on LinkedIn. Examples. Documentation.
Limoncello PHP Framework Contracts (interfaces) required for components integration.
LinkedIn API PHP SDK with OAuth 2.0 & CSRF support. Can be used for social sign in or sharing on LinkedIn. Examples. Documentation.
PHP SDK for Bands In Town API
Transforms Laravel Models to Typescript Interfaces/Types
统计信息
- 总下载量: 312
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 1
- 点击次数: 39
- 依赖项目数: 1
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2024-10-28