cocur/ea
Composer 安装命令:
composer require cocur/ea
包简介
Code generation for testing. Because mocks sometimes just don't cut it.
README 文档
README
Code generation for testing. Because mocks sometimes just don't cut it.
Developed by Florian Eckerstorfer in Vienna, Europe.
Features
- Useful if the code under test contains calls to functions like
method_exists() - Generate classes with methods and properties
- Generate setters, getters, hassers, issers, and adders based on properties
Installation
You can install Ea using Composer:
$ composer require cocur/ea:dev-master
Usage
Ea was developed to generate code for testing purposes. For example, if your code under test contains a call to
a function like method_exists() typical mocks don't work.
You can generate a class using ClassFactory. The first argument is the class name and the second argument is the
namespace (which is optional):
$class = new ClassFactory('Foo', 'Foobar'); echo $class->generate();
will output
namespace Foobar; class Foo { }
You can create properties with PropertyFactory and use the addProperty() method to add them to a class. Properties
have a name, a visibility (public, protected or private), can be static and can have a default value.
$property = new PropertyFactory('foo, 'public'); $property->isStatic(true); $property->setDefault('bar'); $property->generate(); // "public static $foo = 'bar';"
If you call the setDefault() method a default value will be set, even if you use null or false as argument. The
value given to setDefault() will be used as default. If you don't call setDefault() no default value will be set.
You can check if a property has a default value using hasDefault() and remove it using removeDefault().
Alternatively the addProperty() method of ClassFactory can create the PropertyFactory object for you, if you
call it with a string as first parameter instead of an instance of PropertyFactory. The second argument is the
visibility, the third static and the fourth the default value. If you call addProperty() with four arguments a
default value will be set, if not no default value will be set.
$class->addProperty('foo', 'protected', false, null); $class->addProperty('bar', 'public', true); echo $class->generate();
will result in
// ... protected $foo = null; public static $bar; // ...
Methods are created using MethodFactory and added to the class by calling addMethod().
$method = new MethodFactory('foo', 'protected'); $method->isStatic(false); $method->addArgument('foo', null, 21); $method->setBody('return 21+$foo'); $method->generate(); // "protected function foo($foo = 21) { return 21+$foo; }"
The constructor takes the name and the visibility (public, protected, private) as arguments and isStatic() sets
whether the method should be static or not. The code passed to setBody() will be returned as-is as body of the
method. The addArgument() method takes the name of the argument as first parameter, the type (like array or
stdClass; none by default) as second argument and the default value as third argument. If three arguments are provided
the default value will always be set; if less arguments are provided no default value will be set.
For some common property access methods, such as setters, getters, hassers, adders and issers, the ClassFactory
provides helper methods to quickly create these based on a given PropertyFactory.
$class->addGetter($property); $class->addSetter($property); $class->addIsser($property); $class->addHasser($property);
The addAdder() method is a little bit different, as it provides more options. The second argument is a bool to
indicate whether the value should be added by a key. The third argument is the singular name of the property. Example:
$bars = PropertyFactory('bars'); $class->addAdder($bars, true, 'bar'); $class->generate(); // -> "public function addBar($key, $bar) { $this->bars[$key] = $bar; }"
Instead of generating the code you can also dynamically create the classes using execute() of the Ea.
Attention. Internally Ea uses eval() to create the classes.
Ea::create()->addClass($class)->execute();
License
The MIT license applies to Ea. For the full copyright and license information, please view the LICENSE file distributed with this source code.
cocur/ea 适用场景与选型建议
cocur/ea 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 7 次下载、GitHub Stars 达 2, 最近一次更新时间为 2015 年 03 月 15 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 cocur/ea 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 cocur/ea 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 7
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 2
- 点击次数: 20
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2015-03-15