定制 erebot/callable-wrapper 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

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

erebot/callable-wrapper

Composer 安装命令:

composer require erebot/callable-wrapper

包简介

Callable wrapper for PHP < 5.4.0

关键字:

README 文档

README

Warning

This component is now deprecated and is unneeded in newer versions of PHP. Do not use it in new code.

Installation

Download the composer.phar executable or use the installer.

$ curl -sS https://getcomposer.org/installer | php

Create a composer.json that requires Erebot's Callable component.

{
    "require": {
        "erebot/callable-wrapper": "dev-master"
    }
}

Run Composer.

$ php composer.phar install

Usage

To use the wrapper, first include composer's autoloader and call Erebot\\CallableWrapper::initialize(), this will make sure everything is set up properly.

Now, use Erebot\\CallableWrapper::wrap() every time you want to execute some callable code while using the callable typehint.

<?php
    // Load composer's autoloader
    require_once PROJECT_ROOT . DIRECTORY_SEPARATOR . 'vendor' . DIRECTORY_SEPARATOR . 'autoload.php';

    // Initialize the wrapper
    Erebot\CallableWrapper::initialize();

    // Define a function/method that uses the "callable" typehint
    // as you normally would, even for PHP 5.3.x.
    function invokeCode(callable $code) {
        $result = null;
        $code($result);
        return $result;
    }

    // Wrap some code to make it compatible with the typehint.
    // In this case, we used a closure, but you may use anything
    // that is callable by PHP's standards (eg. a function, a method,
    // an anonymous function or an invokable object would be fine too)
    $wrapped = Erebot\CallableWrapper::wrap(
        function (&$retval) {
            $retval = 42;
        }
    );

    // Outputs "int(42)" because the $result from invokeCode()
    // was by reference to the wrapped closure and modified there.
    var_dump(invokeCode($wrapped));
?>

How it works

This is really a two-part process.

Under the hood, the wrapper's initialize method first checks whether it is running on PHP 5.3.x or not. If it is, then Erebot\\CallableInterface gets aliased as callable in every currently-defined namespace. It also defines an autoloader which is in charge of defining that class on the fly in the current namespace when required. This is necessary for code such as the following where callable is used as a variable rather than directly as a typehint:

<?php
    $baseClass = "callable";
    if ($objClass instanceof $baseClass) {
        // ...
    }
?>

Since the callable typehint is only an alias to an interface, this is why you need to wrap your code using the wrap method provided, to convert your callable code into an object compatible with that interface.

This is where the magic of the wrapper's wrap method comes in. It checks whether the given code is actually callable and then identifies that code's signature (names of its arguments, which ones have default values, which ones are passed by reference etc.) It then creates a new class on the fly that implements the Erebot\\CallableInterface interface by defining an __invoke magic method with that same signature.

To speed things up a little and to avoid using too much memory, the wrap method uses a cache, where only a single class is ever created for the same code signature.

Hence, code which relies on this wrapper works the same way both on PHP 5.3.x and on later versions.

License

Erebot's Callable component is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.

Erebot's Callable component is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with Erebot's Callable component. If not, see <http://www.gnu.org/licenses/>.

erebot/callable-wrapper 适用场景与选型建议

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

它主要适用于以下技术方向: 「callable」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

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

围绕 erebot/callable-wrapper 我们能提供哪些服务?
定制开发 / 二次开发

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

  • 总下载量: 1.42k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 3
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

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

其他信息

  • 授权协议: GPL-3.0
  • 更新时间: 2014-03-22