kanel/phpclasseditor 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

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

kanel/phpclasseditor

Composer 安装命令:

composer require kanel/phpclasseditor

包简介

Edit php files / classes by dynamically adding code to them

README 文档

README

build

This library allows you to dynamically edit php class files by injecting code into them

How it works

Important : unless you call the save method, the file won't be changed Each function that edits the php class file return the new content of the file (after adding properties/methods)

1 Indentation

Before you create any property or method, you need to define what indentation to use.

Default one is 4 spaces.

If you want to change the default behaviour :

$classEditor = new ClassEditor('path_to_php_class_file');
$classEditor->useIndentation(Indentation::TABS, 1);

The example above means you want the default indentation to be 1 tab.

You can only use :

Indentation::TABS;
Indentation::SPACES;

2 Adding properties to a class

You can inject properties into a php class file.

$classEditor = new ClassEditor('path_to_php_class_file');
$classEditor->addProperty(new Property('property1', Visibility::PROTECTED));
$classEditor->addProperty(new Property('property2', Visibility::PUBLIC));
$classEditor->addProperty(new Property('property3', Visibility::PRIVATE));

$classEditor->save();

The example above adds three properties to the class

class MyClass {
    
    protected $property1;
    public $property2;
    private $property3;
    ...

}

2.1 Property class

the "addProperty" method takes in a Property class.

A Property class must have at least a name (mandatory), defined in the construct

$property = new Property('myProperty');

This will print in the class :

class MyClass {
    
    $myProperty;
    ...

}

You can also set a "visibility" to your property in the construct (optional)

$property = new Property('myProperty', Visibility::PROTECTED);

All the visibilities are defined in the Visibility class :

Visibility::PROTECTED
Visibility::PUBLIC
Visibility::PRIVATE
Visibility::NONE      //For when you don't want to write the visibility in the php class file

You can also define the property's default value (optional too)

$property = new Property('myProperty', Visibility::PROTECTED, Value::NULL);

will print

class MyClass {
    
    protected $myProperty = null;
    ...

}

You can write whatever value you want (as string), or use the ones predefined in the Value class:

Value::NULL;
Value::TRUE;
Value::FALSE;
Value::EMPTY_ARRAY;
Value::NO_DEFAULT_VALUE;  //The default behaviour of properties, when you property does not have a default value

Finally, the last construct parameter allows to choose if the property is static (true) or not (false):

$property = new Property('myProperty', Visibility::PROTECTED, Value::NULL, true);

will print

class MyClass {
    
    protected static $myProperty = null;
    ...

}

All the parameters in the constuct (except the name) have designated setters, you can use:

$property = new Property('myProperty');
$property->setVisibility(Visibility::PUBLIC);
$property->setDefaultValue('1');
$property->setIsStatic(true);

will print :

class MyClass {
    
    public static $myProperty = 1;
    ...

}

3 Adding methods to a class

You can inject methods into a php class file.

$class = new ClassEditor($this->phpFileName);
$class->addMethod(new Method('bar', Visibility::PROTECTED));
$class->addMethod(new Method('foo', Visibility::PROTECTED));
$classEditor->save();

The example above adds two methods to the class

class MyClass {
    
    ....
    
    protected function bar()
    {
    
    }
    
    protected function foo()
    {
    
    }

}

The name and the visibility are both mandatory in the construct

All the visibilities are defined in the Visibility class :

Visibility::PROTECTED
Visibility::PUBLIC
Visibility::PRIVATE
Visibility::NONE      //For when you don't want to write the visibility in the php class file

You can define multiple things using the Method class : is it static? abstract? final ? does it have a return type? a doc comment? parameters ?

$method = new Method('bar', Visibility::PUBLIC);
$method->setIsStatic(true);
$method->setIsFinal(true);
$method->setIsAbstract(true);
$method->setReturnType(Type::INT);
$method->setDocComment('This is my bar function');

Since a method can not be both final and abtract, it is the last one set to true that is taken.

so this will print:

class MyClass {
    
    ....
    
    /**
     * This is my bar function
     * @return int
     */
    abstract public static function bar(): int
    {
    
    }

}

Note the the return type can be anything you send, you can also use the consts defined in Type class

Type::ARRAY
Type::MIXED
Type::BOOL
Type::CALLABLE
Type::FLOAT
Type::INT
Type::STDCLASS
Type::STRING
Type::NONE

You can also add parameters to your method. The Method class has an "addParameter" function that allows to you add as many parameters as needed. These are all the possible parameters classes you can use :

ArrayParameter
BoolParameter
CallableParameter
FloatParameter
IntParameter
MixedParameter   //When your parameter does not have a single type or none at all
StdClassParameter
StringParameter
ClassParameter

Each one of these parameters classes takes in a mandatory name in their construct

They also take in a default value as second parameter (except for ClassParameter, see below) when there is one

You can use the predefined ones or add yours

Value::NULL;
Value::TRUE;
Value::FALSE;
Value::EMPTY_ARRAY;
Value::NO_DEFAULT_VALUE;  //The default behaviour of properties, when you property does not have a default value

The last parameter is a boolean that tells if the parameter is a splat or not A splat is the ... annotation that allows to send multiple parameters

Please note also that each parameter

The ClassParameter has a slightly different construct as it takes as a second parameter the full class name

$method = new Method('bar', Visibility::PUBLIC);
$method->setDocComment('This is my bar function');
$method->setReturnType(Type::ARRAY);
$method->addParameter(new MixedParameter('param1'));
$method->addParameter(new IntParameter('param2', 1));
$method->addParameter(new ClassParameter('param3', MyClass::class, Value::NULL));

This will print :

class MyClass {
    
    ....
    
    /**
     * This is my bar function
     * @param mixed $param1
     * @param int $param2
     * @param My\NAMESPACE\MyClass $param3
     * @return array
     */
    public function bar($param1, int $param2 = 1, My\NAMESPACE\MyClass $param3 = null): array
    {
    
    }

}

You must have noted that params and return type will always generate a docComment

kanel/phpclasseditor 适用场景与选型建议

kanel/phpclasseditor 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 15 次下载、GitHub Stars 达 0, 最近一次更新时间为 2018 年 02 月 04 日, 在 PHP 生态内属于活跃度较高的组件。

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

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

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: Unknown
  • 更新时间: 2018-02-04