offworks / classgen
一个简单、动态且编程流畅的PHP类生成器。
v1.0.0
2016-11-12 04:44 UTC
This package is not auto-updated.
Last update: 2024-09-23 13:31:39 UTC
README
一个简单、动态且编程流畅的PHP类生成器。
安装
通过 composer
composer require offworks/classgen
用法
类创建操作
创建一个类
$class = new \Classgen\Stub\ClassStub('Acme\Models\Blog');
$class->inherits('BaseModel');
添加属性
$property = $class->addProperty('isPublished', 'bool');
$property = $class->addStaticProperty('timestamp', 'bool');
添加方法
$method = $class->addMethod('doNothing');
然后它会在将对象转换为字符串时打印类。
echo $class;
<?php
namespace Acme\Model;
class Blog extends BaseModel
{
/**
* @var bool
*/
protected $isPublished;
/**
* @var bool
*/
protected static $timestamp;
/**
* @return void
*/
public function doNothing()
{
}
}
类的操作
从 \Closure 生成占位符
通过 initialize($mixed) 方法,如果传递了 \Closure,则会自动生成方法的参数。
// sample
$method = $class->addMethod('setAtPublished');
$method->comment('Publish the article')
->returnAs('self')
->initialize(function($published = true)
{
// mark this article as published
$this->setPublished($published ? 1 : 0);
return $this;
});
此代码可能生成类似这样的方法占位符
/**
* Publish the article
*
* @param $published
* @return self
*/
public function setAsPublished($published = true)
{
// mark this article as published
$this->setPublished($published ? 1 : 0);
return $this;
}
直接从字符串生成占位符
$method = $class->addMethod('isPublished');
$method->comment('Check whether article is published')
->returnAs('bool')
->initialize('return $this->isPublished == true;');
生成类似这样的方法占位符
/**
* Check whether article is published
*
* @return bool
public function isPublished()
{
return $this->isPublished == true;
}
更复杂的代码块
$method = $class->addMethod('isPopular')->returnAs('bool');
// code() method let you code within a safe handler. (enough with pollution)
$method->code(function($code)
{
$code->addBlock('if($this->likes > 1000)', function($code)
{
$code->addBlock('if($this->comments->count > 100)', function($code)
{
$code->write('return true;');
});
});
// continued block will skip the line break
$code->addContinuedBlock('else if($this->isDeleted())', function($code)
{
$code->write(function()
{
throw new \Exception('The article has been deleted. Throw! throw!');
});
});
$code->write('return false;');
});
生成类似这样的方法占位符
/**
* @return bool
*/
public function isPopular()
{
if($this->likes > 1000)
{
if($this->comments->count > 100)
{
return true;
}
}
else if($this->isDeleted())
{
throw new \Exception('The article has been deleted. Throw! throw!');
}
return false;
}
将类保存到所需路径
file_put_contents
最直接的方法
file_put_contents(__DIR_.'/app/MyClass.php', $class->toString());
使用生成器
基于 PSR-4 的生成器
$generator = new \Classgen\Generator\Psr4('Acme\\', __DIR__.'/src');
生成器有自己的类集合。通过传递类名(它会创建一个新的)或添加现有实例来添加类。
$generator->addClass($class); //existing
$class = $generator->addClass('Acme/Routes/AdminRoute');
完成后生成。
$generator->generate();
它将生成结构和文件(因为我们只有一个类)类似于这个
/src
/Model
Article.php
/Routes
AdminRoute.php
基于平面的生成器
在单个目录中生成类
$generator = new \Classgen\Generator\Flat(__DIR__.'/src/Payments');
$generator->addClass('App\Payment\Paypal');
$generator->addClass('App\Payment\Cash');
$generator->addClass('App\Payment\Cc');
然后生成
$generator->generate();
API
\Classgen\Stub\ClassStub
- 继承(string $parentClass) : self
- 接口(array $interfaces) : self
- addMethod(string $name, mixed $initialCode) : \Classgen\Stub\MethodStub'
- getName() : string
- getNamespace() : string
- getShortClassname() : string
- addProperty(string $name, string $type = null) : \Classgen\Stub\PropertyStub
- addStaticProperty(string $name, string $type = null) : Classgen\Stub\PropertyStub
- addMethod(string $name, mixed $code = null) : \Classgen\Stub\MethodStub
- addStaticMethod(string $name, mixed $code = null) : \Classgen\Stub\MethodStub
\Classgen\Stub\PropertyStub
- value(string $value, bool $asString = false) : self
- setAsStatic(bool $static = true) : self
- visibility(string $string) : self
\Classgen\Stub\MethodStub
- initialize(mixed $code) : self
- returnAs(string $type) : self
- visibility(string $visibility) : self
- code(\Closure $handler) : self
- getCode() : \Classgen\Stub\CodeStub
- setAsStatic() : self
- setAsAbstract() : self
\Classgen\Stub\CodeStub
- replace(string $find, string $replace) : self
- filter(\Closure $handler) : self
- write(string|\Closure $code) : self
- prepend(string|\Closure $code) : self
- each(\Closure $handler) : self
- addBlock($header, \Closure $handler = null) : \Classgen\Stub\BlockStub
- addContinuedBlock($header, \Closure $handler = null) : \Classgen\Stub\Blockstub
许可证
见 MIT 许可证
谢谢!
希望你喜欢它!