rotexsoft/leanorm-cli

rotexsoft/leanorm 的命令行工具,用于为指定数据库中的表生成模型、记录和集合类。

2.0.0 2024-03-13 22:17 UTC

This package is auto-updated.

Last update: 2024-09-11 09:22:50 UTC


README

运行 PHP 测试和代码质量工具   GitHub 发布 (最新 SemVer)   GitHub   覆盖状态   GitHub 代码大小 (字节)   Packagist 下载   GitHub 主要语言   Packagist PHP 版本支持   自最新发布以来的 GitHub 提交 (按日期)   GitHub 最后提交   GitHub 发布日期   Libraries.io 对 GitHub 仓库的依赖状态

LeanOrm Cli

关于

这是一个简单的命令行工具,用于为指定数据库中的每个表和视图创建 LeanOrm 集合、模型和记录类。此工具从数据库读取有关表和视图的信息,并使用这些信息生成前面提到的所需类。

安装

composer require --dev rotexsoft/leanorm-cli

用法

  • 使用指定的配置创建所有表和视图的模型、记录和集合类

php ./vendor/bin/generate-leanorm-classes.php /path/to/config.php

  • 使用指定的配置创建指定表或视图的模型、记录和集合类
    • table_or_view_name 替换为你想要生成类的表或视图名称

php ./vendor/bin/generate-leanorm-classes.php /path/to/config.php table_or_view_name

您需要创建一个配置文件,该文件将传递给上述命令。此配置文件必须返回一个包含以下最小结构的数组

return [
    'pdo' => [             // An array with a minimum of 1 item and a maximum of 4 items or an instance of the PDO class
        'sqlite::memory:', // 1st compulsory item is a dsn string to be passed as 1st arg to the PDO consructor
        'username',        // 2nd optional item is a username string to be passed as 2nd arg to the PDO consructor
        'password',        // 3rd optional item is a password string to be passed as 3rd arg to the PDO consructor
        [],                // 4th optional item is an options array to be passed as 4th arg to the PDO consructor
    ],                                                              
    
    'namespace' => null,              // Root Namespace classes will belong to. E.g. 'App\\DataSource'. Null means no namespace.
    'directory' => './model-classes', // Absolute or relative path to where classes are to be written
];

请参阅 sample-config.php 了解配置文件应返回的数组完整结构。示例配置文件中的每个项目都有详细描述,您可以在自己的配置文件中指定您想要的项。

注意:多次运行命令不会导致之前生成的所有类被覆盖。只有以 FieldsMetadata.php 结尾的文件会被覆盖。如果您想重新生成所有类,您必须手动删除它们后再重新运行命令。

注意:当您修改数据库中的表或视图列,并且您之前使用具有 store_table_col_metadata_array_in_file 条目值为 true 的配置生成类时,您应该使用相同的配置重新运行此工具,以更新所有表列元数据文件(即以 FieldsMetadata.php 结尾的文件),以便您的表/视图列修改反映在您的应用程序中。此重新运行不会修改您的模型、记录和集合类文件。

生成的类将具有以下目录结构,对于具有 authors 表和 posts 表的数据库

/path
    /where
        /classes
            /were
                /written
                |_________Authors
                |           |______AuthorsCollection.php     # Collection class for the authors table
                |           |______AuthorsModel.php          # Model class for the authors table 
                |           |______AuthorRecord.php          # Record class for the authors table 
                |           |______AuthorsFieldsMetadata.php # Metadata array for the authors table columns, ONLY generated when the config entry **store_table_col_metadata_array_in_file** has a value of **true**
                |
                |_________Posts
                            |______PostsCollection.php     # Collection class for the posts table
                            |______PostsModel.php          # Model class for the posts table 
                            |______PostRecord.php          # Record class for the posts table 
                            |______PostsFieldsMetadata.php # Metadata array for the posts table columns, ONLY generated when the config entry **store_table_col_metadata_array_in_file** has a value of **true**

这些类中的大多数都是空的,提供它们是为了让您在需要时扩展其行为。它们还帮助IDE进行类型提示的自动完成。

自定义模板

您可以覆盖此工具使用的模板,并提供自己的模板。这可以让您自定义代码生成;例如,添加您自己的常用方法或扩展中间类。

此工具使用的模板位于 此处,您可以查看它们以了解如何制作自定义模板。您的自定义模板可以位于您选择的任何目录/文件夹中,但它们必须与默认模板文件具有相同的名称,即

  • TypesCollection.php.tpl
  • TypesModel.php.tpl
  • TypeRecord.php.tpl

您不必覆盖所有模板文件,您只需覆盖您想要定制的模板即可,未覆盖的模板将继续使用默认模板。例如,您可能只想覆盖模型模板 TypesModel.php.tpl,这将导致默认的集合和记录模板继续用于创建集合和记录类,而您的自定义模型模板将用于创建模型类。

您需要在前面描述的配置文件中指定包含您的自定义模板文件的目录,通过添加具有键 custom_templates_directory 的项,如下所示

return [
    'pdo' => [             // An array with a minimum of 1 item and a maximum of 4 items
        'sqlite::memory:', // 1st compulsory item is a dsn string to be passed as 1st arg to the PDO consructor
        'username',        // 2nd optional item is a username string to be passed as 2nd arg to the PDO consructor
        'password',        // 3rd optional item is a password string to be passed as 3rd arg to the PDO consructor
        [],                // 4th optional item is an options array to be passed as 4th arg to the PDO consructor
    ],                                                              
    
    'namespace' => null,              // Root Namespace classes will belong to. E.g. 'App\\DataSource'. Null means no namespace.
    'directory' => './model-classes', // Absolute or relative path to where classes are to be written
    'custom_templates_directory' => './path', // Absolute / relative path to a location containing 1 or more template files below
                                              // TypesModel.php.tpl, TypesCollection.php.tpl & TypeRecord.php.tpl
];

以下是在模板文件中存在的所有变量/令牌的完整列表

  • {{{COLLECTION_EXTENDED}}} 将替换为每个新集合类将扩展的完全限定集合类名。默认为 \LeanOrm\Model\Collection

  • {{{MODEL_EXTENDED}}} 将替换为每个新模型类将扩展的完全限定模型类名。默认为 \LeanOrm\Model

  • {{{RECORD_EXTENDED}}} 将替换为每个新记录类将扩展的完全限定记录类名。默认为 \LeanOrm\Model\Record

  • {{{CREATED_TIMESTAMP_COLUMN_NAME}}} 将会被替换为 NULL 或数据库中每次插入新记录时需要时间戳的列名

  • {{{UPDATED_TIMESTAMP_COLUMN_NAME}}} 将会被替换为 NULL 或数据库中每次保存记录时需要时间戳的列名

  • {{{DB_COLS_AS_PHP_CLASS_PROPERTIES}}} 将会被替换为 Record 的数据库字段名称的部分文档注释

  • {{{NAME_SPACE}}} 将会被替换为空字符串或指定命名空间名称,并在其后附加 \{{{MODEL_OR_COLLECTION_CLASS_NAME_PREFIX}}}

  • {{{MODEL_OR_COLLECTION_CLASS_NAME_PREFIX}}} 将会被替换为 {{{TABLE_NAME}}} 的复数驼峰命名版本

  • {{{RECORD_CLASS_NAME_PREFIX}}} 将会被替换为 {{{TABLE_NAME}}} 的单数驼峰命名版本

  • {{{PRIMARY_COL_NAME}}} 将会被替换为空字符串或在表 {{{TABLE_NAME}}} 中的主键列名

  • {{{TABLE_NAME}}} 将会被替换为我们正在生成集合、模型和记录类的数据库表名

  • {{{METADATA_ARRAY}}} 将会被替换为一个包含表列元数据的数组,如果配置项 store_table_col_metadata_array_in_file 的值为 true,则该数组将包含在模型构造函数中

  • {{{INCLUDE_TABLE_COL_METADATA}}} 如果配置项 store_table_col_metadata_array_in_file 的值为 true,则将在模型类的构造函数中替换为包含元数据数组的包含语句

贡献

运行测试

要运行此包中的测试,只需运行以下命令

composer test

默认情况下,测试将在使用 PDO 的内存 sqlite 数据库上运行

要更改测试以使用其他数据库引擎,如 mysql,请运行以下命令

composer gen-test-pdo-config

然后编辑 ./tests/pdo.php,使用您要连接的数据库的 PDO 参数

请注意,您只需指向已创建的数据库即可。您不需要创建测试所需的表和视图,它们将在测试套件运行时自动创建。事实上,请确保配置的数据库中没有视图或表。还请确保您指定的用户名(对于非 sqlite 数据库)有创建和删除表以及视图的权限。

由于测试套件的设计方式,内存 sqlite 无法工作。sqlite 数据库必须存储在文件中。这已在默认 pdo 配置中设置。

此包应与 MS Sqlserver 兼容,但测试只能在 sqlite、mysql 和 postgres 数据库上运行。

分支

以下是此存储库中的分支

  • main: 包含此包最新主要版本的代码
  • 1.x: 包含此包 1.x 版本的代码