承接 offworks/classgen 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

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

offworks/classgen

Composer 安装命令:

composer require offworks/classgen

包简介

A simple, dynamic and programmatically fluent PHP class generator.

README 文档

README

A simple, dynamic and programmatically fluent PHP class generator.

Installation

Through composer

composer require offworks/classgen

Usage

Class creation manipulation

Create a class

$class = new \Classgen\Stub\ClassStub('Acme\Models\Blog');

$class->inherits('BaseModel');

Add property

$property = $class->addProperty('isPublished', 'bool');

$property = $class->addStaticProperty('timestamp', 'bool');

Add method

$method = $class->addMethod('doNothing');

And it'll print the class on casting the object to string.

echo $class;
<?php
namespace Acme\Model;

class Blog extends BaseModel
{
    /**
     * @var bool
     */
    protected $isPublished;
    
    /**
     * @var bool
     */
    protected static $timestamp;
    
    /**
     * @return void
     */
    public function doNothing()
    {
    
    }
}

Class's method manipulation

Generate a stub from \Closure

Through the initialize($mixed) method, the stub will automatically generate the method's params, if \Closure is passed.

// 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;
    });

This code might generate a method stub something like 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;
}

Generate the stub directly from string

$method = $class->addMethod('isPublished');

$method->comment('Check whether article is published')
   ->returnAs('bool')
   ->initialize('return $this->isPublished == true;');

Generate a method stub something like this :

/**
 * Check whether article is published
 *
 * @return bool
public function isPublished()
{
    return $this->isPublished == true;
}

More nested code block

$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;');
});

Generate a method stub something like this :

/**
 * @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;
}

Saving the class to the desired path

file_put_contents

Most straight forward way

file_put_contents(__DIR_.'/app/MyClass.php', $class->toString());

With generator

PSR-4 based generator

$generator = new \Classgen\Generator\Psr4('Acme\\', __DIR__.'/src');

The generator has it's own sort of Collection of classes. Adding a class is simply by passing class name (it'll create a new), or add existing instance.

$generator->addClass($class); //existing

$class = $generator->addClass('Acme/Routes/AdminRoute');

And generate once you're done.

$generator->generate();

It will generator a structure and file (since we have only one class) similar to this :

/src
  /Model
    Article.php
  /Routes
    AdminRoute.php

Flat based generator

Generate classes in a single directory

$generator = new \Classgen\Generator\Flat(__DIR__.'/src/Payments');

$generator->addClass('App\Payment\Paypal');
$generator->addClass('App\Payment\Cash');
$generator->addClass('App\Payment\Cc');

And generate

$generator->generate();

API

\Classgen\Stub\ClassStub

  • inherits(string $parentClass) : self
  • interfaces(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

License

See MIT License

Thank you!

Hope you like it!

offworks/classgen 适用场景与选型建议

offworks/classgen 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 24 次下载、GitHub Stars 达 2, 最近一次更新时间为 2016 年 11 月 09 日, 在 PHP 生态内属于活跃度较高的组件。

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

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

围绕 offworks/classgen 我们能提供哪些服务?
定制开发 / 二次开发

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

  • 总下载量: 24
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 2
  • 点击次数: 12
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2016-11-09