gnugat/medio

此软件包已被弃用且不再维护。作者建议使用memio/memio软件包代替。

一个高度定制的PHP代码生成库


README

Memio是一个库,它允许您通过构建“模型”类(例如 new Method('__construct'))来描述PHP代码,然后使用PrettyPrinter生成它!

注意:实际的生成逻辑存储在Twig模板中。如果提供的编码风格不符合您的喜好,您可以轻松地覆盖这些模板。

安装

使用Composer安装

composer require memio/memio:^1.0

完整示例

我们将生成一个具有构造函数和两个属性的类

<?php

require __DIR__.'/vendor/autoload.php';

use Memio\Memio\Config\Build;
use Memio\Model\File;
use Memio\Model\Object;
use Memio\Model\Property;
use Memio\Model\Method;
use Memio\Model\Argument;

// Describe the code you want to generate using "Models"
$file = File::make('src/Vendor/Project/MyService.php')
    ->setStructure(
        Object::make('Vendor\Project\MyService')
            ->addProperty(new Property('createdAt'))
            ->addProperty(new Property('filename'))
            ->addMethod(
                Method::make('__construct')
                    ->addArgument(new Argument('DateTime', 'createdAt'))
                    ->addArgument(new Argument('string', 'filename'))
            )
    )
;

// Generate the code and display in the console
$prettyPrinter = Build::prettyPrinter();
$generatedCode = $prettyPrinter->generateCode($file);
echo $generatedCode;

// Or display it in a browser
// echo '<pre>'.htmlspecialchars($prettyPrinter->generateCode($file)).'</pre>';

在这个简单的示例中,我们得到以下输出

<?php

namespace Vendor\Project;

class MyService
{
    private $createdAt;

    private $filename;

    public function __construct(DateTime $createdAt, $filename)
    {
    }
}

想了解更多吗?

Memio非常强大,通过阅读文档来发现如何使用它

您可以使用以下方式查看当前和过去的版本:

最后是一些元文档

路线图

  • 命令(例如添加使用语句、添加PHPdoc、注入依赖等)
  • 解析现有代码(使用nikicPHP-Parser