devlabs91/php-code-builder
Composer 安装命令:
composer require devlabs91/php-code-builder
包简介
Swaggest JSON-schema enabled PHP code builder
关键字:
README 文档
README
This library generates PHP mapping structures defined by JSON schema
using swaggest/json-schema.
Example
You need to add swaggest/json-schema to your dependencies.
<?php require_once __DIR__ . '/vendor/autoload.php'; $schemaData = json_decode(<<<'JSON' { "type": "object", "properties": { "id": {"type": "integer"}, "name": {"type": "string"}, "parent": {"$ref": "#"}, "children": {"type": "array", "items": {"$ref": "#"}}, "info": {"$ref": "#/definitions/info"} }, "definitions": { "info": { "type": "object", "properties": { "lastName": {"type": "string"}, "birthDate": {"type": "string", "format": "date-time"}, "options": {"$ref": "#/definitions/options"} } }, "options": { "type": "object", "properties": { "rememberSession": {"type": "boolean"}, "allowNotifications": {"type": "boolean"} } } } } JSON ); $swaggerSchema = \Swaggest\JsonSchema\Schema::import($schemaData); $appPath = realpath(__DIR__ . '/tests/src/Tmp') . '/Example'; $appNs = 'Swaggest\PhpCodeBuilder\Tests\Tmp\Example'; $app = new \Swaggest\PhpCodeBuilder\App\PhpApp(); $app->setNamespaceRoot($appNs, '.'); $builder = new \Swaggest\PhpCodeBuilder\JsonSchema\PhpBuilder(); $builder->buildSetters = true; $builder->makeEnumConstants = true; $builder->classCreatedHook = new \Swaggest\PhpCodeBuilder\JsonSchema\ClassHookCallback( function (\Swaggest\PhpCodeBuilder\PhpClass $class, $path, $schema) use ($app, $appNs) { $desc = ''; if ($schema->title) { $desc = $schema->title; } if ($schema->description) { $desc .= "\n" . $schema->description; } if ($fromRefs = $schema->getFromRefs()) { $desc .= "\nBuilt from " . implode("\n" . ' <- ', $fromRefs); } $class->setDescription(trim($desc)); $class->setNamespace($appNs); if ('#' === $path) { $class->setName('User'); // Class name for root schema } elseif (strpos($path, '#/definitions/') === 0)) { $class->setName(\Swaggest\PhpCodeBuilder\PhpCode::makePhpClassName( substr($path, strlen('#/definitions/')))); } $app->addClass($class); } ); $builder->getType($swaggerSchema); $app->clearOldFiles($appPath); $app->store($appPath);
Creating and exporting an instance
$user = new \Swaggest\PhpCodeBuilder\Tests\Tmp\Example\User(); $user->name = "John"; $user->info = (new \Swaggest\PhpCodeBuilder\Tests\Tmp\Example\Info()) ->setLastName("Doe") ->setBirthDate("1980-01-01") ->setOptions( (new \Swaggest\PhpCodeBuilder\Tests\Tmp\Example\Options()) ->setRememberSession(true) ->setAllowNotifications(false) ); // No exception on exporting valid data $jsonData = \Swaggest\PhpCodeBuilder\Tests\Tmp\Example\User::export($user); // {"name":"John","info":{"lastName":"Doe","birthDate":"1980-01-01","options":{"rememberSession":true,"allowNotifications":false}}} echo json_encode($jsonData); // Setting invalid value (integer instead of string) $user->name = 123; // Exception: String expected, 123 received at #->$ref[#]->properties:name $jsonData = \Swaggest\PhpCodeBuilder\Tests\Tmp\Example\User::export($user);
Creating class instance from raw data
// Importing raw data to entity class instance will do validation and mapping $user = \Swaggest\PhpCodeBuilder\Tests\Tmp\Example\User::import( json_decode('{"name":"John","info":{"lastName":"Doe","birthDate":"1980-01-01","options":{"rememberSession":true,"allowNotifications":false}}}') ); var_dump($user->info->options->allowNotifications); // bool(false)
devlabs91/php-code-builder 适用场景与选型建议
devlabs91/php-code-builder 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 3.72k 次下载、GitHub Stars 达 0, 最近一次更新时间为 2019 年 01 月 09 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「library」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 devlabs91/php-code-builder 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 devlabs91/php-code-builder 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 devlabs91/php-code-builder 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Inbox pattern process implementation for your Laravel Applications
Core library that defines common interfaces used by the rest of the intahwebz..
Amqp classes
Provides command to manage a web library directory
CakePHP Bitly Plugin
some useful library of the php
统计信息
- 总下载量: 3.72k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 1
- 依赖项目数: 2
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2019-01-09