jdwil/php-genny

0.1.0 2017-12-17 13:02 UTC

This package is not auto-updated.

Last update: 2024-09-22 07:59:41 UTC


README

概览

PHP-Genny是一个语义代码生成库。它围绕php-parser自带的代码生成API提供流畅的构建器接口,也许更有趣的是,它还提供了智能生成大型互依对象图的API。

安装

composer require jdwil/php-genny

文档

完整的用户文档可以在这里找到。

示例

<?php

use JDWil\PhpGenny\Builder\Builder;
use JDWil\PhpGenny\Builder\Node\Scalar;
use JDWil\PhpGenny\Builder\Node\Variable;
use JDWil\PhpGenny\Type\Class_;
use JDWil\PhpGenny\Type\Interface_;
use JDWil\PhpGenny\Type\Method;
use JDWil\PhpGenny\Type\Property;
use JDWil\PhpGenny\ValueObject\InternalType;
use JDWil\PhpGenny\ValueObject\Visibility;

$b = new Builder();

// Construct a class.

$b
    ->namespace('My\\Classes')
    ->class('MyClass')
        ->makeFinal()
        ->makeAbstract()
        
        ->implements('SomeInterface')
        ->extends('SomeClass')
        
        ->property('foo')
            ->makePrivate()
            ->setType(InternalType::string())
            ->setDefault(Scalar::string('default'))
        ->done()
        
        ->method('getFoo')
            ->return(Variable::named('this')->property('foo'))
        ->done()
    ->done()
;

/**
 * Using the builder above, all nodes must be constructed in the proper order.
 * You can do more complex things using the Type classes...
 */

$someInterface = new Interface_('SomeInterface');
$someClass = new Class_('SomeClass');

$getFoo = new Method('getFoo');
$getFoo->getBody()->return(Variable::named('this')->property('foo'));

$class = new Class_('MyClass');
$class->setNamespace('My\\Classes');
$class->setFinal(true);
$class->setAbstract(true);
$class->implements($someInterface);
$class->setExtends($someClass);
$class->addProperty(
    new Property('foo', Visibility::isPrivate(), InternalType::string(), Scalar::string('default'))
);
$class->addMethod($getFoo);

待办事项

  1. 测试覆盖率更好。
  2. 支持函数类型。