承接 sanmai/later 相关项目开发

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

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

sanmai/later

Composer 安装命令:

composer require sanmai/later

包简介

Later: deferred wrapper object

README 文档

README

Latest Stable Version CI Coverage Status Type Coverage

This rigorously tested fully-typed library just works. It neither defines nor throws any exceptions.

Install

composer require sanmai/later

The latest version requires PHP 7.4 or greater.

Use

To use this pattern you need a generator function, yielding a single item of type you want to produce lazily. Pass it to later(), a static wrapper returning a Deferred object:

For example:

use function Later\later;

$deferred = later(function () {
    $deepThought = new DeepThought();
    $deepThought->solveTheQuestion();

    yield $deepThought;
});

And then call get() when needed, as many times as needed:

$deferred->get()->getAnswer(); // 42
$deferred->get()->getAnswer(); // same 42

Using a generator instead of a traditional callback comes with a major benefit: any generator is guaranteed by the language to be used exactly once. You can be sure that it won't be called twice.

But that's not all: read on.

No Callbacks Required

Making a closure generator on the spot isn't always convenient. And not to say these closures are much different from all-too-familiar callbacks. Not at all different from the looks of them.

The power of this pattern is in its ability to make use of any function, previously returning a single value, without any need for any additional callbacks or closures.

Consider this diff:

 private function makeFooBar()
 {
    //...

-    return $foo;
+    yield $foo;
 }

After adding Later\lazy to the mix:

 use function Later\lazy;

 public function __construct()
 {
-    $this->fooBar = $this->makeFooBar();
+    $this->lazyFooBar = lazy($this->makeFooBar());
 }

 public function usesFooBar()
 {
     if ($fooBarReallyRequired) {
-        $this->fooBar->getResult();
+        $this->lazyFooBar->get()->getResult();
     }
 }

We can see, this simple, single-line, change in the original method freed our program from creating things it may not need, postponing this process until the last moment, while also avoiding any use of callbacks.

Type Transparency

The library is completely typed. PHPStan, Psalm, and Phan are all routinely supported.

To use this capability it is recommended to declare a variable holding this object as \Later\Interfaces\Deferred<Type>.

In this example it will be Deferred<DeepThought>:

use Later\Interfaces\Deferred;
use function Later\lazy;

final class HyperIntelligentMice
{
    /** @var Deferred<DeepThought> */
    private $supercomputer;

    public function __construct(DeepThought $deepThought)
    {
        $this->supercomputer = lazy(self::updateDeepThought($deepThought));
    }

    /** @return iterable<DeepThought> */
    private static function updateDeepThought(DeepThought $deepThought): iterable
    {
        $deepThought->solveTheQuestion();

        yield $deepThought;
    }

    public function getAnswer(): int
    {
        return $this->supercomputer->get()->getAnswer();
    }
}

Following this approach, a static analyzer or IDE will be able to understand what is called, and what is returned.

Proxy Syntax

The library supports convenient proxy syntax for accessing methods and properties directly from the deferred object:

/** @var \Later\Interfaces\Deferred<DeepThought> $deferred */
$deferred->getAnswer(); // 42
$deferred->answer;      // 42

With the explicit type IDEs such as PhpStorm can autocomplete proxied members.

Eager Execution

What if a program calls for Deferred object, but lazy evaluation is not required? For example, because a result is already available being loaded from a cache.

No problem, there's a function for this:

use function Later\now;

$deferred = now($result);
$deferred->get(); // returns $result

This deferred-but-not-deferred object implements the same interface, and can be used anywhere where a normal Deferred object would go.

Writing Tests

The underlying Deferred object is fairly lax about input types. It will be happy to accept any iterable, not just generators.

This makes it super easy to use in mocks:

use function Later\lazy;

$this->lazyDependency = lazy([
    $myDependency,
]);

Yet for constant and already-known answers best to use a non-deferred variant:

use function Later\now;

$this->lazyDependency = now($myDependency);

And that's it. No need to go through loops assembling closures and whatnot.

If nothing else, one can make a common mock for it:

$deferredMock = $this->createMock(\Later\Interfaces\Deferred::class);
$deferredMock
    ->expects($this->once())
    ->method('get')
    ->willReturn($myDependency)
;

API Overview

Method Takes Returns
Later\lazy() iterable<T> \Later\Interfaces\Deferred<T>
Later\later() A generator callback for T \Later\Interfaces\Deferred<T>
Later\now() T \Later\Interfaces\Deferred<T>

sanmai/later 适用场景与选型建议

sanmai/later 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 24.82M 次下载、GitHub Stars 达 72, 最近一次更新时间为 2020 年 03 月 11 日, 在 PHP 生态内属于活跃度较高的组件。

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

围绕 sanmai/later 我们能提供哪些服务?
定制开发 / 二次开发

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

  • 总下载量: 24.82M
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 73
  • 点击次数: 12
  • 依赖项目数: 8
  • 推荐数: 0

GitHub 信息

  • Stars: 72
  • Watchers: 2
  • Forks: 3
  • 开发语言: PHP

其他信息

  • 授权协议: Apache-2.0
  • 更新时间: 2020-03-11