kshabazz/json-to-src 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

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

kshabazz/json-to-src

Composer 安装命令:

composer require kshabazz/json-to-src

包简介

A program to convert a JSON file to source code.

README 文档

README

Convert JSON to source code.

This tool takes a JSON file, along with other required input, and on success will parse the JSON and output (PHP by default) source files. It works recursively on the JSON, and will parse nested keys where the value is an object. In addition, an array that contain objects will produce a class, but only one since all elements are assumed to be of the same type.

Features

  • Parses JSON into an hash-array to be used in a template engine (see schema).
  • Outputs file(s) to a directory of your choice (assuming it is writable).
  • Can turn off/on generating unit tests.
  • Optional set a namespace.
  • set a callaback to manipulate the output of the source code generated.

Using the Default PHP template

  • Will convert to PHP, turning fields into properties with getters and setters (fluent style).
  • Optional scalar type hints for PHP 7 Function signatures. Example:
    /**
     * @var int
     */
    private $client;
    
    /**
     * Get ClientId.
     *
     * @param int $ClientId
     * @return $this;
     */
    public function setClientId(int $ClientId)
    {
        $this->ClientId = $ClientId; 
    }

Side-effect

Some things to make note of when using this tool include:

  • Bad characters like "$,-" Will automatically be removed from keys in the JSON file.
  • A field that is an array and has an object as its first element will result in a class being generated.
  • All objects found will produce a class file, the key that contained that object will be used as the name.
  • Will generate namespaces for nested objects to prevent class name collision.

Here's a rough idea of how it handles JSON types:

JSON PHP
null null
123 int
1.00 double (a.k.a float)
"string" string (no interpolation, ex: 'string')
"Company": {"name": "Kohirens"} class Company {}
"Clients": [{"id":1}] class Clients { private $id; getId,setId }
"Company": {} Will produce no class, since the class would have no properties.

Installation

Composer.json

composer require --dev kshabazz/json-to-src

- or -

{
  "require": {
    "kshabazz/json-to-src": "^1.0"
  }
}

Shell or Command Line

composer.phar require --dev kshabazz/json-to-src

Template Data Schema

This is the array that is passed to the tempalte engines render method:

$renderData = [
    "className" => Company
    "classProperties" => [
        [
            "name" => company
            "type" => string
            "isCustomType" => 
            "paramType" => string
            "value" => Kohirens
            "arrayType" => 
        ],
        [
            "name" => employees
            "type" => array
            "isCustomType" => 
            "paramType" => array
            "value" => ""
            "arrayType" => employees
        ],
        [
            "name" => location
            "type" => Location
            "isCustomType" => 1
            "paramType" => Location
            "value" => stdClass Object
                (
                    "type" => Point
                    "coordinates" => Array
                        (
                            [0" => -83.0466419
                            [1" => 42.3323378
                        )

                    "city" => Detroit
                    "state" => MI
                    "zip_code" => 48226
                )

            "arrayType" => 
        ],
        [
            "name" => categories
            "type" => array
            "isCustomType" => 
            "paramType" => array
            "value" => []
            "arrayType" => categories
        ]
    ],
    "classNamespace" => Tests
];

Set A Callback

You can also set a callback that will recieve the render data, to control thins such as the class name, namespace, and etc. Take the following example.

Callback script: preRenderCallback.php

return function (array $renderData, $isUnitTest) {
    if (!$isUnitTest) {
        $renderData['classNamespace'] .= '\\Bar';
    }
    
    return $renderData;
};

Pass preRenderCallback.php to the command line script:

jsontosrc -n Tests -c preRenderCallback.php company.json Company tmp

Command Line

The "jsontosrc" command line script take several arguments and options.

-n Takes a string to use as a namespace. -u A separate directory to output unit test. -a Set the property access, the default is "private." -c A callback function to modify template data before render. -r Control how deep to go for nested objects, the default is 20. -p An optional string to be used as a namespace prefix. -v Add debug messages to the output. -t Turn on PHP 7 type hints.

kshabazz/json-to-src 适用场景与选型建议

kshabazz/json-to-src 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 1.23k 次下载、GitHub Stars 达 0, 最近一次更新时间为 2017 年 02 月 15 日, 在 PHP 生态内属于活跃度较高的组件。

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

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

围绕 kshabazz/json-to-src 我们能提供哪些服务?
定制开发 / 二次开发

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2017-02-15