kanel / phpclasseditor
通过动态添加代码来编辑 php 文件/类
Requires
- php: >=7.0
Requires (Dev)
- phpunit/phpunit: ^6.5
This package is not auto-updated.
Last update: 2024-09-20 20:56:56 UTC
README
此库允许您通过向其中注入代码来动态编辑 php 类文件
工作原理
重要:除非您调用保存方法,否则文件不会更改。每个编辑 php 类文件的函数都会返回文件的新内容(添加属性/方法之后)
1 缩进
在创建任何属性或方法之前,您需要定义要使用的缩进。
默认为 4 个空格。
如果您想更改默认行为
$classEditor = new ClassEditor('path_to_php_class_file'); $classEditor->useIndentation(Indentation::TABS, 1);
上面的示例意味着您想要默认缩进为 1 个制表符。
您只能使用
Indentation::TABS;
Indentation::SPACES;
2 向类中添加属性
您可以将属性注入到 php 类文件中。
$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();
上面的示例向类中添加了三个属性
class MyClass { protected $property1; public $property2; private $property3; ... }
2.1 属性类
"addProperty" 方法接受一个属性类。
属性类必须在构造函数中至少有一个名称(必填)。
$property = new Property('myProperty');
这将在类中打印
class MyClass { $myProperty; ... }
您还可以在构造函数中设置属性的 "可见性"(可选)。
$property = new Property('myProperty', Visibility::PROTECTED);
所有可见性都在可见性类中定义。
Visibility::PROTECTED
Visibility::PUBLIC
Visibility::PRIVATE
Visibility::NONE //For when you don't want to write the visibility in the php class file
您还可以定义属性的默认值(可选)。
$property = new Property('myProperty', Visibility::PROTECTED, Value::NULL);
将打印
class MyClass { protected $myProperty = null; ... }
您可以写入任何您想要的值(作为字符串),或者使用 Value 类中预定义的值
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
最后,最后一个构造函数参数允许您选择属性是静态(true)还是非静态(false)。
$property = new Property('myProperty', Visibility::PROTECTED, Value::NULL, true);
将打印
class MyClass { protected static $myProperty = null; ... }
构造函数中的所有参数(除名称外)都有指定的设置器,您可以使用
$property = new Property('myProperty'); $property->setVisibility(Visibility::PUBLIC); $property->setDefaultValue('1'); $property->setIsStatic(true);
将打印
class MyClass { public static $myProperty = 1; ... }
3 向类中添加方法
您可以将方法注入到 php 类文件中。
$class = new ClassEditor($this->phpFileName); $class->addMethod(new Method('bar', Visibility::PROTECTED)); $class->addMethod(new Method('foo', Visibility::PROTECTED)); $classEditor->save();
上面的示例向类中添加了两个方法
class MyClass { .... protected function bar() { } protected function foo() { } }
名称和可见性在构造函数中都是必填项。
所有可见性都在可见性类中定义。
Visibility::PROTECTED
Visibility::PUBLIC
Visibility::PRIVATE
Visibility::NONE //For when you don't want to write the visibility in the php class file
您可以使用方法类定义多个内容:它是静态的?抽象的?最终的?是否有返回类型?是否有文档注释?是否有参数?
$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');
由于方法不能同时是最终的和抽象的,所以最后一个设置为 true 的将被采用。
因此这将打印
class MyClass { .... /** * This is my bar function * @return int */ abstract public static function bar(): int { } }
注意返回类型可以是您发送的任何内容,您也可以使用在 Type 类中定义的常量
Type::ARRAY
Type::MIXED
Type::BOOL
Type::CALLABLE
Type::FLOAT
Type::INT
Type::STDCLASS
Type::STRING
Type::NONE
您还可以向方法添加参数。方法类有一个 "addParameter" 函数,允许您添加所需数量的参数。以下是可以使用的所有可能的参数类
ArrayParameter
BoolParameter
CallableParameter
FloatParameter
IntParameter
MixedParameter //When your parameter does not have a single type or none at all
StdClassParameter
StringParameter
ClassParameter
每个参数类在其构造函数中都有一个必填的名称。
当有默认值时,它们还接受第二个参数(除了 ClassParameter,见下文)
您可以使用预定义的值或添加您自己的值
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
最后一个参数是一个布尔值,它告诉参数是否是 splat(可选)。splat 是 ... 注释,允许发送多个参数
请注意,每个参数
ClassParameter 的构造函数略有不同,因为它将完整的类名作为第二个参数。
$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));
这将打印
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 { } }
您必须注意到参数和返回类型将始终生成文档注释