定制 nannehuiges/jsend 二次开发

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

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

nannehuiges/jsend

Composer 安装命令:

composer require nannehuiges/jsend

包简介

A simple PHP implementation of the JSend specification.

README 文档

README

Build Status Build Status Build Status Build Status

Code Climate
Issue Count

Total Downloads

JSend

A simple PHP implementation of the JSend specification.

Usage

use JSend\JSendResponse;

New response

$success = new JSendResponse('success', $data);
$fail = new JSendResponse('fail', $data);
$error = new JSendResponse('error', $data, 'Not cool.', 9001);
$success = JSendResponse::success($data);
$fail = JSendResponse::fail($data);
$error = JSendResponse::error('Not cool.', 9001, $data);

Note: an InvalidJSendException is thrown if the status is invalid or if you're creating an error without a message.

Convert JSendResponse to JSON

__toString() is overridden to encode JSON automatically.

$json = $success->encode();
$json = (string) $success;

As JSendResponse is JsonSerializable, you can use the object directly in json_encode

json_encode($success);

Setting flags

You can set flags if needed:

$success->setEncodingOptions(\JSON_PRETTY_PRINT | \JSON_BIGINT_AS_STRING);
$json = $success->encode();

Convert JSON to JSendResponse

try {
    $response = JSendResponse::decode($json);
} catch (InvalidJSendException $e) {
    echo "You done gone passed me invalid JSend.";
}

Send JSON as HTTP Response

This sets the Content-Type header to application/json and spits out the JSON.

$jsend = new JSendResponse('success', $data);
$jsend->respond();

Get info

$isSuccess = $response->isSuccess();
$isError = $response->isError();
$isFail = $response->isFail();
$status = $response->getStatus();
$data = $response->getData();
$array = $response->asArray();

Additionally, you can call the following methods on an error. A BadMethodCallException is thrown if the status is not error, so check first.

if ($response->isError()) {
    $code = $response->getErrorCode;
    $message = $response->getErrorMessage;
}

Development

For your convenience, there is a dockerfile with the right dependencies (php, composer) available. Please use those to run various things (composer, phpunit, etc). You will need docker installed, but you don't need PHP or composer or any of the other dependencies.

Setting up and using a local environment

To start using the local environment for testing and debugging all you have to is open a shell in the root folder of where this project is checked out. Then run the following command.

make build install

This command should be run occasionally to keep the local environment up to date. For instance when the composer dependencies are changed.

Using the shell

To open a shell in the docker container run the following command.

make shell

Available commands are in /bin

Running the code quality tools locally

We use a variety of tools to keep the code quality of the library high. To run one the tools you only need to run

make <tool_name>

Available tools:

  • phpstan PHPStan is static analyser tool that can detect various code issues.
  • phpunit PHPUnit is the unit testing framework we use for this library.
  • codeclimate CodeClimate

Notes

  • Note that the composer.lock file is ignored.

Credits

The library was written by Jamie Schembri. It has been transfered to the current account Nanne Huiges in december 2015.

nannehuiges/jsend 适用场景与选型建议

nannehuiges/jsend 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 387.19k 次下载、GitHub Stars 达 31, 最近一次更新时间为 2015 年 12 月 11 日, 在 PHP 生态内属于活跃度较高的组件。

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

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

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

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

  • 总下载量: 387.19k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 31
  • 点击次数: 20
  • 依赖项目数: 2
  • 推荐数: 1

GitHub 信息

  • Stars: 31
  • Watchers: 2
  • Forks: 5
  • 开发语言: PHP

其他信息

  • 授权协议: BSD-3-Clause
  • 更新时间: 2015-12-11