memio/spec-gen
Composer 安装命令:
composer require memio/spec-gen
包简介
phpspec extension for better code generation
README 文档
README
This extension for phpspec provides a powerful code generator:
- method generation:
- it inserts method at the end of the class
- it typehints arguments (uses interface when possible)
- it names object arguments after their type (strips
Interfacesuffix from names) - it names scalar arguments after a generic name (
argument) - it adds number on names that could collide (e.g.
$argument1, $argument2)
- constructor generation, same as method except:
- it inserts constructor at the begining of the class
- it inserts properties with initialization for each constructor arguments
Note: Currently it is not possible to provide custom templates to SpecGen (it is not compatible with phpspec templates).
Installation
First install it using Composer:
composer require --dev memio/spec-gen:^0.10.1
Then enable it in phpspec.yml:
extensions: Memio\SpecGen\MemioSpecGenExtension: ~
Version guide:
- using phpspec 6? Then use spec-gen v0.10
- using phpspec 5? Then use spec-gen v0.9
- using phpspec 4? Then use spec-gen v0.8
- using phpspec 3 and PHP 7? Then use spec-gen v0.7
- using phpspec 3 and PHP 5.6? Then use spec-gen v0.6
- using phpspec 2? Then use spec-gen v0.4
Full example
Let's write the following specification:
<?php namespace spec\Vendor\Project; use Vendor\Project\Service\Filesystem; use Vendor\Project\File; use PhpSpec\ObjectBehavior; class TextEditorSpec extends ObjectBehavior { const FILENAME = '/tmp/file.txt'; const FORCE_FILE_CREATION = true; function let(Filesystem $filesystem) { $this->beConstructedWith($filesystem); } function it_creates_new_files(File $file, Filesystem $filesystem) { $filesystem->exists(self::FILENAME)->willReturn(false); $filesystem->create(self::FILENAME)->willReturn($file); $this->open(self::FILENAME, self::FORCE_FILE_CREATION)->shouldBe($file); } }
Executing the specifications (phpspec run) will generate the following class:
<?php namespace Vendor\Project; use Vendor\Project\Service\Filesystem; class TextEditor { private $filesystem; public function __construct(Filesytem $filesystem) { $this->filesystem = $filesystem; } public function open(string $argument1, bool $argument2) { } }
Now we can start naming those generic arguments and write the code.
Want to know more?
You can see the current and past versions using one of the following:
- the
git tagcommand - the releases page on Github
- the file listing the changes between versions
And finally some meta documentation:
Roadmap
- constructor property promotion
- return type hints
- method body (mirror of test method body)
- better scalar argument naming (based on names used in test)
memio/spec-gen 适用场景与选型建议
memio/spec-gen 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 204.7k 次下载、GitHub Stars 达 66, 最近一次更新时间为 2015 年 05 月 12 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「generator」 「php」 「code」 「extension」 「phpspec」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 memio/spec-gen 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 memio/spec-gen 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 memio/spec-gen 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Memio's PrettyPrinter, used to generate PHP code from given Model
Promotional Codes Generator for Laravel 5.1
Zend Captcha bundle
Base Skeleton for Laravel Application
Alfabank REST API integration
Statistic of code usage inspired by Laravel Stats
统计信息
- 总下载量: 204.7k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 67
- 点击次数: 11
- 依赖项目数: 34
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2015-05-12