layla/cody

0.2.1 2014-04-16 11:59 UTC

This package is not auto-updated.

Last update: 2024-09-14 12:36:30 UTC


README

Cody 是一个代码生成器,用于为不同的语言和框架生成对象和资源。Cody 使用一个非常简单的配置格式来定义您的对象和资源。

输入格式

生成器的输入格式如下(示例使用 yaml 格式,其他格式也支持)

根目录

输入的根目录包含包名以及包内存在的资源。

package: Vendor.Name
resources:
  __RESOURCES__

资源

资源通过名称作为键,配置作为值进行定义。

示例

Models.User:
  __RESOURCE_CONFIGURATION__
Models.NewsItem
  __RESOURCE_CONFIGURATION__

资源配置

资源配置可能只包含 2 个键。编译器键必须始终存在,它告诉 Cody 应使用哪些编译器来编译您的资源。第二个键可以是以下之一

编译器期望资源只包含 compilers 属性和其中一种可用的类型。

示例

Models.User:
  class:
    __CLASS_CONFIGURATION__
  compilers:
    - php-laravel

类配置

类配置包含构建类所需的所有信息。可用的选项包括

base: MyApp.Foundation.Models.Base
properties:
  rules:
    __PROPERTY_CONFIGURATION__
methods:
  get.rules:
    __METHOD_CONFIGURATION__
  set.rules
    __METHOD_CONFIGURATION__

模型配置

模型是类的扩展,允许您指定关系和列,并且将根据编译器自动添加必要的方 法/属性。

base: MyApp.Foundation.Models.Base
properties:
  rules:
    __property_configuration
methods:
  get.rules:
    __METHOD_CONFIGURATION__
  set.rules
    __METHOD_CONFIGURATION__
relations:
  __RELATION_CONFIGURATION__
columns:
  __COLUMN_CONFIGURATION__

方法配置

可以将方法添加到类资源或其子类(模型、控制器等)中。

示例

body:
  php-core: return $this->rules;
comment: Get the rules for this model
returnType: array
## 属性配置

属性可以添加到类资源或其子类(模型、控制器等)中。

示例:

value: name: required email: required|email comment: The rules for this model
## 关系配置

关系可以添加到模型资源或其子类中。

示例:

type: hasMany other: Models.TrailCategory
## 列配置

列可以添加到模型资源或其子类中。

示例:

type: string max: 255 nullable: true

从 CLI 生成

生成器可以接收您的输入文件,并输出 JSON,或者将文件保存到计算出的目标位置。输入甚至可以是文件夹,如果是这种情况,Cody 将使用最顶层两个文件夹作为包名,所有下面的文件夹表示命名空间。最深层的文件夹中找到的文件表示资源名称,文件的内容表示资源配置。此设置的示例可以在 vendor/cody/example 中找到。

./generator generate [--format="yml"] [--save] [--path="."] [--json] [--sync] [path]

参数

选项

从 PHP 生成

  1. 将以下行添加到您的 composer.json 中的 require 部分

"layla/cody": "dev-master"

  1. 运行 composer update

  2. 通过调用以下代码注册 Cody 的服务

use Layla\Cody\CodyServiceProvider;
use Illuminate\Container\Container as Application;

$app = new Application;
$provider = new CodyServiceProvider($app);
$provider->register();

如果您已经有一个(兼容的)容器,可以将它传递给 ServiceProvider。

  1. 获利!
$input = array(
  'package' => 'Example.Package',
  'resources' => array(
    'Models.News' => array(
      'model' => array(
        'relations' => array(
          'categories' => array(
            'other' => 'Models.Category'
          )
        )
      ),
      'compilers' => array(
        'laravel-php'
      )
    )
  )
);

$files = $app->make('cody')->compileInput($input);

// Or, if you like your input to be parsed, specify the name of the parser as the second argument (gotta love YAML :)

$input = "
package: Example.Package
resources:
  Models.News:
    model:
      relations:
        categories:
          other: Models.Category
    compilers:
      laravel-php
";

$files = $app->make('cody')->compileInput($input, 'yml');

// Or json if you like

$input = '
{
  "package": "Example.Package",
  "resources": {
    Models."News": {
      "model": {
        "relations": {
          "categories": {
            "other": "Models.Category"
          }
        }
      }
      "compilers": [
        "laravel-php"
      ]
    }
  }
}';

$files = $app->make('cody')->compileInput($input, 'json');

foreach($files as $filename => $content)
{
  // save it, echo it, do whatever you want to do with it
}