kanel/enuma

该软件包最新版本(1.0.0)没有可用的许可信息。

动态创建和编辑php类、接口和特性

1.0.0 2018-03-04 20:25 UTC

This package is not auto-updated.

Last update: 2024-09-24 21:00:33 UTC


README

帮助您动态创建和编辑php类、接口和特性的软件包

build

创建类、接口和特性

基本用法

  1. 创建一个类
$phpClass = new PhpClass('Foo');

$enuma = new Enuma();
$enuma->save($phpClass, 'path/to/a/file);

将创建或重写文件

<?php

class Foo
{

}
  1. 创建一个接口
$phpInterface = new PhpInterface('Foo');

$enuma = new Enuma();
$enuma->save($phpInterface, 'path/to/a/file);

将创建或重写文件

<?php

interface Foo
{

}
  1. 创建一个特性
$phpTrait = new PhpTrait('Foo');

$enuma = new Enuma();
$enuma->save($phpTrait, 'path/to/a/file);

将创建或重写文件

<?php

trait Foo
{

}

高级用法

编码风格

默认编码风格是PSR2。如果您需要自定义,您可以

  1. 编码

默认PSR2编码是UTF-8,如果您想使用自己的

$customCodingStyle = new CustomCodingStyle();
$customCodingStyle->setEncoding('your encoding');
  1. PHP闭合标签

默认行为是没有

$customCodingStyle = new CustomCodingStyle();
$customCodingStyle->setUsePhpClosingTag(true);
  1. 缩进

默认行为是4个空格,如果您想更改它(只允许空格字符)

$customCodingStyle = new CustomCodingStyle();
$customCodingStyle->setIndentation("\t");
  1. 类花括号

默认行为是将类花括号放在新行,如果您想将它们放在类同一行上

$customCodingStyle = new CustomCodingStyle();
$customCodingStyle->setClassBracesInNewLine(false);

//will generate : 

class A {

}
  1. 方法花括号

默认行为是将方法花括号放在新行,如果您想将它们放在方法同一行上

$customCodingStyle = new CustomCodingStyle();
$customCodingStyle->setMethodBracesInNewLine(false);

//will generate : 

class A 
{
    public function aaa() {
    
    }
}
  1. Unix换行符

默认行为是在类花括号关闭后有一个额外的换行符,如果您不想要它

$customCodingStyle = new CustomCodingStyle();
$customCodingStyle->setUnixLineFeedEnding(false);
  1. Windows换行符

默认行为是Unix换行符\n。如果您想要Windows换行符\r\n

$customCodingStyle = new CustomCodingStyle();
$customCodingStyle->useWindowsNewLine(true);
  1. 数组注解

默认行为是短注解[]。如果您想使用标准注解array()

$customCodingStyle = new CustomCodingStyle();
$customCodingStyle->useShortArrayNotation(false);
  1. 自动注释

默认行为是为方法添加自动的@param和@return注释。如果您不想要它们

$customCodingStyle = new CustomCodingStyle();
$customCodingStyle->setAutoComments(false);

类创建

在创建类/接口/特性时可以定义许多内容

  1. 编码风格
$customCodingStyle = new CustomCodingStyle();
$customCodingStyle->setClassBracesInNewLine(false);

$phpClass = new PhpClass('Foo', $customCodingStyle);

$enuma = new Enuma();
echo $enuma->stringify($phpClass);

将输出

<?php

class Foo {

}
  1. 命名空间

适用于:PhpClass,PhpInterface和PhpTrait

$phpClass = new PhpClass('Foo');
$phpClass->namespace('My\\Name\\Space');

$enuma = new Enuma();
echo $enuma->stringify($phpClass);

将输出

<?php

namespace My\Name\Space; 

class Foo
{

}
  1. 使用类

适用于:PhpClass,PhpInterface和PhpTrait

$phpClass = new PhpClass('Foo');
$phpClass->use('My\\Package\\Class1');

$enuma = new Enuma();
echo $enuma->stringify($phpClass);

将输出

<?php

use My\Package\Class1;

class Foo
{

}
  1. 类注释

适用于:PhpClass,PhpInterface和PhpTrait

$phpClass = new PhpClass('Foo');
$phpClass
    ->namespace('My\\Name\\Space')
    ->use('My\\Package\\Class1')
    ->addComment("This is my class\n@package My\\Name\\Space");

$enuma = new Enuma();
echo $enuma->stringify($phpClass);

将输出

<?php

namespace My\Name\Space;

use My\Package\Class1;

/**
 * This is my class
 * @package My\Name\Space
 */
class Foo
{

}
  1. 将类设为抽象

仅适用于PhpClass

$phpClass = new PhpClass('Foo');
$phpClass
    ->namespace('My\\Name\\Space')
    ->abstract(true);

$enuma = new Enuma();
echo $enuma->stringify($phpClass);

将输出

<?php

namespace My\Name\Space;

abstract class Foo
{

}
  1. 将类设为最终

仅适用于PhpClass

$phpClass = new PhpClass('Foo');
$phpClass
    ->namespace('My\\Name\\Space')
    ->final(true);

$enuma = new Enuma();
echo $enuma->stringify($phpClass);

将输出

<?php

namespace My\Name\Space;

final class Foo
{

}

由于一个类可以是最终的或抽象的,设置一个为true将自动将另一个设置为false。

  1. 扩展一个类

仅适用于PhpClass

$phpClass = new PhpClass('Foo');
$phpClass
    ->namespace('My\\Name\\Space')
    ->extends('My\\OtherPackage\\Class2');

$enuma = new Enuma();
echo $enuma->stringify($phpClass);

将输出

<?php

namespace My\Name\Space;

use My\OtherPackage\Class2;

final class Foo extends Class2
{

}
  1. 实现接口

仅适用于PhpClass

$phpClass = new PhpClass('Foo');
$phpClass
    ->namespace('My\\Name\\Space')
    ->implements('My\\OtherPackage\\Interface1')
    ->implements('My\\OtherPackage\\Interface2');

$enuma = new Enuma();
echo $enuma->stringify($phpClass);

将输出

<?php

namespace My\Name\Space;

use My\OtherPackage\Interface1;
use My\OtherPackage\Interface2;

class Foo implements Interface1, Interface2
{

}
  1. 使用一个特性

适用于PhpClass和PhpTrait

$phpClass = new PhpClass('Foo');
$phpClass
    ->namespace('My\\Name\\Space')
    ->useTrait('My\\OtherPackage\\Trait1')
    ->useTrait('My\\OtherPackage\\Trait2');

$enuma = new Enuma();
echo $enuma->stringify($phpClass);

将输出

<?php

namespace My\Name\Space;

use My\OtherPackage\Trait1;
use My\OtherPackage\Trait2;

class Foo
{
    use Trait1;
    use Trait2;
}
  1. 添加一个常量

适用于PhpClass和PhpInterface

$phpClass = new PhpClass('Foo');
$phpClass
    ->namespace('My\\Name\\Space')
    ->addConst(new Constant('MY_CONST', true));
    ->addConst(new Constant('MY_OTHER_CONST', array(1, 2, 3));


$enuma = new Enuma();
echo $enuma->stringify($phpClass);

将输出

<?php

namespace My\Name\Space;

class Foo
{
    const My_CONST = true;
    const MY_OTHER_CONST = [1, 2, 3];
}

10.1. 添加一个属性

适用于PhpClass和PhpTrait

$phpClass = new PhpClass('Foo');
$phpClass
    ->namespace('My\\Name\\Space')
    ->addProperty(new Property('property1'));

$enuma = new Enuma();
echo $enuma->stringify($phpClass);

将输出

<?php

namespace My\Name\Space;

class Foo
{
    public $property1;
}

10.2. 设置属性的可见性

您可以使用Hint类常量指定属性的可见性

Kanel\Enuma\Hint\Visibility::PROTECTED;
Kanel\Enuma\Hint\Visibility::PUBLIC;    <-- default
Kanel\Enuma\Hint\Visibility::PRIVATE;
$phpClass = new PhpClass('Foo');
$phpClass
    ->namespace('My\\Name\\Space')
    ->addProperty(new Property('property1', Visibility::PROTECTED));

$enuma = new Enuma();
echo $enuma->stringify($phpClass);

将输出

<?php

namespace My\Name\Space;

class Foo
{
    protected $property1;
}

10.3. 将属性设为静态

$property = new Property('property1', Visibility::PROTECTED);
$property->setIsStatic(true);

$phpClass = new PhpClass('Foo');
$phpClass
    ->namespace('My\\Name\\Space')
    ->addProperty($property);

$enuma = new Enuma();
echo $enuma->stringify($phpClass);

将输出

<?php

namespace My\Name\Space;

class Foo
{
    protected static $property1;
}

10.4. 设置属性的默认值

$property = new Property('property1', Visibility::PROTECTED);
$property->setValue(null);

$phpClass = new PhpClass('Foo');
$phpClass
    ->namespace('My\\Name\\Space')
    ->addProperty($property);

$enuma = new Enuma();
echo $enuma->stringify($phpClass);

将输出

<?php

namespace My\Name\Space;

class Foo
{
    protected static $property1 = null;
}
  1. 添加一个方法

适用于:PhpClass,PhpInterface和PhpTrait

$phpClass = new PhpClass('Foo');
$phpClass
    ->namespace('My\\Name\\Space')
    ->addMethod(new Method('bar'));

$enuma = new Enuma();
echo $enuma->stringify($phpClass);

将输出

<?php

namespace My\Name\Space;

class Foo
{
    /**
     * @return mixed 
     */
    public function bar()
    {
        
    }
}

11.1. 添加方法可见性

对于接口,可见性始终为public

$phpClass = new PhpClass('Foo');
$phpClass
    ->namespace('My\\Name\\Space')
    ->addMethod(new Method('bar', Visibility::PROTECTED));

$enuma = new Enuma();
echo $enuma->stringify($phpClass);

将输出

<?php

namespace My\Name\Space;

class Foo
{
    /**
     * @return mixed 
     */
    protected function bar()
    {
        
    }
}

11.2. 将方法设为静态

$method = new Method('bar', Visibility::PROTECTED);
$method->setIsStatic(true);

$phpClass = new PhpClass('Foo');
$phpClass
    ->namespace('My\\Name\\Space')
    ->addMethod($method);

$enuma = new Enuma();
echo $enuma->stringify($phpClass);

将输出

<?php

namespace My\Name\Space;

class Foo
{
    /**
     * @return mixed 
     */
    protected static function bar()
    {
        
    }
}

11.3. 将方法设为抽象(并隐式地将类设为抽象)

$method = new Method('bar', Visibility::PROTECTED);
$method->setIsAbstract(true);

$phpClass = new PhpClass('Foo');
$phpClass
    ->namespace('My\\Name\\Space')
    ->addMethod($method);

$enuma = new Enuma();
echo $enuma->stringify($phpClass);

将输出

<?php

namespace My\Name\Space;

abstract class Foo
{
    /**
     * @return mixed 
     */
    abstract protected function bar();
}

11.4. 将方法设为最终

$method = new Method('bar', Visibility::PROTECTED);
$method->setIsFinal(true);

$phpClass = new PhpClass('Foo');
$phpClass
    ->namespace('My\\Name\\Space')
    ->addMethod($method);

$enuma = new Enuma();
echo $enuma->stringify($phpClass);

将输出

<?php

namespace My\Name\Space;

class Foo
{
    /**
     * @return mixed 
     */
    final protected function bar()
    {
        
    }
}

由于一个方法可以是最终的或抽象的,设置一个为true将自动将另一个设置为false。

11.5. 添加方法注释

$method = new Method('bar', Visibility::PROTECTED);
$method->setComment('This is my function');

$phpClass = new PhpClass('Foo');
$phpClass
    ->namespace('My\\Name\\Space')
    ->addMethod($method);

$enuma = new Enuma();
echo $enuma->stringify($phpClass);

将输出

<?php

namespace My\Name\Space;

class Foo
{
    /**
     * This is my function
     * @return mixed 
     */
    protected function bar()
    {
        
    }
}

11.6. 添加方法返回类型

$method = new Method('bar', Visibility::PROTECTED);
$method->setReturnType('bool');

$phpClass = new PhpClass('Foo');
$phpClass
    ->namespace('My\\Name\\Space')
    ->addMethod($method);

$enuma = new Enuma();
echo $enuma->stringify($phpClass);

将输出

<?php

namespace My\Name\Space;

class Foo
{
    /**
     * This is my function
     * @return bool
     */
    protected function bar(): bool
    {
        
    }
}

11.6. 添加方法参数

$method = new Method('bar', Visibility::PROTECTED);
$parameter = new Parameter("test");
$parameter->setValue(true);
$parameter->setType('bool');

$method->addParameter($parameter);

$phpClass = new PhpClass('Foo');
$phpClass
    ->namespace('My\\Name\\Space')
    ->addMethod($method);

$enuma = new Enuma();
echo $enuma->stringify($phpClass);

将输出

<?php

namespace My\Name\Space;

class Foo
{
    /**
     * @param bool $test   
     * @return mixed
     */
    protected function bar(bool $test = true)
    {
        
    }
}

编辑类

即将推出