承接 saad/api-debugger 相关项目开发

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

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

saad/api-debugger

Composer 安装命令:

composer require saad/api-debugger

包简介

Easily debug your JSON API.

README 文档

README

Travis

Easily debug your JSON API.

This Package is an enhancement of Laravel-API-Debugger Package to add compatibility for Lumen framework

When you are developing JSON API sometimes you need to debug it, but if you will use dd() or var_dump() you will break the output that will affect every client that is working with your API at the moment. Debugger is made to provide you with all your debug information and not corrupt the output.

{
  "posts": [
    {
      "id": 1,
      "title": "Title 1",
      "body": "Body 1"
    },
    {
      "id": 2,
      "title": "Title 2",
      "body": "Body 2"
    }
  ],
  "meta": {
    "total": 2
  },
  "debug": {
    "database": {
      "total": 2,
      "time": 2.72,
      "items": [
        {
          "connection": "accounts",
          "query": "select * from `users` where `email` = 'john.doe@acme.com' limit 1;",
          "time": 0.38
        },
        {
          "connection": "posts",
          "query": "select * from `posts` where `author` = '1';",
          "time": 1.34
        },
        {
          "connection": "posts",
          "query": "select * from `posts` where `author` = '1';",
          "time": 1
        }
      ],
      "duplicated": [
        {
          "connection": "posts",
          "query": "select * from `posts` where `author` = '1';",
          "total_time": 2.34,
          "executions_count": 2
        }
      ]
    },
    "dump": [
      "foo",
      [
        1,
        2,
        "bar"
      ]
    ]
  }
}

Installation

This help is for Laravel 5.4 only. Readme for earlier versions can be found in the relevant branches of this repo.

PHP >=5.5.9+ or HHVM 3.3+, Composer and Laravel 5.4+ are required.

Installation

composer require saad/api-debugger

For Lumen

1- Register the package service provider

// bootstrap/app.php
 $app->register(Lanin\Laravel\ApiDebugger\ServiceProvider::class);

2- this package will be enabled/disabled according to api-debugger.enabled configuration which depends on this env value ENABLE_API_DEBUG

you can copy the configuration file api-debugger.php to config directory and customize it as you like by doing this, you will have to register the new configuration file in bootstrap/app.php

// bootstrap/app.php
 $app->configure('api-debugger');

For Laravel 5.4

Open up config/app.php and add the following to the providers key.

Lanin\Laravel\ApiDebugger\ServiceProvider::class,

Also you can register a Facade for easier access to the Debugger methods.

'Debugger' => Lanin\Laravel\ApiDebugger\Facade::class,

For Laravel 5.5

package supports package discovery feature.

Json response

Before extension will populate your answer it will try to distinguish if it is a json response. It will do it by validating if it is a JsonResponse instance. The best way to do it is to return response()->json(); in your controller's method.

Also please be careful with what you return. As if your answer will not be wrapped in any kind of data attribute (pages in the example above), frontend could be damaged because of waiting the particular set of attributes but it will return extra debug one.

So the best way to return your responses is like this

$data = [
    'foo' => 'bar',
    'baz' => 1,
];

return response()->json([
    'data' => [
        'foo' => 'bar',
        'baz' => 1,
    ],
]);

For more info about better practices in JSON APIs you can find here http://jsonapi.org/

Debugging

Debugger's two main tasks are to dump variables and collect anny additional info about your request.

Var dump

Debugger provides you with the easy way to dump any variable you want right in your JSON answer. This functionality sometimes very handy when you have to urgently debug your production environment.

$foo = 'foo';
$bar = [1, 2, 'bar'];

// As a helper
lad($foo, $bar);

// or as a facade
\Debugger::dump($foo, $bar);

You can simultaneously dump as many vars as you want and they will appear in the answer.

Note! Of course it it not the best way do debug your production environment, but sometimes it is the only way. So be careful with this, because everyone will see your output, but at least debug will not break your clients.

Collecting data

Note! By default Debugger will collect data ONLY when you set APP_DEBUG=true. So you don't have to worry that someone will see your system data on production.

All available collections can be found in api-debugger.php config that you can publish and update as you wish.

QueriesCollection

This collections listens to all queries events and logs them in connections, query, time structure.

CacheCollection

It can show you cache hits, misses, writes and forgets.

ProfilingCollection

It allows you to measure time taken to perform actions in your code. There are 2 ways to do it.

Automatically:

Debugger::profileMe('event-name', function () {
    sleep(1);
});

Or manually:

Debugger::startProfiling('event-name');
usleep(300);
Debugger::stopProfiling('event-name');

Also helpers are available:

lad_pr_start();
lad_pr_stop();
lad_pr_me();

Extending

You can easily add your own data collections to debug output. Just look at how it was done in the package itself and repeat for anything you want (for example HTTP requests).

Contributing

Please feel free to fork this package and contribute by submitting a pull request to enhance the functionalities.

saad/api-debugger 适用场景与选型建议

saad/api-debugger 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 74.58k 次下载、GitHub Stars 达 11, 最近一次更新时间为 2019 年 01 月 29 日, 在 PHP 生态内属于活跃度较高的组件。

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

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

围绕 saad/api-debugger 我们能提供哪些服务?
定制开发 / 二次开发

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

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

GitHub 信息

  • Stars: 11
  • Watchers: 1
  • Forks: 57
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2019-01-29