absszero/phead

一个通过您的 LAYOUT 文件生成代码的 PHP 代码生成器。

0.2.1 2024-06-22 01:55 UTC

This package is auto-updated.

Last update: 2024-09-22 09:54:06 UTC


README

一个通过您的 LAYOUT 文件生成代码的 PHP 代码生成器。

Build Status

安装

  1. 安装包

    composer global require absszero/phead
  2. 设置 Composer bin 路径

    Linux / macOS

    # Bash shell
    echo 'export PATH="$PATH:~/.composer/vendor/bin"' >> ~/.bashrc
    source ~/.bashrc
    
    # Z shell
    echo 'export PATH="$PATH:~/.composer/vendor/bin"' >> ~/.zshrc
    source ~/.zshrc
    
    # Fish shell
    fish_add_path ~/.composer/vendor/bin

    Windows

    1. 右键点击开始 -> 系统 -> 高级系统设置 -> 环境变量 -> 系统变量[下面框] -> 选择 Path 并点击编辑。
    2. 点击新建并添加此值 %USERPROFILE%\AppData\Roaming\Composer\vendor\bin

使用方法

从示例

# Get a sample layout file.
$ phead --sample my-layout.yaml

my-layout.yaml is generated.

# Generate code via your layout file.
$ phead my-layout.yaml

Generating files...
Hello/MyController.php
Hello/MyModel.php

干运行

$ phead my-layout.yaml --dry

Generating files... (dry run)
Hello/MyController.php (skip)
Hello/MyModel.php (skip)

覆盖文件

现有文件将不会被重新生成,您可以使用 --force 覆盖。

$ phead my-layout.yaml --force

Generating files... (force)
Hello/MyController.php (overwrite)
Hello/MyModel.php (overwrite)

仅这些文件

仅生成这些文件键。

$ phead my-layout.yaml --only=model1,model2

Generating files...
Hello/MyModel1.php
Hello/MyModel2.php

布局

布局文件有两个主要部分

  • $globals:全局变量。
  • $files:自动生成的文件。
$globals:
  dir: Hello
  # Define a variable via environment variable
  user: '{{ $env.USER }}'

$files:
  # The file key
  model:

    # The file variables.
    # 'namespace', 'class' variables will be auto generated via 'to' path
    vars:
      foo: bar
      # Overwrite default namespace
      namespace: App\Hello

    # 'from', 'to' are required
    # 'from' can also be a stub file path,
    # EX. from: /my/stub/file.stub
    from: |
      <?php

      namespace {{ namespace }};

      class {{ class }}
      {
          public $foo = '{{ foo }}';
      }
    to: "{{ $globals.dir }}/MyModel.php"
  controller:
    from: |
      <?php

      namespace {{ namespace }};

      class {{ class }}
      {
          public index()
          {
            // {{ $files.model }} will be replaced with \App\Hello\MyModel
            $model = new {{ $files.model }};
          }
      }
    to: "{{ $globals.dir }}/MyController.php"
    # Append new methods to the class file
    methods:
      - |
        public function get()
        {
            return 'something';
        }

  test:
   # Skip the file
   skip: true
   from: |
      <?php

      namespace {{ namespace }};

      class {{ class }}
      {
          public testSomething()
          {
          }
      }
   to: "{{ $globals.dir }}/MyTest.php"